Thread View: linux.kernel.git
1 messages
1 total messages
Started by Erik Cervin-Edin
Tue, 24 Nov 2026 23:32
[PATCH v4 1/3] commit: write the --fixup subject the way --squash does
Author: Erik Cervin-Edin
Date: Tue, 24 Nov 2026 23:32
Date: Tue, 24 Nov 2026 23:32
122 lines
15463 bytes
15463 bytes
In prepare_to_commit(), the source of the message is chosen by an
if/else-if chain, one arm per option. Originally [1], when using
--fixup you couldn't specify a message source, but then [2] taught
commit how to combine --fixup with -m. Since then, the -m arm is
skipped for fixup commits and instead the fixup arm writes the
"fixup!" or "amend!" subject, appending any -m messages to a "fixup!".
Unlike --fixup, --squash has supported all message source options since
it was introduced [3]. It writes its "squash!" subject first and then
lets the chain write the rest of the message.
Two commits that follow teach commit --fixup how to take messages like
--squash. In preparation for that, refactor prepare_to_commit() to
write the --fixup subject in a block before the chain, as --squash does.
Since the two options are mutually exclusive, put each subject block in
its own branch of an if/else-if.
The fixup arm is left with the one message source that takes no
command-line option: an amend, which supplies the message by opening
an editor prefilled with the fixup target's message.
The -m arm now runs before the fixup arm, so the error for -m with an
amend would never be reached there. Raise it in the subject block.
While moving the subject code, rename its "commit" variable to "c" to
match the --squash block.
1. d71b8ba7c9 (commit: --fixup option for use with rebase --autosquash,
2010-11-02)
2. 30884c9afc (commit: add support for --fixup <commit> -m"<extra
message>", 2017-12-22)
3. 89ac1223c0 (commit: --squash option for use with rebase --autosquash,
2010-11-02)
Signed-off-by: Erik Cervin-Edin <erik@cervined.in>
---
builtin/commit.c | 51 +++++++++++++++++++++---------------------------
1 file changed, 22 insertions(+), 29 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 36e47e18ed..698823d4fa 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -781,7 +781,21 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
"pre-commit", NULL))
return 0;
- if (squash_message) {
+ if (fixup_message) {
+ struct pretty_print_context ctx = {0};
+ struct commit *c;
+ char *fmt;
+ c = lookup_commit_reference_by_name(fixup_commit);
+ if (!c)
+ die(_("could not lookup commit '%s'"), fixup_commit);
+ ctx.output_encoding = get_commit_output_encoding();
+ fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
+ repo_format_commit_message(the_repository, c, fmt, &sb,
+ &ctx);
+ free(fmt);
+ if (have_option_m && !strcmp(fixup_prefix, "amend"))
+ die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
+ } else if (squash_message) {
/*
* Insert the proper subject line before other commit
* message options add their content.
@@ -801,7 +815,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
}
}
- if (have_option_m && !fixup_message) {
+ if (have_option_m) {
strbuf_addbuf(&sb, &message);
hook_arg1 = "message";
} else if (logfile && !strcmp(logfile, "-")) {
@@ -823,35 +837,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
hook_arg1 = "commit";
hook_arg2 = use_message;
} else if (fixup_message) {
- struct pretty_print_context ctx = {0};
- struct commit *commit;
- char *fmt;
- commit = lookup_commit_reference_by_name(fixup_commit);
- if (!commit)
- die(_("could not lookup commit '%s'"), fixup_commit);
- ctx.output_encoding = get_commit_output_encoding();
- fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
- repo_format_commit_message(the_repository, commit, fmt, &sb,
- &ctx);
- free(fmt);
hook_arg1 = "message";
-
- /*
- * Only `-m` commit message option is checked here, as
- * it supports `--fixup` to append the commit message.
- *
- * The other commit message options `-c`/`-C`/`-F` are
- * incompatible with all the forms of `--fixup` and
- * have already errored out while parsing the `git commit`
- * options.
- */
- if (have_option_m && !strcmp(fixup_prefix, "fixup"))
- strbuf_addbuf(&sb, &message);
-
if (!strcmp(fixup_prefix, "amend")) {
- if (have_option_m)
- die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
- prepare_amend_commit(commit, &sb, &ctx);
+ struct pretty_print_context ctx = {0};
+ struct commit *c = lookup_commit_reference_by_name(fixup_commit);
+ if (!c)
+ die(_("could not lookup commit '%s'"), fixup_commit);
+ ctx.output_encoding = get_commit_output_encoding();
+ prepare_amend_commit(c, &sb, &ctx);
}
} else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
size_t merge_msg_start;
--
2.56.1.978.gcc9bcf3eaa
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads