Skip to contentSkip to conversation

git-rebase-interactive: do not squash commits on abort

PATCH In review Dmitry Potapov 4 participants 12 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 git-rebase-interactive: do not squash commits on abort

If git rebase interactive is stopped by "edit" command and then the user said "git rebase --continue" while having some stage changes, git rebase interactive is trying to amend the last commit by doing:
git --soft reset && git commit

However, the user can abort commit for some reason by providing an empty log message, and that would leave the last commit undone, while the user being completely unaware about what happened. Now if the user tries to continue, by issuing "git rebase --continue" that squashes two previous commits.

---
 git-rebase--interactive.sh |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +5 -2
index 929d681..5b2b1e5 100755
@@ -429,12 +429,15 @@ do
 				die "Cannot find the author identity"
 			if test -f "$DOTEST"/amend
 			then
+				amend=$(git rev-parse --verify HEAD)
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi
 			export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
-			git commit --no-verify -F "$DOTEST"/message -e ||
-			die "Could not commit staged changes."
+			git commit --no-verify -F "$DOTEST"/message -e || {
+				test -n "$amend" && git reset --soft $amend
+				die "Could not commit staged changes."
+			}
 		fi
 
 		require_clean_work_tree
Signature · 1 line
1.6.0
#1 lore raw

PATCH 2/2 git-rebase--interactive: auto amend only edited commit

"git rebase --continue" issued after git rebase being stop by "edit" command is trying to amend the last commit using stage changes. However, if the last commit is not the commit that was marked as "edit" then it can produce unexpected results.

For instance, after being stop by "edit", I have made some changes to commit message using "git commit --amend". After that I realized that I forgot to add some changes to some file. So, I said "git add file" and the "git rebase --continue". Unfortunately, it caused that the new commit message was lost.

Another problem is that after being stopped at "edit", the user adds new commits. In this case, automatic amend behavior of git rebase triggered by some stage changes causes that not only that the log message of the last commit is lost but that it will contain also wrong Author and Date information.

Therefore, this patch restrict automatic amend only to the situation where HEAD is the commit at which git rebase stop by "edit" command.

---
 git-rebase--interactive.sh |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
