threads / discuss / 51630

minor interactive rebase regression: HEAD points to wrong commit while rewording

Subject: minor interactive rebase regression: HEAD points to wrong commit while rewording

## tl;dr

10 messages between Aug 12, 2019 and Aug 15, 2019.

replies: 9people: 4as markdown or json

SZEDER Gábor· Aug 12, 2019, 17:50 UTC · lore

When running interactive rebase to reword a commit message, I would expect that the commit whose message I'm rewording is checked out. This is not quite the case when rewording multiple subsequent commit messages.

Let's start with four commits, and start an interactive rebase from the first commit:

  $ git log --oneline
  5835aa1 (HEAD -> master) fourth
  64ecc64 third
  d5fad83 second
  384b86f first
  $ git rebase -i 384b86f

Update the instruction sheet to edit the log messages of two subsequent commits:

  r d5fad83 second
  r 64ecc64 third
  pick 5835aa1 fourth

Now, after the editor opens up the second commit's log message, start a new terminal and check where HEAD is pointing to:

  ~/tmp/reword (master|REBASE-i 1/3)$ head -n1 .git/COMMIT_EDITMSG 
  second
  ~/tmp/reword (master|REBASE-i 1/3)$ git log --oneline -1
  d5fad83 (HEAD) second

So far so good. Save the updated commit message, and after the editor opens up the third commit's log message, check again where HEAD is pointing to now:

  ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG 
  third
  ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
  c3db735 (HEAD) second - updated

As you can see, HEAD still points to the (now rewritten) second commit.

It's only HEAD, though: notice the '+' in the git prompt, indicating that both the worktree and index are dirty. And indeed, they both already match the state of the currently reworded, i.e. third, commit:

  ~/tmp/reword (master +|REBASE-i 2/3)$ cat file
  third

This is good, because even though HEAD has not been updated yet, it already allows users to take a look at the "big picture", i.e. actual file contents, in case the diff included in the commit message template doesn't show enough context.

This behavior changed in commit 18633e1a22 (rebase -i: use the rebase--helper builtin, 2017-02-09); prior to that HEAD pointed to the third commit while editing its log message.

It's important to reword subsequent commits. When rewording multiple, but non subsequent commits (e.g. reword, pick, reword in the instruction sheet), then HEAD is pointing to the right commits during both rewords.

Junio C Hamano· Aug 12, 2019, 18:17 UTC · re: SZEDER Gábor · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

SZEDER Gábor <szeder.dev@gmail.com> writes:
Show 14 quoted lines
> ...
> So far so good.
> Save the updated commit message, and after the editor opens up the
> third commit's log message, check again where HEAD is pointing to now:
>
>   ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG 
>   third
>   ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
>   c3db735 (HEAD) second - updated
>
> As you can see, HEAD still points to the (now rewritten) second
> commit.
>
> It's only HEAD,...
Yuck.

That would still be annoying to some people and outright buggy to others, if their workflow relies on HEAD (e.g. compare with HEAD while reviewing the log messsage) and then now they instead need to adjust (e.g. compare with the index instead). Perhaps you are one of them (apparently I am not, as I did not notice the behaviour change until you pointed it out here).

SZEDER Gábor· Aug 12, 2019, 18:56 UTC · re: Junio C Hamano · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

On Mon, Aug 12, 2019 at 11:17:01AM -0700, Junio C Hamano wrote:
Show 23 quoted lines
> SZEDER Gábor <szeder.dev@gmail.com> writes:
> 
> > ...
> > So far so good.
> > Save the updated commit message, and after the editor opens up the
> > third commit's log message, check again where HEAD is pointing to now:
> >
> >   ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG 
> >   third
> >   ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
> >   c3db735 (HEAD) second - updated
> >
> > As you can see, HEAD still points to the (now rewritten) second
> > commit.
> >
> > It's only HEAD,...
> 
> Yuck.
> 
> That would still be annoying to some people and outright buggy to
> others, if their workflow relies on HEAD (e.g. compare with HEAD
> while reviewing the log messsage) and then now they instead need to
> adjust (e.g. compare with the index instead).

Well, it's caused by a 2.5 year old commit, so either all those people have been lucky so far (or just don't do subsequent rewords?), or those who stumbled upon it already adjusted without complaining to us.

> Perhaps you are one of them

No; during the second reword I ran 'git log -2' in the other terminal (wanted to copy a sentence from the previous, just reworded commit), and noticed that the two listed commits were "off by one".

