git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] rebase: teach --autosquash to match on sha1 in addition to message

From
Kevin Ballard <kevin@sb.org>
Date
Nov 4, 2010, 02:41 UTC
Message-ID
<1288838504-69114-2-git-send-email-kevin@sb.org>
In-Reply-To
<1288838504-69114-1-git-send-email-kevin@sb.org>

Support lines of the form "fixup! 7a235b" that specify an exact commit in addition to the normal "squash! Old commit message" form.

Signed-off-by: Kevin Ballard <kevin@sb.org>
---
I chose 4 characters as the restriction because that's the minimum number
that `git rev-parse --short=<n>` will emit.
 git-rebase--interactive.sh   |   13 +++++++++----
 t/t3415-rebase-autosquash.sh |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 379bbac..9121bb6 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -693,12 +693,17 @@ rearrange_squash () {
 			case " $used" in
 			*" $squash "*) continue ;;
 			esac
-			case "$message" in
-			"$msg"*)
+			emit=0
+			case "$message" in "$msg"*) emit=1;; esac
+			if test $emit != 1; then
+				case "$sha1" in "$msg"*) emit=1;; esac
+				# ensure the message is at least 4 characters long
+				case "$msg" in ????*);; *) emit=0;; esac
+			fi
+			if test $emit = 1; then
 				printf '%s\n' "$action $squash $action! $msg"
 				used="$used$squash "
-				;;
-			esac
+			fi
 		done <"$1.sq"
 	done >"$1.rearranged" <"$1"
 	cat "$1.rearranged" >"$1"
diff --git a/t/t3415-rebase-autosquash.sh b/t/t3415-rebase-autosquash.sh
index 712bbe8..14cdbeb 100755
--- a/t/t3415-rebase-autosquash.sh
+++ b/t/t3415-rebase-autosquash.sh
@@ -136,5 +136,38 @@ test_expect_success 'auto squash that matches a commit after the squash' '
 	test 1 = $(git cat-file commit HEAD | grep third | wc -l) &&
 	test 1 = $(git cat-file commit HEAD^ | grep third | wc -l)
 '
+test_expect_success 'auto squash that matches a sha1' '
+	git reset --hard base &&
+	echo 1 >file1 &&
+	git add -u &&
+	test_tick &&
+	git commit -m "squash! $(git rev-parse --short HEAD^)" &&
+	git tag final-shasquash &&
+	test_tick &&
+	git rebase --autosquash -i HEAD^^^ &&
+	git log --oneline >actual &&
+	test 3 = $(wc -l <actual) &&
+	git diff --exit-code final-shasquash &&
+	test 1 = "$(git cat-file blob HEAD^:file1)" &&
+	test 1 = $(git cat-file commit HEAD^ | grep squash | wc -l)
+'
+
+# this test just ensures that < 4 characters can't match a sha1
+test_expect_success 'auto squash that accidentally matches a sha1' '
+	git reset --hard base &&
+	echo 1 >file1 &&
+	git add -u &&
+	test_tick &&
+	git commit -m "squash! $(git rev-parse HEAD^ | cut -c 1-3)" &&
+	git tag final-badshasquash &&
+	test_tick &&
+	git rebase --autosquash -i HEAD^^^ &&
+	git log --oneline >actual &&
+	test 4 = $(wc -l <actual) &&
+	git diff --exit-code final-badshasquash &&
+	test 0 = "$(git cat-file blob HEAD^^:file1)" &&
+	test 0 = $(git cat-file commit HEAD^^ | grep squash | wc -l) &&
+	test 1 = $(git cat-file commit HEAD | grep squash | wc -l)
+'
 
 test_done
-- 
1.7.3.2.201.g24941.dirty
Previous: Kevin BallardNext: Kevin Ballard
Message 2 of 13 in “rebase: better rearranging of fixup!/squash! lines with --autosquash”
  1. 1/2 rebase: better rearranging of fixup!/squash! lines with --autosquashKevin Ballard, Nov 4, 2010
  2. 2/2 rebase: teach --autosquash to match on sha1 in addition to messageKevin Ballard, Nov 4, 2010
  3. Kevin BallardNov 4, 2010
  4. Sverre RabbelierNov 4, 2010
  5. Kevin BallardNov 4, 2010
  6. Junio C HamanoNov 4, 2010
  7. 1/2 rebase: better rearranging of fixup!/squash! lines with --autosquashKevin Ballard, Nov 4, 2010
  8. 2/2 rebase: teach --autosquash to match on sha1 in addition to messageKevin Ballard, Nov 4, 2010
  9. Peter KreftingNov 5, 2010
  10. 1/2 rebase: better rearranging of fixup!/squash! lines with --autosquashKevin Ballard, Nov 8, 2010
  11. Yann DirsonNov 8, 2010
  12. Kevin BallardNov 8, 2010
  13. 2/2 rebase: teach --autosquash to match on sha1 in addition to messageKevin Ballard, Nov 8, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.