]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit: allow -c/-C for all kinds of --fixup
authorErik Cervin-Edin <erik@cervined.in>
Tue, 26 May 2026 10:47:44 +0000 (12:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 May 2026 11:46:24 +0000 (20:46 +0900)
The previous commit allowed -m and -F for all --fixup variations.  The
-c/-C flags were blocked by the same higher-layer incompatibility check
that previously caught -F, namely die_for_incompatible_opt4() grouping
them with --fixup.

Drop --fixup from that check and route the resolved commit through
prepare_amend_commit() in the fixup path, mirroring the no-message-source
behaviour of --fixup=amend.  With this in place, -m/-F/-c/-C all behave
consistently across the plain, amend, and reword --fixup forms.

Signed-off-by: Erik Cervin-Edin <erik@cervined.in>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-commit.adoc
builtin/commit.c
t/t7500-commit-template-squash-signoff.sh

index 61efd29e66e7d0c06e5aab2edec198b5d9dc9cba..98c50a3be5c81caea5d44cd5a62a06106c07c23f 100644 (file)
@@ -102,8 +102,8 @@ include::diff-context-options.adoc[]
 +
 The commit created by plain `--fixup=<commit>` has a title
 composed of "fixup!" followed by the title of _<commit>_,
-and is recognized specially by `git rebase --autosquash`. The `-m`
-or `-F` option may be used to supplement the log message
+and is recognized specially by `git rebase --autosquash`. The `-m`,
+`-F`, `-C`, or `-c` option may be used to supplement the log message
 of the created commit, but the additional commentary will be thrown
 away once the "fixup!" commit is squashed into _<commit>_ by
 `git rebase --autosquash`.
@@ -112,8 +112,9 @@ The commit created by `--fixup=amend:<commit>` is similar but its
 title is instead prefixed with "amend!". The log message of
 _<commit>_ is copied into the log message of the "amend!" commit and
 opened in an editor so it can be refined. The replacement message may
-also be supplied directly using `-m` or `-F`, bypassing the
-need to open an editor. When `git rebase
+also be supplied directly using `-m`, `-F`, or `-C`, bypassing the
+need to open an editor, or using `-c` to open the editor pre-populated
+with the referenced commit's message. When `git rebase
 --autosquash` squashes the "amend!" commit into _<commit>_, the log
 message of _<commit>_ is replaced by the refined log message from the
 "amend!" commit. It is an error for the "amend!" commit's log message
index 3f1fca291968de598c1e19bb58abf79c030d92eb..fcf148eb21b95b2732fe2e23a6764943a419be9c 100644 (file)
@@ -837,9 +837,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                hook_arg1 = "message";
 
                /*
-                * Only `-m` and `-F` are handled here. `-c`/`-C` are
-                * incompatible with --fixup and have already errored out
-                * during option parsing.
+                * `-m`, `-F`, `-C`, and `-c` provide the message body.
+                * If none was given and this is an amend, use the target
+                * commit's body instead.
                 */
                if (have_option_m) {
                        strbuf_addbuf(&sb, &message);
@@ -851,6 +851,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                } else if (logfile) {
                        if (strbuf_read_file(&sb, logfile, 0) < 0)
                                die_errno(_("could not read log file '%s'"), logfile);
+               } else if (use_message) {
+                       struct commit *c = lookup_commit_reference_by_name(use_message);
+                       if (!c)
+                               die(_("could not lookup commit '%s'"), use_message);
+                       prepare_amend_commit(c, &sb, &ctx);
                } else if (!strcmp(fixup_prefix, "amend")) {
                        prepare_amend_commit(commit, &sb, &ctx);
                }
@@ -1341,7 +1346,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
                die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
        die_for_incompatible_opt3(!!use_message, "-C",
                                  !!edit_message, "-c",
-                                 !!fixup_message, "--fixup");
+                                 !!logfile, "-F");
        die_for_incompatible_opt4(have_option_m, "-m",
                                  !!edit_message, "-c",
                                  !!use_message, "-C",
index 01c7400136d437282761acee67f88bc2dede7d4c..48e1247d9ebb5a0633e6333208c7577ae9a951e8 100755 (executable)
@@ -492,6 +492,62 @@ test_expect_success 'commit --fixup works with -F' '
        EOF
 '
 
+test_expect_success 'commit --fixup works with -C' '
+       commit_for_rebase_autosquash_setup &&
+       git commit --fixup HEAD~ -C HEAD &&
+       test_commit_message HEAD <<-EOF
+       fixup! $(git log -1 --format=%s HEAD~2)
+
+       $(get_commit_msg HEAD~)
+       EOF
+'
+
+test_expect_success 'commit --fixup=amend: works with -c' '
+       commit_for_rebase_autosquash_setup &&
+       test_set_editor : &&
+       git commit --fixup=amend:HEAD -c HEAD~ &&
+       test_commit_message HEAD <<-EOF
+       amend! intermediate commit
+
+       target message subject line
+
+       target message body line 1
+       target message body line 2
+       EOF
+'
+
+test_expect_success 'commit --fixup=amend:HEAD with -C HEAD and without have the same message' '
+       commit_for_rebase_autosquash_setup &&
+       start=$(git rev-parse HEAD) &&
+
+       git commit --fixup=amend:HEAD -C HEAD &&
+       git commit --fixup=amend:HEAD -C HEAD &&
+       git log -1 --pretty=%B >with-c &&
+
+       git reset --hard "$start" &&
+       test_set_editor : &&
+       git commit --fixup=amend:HEAD &&
+       git commit --fixup=amend:HEAD &&
+       git log -1 --pretty=%B >without-c &&
+
+       test_cmp with-c without-c
+'
+
+test_expect_success 'commit --fixup=amend: with -C copies full subject + body of squash commit' '
+       commit_for_rebase_autosquash_setup &&
+       git commit --squash HEAD~ -m "inner body" &&
+       echo "extra" >>foo &&
+       git add foo &&
+       git commit --fixup=amend:HEAD -C HEAD &&
+       test_commit_message HEAD <<-EOF
+       amend! squash! $(git log -1 --format=%s HEAD~3)
+
+       squash! $(git log -1 --format=%s HEAD~3)
+
+       inner body
+       EOF
+'
+
 test_expect_success 'commit --fixup=reword: works with -F' '
        commit_for_rebase_autosquash_setup &&
        echo "message from file" >msgfile &&
@@ -553,9 +609,7 @@ test_expect_success 'invalid message options when using --fixup' '
        echo changes >>foo &&
        echo "message" >log &&
        git add foo &&
-       test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
-       test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
-       test_must_fail git commit --fixup HEAD~1 -c HEAD~2
+       test_must_fail git commit --fixup HEAD~1 --squash HEAD~2
 '
 
 cat >expected-template <<EOF