Subject: Re: [PATCH] rebase -i: fixup fixup! fixup!

From: Andrew Pimlott <[email-masked]>
Date: Fri, 14 Jun 2013 23:50:21 -0700

Excerpts from Andrew Pimlott's message of Fri Jun 14 12:31:57 -0700 2013:
> It happened to work and I added a test.  But then it occurred to me that
> it might have been better to fix commit --fixup/--squash to strip the
> fixup! or squash! from the referenced commit in the first place.
> Anyhow, below is my patch for --autosquash, but unles someone has an
> objection to doing it in commit, I'll work on that.

Here is a patch for commit.c that does this.

Andrew

When building the commit message for --fixup/--squash, ignore a leading
fixup! or squash! on the referenced commit.  Handy in case the user referred
to an earlier squash/fixup commit instead of the original commit, for
example with :/msg.

Signed-off-by: Andrew Pimlott <[email-masked]>
---
 builtin/commit.c  |   18 ++++++++++++++----
 t/t7500-commit.sh |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 1621dfc..370df88 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -601,8 +601,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 			if (!c)
 				die(_("could not lookup commit %s"), squash_message);
 			ctx.output_encoding = get_commit_output_encoding();
-			format_commit_message(c, "squash! %s\n\n", &sb,
-					      &ctx);
+			format_commit_message(c, "%s\n\n", &sb, &ctx);
+			if (!prefixcmp(sb.buf, "fixup! ")) {
+				strbuf_remove(&sb, 0, strlen("fixup! "));
+			} else if (!prefixcmp(sb.buf, "squash! ")) {
+				strbuf_remove(&sb, 0, strlen("squash! "));
+			}
+			strbuf_insert(&sb, 0, "squash! ", strlen("squash! "));
 		}
 	}
 
@@ -634,8 +639,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		if (!commit)
 			die(_("could not lookup commit %s"), fixup_message);
 		ctx.output_encoding = get_commit_output_encoding();
-		format_commit_message(commit, "fixup! %s\n\n",
-				      &sb, &ctx);
+		format_commit_message(commit, "%s\n\n", &sb, &ctx);
+		if (!prefixcmp(sb.buf, "fixup! ")) {
+			strbuf_remove(&sb, 0, strlen("fixup! "));
+		} else if (!prefixcmp(sb.buf, "squash! ")) {
+			strbuf_remove(&sb, 0, strlen("squash! "));
+		}
+		strbuf_insert(&sb, 0, "fixup! ", strlen("fixup! "));
 		hook_arg1 = "message";
 	} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
 		if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
diff --git a/t/t7500-commit.sh b/t/t7500-commit.sh
index 436b7b6..ecdf74a 100755
--- a/t/t7500-commit.sh
+++ b/t/t7500-commit.sh
@@ -320,4 +320,40 @@ test_expect_success 'invalid message options when using --fixup' '
 	test_must_fail git commit --fixup HEAD~1 -F log
 '
 
+test_expect_success 'commit --fixup of existing fixup' '
+	commit_for_rebase_autosquash_setup &&
+	git commit --fixup HEAD~1 &&
+	echo "fourth content line" >>foo &&
+	git add foo
+	git commit --fixup HEAD &&
+	commit_msg_is "fixup! target message subject line"
+'
+
+test_expect_success 'commit --fixup of existing squash' '
+	commit_for_rebase_autosquash_setup &&
+	git commit --squash HEAD~1 &&
+	echo "fourth content line" >>foo &&
+	git add foo
+	git commit --fixup HEAD &&
+	commit_msg_is "fixup! target message subject line"
+'
+
+test_expect_success 'commit --squash of existing squash' '
+	commit_for_rebase_autosquash_setup &&
+	git commit --squash HEAD~1 &&
+	echo "fourth content line" >>foo &&
+	git add foo
+	git commit --squash HEAD &&
+	commit_msg_is "squash! target message subject linecommit message"
+'
+
+test_expect_success 'commit --squash of existing fixup' '
+	commit_for_rebase_autosquash_setup &&
+	git commit --fixup HEAD~1 &&
+	echo "fourth content line" >>foo &&
+	git add foo
+	git commit --squash HEAD &&
+	commit_msg_is "squash! target message subject linecommit message"
+'
+
 test_done
-- 
1.7.10.4