BTW, rewriting the first commit appears to be important. When doing an edit-reword of two subsequent commits, then this issue only happens if the edit actually rewrites the commit. However, when, instead of editing anything, I do a 'git rebase --continue' right away, then HEAD points to where it should during the subsequent reword.

Anyway, Cc:-ing Dscho for his rebase expertise.
Phillip Wood· Aug 12, 2019, 20:28 UTC · re: SZEDER Gábor · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

On 12/08/2019 18:50, SZEDER Gábor wrote:
Show 32 quoted lines
> 
> When running interactive rebase to reword a commit message, I would
> expect that the commit whose message I'm rewording is checked out.
> This is not quite the case when rewording multiple subsequent commit
> messages.
> 
> Let's start with four commits, and start an interactive rebase from
> the first commit:
> 
>    $ git log --oneline
>    5835aa1 (HEAD -> master) fourth
>    64ecc64 third
>    d5fad83 second
>    384b86f first
>    $ git rebase -i 384b86f
> 
> Update the instruction sheet to edit the log messages of two
> subsequent commits:
> 
>    r d5fad83 second
>    r 64ecc64 third
>    pick 5835aa1 fourth
> 
> Now, after the editor opens up the second commit's log message, start
> a new terminal and check where HEAD is pointing to:
> 
>    ~/tmp/reword (master|REBASE-i 1/3)$ head -n1 .git/COMMIT_EDITMSG
>    second
>    ~/tmp/reword (master|REBASE-i 1/3)$ git log --oneline -1
>    d5fad83 (HEAD) second
> 
> So far so good.

Because the sequencer can fast-forwarded to second from first it does that and then run 'commit --amend' to do the reword.

Show 7 quoted lines
> Save the updated commit message, and after the editor opens up the
> third commit's log message, check again where HEAD is pointing to now:
> 
>    ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG
>    third
>    ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
>    c3db735 (HEAD) second - updated

As second has been updated the sequencer cannot fast-forward to third so it cherry-picks third and then passes --edit when it runs 'git commit' to commit the cherry-pick. HEAD is updated once the reworded commit has been created.

I think the scripted rebase always ran cherry-pick and then ran 'commit --amend' afterwards if the commit was being reworded. The C implementation is more efficient as it avoids creating an redundant commit but has the side effect that HEAD is not updated before the reword which was surprising here.

I don't think I've ever looked at HEAD while rewording, my shell prompt gets the current pick from .git/rebase-merge/done so does not look at HEAD. While it might seem odd if the user looks at HEAD it's quite nice not to create a new commit only to amend it straight away when it's reworded. We have REBASE_HEAD which always points to the current pick - HEAD is also an unreliable indicator of the current pick if there are conflicts.

Best Wishes
Phillip
Show 25 quoted lines
> As you can see, HEAD still points to the (now rewritten) second
> commit.
> 
> It's only HEAD, though: notice the '+' in the git prompt, indicating
> that both the worktree and index are dirty.  And indeed, they both
> already match the state of the currently reworded, i.e. third, commit:
> 
>    ~/tmp/reword (master +|REBASE-i 2/3)$ cat file
>    third
> 
> This is good, because even though HEAD has not been updated yet, it
> already allows users to take a look at the "big picture", i.e. actual
> file contents, in case the diff included in the commit message
> template doesn't show enough context.
> 
> This behavior changed in commit 18633e1a22 (rebase -i: use the
> rebase--helper builtin, 2017-02-09); prior to that HEAD pointed to the
> third commit while editing its log message.
> 
> It's important to reword subsequent commits.  When rewording multiple,
> but non subsequent commits (e.g. reword, pick, reword in the
> instruction sheet), then HEAD is pointing to the right commits during
> both rewords.
> 
> 
Johannes Schindelin· Aug 14, 2019, 20:45 UTC · re: Phillip Wood · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

