rebase -i -p leaves index changed
Hi,
'rebase -i -p' appears to be a bit confused about what it is doing. Try a history like this:
O -- A -- M -- B
\ /
\- C -/
built by:
git init
touch foo
git add foo
git ci -m initial
git tag root
git co -b side
echo blah > other
git add other
git ci -m other
git co master
echo a >> foo
git add foo
git ci -m a
git add foo
git merge side
echo b >> foo
git add foo
git ci -m 'b: depends on side'
First try this:
GIT_EDITOR=true git rebase -i -p root git diff --cached
resulting in
diff --git a/foo b/foo index 422c2b7..e69de29 100644 --- a/foo +++ b/foo @@ -1,2 +0,0 @@ -a -b diff --git a/other b/other deleted file mode 100644 index 907b308..0000000 --- a/other +++ /dev/null @@ -1 +0,0 @@ -blah
Second, 'edit' is also a bit suspicious:
git reset --hard GIT_EDITOR='perl -i -pe "s/pick 096/edit 096/"' git rebase -i -p root
Despite claiming "Stopped at 096[...] a", a quick 'git show' tells us that it is actually stuck at 'initial'. (At least 'git rebase --continue' then ends up with the same result as the first test.)
Granted, I'm not entirely sure what it _should_ do. In my use case (relating to the filter-branch topic), C was a commit from elsewhere that B depended on. So I kind of hoped 'rebase -i -p' would let me edit A, then rebuild M and B on top, while leaving C alone.
- Thomas
Signature · 2 lines
Thomas Rast [email protected]
Hi,
Quoted 7 lines · Thomas Rast
'rebase -i -p' appears to be a bit confused about what it is doing. Try a history like this:
O -- A -- M -- B
\ /\- C -/
Quoted 8 lines
built by:
git init
touch foo
git add foo
git ci -m initial
git tag root
git co -b side
Puh, it is nice that you provide a sequence how you generated your test case, but "ci" and "co" are no git commands. Ok, because of the sequence it was clear to me that you mean "commit" and "checkout", but I couldn't just c&p it into my xterm :(
;-)
Quoted 4 lines
Second, 'edit' is also a bit suspicious:
git reset --hard GIT_EDITOR='perl -i -pe "s/pick 096/edit 096/"' git rebase -i -p root
This perl expression does nothing if your commit is not prefixed with 096 ;) But I tested by changing the line of commit "a" to "edit".
Quoted 3 lines
Despite claiming "Stopped at 096[...] a", a quick 'git show' tells us that it is actually stuck at 'initial'. (At least 'git rebase --continue' then ends up with the same result as the first test.)
Regarding your [BUG?]: this is a bug, definitely.
Quoted 4 lines
Granted, I'm not entirely sure what it _should_ do. In my use case (relating to the filter-branch topic), C was a commit from elsewhere that B depended on. So I kind of hoped 'rebase -i -p' would let me edit A, then rebuild M and B on top, while leaving C alone.
I think your hope is right. So this is also a bug.
I tested this with my sequencer-based rebase -i -p first and it worked as you hope. I tested it with your v2 patches and it works, too. ;) So I'm having a deeper look at your patches in some minutes...
Regards,
Stephan
Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
PATCH 1/2 rebase -i -p: handle index and workdir correctly
In reply to Thomas Rast'git rebase -i -p' forgot to update the index and working directory during fast forwards. Fix this. Restores plain 'git rebase -i -p' to be a no-op.
Also, it attempted to do a fast forward even if it was instructed not to commit (via -n). Fall back to the cherry-pick code path and let that handle the issue for us.
--- git-rebase--interactive.sh | 14 ++++++++++++-- t/t3404-rebase-interactive.sh | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +12 -2
@@ -145,7 +145,16 @@ pick_one () { } pick_one_preserving_merges () { - case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac + fast_forward=t + case "$1" in + -n) + fast_forward=f + sha1=$2 + ;; + *) + sha1=$1 + ;; + esac sha1=$(git rev-parse $sha1) if test -f "$DOTEST"/current-commit
@@ -181,7 +190,8 @@ pick_one_preserving_merges () { case $fast_forward in t) output warn "Fast forward to $sha1" - test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 + output git reset --hard $sha1 || + die "Cannot fast forward to $sha1" ;; f) test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
t/t3404-rebase-interactive.sh +6 -0
@@ -202,6 +202,9 @@ test_expect_success 'retain authorship when squashing' ' test_expect_success '-p handles "no changes" gracefully' ' HEAD=$(git rev-parse HEAD) && git rebase -i -p HEAD^ && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $HEAD = $(git rev-parse HEAD) '
@@ -235,6 +238,9 @@ test_expect_success 'preserve merges with -p' ' git checkout -b to-be-rebased && test_tick && git rebase -i -p --onto branch1 master && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $(git rev-parse HEAD~6) = $(git rev-parse branch1) && test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) && test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
Signature · 1 line
1.6.0.rc2.38.g33ece.dirty
Re: [PATCH 1/2] rebase -i -p: handle index and workdir correctly
git-rebase--interactive.sh pick_one_preserving_merges () { · line 194
Quoted 3 lines · Thomas Rast
git-rebase--interactive.sh +2
-1- test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 + output git reset --hard $sha1 || + die "Cannot fast forward to $sha1"
Doh. I obviously shouldn't try to split patches before lunch.
Please disregard, I'll fix and resend when I get back.
Signature · 2 lines
Thomas Rast [email protected]
PATCH v2 1/2 rebase -i -p: handle index and workdir correctly
'git rebase -i -p' forgot to update the index and working directory during fast forwards. Fix this. Makes 'GIT_EDITOR=true rebase -i -p <ancestor>' a no-op again.
Also, it attempted to do a fast forward even if it was instructed not to commit (via -n). Fall back to the cherry-pick code path and let that handle the issue for us.
--- git-rebase--interactive.sh | 13 ++++++++++++- t/t3404-rebase-interactive.sh | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletions(-)
git-rebase--interactive.sh +12 -1
@@ -145,7 +145,16 @@ pick_one () { } pick_one_preserving_merges () { - case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac + fast_forward=t + case "$1" in + -n) + fast_forward=f + sha1=$2 + ;; + *) + sha1=$1 + ;; + esac sha1=$(git rev-parse $sha1) if test -f "$DOTEST"/current-commit
@@ -182,6 +191,8 @@ pick_one_preserving_merges () { t) output warn "Fast forward to $sha1" test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 + output git reset --hard $sha1 || + die "Cannot fast forward to $sha1" ;; f) test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
t/t3404-rebase-interactive.sh +6 -0
@@ -202,6 +202,9 @@ test_expect_success 'retain authorship when squashing' ' test_expect_success '-p handles "no changes" gracefully' ' HEAD=$(git rev-parse HEAD) && git rebase -i -p HEAD^ && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $HEAD = $(git rev-parse HEAD) '
@@ -235,6 +238,9 @@ test_expect_success 'preserve merges with -p' ' git checkout -b to-be-rebased && test_tick && git rebase -i -p --onto branch1 master && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $(git rev-parse HEAD~6) = $(git rev-parse branch1) && test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) && test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
Signature · 1 line
1.6.0.rc2.36.g234a
Re: [PATCH v2 1/2] rebase -i -p: handle index and workdir correctly
Hi,
Quoted 15 lines · Thomas Rast
Also, it attempted to do a fast forward even if it was instructed not to commit (via -n). Fall back to the cherry-pick code path and let that handle the issue for us.
Signed-off-by Thomas Rast <[email protected]>--- git-rebase--interactive.sh | 13 ++++++++++++- t/t3404-rebase-interactive.sh | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletions(-)git-rebase--interactive.sh +0
-0
git-rebase--interactive.sh pick_one () { · line 152
Quoted 9 lines
git-rebase--interactive.sh +5
-1@@ -145,7 +145,16 @@ pick_one () { } pick_one_preserving_merges () { - case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac + fast_forward=t + case "$1" in + -n) + fast_forward=f + sha1=$2
The intention of setting fast_forward=f is clear, but it is overwritten later on line 169 that is not shown in this patch:
168: # rewrite parents; if none were rewritten, we can fast-forward.
169: fast_forward=t
Clearly, this is due to your patch split, because after the second patch, this is fixed.
The rest of this patch seems to be ok to me.
Regards.
Signature · 1 line
Stephan Beyer <[email protected]>, PGP 0x6EDDD207FCC5040F
PATCH v3 1/2 rebase -i -p: handle index and workdir correctly
'git rebase -i -p' forgot to update the index and working directory during fast forwards. Fix this. Makes 'GIT_EDITOR=true rebase -i -p <ancestor>' a no-op again.
Also, it attempted to do a fast forward even if it was instructed not to commit (via -n). Fall back to the cherry-pick code path and let that handle the issue for us.
---
Quoted 3 lines · Stephan Beyer
The intention of setting fast_forward=f is clear, but it is overwritten later on line 169 that is not shown in this patch:
[...]
Quoted 2 lines
Clearly, this is due to your patch split, because after the second patch, this is fixed.
Indeed, thanks for catching this.
git-rebase--interactive.sh | 14 ++++++++++++-- t/t3404-rebase-interactive.sh | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +12 -2
@@ -145,7 +145,16 @@ pick_one () { } pick_one_preserving_merges () { - case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac + fast_forward=t + case "$1" in + -n) + fast_forward=f + sha1=$2 + ;; + *) + sha1=$1 + ;; + esac sha1=$(git rev-parse $sha1) if test -f "$DOTEST"/current-commit
@@ -157,7 +166,6 @@ pick_one_preserving_merges () { fi # rewrite parents; if none were rewritten, we can fast-forward. - fast_forward=t preserve=t new_parents= for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
@@ -182,6 +190,8 @@ pick_one_preserving_merges () { t) output warn "Fast forward to $sha1" test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 + output git reset --hard $sha1 || + die "Cannot fast forward to $sha1" ;; f) test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
t/t3404-rebase-interactive.sh +6 -0
@@ -202,6 +202,9 @@ test_expect_success 'retain authorship when squashing' ' test_expect_success '-p handles "no changes" gracefully' ' HEAD=$(git rev-parse HEAD) && git rebase -i -p HEAD^ && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $HEAD = $(git rev-parse HEAD) '
@@ -235,6 +238,9 @@ test_expect_success 'preserve merges with -p' ' git checkout -b to-be-rebased && test_tick && git rebase -i -p --onto branch1 master && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && test $(git rev-parse HEAD~6) = $(git rev-parse branch1) && test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) && test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
Signature · 1 line
1.6.0.rc2.69.g29dd9
PATCH v3 2/2 rebase -i -p: fix parent rewriting
The existing parent rewriting did not handle the case where a previous commit was amended (via edit or squash). Fix by always putting the new sha1 of the last commit into the $REWRITTEN map.
--- git-rebase--interactive.sh | 6 ++---- t/t3404-rebase-interactive.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-)
git-rebase--interactive.sh +2 -4
@@ -165,14 +165,14 @@ pick_one_preserving_merges () { die "Cannot write current commit's replacement sha1" fi + echo $sha1 > "$DOTEST"/current-commit + # rewrite parents; if none were rewritten, we can fast-forward. - preserve=t new_parents= for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) do if test -f "$REWRITTEN"/$p then - preserve=f new_p=$(cat "$REWRITTEN"/$p) test $p != $new_p && fast_forward=f case "$new_parents" in
@@ -189,7 +189,6 @@ pick_one_preserving_merges () { case $fast_forward in t) output warn "Fast forward to $sha1" - test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 output git reset --hard $sha1 || die "Cannot fast forward to $sha1" ;;
@@ -201,7 +200,6 @@ pick_one_preserving_merges () { output git checkout $first_parent 2> /dev/null || die "Cannot move HEAD to $first_parent" - echo $sha1 > "$DOTEST"/current-commit case "$new_parents" in ' '*' '*) # redo merge
t/t3404-rebase-interactive.sh +12 -0
@@ -250,6 +250,18 @@ test_expect_success 'preserve merges with -p' ' test $(git show HEAD:unrelated-file) = 1 ' +test_expect_success 'edit ancestor with -p' ' + FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 && + echo 2 > unrelated-file && + test_tick && + git commit -m L2-modified --amend unrelated-file && + git rebase --continue && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && + test $(git show HEAD:unrelated-file) = 2 +' + test_expect_success '--continue tries to commit' ' test_tick && test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
Signature · 1 line
1.6.0.rc2.69.g29dd9
PATCH v2 2/2 rebase -i -p: fix parent rewriting
In reply to Thomas RastThe existing parent rewriting did not handle the case where a previous commit was amended (via edit or squash). Fix by always putting the new sha1 of the last commit into the $REWRITTEN map.
--- git-rebase--interactive.sh | 7 ++----- t/t3404-rebase-interactive.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-)
git-rebase--interactive.sh +2 -5
@@ -165,15 +165,14 @@ pick_one_preserving_merges () { die "Cannot write current commit's replacement sha1" fi + echo $sha1 > "$DOTEST"/current-commit + # rewrite parents; if none were rewritten, we can fast-forward. - fast_forward=t - preserve=t new_parents= for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) do if test -f "$REWRITTEN"/$p then - preserve=f new_p=$(cat "$REWRITTEN"/$p) test $p != $new_p && fast_forward=f case "$new_parents" in
@@ -190,7 +189,6 @@ pick_one_preserving_merges () { case $fast_forward in t) output warn "Fast forward to $sha1" - test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 output git reset --hard $sha1 || die "Cannot fast forward to $sha1" ;;
@@ -202,7 +200,6 @@ pick_one_preserving_merges () { output git checkout $first_parent 2> /dev/null || die "Cannot move HEAD to $first_parent" - echo $sha1 > "$DOTEST"/current-commit case "$new_parents" in ' '*' '*) # redo merge
t/t3404-rebase-interactive.sh +12 -0
@@ -250,6 +250,18 @@ test_expect_success 'preserve merges with -p' ' test $(git show HEAD:unrelated-file) = 1 ' +test_expect_success 'edit ancestor with -p' ' + FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 && + echo 2 > unrelated-file && + test_tick && + git commit -m L2-modified --amend unrelated-file && + git rebase --continue && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && + test $(git show HEAD:unrelated-file) = 2 +' + test_expect_success '--continue tries to commit' ' test_tick && test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
Signature · 1 line
1.6.0.rc2.36.g234a
PATCH 2/2 rebase -i -p: fix parent rewriting
In reply to Thomas RastThe existing parent rewriting did not handle the case where a previous commit was amended (via edit or squash). Fix by always putting the new sha1 of the last commit into the $REWRITTEN map.
--- git-rebase--interactive.sh | 6 ++---- t/t3404-rebase-interactive.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-)
git-rebase--interactive.sh +2 -4
@@ -165,15 +165,14 @@ pick_one_preserving_merges () { die "Cannot write current commit's replacement sha1" fi + echo $sha1 > "$DOTEST"/current-commit + # rewrite parents; if none were rewritten, we can fast-forward. - fast_forward=t - preserve=t new_parents= for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) do if test -f "$REWRITTEN"/$p then - preserve=f new_p=$(cat "$REWRITTEN"/$p) test $p != $new_p && fast_forward=f case "$new_parents" in
@@ -201,7 +200,6 @@ pick_one_preserving_merges () { output git checkout $first_parent 2> /dev/null || die "Cannot move HEAD to $first_parent" - echo $sha1 > "$DOTEST"/current-commit case "$new_parents" in ' '*' '*) # redo merge
t/t3404-rebase-interactive.sh +12 -0
@@ -250,6 +250,18 @@ test_expect_success 'preserve merges with -p' ' test $(git show HEAD:unrelated-file) = 1 ' +test_expect_success 'edit ancestor with -p' ' + FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 && + echo 2 > unrelated-file && + test_tick && + git commit -m L2-modified --amend unrelated-file && + git rebase --continue && + git update-index --refresh && + git diff-files --quiet && + git diff-index --quiet --cached HEAD -- && + test $(git show HEAD:unrelated-file) = 2 +' + test_expect_success '--continue tries to commit' ' test_tick && test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
Signature · 1 line
1.6.0.rc2.38.g33ece.dirty