Skip to contentSkip to conversation

git-history(1) fixup broken with worktrees?

DISCUSSION Toon Claes 4 participants 6 messages Jul 17 – Nov 23

lore
Share & tools
lore

Toon Claes

author
#0 lore raw

Hi,

Imagine this repoducer:

    $ git init
    $ echo Hello > README
    $ git add .
    $ git commit -m'initial commit'
    $ git worktree add ../feature
    $ echo world >> README
    $ git add .
    $ git history fixup HEAD
    $ cd ../feature

Now running git-status(1) in that other worktree gives me:

    $ git status

On branch feature
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README

And:

    $ git diff --staged
    diff --git a/README b/README
    index 65a56c3..e965047 100644
    --- a/README
    +++ b/README
    @@ -1,2 +1 @@
     Hello
    -world

So suddenly my other worktree is dirty? With staged changes? And I didn't even touch it.

Now the commit history is correct:

    $ git log --graph --oneline --all

* 16ef548 (HEAD -> feature, main) initial commit

Signature · 2 lines
Cheers,
Toon
#1 lore raw

Hi Toon

Quoted 35 lines · Toon Claes

Imagine this repoducer:

     $ git init
     $ echo Hello > README
     $ git add .
     $ git commit -m'initial commit'
     $ git worktree add ../feature
     $ echo world >> README
     $ git add .
     $ git history fixup HEAD
     $ cd ../feature

Now running git-status(1) in that other worktree gives me:

     $ git status

On branch feature
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README

And:

     $ git diff --staged
     diff --git a/README b/README
     index 65a56c3..e965047 100644
     --- a/README
     +++ b/README
     @@ -1,2 +1 @@
      Hello
     -world
Quoted 2 lines

So suddenly my other worktree is dirty? With staged changes? And I didn't even touch it.

I think what's happening is that the branch "feature" is updated because the commit it points to is rewritten, but the index and working copy in the work tree "feature" are not. Rebase's --update-refs option refuses to update branches that are checked out in other workers by default to avoid exactly this problem[1]. As you can see in that thread there was some discussion about updating the index and working copy when the work tree is clean instead. I think that is a friendlier approach as it preserves the relationships between branches and avoids materializing changes in other worktrees.

On a related note, rebase refuses to rewrite a branch that is being rewritten by another rebase running in a different work tree. That's an important safety measure that I think the history command is missing.

Thanks

Phillip

[1] https://lore.kernel.org/git/[email protected]/

Quoted 8 lines

Now the commit history is correct:

     $ git log --graph --oneline --all

* 16ef548 (HEAD -> feature, main) initial commit

author
#2 lore raw
Quoted 3 lines · Phillip Wood

I think what's happening is that the branch "feature" is updated because the commit it points to is rewritten, but the index and working copy in the work tree "feature" are not.

Yeah, it's relatively easy to understand what goes wrong, the fix would be a bit more involved.

Quoted 3 lines

Rebase's --update-refs option refuses to update branches that are checked out in other workers by default to avoid exactly this problem[1].

Ah, thanks for finding this existing discussion, interesting.

Quoted 4 lines

As you can see in that thread there was some discussion about updating the index and working copy when the work tree is clean instead. I think that is a friendlier approach as it preserves the relationships between branches and avoids materializing changes in other worktrees.

Yeah, I agree it would be nice if git-history(1) would give it their "best-effort".

Quoted 3 lines

On a related note, rebase refuses to rewrite a branch that is being rewritten by another rebase running in a different work tree. That's an important safety measure that I think the history command is missing.

From [2] I've learned there are multiple ways a branch can be checked out. We have to take them all into account. It can be build on top of the changes in that patch.

[2]: https://lore.kernel.org/git/[email protected]

Signature · 2 lines
Cheers,
Toon
#3 lore raw
Quoted 15 lines · Toon Claes
Quoted 3 lines · Phillip Wood

I think what's happening is that the branch "feature" is updated because the commit it points to is rewritten, but the index and working copy in the work tree "feature" are not.

Yeah, it's relatively easy to understand what goes wrong, the fix would be a bit more involved.

Quoted 3 lines

Rebase's --update-refs option refuses to update branches that are checked out in other workers by default to avoid exactly this problem[1].

Ah, thanks for finding this existing discussion, interesting.

Quoted 7 lines
Quoted 4 lines

As you can see in that thread there was some discussion about updating the index and working copy when the work tree is clean instead. I think that is a friendlier approach as it preserves the relationships between branches and avoids materializing changes in other worktrees.

Yeah, I agree it would be nice if git-history(1) would give it their "best-effort".

Sorry, I missed this thread for quite a while. In any case, I agree that we should probably do the same for git-history(1) as well and refuse updating any branches that are currently checked out in another worktree.

I've created an issue [1] for this scheduled for the next release cycle. Happy if anybody else beats us to it though. Thanks!

Patrick

[1]: https://gitlab.com/gitlab-org/git/-/work_items/772

#4 lore raw

Hi Patrick,

Quoted 2 lines

I've created an issue [1] for this scheduled for the next release cycle. Happy if anybody else beats us to it though.

It seems nobody has picked this up. Can I send a patch so that git history refuses to update a branch that is checked out in another worktree or is getting rebased when the update would change its tree? This should cover Nikita's report.

Thanks,
Aniket

author
#5 lore raw
Quoted 9 lines · Aniket Gupta

Hi Patrick,

Quoted 2 lines

I've created an issue [1] for this scheduled for the next release cycle. Happy if anybody else beats us to it though.

It seems nobody has picked this up. Can I send a patch so that git history refuses to update a branch that is checked out in another worktree or is getting rebased when the update would change its tree? This should cover Nikita's report.

At GitLab we currently don't have anyone planned to work on this. So yes, you're welcome to pick this up. Feel free to Cc me for review.

Signature · 2 lines
Laters,
Toon