Subject: [PATCH v3] rebase -i: do leave commit message intact in fixup! chains

From: "Johannes Schindelin via GitGitGadget" <[email-masked]>
Date: Thu, 28 Jan 2021 16:16:42 +0000

From: Johannes Schindelin <[email-masked]>

In 6e98de72c03 (sequencer (rebase -i): add support for the 'fixup' and
'squash' commands, 2017-01-02), this developer introduced a change of
behavior by mistake: when encountering a `fixup!` commit (or multiple
`fixup!` commits) without any `squash!` commit thrown in, the final `git
commit` was invoked with `--cleanup=strip`. Prior to that commit, the
commit command had been called without that `--cleanup` option.

Since we explicitly read the original commit message from a file in that
case, there is really no sense in forcing that clean-up.

We actually need to actively suppress that clean-up lest a configured
`commit.cleanup` may interfere with what we want to do: leave the commit
message unchanged.

Reported-by: Vojtěch Knyttl <[email-masked]>
Helped-by: Martin Ågren <[email-masked]>
Signed-off-by: Johannes Schindelin <[email-masked]>
---
    Fix bug in interactive rebases where fixup! cleans up the commit message
    
    Original report here:
    https://lore.kernel.org/git/CANVGpwZGbzYLMeMze64e_OU9p3bjyEgzC5thmNBr6LttBt%2BYGw%40mail.gmail.com/t
    
    Changes since v2:
    
     * Added a safeguard so that CLEANUP_MSG and VERBATIM_MSG aren't used
       together by mistake.
     * Changes since v1:
     * The fix now works even if commit.cleanup = commit

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-818%2Fdscho%2Fautosquash-without-scissors-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-818/dscho/autosquash-without-scissors-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/818

Range-diff vs v2:

 1:  c760d6cd203 ! 1:  8acb9b4562e rebase -i: do leave commit message intact in fixup! chains
     @@ sequencer.c: N_("you have staged changes in your working tree\n"
       
       static int run_command_silent_on_success(struct child_process *cmd)
       {
     +@@ sequencer.c: static int run_git_commit(const char *defmsg,
     + {
     + 	struct child_process cmd = CHILD_PROCESS_INIT;
     + 
     ++	if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
     ++		BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
     ++
     + 	cmd.git_cmd = 1;
     + 
     + 	if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
      @@ sequencer.c: static int run_git_commit(const char *defmsg,
       		strvec_pushl(&cmd.args, "-C", "HEAD", NULL);
       	if ((flags & CLEANUP_MSG))
     @@ sequencer.c: static int run_git_commit(const char *defmsg,
       	if ((flags & EDIT_MSG))
       		strvec_push(&cmd.args, "-e");
       	else if (!(flags & CLEANUP_MSG) &&
     +@@ sequencer.c: static int try_to_commit(struct repository *r,
     + 	enum commit_msg_cleanup_mode cleanup;
     + 	int res = 0;
     + 
     ++	if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
     ++		BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
     ++
     + 	if (parse_head(r, &current_head))
     + 		return -1;
     + 
      @@ sequencer.c: static int try_to_commit(struct repository *r,
       
       	if (flags & CLEANUP_MSG)

 sequencer.c                  | 13 ++++++++++++-
 t/t3415-rebase-autosquash.sh |  8 ++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/sequencer.c b/sequencer.c
index 8909a467700..264d494a64c 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -943,6 +943,7 @@ N_("you have staged changes in your working tree\n"
 #define CLEANUP_MSG (1<<3)
 #define VERIFY_MSG  (1<<4)
 #define CREATE_ROOT_COMMIT (1<<5)
+#define VERBATIM_MSG (1<<6)
 
 static int run_command_silent_on_success(struct child_process *cmd)
 {
@@ -979,6 +980,9 @@ static int run_git_commit(const char *defmsg,
 {
 	struct child_process cmd = CHILD_PROCESS_INIT;
 
+	if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
+		BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
+
 	cmd.git_cmd = 1;
 
 	if (is_rebase_i(opts) && read_env_script(&cmd.env_array)) {
@@ -1012,6 +1016,8 @@ static int run_git_commit(const char *defmsg,
 		strvec_pushl(&cmd.args, "-C", "HEAD", NULL);
 	if ((flags & CLEANUP_MSG))
 		strvec_push(&cmd.args, "--cleanup=strip");
+	if ((flags & VERBATIM_MSG))
+		strvec_push(&cmd.args, "--cleanup=verbatim");
 	if ((flags & EDIT_MSG))
 		strvec_push(&cmd.args, "-e");
 	else if (!(flags & CLEANUP_MSG) &&
@@ -1380,6 +1386,9 @@ static int try_to_commit(struct repository *r,
 	enum commit_msg_cleanup_mode cleanup;
 	int res = 0;
 
+	if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
+		BUG("CLEANUP_MSG and VERBATIM_MSG are mutually exclusive");
+
 	if (parse_head(r, &current_head))
 		return -1;
 
@@ -1454,6 +1463,8 @@ static int try_to_commit(struct repository *r,
 
 	if (flags & CLEANUP_MSG)
 		cleanup = COMMIT_MSG_CLEANUP_ALL;
+	else if (flags & VERBATIM_MSG)
+		cleanup = COMMIT_MSG_CLEANUP_NONE;
 	else if ((opts->signoff || opts->record_origin) &&
 		 !opts->explicit_cleanup)
 		cleanup = COMMIT_MSG_CLEANUP_SPACE;
@@ -2002,7 +2013,7 @@ static int do_pick_commit(struct repository *r,
 		if (!final_fixup)
 			msg_file = rebase_path_squash_msg();
 		else if (file_exists(rebase_path_fixup_msg())) {
-			flags |= CLEANUP_MSG;
+			flags |= VERBATIM_MSG;
 			msg_file = rebase_path_fixup_msg();
 		} else {
 			const char *dest = git_path_squash_msg(r);
diff --git a/t/t3415-rebase-autosquash.sh b/t/t3415-rebase-autosquash.sh
index 7bab6000dc7..88040bc4352 100755
--- a/t/t3415-rebase-autosquash.sh
+++ b/t/t3415-rebase-autosquash.sh
@@ -440,4 +440,12 @@ test_expect_success 'fixup a fixup' '
 	test XZWY = $(git show | tr -cd W-Z)
 '
 
+test_expect_success 'fixup does not clean up commit message' '
+	oneline="#818" &&
+	git commit --allow-empty -m "$oneline" &&
+	git commit --fixup HEAD --allow-empty &&
+	git -c commit.cleanup=strip rebase -ki --autosquash HEAD~2 &&
+	test "$oneline" = "$(git show -s --format=%s)"
+'
+
 test_done

base-commit: ba2aa15129e59f248d8cdd30404bc78b5178f61d
-- 
gitgitgadget