# [BUG?] rebase -i -p leaves index changed

10 messages from 2008-08-12 to 2008-08-13. Participants: Thomas Rast, Stephan Beyer.
Thread: https://gitlist.dev/t/14955

## Thomas Rast, 2008-08-12 09:16

Subject: [BUG?] rebase -i -p leaves index changed
Message-ID: <200808121116.41535.trast@student.ethz.ch>
URL: https://gitlist.dev/e/200808121116.41535.trast%40student.ethz.ch

```
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

-- 
Thomas Rast
trast@student.ethz.ch






```

## Thomas Rast, 2008-08-13 10:04

Subject: [PATCH 1/2] rebase -i -p: handle index and workdir correctly
Message-ID: <8bbecf58e7fba14497720afe5cdaf52fec6bcb40.1218621695.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/8bbecf58e7fba14497720afe5cdaf52fec6bcb40.1218621695.git.trast%40student.ethz.ch
In-Reply-To: <200808121116.41535.trast@student.ethz.ch>

```
'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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-rebase--interactive.sh    |   14 ++++++++++++--
 t/t3404-rebase-interactive.sh |    6 ++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 4e334ba..51a6147 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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"
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ffe3dd9..4d62b9a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^^^) &&
-- 
1.6.0.rc2.38.g33ece.dirty

```

## Thomas Rast, 2008-08-13 10:04

Subject: [PATCH 2/2] rebase -i -p: fix parent rewriting
Message-ID: <65932ae2b77a1249da300e384704394d65696caf.1218621695.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/65932ae2b77a1249da300e384704394d65696caf.1218621695.git.trast%40student.ethz.ch
In-Reply-To: <8bbecf58e7fba14497720afe5cdaf52fec6bcb40.1218621695.git.trast@student.ethz.ch>

```
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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-rebase--interactive.sh    |    6 ++----
 t/t3404-rebase-interactive.sh |   12 ++++++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 51a6147..929d681 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 4d62b9a..5aa487a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^ &&
-- 
1.6.0.rc2.38.g33ece.dirty

```

## Thomas Rast, 2008-08-13 10:07

Subject: Re: [PATCH 1/2] rebase -i -p: handle index and workdir correctly
Message-ID: <200808131207.31616.trast@student.ethz.ch>
URL: https://gitlist.dev/e/200808131207.31616.trast%40student.ethz.ch
In-Reply-To: <8bbecf58e7fba14497720afe5cdaf52fec6bcb40.1218621695.git.trast@student.ethz.ch>

```
Thomas Rast wrote:
> -		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.

-- 
Thomas Rast
trast@student.ethz.ch


```

## Thomas Rast, 2008-08-13 11:56

Subject: [PATCH v2 1/2] rebase -i -p: handle index and workdir correctly
Message-ID: <23b4780a3a7bbe06f4157b6843d1d9dab26d7087.1218628444.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/23b4780a3a7bbe06f4157b6843d1d9dab26d7087.1218628444.git.trast%40student.ethz.ch
In-Reply-To: <200808131207.31616.trast@student.ethz.ch>

```
'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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-rebase--interactive.sh    |   13 ++++++++++++-
 t/t3404-rebase-interactive.sh |    6 ++++++
 2 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 4e334ba..1dc24b1 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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"
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ffe3dd9..4d62b9a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^^^) &&
-- 
1.6.0.rc2.36.g234a

```

## Thomas Rast, 2008-08-13 11:57

Subject: [PATCH v2 2/2] rebase -i -p: fix parent rewriting
Message-ID: <9cc595258568081dd3f773ec859d552d5d892990.1218628445.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/9cc595258568081dd3f773ec859d552d5d892990.1218628445.git.trast%40student.ethz.ch
In-Reply-To: <23b4780a3a7bbe06f4157b6843d1d9dab26d7087.1218628444.git.trast@student.ethz.ch>

```
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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-rebase--interactive.sh    |    7 ++-----
 t/t3404-rebase-interactive.sh |   12 ++++++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 1dc24b1..929d681 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 4d62b9a..5aa487a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^ &&
-- 
1.6.0.rc2.36.g234a

```

## Stephan Beyer, 2008-08-13 20:51

Subject: Re: [BUG?] rebase -i -p leaves index changed
Message-ID: <20080813205103.GB10758@leksak.fem-net>
URL: https://gitlist.dev/e/20080813205103.GB10758%40leksak.fem-net
In-Reply-To: <200808121116.41535.trast@student.ethz.ch>

```
Hi,

Thomas Rast wrote:
> '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

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 :(
;-)

> 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".

> 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.

> 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

-- 
Stephan Beyer <s-beyer@gmx.net>, PGP 0x6EDDD207FCC5040F

```

## Stephan Beyer, 2008-08-13 20:58

Subject: Re: [PATCH v2 1/2] rebase -i -p: handle index and workdir correctly
Message-ID: <20080813205806.GC10758@leksak.fem-net>
URL: https://gitlist.dev/e/20080813205806.GC10758%40leksak.fem-net
In-Reply-To: <23b4780a3a7bbe06f4157b6843d1d9dab26d7087.1218628444.git.trast@student.ethz.ch>

```
Hi,

Thomas Rast wrote:
> 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 <trast@student.ethz.ch>
> ---
>  git-rebase--interactive.sh    |   13 ++++++++++++-
>  t/t3404-rebase-interactive.sh |    6 ++++++
>  2 files changed, 18 insertions(+), 1 deletions(-)
> 
> diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
> index 4e334ba..1dc24b1 100755
> --- a/git-rebase--interactive.sh
> +++ b/git-rebase--interactive.sh
> @@ -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.

-- 
Stephan Beyer <s-beyer@gmx.net>, PGP 0x6EDDD207FCC5040F

```

## Thomas Rast, 2008-08-13 21:41

Subject: [PATCH v3 1/2] rebase -i -p: handle index and workdir correctly
Message-ID: <f3d190b3dde04470c0f431d93cf6be6e3990c510.1218663527.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/f3d190b3dde04470c0f431d93cf6be6e3990c510.1218663527.git.trast%40student.ethz.ch
In-Reply-To: <20080813205806.GC10758@leksak.fem-net>

```
'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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

Stephan Beyer <s-beyer@gmx.net> wrote:
>
> The intention of setting fast_forward=f is clear, but it is overwritten
> later on line 169 that is not shown in this patch:
[...]
> 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(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 4e334ba..58126bd 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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"
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ffe3dd9..4d62b9a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^^^) &&
-- 
1.6.0.rc2.69.g29dd9

```

## Thomas Rast, 2008-08-13 21:41

Subject: [PATCH v3 2/2] rebase -i -p: fix parent rewriting
Message-ID: <e345bea8d069f267772d323ca5f76182083f2f6b.1218663527.git.trast@student.ethz.ch>
URL: https://gitlist.dev/e/e345bea8d069f267772d323ca5f76182083f2f6b.1218663527.git.trast%40student.ethz.ch
In-Reply-To: <f3d190b3dde04470c0f431d93cf6be6e3990c510.1218663527.git.trast@student.ethz.ch>

```
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.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 git-rebase--interactive.sh    |    6 ++----
 t/t3404-rebase-interactive.sh |   12 ++++++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 58126bd..929d681 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -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
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index 4d62b9a..5aa487a 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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^ &&
-- 
1.6.0.rc2.69.g29dd9

```
