Skip to contentSkip to conversation

Allow hashes in git rebase --interactive --autosquash.

PATCH In review Peter Krefting 2 participants 2 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Allow hashes in git rebase --interactive --autosquash.

In addition to matching the "fixup!" and "squash!" commits to the commit message, also match the commit hash. This allows a commit message like "fixup! e83c5163316f89bfbde7d9ab23ca2e25604af290" to be detected as a fixup commit.

---

I actually misread the git-rebase manual page to think that this was how "fixup!" and "squash!" was supposed to be used, and now I have so many "fixup! <hash>" commits to rebase that I felt it worth it to implement support for it... :-)

 Documentation/git-rebase.txt |    8 ++++----
 git-rebase--interactive.sh   |   12 +++++++++---
 t/t3415-rebase-autosquash.sh |   17 +++++++++++++++++
 3 files changed, 30 insertions(+), 7 deletions(-)
Documentation/git-rebase.txt +4 -4
index 0d07b1b..e28d742 100644
@@ -318,10 +318,10 @@ link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
 --autosquash::
 	When the commit log message begins with "squash! ..." (or
 	"fixup! ..."), and there is a commit whose title begins with
-	the same ..., automatically modify the todo list of rebase -i
-	so that the commit marked for squashing comes right after the
-	commit to be modified, and change the action of the moved
-	commit from `pick` to `squash` (or `fixup`).
+	the same ..., or whose hash is ..., automatically modify the
+        todo list of rebase -i so that the commit marked for squashing
+        comes right after the commit to be modified, and change the
+        action of the moved commit from `pick` to `squash` (or `fixup`).
 +
 This option is only valid when the '--interactive' option is used.
 
git-rebase--interactive.sh +9 -3
index 436b7f5..d639ee6 100755
@@ -634,9 +634,9 @@ get_saved_options () {
 }
 
 # Rearrange the todo list that has both "pick sha1 msg" and
-# "pick sha1 fixup!/squash! msg" appears in it so that the latter
-# comes immediately after the former, and change "pick" to
-# "fixup"/"squash".
+# "pick sha1 fixup!/squash! msg" or "pick sha1 fixup!/squash!
+# sha1" appears in it so that the latter comes immediately
+# after the former, and change "pick" to "fixup"/"squash".
 rearrange_squash () {
 	sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
 		-e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
@@ -658,6 +658,12 @@ rearrange_squash () {
 				used="$used$squash "
 				;;
 			esac
+			case "$msg" in
+			"$sha1"*)
+				echo "$action $squash $action! $msg"
+				used="$used$squash "
+				;;
+			esac
 		done <"$1.sq"
 	done >"$1.rearranged" <"$1"
 	cat "$1.rearranged" >"$1"
t/t3415-rebase-autosquash.sh +17 -0
index b63f4e2..2f1a1b4 100755
@@ -38,6 +38,23 @@ test_expect_success 'auto fixup' '
 	test 1 = $(git cat-file commit HEAD^ | grep first | wc -l)
 '
 
+test_expect_success 'auto fixup with hash' '
+	git reset --hard base &&
+	echo 1 >file1 &&
+	git add -u &&
+	test_tick &&
+	git log -1 --pretty="fixup! %h" HEAD^ | git commit -F -
+
+	git tag final-fixup-hash &&
+	test_tick &&
+	git rebase --autosquash -i HEAD^^^ &&
+	git log --oneline >actual &&
+	test 3 = $(wc -l <actual) &&
+	git diff --exit-code final-fixup-hash &&
+	test 1 = "$(git cat-file blob HEAD^:file1)" &&
+	test 1 = $(git cat-file commit HEAD^ | grep first | wc -l)
+'
+
 test_expect_success 'auto squash' '
 	git reset --hard base &&
 	echo 1 >file1 &&
Signature · 1 line
1.7.1
#1 lore raw
Quoted 5 lines · Peter Krefting

In addition to matching the "fixup!" and "squash!" commits to the commit message, also match the commit hash. This allows a commit message like "fixup! e83c5163316f89bfbde7d9ab23ca2e25604af290" to be detected as a fixup commit.

Quoted 7 lines

---

I actually misread the git-rebase manual page to think that this was how "fixup!" and "squash!" was supposed to be used, and now I have so many "fixup! <hash>" commits to rebase that I felt it worth it to implement support for it... :-)

Heh. I have this alias for creating my fixup commits:

$ git help fixup
`git fixup' is aliased to `!f() { git commit -m "$(git show -s
--pretty='format:fixup! %s%n%nFixup for %h%n' "$@")"; }; f'

:-)

j.