git-rebase--interactive.sh +3 -1
index 5b2b1e5..84721c9 100755
@@ -284,7 +284,7 @@ do_next () {
 		pick_one $sha1 ||
 			die_with_patch $sha1 "Could not apply $sha1... $rest"
 		make_patch $sha1
-		: > "$DOTEST"/amend
+		echo $sha1 > "$DOTEST"/amend
 		warn "Stopped at $sha1... $rest"
 		warn "You can amend the commit now, with"
 		warn
@@ -430,6 +430,8 @@ do
 			if test -f "$DOTEST"/amend
 			then
 				amend=$(git rev-parse --verify HEAD)
+				test "$amend" = $(cat "$DOTEST"/amend) ||
+				die "You have uncommitted changes"
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi
Signature · 1 line
1.6.0
#2 lore raw

Re: [PATCH 2/2] git-rebase--interactive: auto amend only edited commit

Quoted 8 lines · Dmitry Potapov

Another problem is that after being stopped at "edit", the user adds new commits. In this case, automatic amend behavior of git rebase triggered by some stage changes causes that not only that the log message of the last commit is lost but that it will contain also wrong Author and Date information.

Therefore, this patch restrict automatic amend only to the situation where HEAD is the commit at which git rebase stop by "edit" command.

...

git-rebase--interactive.sh do · line 434

Quoted 6 lines
git-rebase--interactive.sh +2 -0
@@ -430,6 +430,8 @@ do
 			if test -f "$DOTEST"/amend
 			then
 				amend=$(git rev-parse --verify HEAD)
+				test "$amend" = $(cat "$DOTEST"/amend) ||
+				die "You have uncommitted changes"

Doesn't this terse message carry a bit of a "WTF?" factor? In other situations rebase --continue goes into git-commit just fine, but it does not under these special conditions. How about this:

"Will not auto-commit uncommitted changes after you have already committed something. Please run 'git commit --amend' yourself."

git-rebase--interactive.sh do · line 437

Quoted 3 lines
git-rebase--interactive.sh +0 -0
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi

-- Hannes

#3 lore raw

Re: [PATCH 2/2] git-rebase--interactive: auto amend only edited commit

Quoted 10 lines · Johannes Sixt
Quoted 8 lines · Dmitry Potapov

Another problem is that after being stopped at "edit", the user adds new commits. In this case, automatic amend behavior of git rebase triggered by some stage changes causes that not only that the log message of the last commit is lost but that it will contain also wrong Author and Date information.

Therefore, this patch restrict automatic amend only to the situation where HEAD is the commit at which git rebase stop by "edit" command.

...

Quoted 8 lines

git-rebase--interactive.sh do · line 434

Quoted 6 lines
git-rebase--interactive.sh +2 -0
@@ -430,6 +430,8 @@ do
 			if test -f "$DOTEST"/amend
 			then
 				amend=$(git rev-parse --verify HEAD)
+				test "$amend" = $(cat "$DOTEST"/amend) ||
+				die "You have uncommitted changes"

Doesn't this terse message carry a bit of a "WTF?" factor?

Agreed. However, the current message in the case when you have some unstaged changes in your working tree is not much better:
"Working tree is dirty"

Quoted 6 lines

In other
situations rebase --continue goes into git-commit just fine, but it does not under these special conditions. How about this:

"Will not auto-commit uncommitted changes after you have already committed something. Please run 'git commit --amend' yourself."

I don't think this is the right suggestion. In cases that I mentioned above (and in some others), you may want to run 'git commit' *without* --amend. Only user may know how those changes should be committed. Giving him/her a direct instruction to run some specific command will produce the wrong result in half cases. So, how about this:

"You have uncommitted changes in your working tree. Please, commit them first and then run 'git rebase --continue' again."

or if you want to describe the cause why auto-commit does not work:

"Will not auto-commit uncommitted changes after you have already committed something. Please commit them first and then run 'git rebase --continue' again."

Personally, I believe those words about auto-commit is not very helpful. Auto-commit-on-edit feature is undocumented. So, those words may be more confusing than helpful, because the user starts thinking what this auto- commit means, while the real question here is whether changes should be committed with --amend or without it.

Dmitry

#4 lore raw

Re: [PATCH 2/2] git-rebase--interactive: auto amend only edited commit

In reply to Dmitry Potapov

I may not be reading your patch correctly; I am confused by what you are trying to do.

git-rebase--interactive.sh do_next () { · line 287

Quoted 10 lines
git-rebase--interactive.sh +1 -1
index 5b2b1e5..84721c9 100755
@@ -284,7 +284,7 @@ do_next () {
 		pick_one $sha1 ||
 			die_with_patch $sha1 "Could not apply $sha1... $rest"
 		make_patch $sha1
-		: > "$DOTEST"/amend
+		echo $sha1 > "$DOTEST"/amend

You record the $sha1 from the TODO file. If you are editing the first one in the insn sequence, that is also the same as HEAD (i.e. the commit you are telling the user to modify with "commit --amend"). If you already have edited earlier ones, it is not the value of HEAD at this point.

git-rebase--interactive.sh do · line 434

Quoted 9 lines
git-rebase--interactive.sh +2 -0
 		warn "Stopped at $sha1... $rest"
 		warn "You can amend the commit now, with"
 		warn
@@ -430,6 +430,8 @@ do
 			if test -f "$DOTEST"/amend
 			then
 				amend=$(git rev-parse --verify HEAD)
+				test "$amend" = $(cat "$DOTEST"/amend) ||
+				die "You have uncommitted changes"

And then you complain if HEAD, after running "commit --amend", is not the value you recorded above.

git-rebase--interactive.sh do · line 437

Quoted 3 lines
git-rebase--interactive.sh +0 -0
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi

If the first commit was marked as "edit", then the value you recorded in "$DOTEST/amend" was the HEAD, the commit the user was told to modify with "commit --amend". $DOTEST/amend being the same as HEAD here would be a sign that the user staged changes but hasn't done "commit --amend".

But if we think about the second and subsequent commits that are marked as "edit" in TODO file, the value recorded in "$DOTEST/amend" would not be the value of HEAD at this point, and whether "commit --amend" has been run or not, the value of HEAD is very likely to be different from that value (unless the user said "reset --hard $that_one"). Wouldn't this test almost always complain for them?

Perhaps you wanted to record the value of the HEAD in the first hunk?

How would this change interact with the workflow of splitting existing commits, described at the end of git-rebase documentation?

#5 lore raw

Re: [PATCH 2/2] git-rebase--interactive: auto amend only edited commit

Quoted 15 lines · Junio C Hamano

I may not be reading your patch correctly; I am confused by what you are trying to do.

git-rebase--interactive.sh do_next () { · line 287

Quoted 10 lines
git-rebase--interactive.sh +1 -1
index 5b2b1e5..84721c9 100755
@@ -284,7 +284,7 @@ do_next () {
 		pick_one $sha1 ||
 			die_with_patch $sha1 "Could not apply $sha1... $rest"
 		make_patch $sha1
-		: > "$DOTEST"/amend
+		echo $sha1 > "$DOTEST"/amend

You record the $sha1 from the TODO file.

Oops... I should have gone to bed instead of sending this patch...

Quoted 2 lines

Perhaps you wanted to record the value of the HEAD in the first hunk?

You are asbolutely right. It should be:

	git rev-parse --verify HEAD > "$DOTEST"/amend
Quoted 3 lines

How would this change interact with the workflow of splitting existing commits, described at the end of git-rebase documentation?

It is not affected, because accordingly to the documentation, you commit all your changes "until your working tree is clean."

However, if you forgot to commit your changes, without my patch you may lose some of your splitting work. Here is the script that demonstrates the problem

-- >8 --
#!/bin/sh

set -e

rm -rf git-test
mkdir -p git-test
cd git-test

git init
for ((i=0;$i<10;i++)) do echo $i; done > foo
git add foo
git commit -m 'add foo'

sed -e 's/^0$/first line/;s/^9/last line/' < foo > tmp mv tmp foo
git add foo
git commit -m 'change foo'

echo bar > bar
git add bar
git commit -m 'more changes'

git log
echo '==='

GIT_EDITOR='sed -e '\''s/pick \(.* change foo\)/edit \1/'\'' < "$1" > tmp && mv tmp' git rebase -i HEAD~2

git reset HEAD^

{ echo y; echo n; } | git add -p

git commit -m 'commit chunk 1'

git add foo
# If I commit chunk 2 then everything will be fine with and without

# my patch
#
#   git commit -m 'commit chunk 2'
#
# However, if I forgot to commit chunk 2 then without my patch rebase
# will lose my work on splitting these chunks!
#
# With my patch, it will error out: You have uncommitted changes

GIT_EDITOR=cat git rebase --continue

git log
-- >8 --

Dmitry

#6 lore raw

PATCH v2 1/1 git-rebase--interactive: auto amend only edited commit

"git rebase --continue" issued after git rebase being stop by "edit" command is trying to amend the last commit using stage changes. However, if the last commit is not the commit that was marked as "edit" then it can produce unexpected results.

For instance, after being stop by "edit", I have made some changes to commit message using "git commit --amend". After that I realized that I forgot to add some changes to some file. So, I said "git add file" and the "git rebase --continue". Unfortunately, it caused that the new commit message was lost.

Another problem is that after being stopped at "edit", the user adds new commits. In this case, automatic amend behavior of git rebase triggered by some stage changes causes that not only that the log message of the last commit is lost but that it will contain also wrong Author and Date information.

Therefore, this patch restrict automatic amend only to the situation where HEAD is the commit at which git rebase stop by "edit" command.

---

Changes:
1. I have correct the obvious bug pointed by Junio.
2. The error message is more verbose now.

 git-rebase--interactive.sh |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
git-rebase--interactive.sh +5 -1
index 5b2b1e5..d2aa954 100755
@@ -284,7 +284,7 @@ do_next () {
 		pick_one $sha1 ||
 			die_with_patch $sha1 "Could not apply $sha1... $rest"
 		make_patch $sha1
-		: > "$DOTEST"/amend
+		git rev-parse --verify HEAD > "$DOTEST"/amend
 		warn "Stopped at $sha1... $rest"
 		warn "You can amend the commit now, with"
 		warn
@@ -430,6 +430,10 @@ do
 			if test -f "$DOTEST"/amend
 			then
 				amend=$(git rev-parse --verify HEAD)
+				test "$amend" = $(cat "$DOTEST"/amend) ||
+				die "\
+You have uncommitted changes in your working tree. Please, commit them
+first and then run 'git rebase --continue' again."
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi
Signature · 1 line
1.6.0
#7 lore raw
In reply to Dmitry Potapov
Quoted 26 lines · Dmitry Potapov

If git rebase interactive is stopped by "edit" command and then the user said "git rebase --continue" while having some stage changes, git rebase interactive is trying to amend the last commit by doing:
git --soft reset && git commit

However, the user can abort commit for some reason by providing an empty log message, and that would leave the last commit undone, while the user being completely unaware about what happened. Now if the user tries to continue, by issuing "git rebase --continue" that squashes two previous commits.

---
 git-rebase--interactive.sh |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +1 -0
index 929d681..5b2b1e5 100755
@@ -429,12 +429,15 @@ do
 				die "Cannot find the author identity"
 			if test -f "$DOTEST"/amend
 			then
+				amend=$(git rev-parse --verify HEAD)
 				git reset --soft HEAD^ ||

git-rebase--interactive.sh do · line 441

Quoted 10 lines
git-rebase--interactive.sh +4 -2
 				die "Cannot rewind the HEAD"
 			fi
 			export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
-			git commit --no-verify -F "$DOTEST"/message -e ||
-			die "Could not commit staged changes."
+			git commit --no-verify -F "$DOTEST"/message -e || {
+				test -n "$amend" && git reset --soft $amend
+				die "Could not commit staged changes."
+			}
 		fi

Feels obviously correct from a cursory look, although I admit I haven't thought about other possible corner cases that this additional reset may fire when it shouldn't. Dscho?

Quoted 9 lines
git-rebase--interactive.sh +0 -0
 		require_clean_work_tree
Signature · 6 lines
1.6.0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#8 lore raw

Hi,

Quoted 34 lines · Junio C Hamano
Quoted 32 lines · Dmitry Potapov

If git rebase interactive is stopped by "edit" command and then the user said "git rebase --continue" while having some stage changes, git rebase interactive is trying to amend the last commit by doing:
git --soft reset && git commit

However, the user can abort commit for some reason by providing an empty log message, and that would leave the last commit undone, while the user being completely unaware about what happened. Now if the user tries to continue, by issuing "git rebase --continue" that squashes two previous commits.

---
 git-rebase--interactive.sh |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +2 -2
index 929d681..5b2b1e5 100755
@@ -429,12 +429,15 @@ do
 				die "Cannot find the author identity"
 			if test -f "$DOTEST"/amend
 			then
+				amend=$(git rev-parse --verify HEAD)
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi
 			export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
-			git commit --no-verify -F "$DOTEST"/message -e ||
-			die "Could not commit staged changes."
+			git commit --no-verify -F "$DOTEST"/message -e || {
Quoted 8 lines

git-rebase--interactive.sh do · line 441

Quoted 4 lines
git-rebase--interactive.sh +3 -0
+				test -n "$amend" && git reset --soft $amend
+				die "Could not commit staged changes."
+			}
 		fi

Feels obviously correct from a cursory look, although I admit I haven't thought about other possible corner cases that this additional reset may fire when it shouldn't. Dscho?

It might be safer to set the variable "amend" to empty before the "if". Other than that, I think that patch makes tons of sense.

Thanks,
Dscho

#9 lore raw

PATCH v2 1/1 git-rebase-interactive: do not squash commits on abort

If git rebase interactive is stopped by "edit" command and then the user said "git rebase --continue" while having some stage changes, git rebase interactive is trying to amend the last commit by doing:
git --soft reset && git commit

However, the user can abort commit for some reason by providing an empty log message, and that would leave the last commit undone, while the user being completely unaware about what happened. Now if the user tries to continue, by issuing "git rebase --continue" that squashes two previous commits.

---

Quoted 2 lines · Johannes Schindelin

_
It might be safer to set the variable "amend" to empty before the "if".

OK. I have added it.

 git-rebase--interactive.sh |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +6 -2
index 929d681..aaca915 100755
@@ -427,14 +427,18 @@ do
 		else
 			. "$DOTEST"/author-script ||
 				die "Cannot find the author identity"
+			amend=""
 			if test -f "$DOTEST"/amend
 			then
+				amend=$(git rev-parse --verify HEAD)
 				git reset --soft HEAD^ ||
 				die "Cannot rewind the HEAD"
 			fi
 			export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
-			git commit --no-verify -F "$DOTEST"/message -e ||
-			die "Could not commit staged changes."
+			git commit --no-verify -F "$DOTEST"/message -e || {
+				test -n "$amend" && git reset --soft $amend
+				die "Could not commit staged changes."
+			}
 		fi
 
 		require_clean_work_tree
Signature · 1 line
1.6.0
#10 lore raw

Hi,

Quoted 14 lines · Dmitry Potapov
Quoted 2 lines · Johannes Schindelin

_
It might be safer to set the variable "amend" to empty before the "if".

OK. I have added it.

 git-rebase--interactive.sh |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
git-rebase--interactive.sh +0 -0
index 929d681..aaca915 100755

git-rebase--interactive.sh do · line 430

Quoted 4 lines
git-rebase--interactive.sh +1 -0
@@ -427,14 +427,18 @@ do
 		else
 			. "$DOTEST"/author-script ||
 				die "Cannot find the author identity"
+			amend=""

Sorry, my mistake... I should have been more explicit. In most (if not all) shell scripts, we prefer to set to the empty string with the expression

amend=

Sorry to be nitting here, but I think it might be desirable in the long run to have consistent code style, as it makes it a bigger pleasure to work with the code.

Ciao,
Dscho

#11 lore raw
Quoted 14 lines · Johannes Schindelin

git-rebase--interactive.sh do · line 430

Quoted 9 lines
git-rebase--interactive.sh +1 -0
index 929d681..aaca915 100755
@@ -427,14 +427,18 @@ do
 		else
 			. "$DOTEST"/author-script ||
 				die "Cannot find the author identity"
+			amend=""

Sorry, my mistake... I should have been more explicit. In most (if not all) shell scripts, we prefer to set to the empty string with the expression

Quoted 1 line

amend=

Don't worry. I would notice and remove "" in my MUA for things like these; especially the patch is this small it is hard to miss.