Hi,
On Mon, 12 Aug 2019, Phillip Wood wrote:
Show 53 quoted lines
> On 12/08/2019 18:50, SZEDER Gábor wrote:
> >
> > When running interactive rebase to reword a commit message, I would
> > expect that the commit whose message I'm rewording is checked out.
> > This is not quite the case when rewording multiple subsequent commit
> > messages.
> >
> > Let's start with four commits, and start an interactive rebase from
> > the first commit:
> >
> >    $ git log --oneline
> >    5835aa1 (HEAD -> master) fourth
> >    64ecc64 third
> >    d5fad83 second
> >    384b86f first
> >    $ git rebase -i 384b86f
> >
> > Update the instruction sheet to edit the log messages of two
> > subsequent commits:
> >
> >    r d5fad83 second
> >    r 64ecc64 third
> >    pick 5835aa1 fourth
> >
> > Now, after the editor opens up the second commit's log message, start
> > a new terminal and check where HEAD is pointing to:
> >
> >    ~/tmp/reword (master|REBASE-i 1/3)$ head -n1 .git/COMMIT_EDITMSG
> >    second
> >    ~/tmp/reword (master|REBASE-i 1/3)$ git log --oneline -1
> >    d5fad83 (HEAD) second
> >
> > So far so good.
>
> Because the sequencer can fast-forwarded to second from first it does that and
> then run 'commit --amend' to do the reword.
>
> > Save the updated commit message, and after the editor opens up the
> > third commit's log message, check again where HEAD is pointing to now:
> >
> >    ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG
> >    third
> >    ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
> >    c3db735 (HEAD) second - updated
>
> As second has been updated the sequencer cannot fast-forward to third so it
> cherry-picks third and then passes --edit when it runs 'git commit' to commit
> the cherry-pick. HEAD is updated once the reworded commit has been created.
>
> I think the scripted rebase always ran cherry-pick and then ran 'commit
> --amend' afterwards if the commit was being reworded. The C implementation is
> more efficient as it avoids creating an redundant commit but has the side
> effect that HEAD is not updated before the reword which was surprising here.
Indeed, that was even intentional.
Show 6 quoted lines
> I don't think I've ever looked at HEAD while rewording, my shell prompt gets
> the current pick from .git/rebase-merge/done so does not look at HEAD. While
> it might seem odd if the user looks at HEAD it's quite nice not to create a
> new commit only to amend it straight away when it's reworded. We have
> REBASE_HEAD which always points to the current pick - HEAD is also an
> unreliable indicator of the current pick if there are conflicts.

That is interesting; I would never have thought about scripting around `reword`.

However, I am reluctant to accept the performance impact: in the long run, I would love to have an interactive rebase that actually only updates `HEAD` (and the worktree) when interrupting the rebase (via `break` or `edit`), and `reword` does not qualify for "interrupting" in my mind.

Ciao, Dscho

SZEDER Gábor· Aug 14, 2019, 21:40 UTC · re: Johannes Schindelin · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

On Wed, Aug 14, 2019 at 10:45:22PM +0200, Johannes Schindelin wrote:
Show 5 quoted lines
> However, I am reluctant to accept the performance impact: in the long
> run, I would love to have an interactive rebase that actually only
> updates `HEAD` (and the worktree) when interrupting the rebase (via
> `break` or `edit`), and `reword` does not qualify for "interrupting" in
> my mind.

It's crucial that users can look at the files in the currently reworded commit while it is being reworded.

SZEDER Gábor· Aug 14, 2019, 21:20 UTC · re: Phillip Wood · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

On Mon, Aug 12, 2019 at 09:28:52PM +0100, Phillip Wood wrote:
Show 17 quoted lines
> >Save the updated commit message, and after the editor opens up the
> >third commit's log message, check again where HEAD is pointing to now:
> >
> >   ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG
> >   third
> >   ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
> >   c3db735 (HEAD) second - updated
> 
> As second has been updated the sequencer cannot fast-forward to third so it
> cherry-picks third and then passes --edit when it runs 'git commit' to
> commit the cherry-pick. HEAD is updated once the reworded commit has been
> created.
> 
> I think the scripted rebase always ran cherry-pick and then ran 'commit
> --amend' afterwards if the commit was being reworded. The C implementation
> is more efficient as it avoids creating an redundant commit but has the side
> effect that HEAD is not updated before the reword which was surprising here.

I'm not sure about this more efficient thing. I mean, 'git rebase' is about to launch the editor to let the user type something... Compared to that the time spent in creating an extra commit object is surely negligible.

Phillip Wood· Aug 15, 2019, 13:47 UTC · re: SZEDER Gábor · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

On 14/08/2019 22:20, SZEDER Gábor wrote:
Show 23 quoted lines
> On Mon, Aug 12, 2019 at 09:28:52PM +0100, Phillip Wood wrote:
>>> Save the updated commit message, and after the editor opens up the
>>> third commit's log message, check again where HEAD is pointing to now:
>>>
>>>    ~/tmp/reword (master +|REBASE-i 2/3)$ head -n1 .git/COMMIT_EDITMSG
>>>    third
>>>    ~/tmp/reword (master +|REBASE-i 2/3)$ git log --oneline -1
>>>    c3db735 (HEAD) second - updated
>>
>> As second has been updated the sequencer cannot fast-forward to third so it
>> cherry-picks third and then passes --edit when it runs 'git commit' to
>> commit the cherry-pick. HEAD is updated once the reworded commit has been
>> created.
>>
>> I think the scripted rebase always ran cherry-pick and then ran 'commit
>> --amend' afterwards if the commit was being reworded. The C implementation
>> is more efficient as it avoids creating an redundant commit but has the side
>> effect that HEAD is not updated before the reword which was surprising here.
> 
> I'm not sure about this more efficient thing.  I mean, 'git rebase' is
> about to launch the editor to let the user type something...  Compared
> to that the time spent in creating an extra commit object is surely
> negligible.

I changed the sequencer to always commit the cherry-pick and then run 'git commit --amend' for rewords [1]. Running

	time env GIT_EDITOR=true GIT_SEQUENCE_EDITOR='sed -i s/pick/reword/' 
../bin-wrappers/git rebase -i --root

over 100 commits I cannot see any real difference in the timings between master and that branch. Any difference is within the variation of the times of multiple runs. The change also fixes a bug when rewording a re-arranged root commit.

Best Wishes
Phillip
[1] https://github.com/phillipwood/git/commits/wip/rebase-reword-update-head
Junio C Hamano· Aug 15, 2019, 16:36 UTC · re: Phillip Wood · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 12 quoted lines
> On 14/08/2019 22:20, SZEDER Gábor wrote:
>
> I changed the sequencer to always commit the cherry-pick and then run
> 'git commit --amend' for rewords [1]. Running
>
> 	time env GIT_EDITOR=true GIT_SEQUENCE_EDITOR='sed -i
> s/pick/reword/' ../bin-wrappers/git rebase -i --root
>
> over 100 commits I cannot see any real difference in the timings
> between master and that branch. Any difference is within the variation
> of the times of multiple runs. The change also fixes a bug when
> rewording a re-arranged root commit.

I guess that settles the "efficiency" story; besides, showing a wrong intermediate state to users and scripts efficiently is just as bad as showing a wrong state less efficiently.

Junio C Hamano· Aug 15, 2019, 16:54 UTC · re: Junio C Hamano · lore

Re: minor interactive rebase regression: HEAD points to wrong commit while rewording

Junio C Hamano <gitster@pobox.com> writes:
Show 18 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
>> On 14/08/2019 22:20, SZEDER Gábor wrote:
>>
>> I changed the sequencer to always commit the cherry-pick and then run
>> 'git commit --amend' for rewords [1]. Running
>>
>> 	time env GIT_EDITOR=true GIT_SEQUENCE_EDITOR='sed -i
>> s/pick/reword/' ../bin-wrappers/git rebase -i --root
>>
>> over 100 commits I cannot see any real difference in the timings
>> between master and that branch. Any difference is within the variation
>> of the times of multiple runs. The change also fixes a bug when
>> rewording a re-arranged root commit.
>
> I guess that settles the "efficiency" story; besides, showing a
> wrong intermediate state to users and scripts efficiently is just as
> bad as showing a wrong state less efficiently.

Needless to say, there is no need for us to make things less efficient when we do not need to. If there are two back-to-back "pick"s, and there is no hook and the like that gives an end-user and/or a script a reliable trigger point that can be used to "observe" and "utilize" the state immediately after the first "pick", we do not have to update the HEAD (or update the working tree for that matter) between these two "pick"s.

The external process being able to observe alone is not an enough reason to force us go inefficient here---if the triggering event is like "spawn an editor and wait for it to exit", that gives the external process to not just "observe" the updated state after the first pick, but also "utilize" that state (e.g. build, compare with some other tree, etc.) knowing that the second "pick" does not start changing the state until it relinquishes the control.

But unless the external process can reliably utilize the updated state, it is of not much use to just be able to notice the change. For example, an argument like "constantly monitoring changes in the $GIT_DIR/rebase-merge directory, we can notice that each 'pick' gets done" is not a reason to force us to update HEAD every time any instruction is consumed in the todo file, as the next "pick" cannot be reliably delayed by somebody who is merely observing the directory to utilize the updated state.

← back to recent threads