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

[PATCH v6 2/6] commit: add amend suboption to --fixup to create amend! commit

From
Charvi Mendiratta <charvi077@gmail.com>
Date
Mar 15, 2021, 07:54 UTC
Message-ID
<20210315075435.18229-3-charvi077@gmail.com>
In-Reply-To
<20210310194306.32565-1-charvi077@gmail.com>

`git commit --fixup=amend:<commit>` will create an "amend!" commit. The resulting commit message subject will be "amend! ..." where "..." is the subject line of <commit> and the initial message body will be <commit>'s message.

The "amend!" commit when rebased with --autosquash will fixup the contents and replace the commit message of <commit> with the "amend!" commit's message body.

In order to prevent rebase from creating commits with an empty message we refuse to create an "amend!" commit if commit message body is empty.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
---
 builtin/commit.c | 103 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 93 insertions(+), 10 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 505fe60956..9697bba10b 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -105,7 +105,8 @@ static const char *template_file;
  */
 static const char *author_message, *author_message_buffer;
 static char *edit_message, *use_message;
-static char *fixup_message, *squash_message;
+static char *fixup_message, *fixup_commit, *squash_message;
+static const char *fixup_prefix;
 static int all, also, interactive, patch_interactive, only, amend, signoff;
 static int edit_flag = -1; /* unspecified */
 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
@@ -357,7 +358,8 @@ static const char *prepare_index(const char **argv, const char *prefix,
 		die(_("--pathspec-file-nul requires --pathspec-from-file"));
 	}
 
-	if (!pathspec.nr && (also || (only && !amend && !allow_empty)))
+	if (!pathspec.nr && (also || (only && !allow_empty &&
+	    (!amend || (fixup_message && strcmp(fixup_prefix, "amend"))))))
 		die(_("No paths with --include/--only does not make sense."));
 
 	if (read_cache_preload(&pathspec) < 0)
@@ -681,6 +683,22 @@ static void adjust_comment_line_char(const struct strbuf *sb)
 	comment_line_char = *p;
 }
 
+static void prepare_amend_commit(struct commit *commit, struct strbuf *sb,
+				struct pretty_print_context *ctx)
+{
+	const char *buffer, *subject, *fmt;
+
+	buffer = get_commit_buffer(commit, NULL);
+	find_commit_subject(buffer, &subject);
+	/*
+	 * If we amend the 'amend!' commit then we don't want to
+	 * duplicate the subject line.
+	 */
+	fmt = starts_with(subject, "amend!") ? "%b" : "%B";
+	format_commit_message(commit, fmt, sb, ctx);
+	unuse_commit_buffer(commit, buffer);
+}
+
 static int prepare_to_commit(const char *index_file, const char *prefix,
 			     struct commit *current_head,
 			     struct wt_status *s,
@@ -745,15 +763,33 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 	} else if (fixup_message) {
 		struct pretty_print_context ctx = {0};
 		struct commit *commit;
-		commit = lookup_commit_reference_by_name(fixup_message);
+		char *fmt;
+		commit = lookup_commit_reference_by_name(fixup_commit);
 		if (!commit)
-			die(_("could not lookup commit %s"), fixup_message);
+			die(_("could not lookup commit %s"), fixup_commit);
 		ctx.output_encoding = get_commit_output_encoding();
-		format_commit_message(commit, "fixup! %s\n\n",
-				      &sb, &ctx);
-		if (have_option_m)
-			strbuf_addbuf(&sb, &message);
+		fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
+		format_commit_message(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(_("cannot combine -m with --fixup:%s"), fixup_message);
+			prepare_amend_commit(commit, &sb, &ctx);
+		}
 	} else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
 		size_t merge_msg_start;
 
@@ -1170,7 +1206,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
 	if (force_author && renew_authorship)
 		die(_("Using both --reset-author and --author does not make sense"));
 
-	if (logfile || have_option_m || use_message || fixup_message)
+	if (logfile || have_option_m || use_message)
 		use_editor = 0;
 	if (0 <= edit_flag)
 		use_editor = edit_flag;
@@ -1227,6 +1263,36 @@ static int parse_and_validate_options(int argc, const char *argv[],
 
 	if (also + only + all + interactive > 1)
 		die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
+
+	if (fixup_message) {
+		/*
+		 * We limit --fixup's suboptions to only alpha characters.
+		 * If the first character after a run of alpha is colon,
+		 * then the part before the colon may be a known suboption
+		 * name `amend` or a misspelt suboption name. In this case,
+		 * we treat it as --fixup=<suboption>:<arg>.
+		 *
+		 * Otherwise, we are dealing with --fixup=<commit>.
+		 */
+		char *p = fixup_message;
+		while (isalpha(*p))
+			p++;
+		if (p > fixup_message && *p == ':') {
+			*p = '\0';
+			fixup_commit = p + 1;
+			if (!strcmp("amend", fixup_message)) {
+				fixup_prefix = "amend";
+				allow_empty = 1;
+			} else {
+				die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
+			}
+		} else {
+			fixup_commit = fixup_message;
+			fixup_prefix = "fixup";
+			use_editor = 0;
+		}
+	}
+
 	cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
 
 	handle_untracked_files_arg(s);
@@ -1504,7 +1570,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
 		OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
 		OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
-		OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
+		/*
+		 * TRANSLATORS: Leave "[amend:]" as-is, and
+		 * only translate <commit>.
+		 */
+		OPT_STRING(0, "fixup", &fixup_message, N_("[amend:]commit"), N_("use autosquash formatted message to fixup or amend specified commit")),
 		OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
 		OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
 		OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),
@@ -1663,6 +1733,19 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		exit(1);
 	}
 
+	if (fixup_message && starts_with(sb.buf, "amend! ") &&
+	    !allow_empty_message) {
+		struct strbuf body = STRBUF_INIT;
+		size_t len = commit_subject_length(sb.buf);
+		strbuf_addstr(&body, sb.buf + len);
+		if (message_is_empty(&body, cleanup_mode)) {
+			rollback_index_files();
+			fprintf(stderr, _("Aborting commit due to empty commit message body.\n"));
+			exit(1);
+		}
+		strbuf_release(&body);
+	}
+
 	if (amend) {
 		const char *exclude_gpgsig[3] = { "gpgsig", "gpgsig-sha256", NULL };
 		extra = read_commit_extra_headers(current_head, exclude_gpgsig);
-- 
2.29.0.rc1
Previous: Charvi MendirattaNext: Charvi Mendiratta
Message 66 of 95 in “[Outreachy] commit: Implementation of "amend!" commit”
  1. 0/6 [Outreachy] commit: Implementation of "amend!" commitCharvi Mendiratta, Mar 1, 2021
  2. 2/6 commit: add amend suboption to --fixup to create amend! commitCharvi Mendiratta, Mar 1, 2021
  3. Junio C HamanoMar 1, 2021
  4. Charvi MendirattaMar 3, 2021
  5. Eric SunshineMar 1, 2021
  6. Junio C HamanoMar 1, 2021
  7. Eric SunshineMar 1, 2021
  8. Charvi MendirattaMar 3, 2021
  9. Charvi MendirattaMar 3, 2021
  10. Eric SunshineMar 3, 2021
  11. Charvi MendirattaMar 3, 2021
  12. Junio C HamanoMar 4, 2021
  13. Charvi MendirattaMar 4, 2021
  14. Charvi MendirattaMar 3, 2021
  15. Eric SunshineMar 3, 2021
  16. Charvi MendirattaMar 3, 2021
  17. 1/6 sequencer: export subject_length()Charvi Mendiratta, Mar 1, 2021
  18. Eric SunshineMar 1, 2021
  19. Junio C HamanoMar 3, 2021
  20. Charvi MendirattaMar 3, 2021
  21. 5/6 t3437: use --fixup with options to create amend! commitCharvi Mendiratta, Mar 1, 2021
  22. 4/6 t7500: add tests for --fixup=[amend|reword] optionsCharvi Mendiratta, Mar 1, 2021
  23. Eric SunshineMar 2, 2021
  24. Junio C HamanoMar 3, 2021
  25. Charvi MendirattaMar 3, 2021
  26. 3/6 commit: add a reword suboption to --fixupCharvi Mendiratta, Mar 1, 2021
  27. Junio C HamanoMar 1, 2021
  28. Charvi MendirattaMar 3, 2021
  29. Eric SunshineMar 1, 2021
  30. Charvi MendirattaMar 3, 2021
  31. 6/6 doc/git-commit: add documentation for fixup=[amend|reword] optionsCharvi Mendiratta, Mar 1, 2021
  32. Junio C HamanoMar 1, 2021
  33. Charvi MendirattaMar 3, 2021
  34. Eric SunshineMar 2, 2021
  35. Charvi MendirattaMar 3, 2021
  36. Eric SunshineMar 3, 2021
  37. Charvi MendirattaMar 3, 2021
  38. Junio C HamanoMar 4, 2021
  39. Charvi MendirattaMar 4, 2021
  40. Junio C HamanoMar 4, 2021
  41. Charvi MendirattaMar 5, 2021
  42. Junio C HamanoMar 5, 2021
  43. Charvi MendirattaMar 6, 2021
  44. Eric SunshineMar 6, 2021
  45. Junio C HamanoMar 1, 2021
  46. Charvi MendirattaMar 3, 2021
  47. 0/6 [Outreachy] commit: Implementation of "amend!" commitCharvi Mendiratta, Mar 10, 2021
  48. Eric SunshineMar 11, 2021
  49. Charvi MendirattaMar 11, 2021
  50. 3/6 commit: add a reword suboption to --fixupCharvi Mendiratta, Mar 13, 2021
  51. 0/6 [Outreachy] commit: Implementation of "amend!" commitCharvi Mendiratta, Mar 13, 2021
  52. 2/6 commit: add amend suboption to --fixup to create amend! commitCharvi Mendiratta, Mar 13, 2021
  53. Eric SunshineMar 14, 2021
  54. 1/6 sequencer: export and rename subject_length()Charvi Mendiratta, Mar 13, 2021
  55. 4/6 t7500: add tests for --fixup=[amend|reword] optionsCharvi Mendiratta, Mar 13, 2021
  56. 5/6 t3437: use --fixup with options to create amend! commitCharvi Mendiratta, Mar 13, 2021
  57. 6/6 doc/git-commit: add documentation for fixup=[amend|reword] optionsCharvi Mendiratta, Mar 13, 2021
  58. Eric SunshineMar 14, 2021
  59. Charvi MendirattaMar 14, 2021
  60. 0/6 [Outreachy] commit: Implementation of "amend!" commitCharvi Mendiratta, Mar 15, 2021
  61. Junio C HamanoMar 19, 2021
  62. Eric SunshineMar 19, 2021
  63. Charvi MendirattaMar 19, 2021
  64. 1/6 sequencer: export and rename subject_length()Charvi Mendiratta, Mar 15, 2021
  65. 3/6 commit: add a reword suboption to --fixupCharvi Mendiratta, Mar 15, 2021
  66. 2/6 commit: add amend suboption to --fixup to create amend! commitCharvi Mendiratta, Mar 15, 2021
  67. 4/6 t7500: add tests for --fixup=[amend|reword] optionsCharvi Mendiratta, Mar 15, 2021
  68. 6/6 doc/git-commit: add documentation for fixup=[amend|reword] optionsCharvi Mendiratta, Mar 15, 2021
  69. 5/6 t3437: use --fixup with options to create amend! commitCharvi Mendiratta, Mar 15, 2021
  70. 2/6 commit: add amend suboption to --fixup to create amend! commitCharvi Mendiratta, Mar 10, 2021
  71. Eric SunshineMar 11, 2021
  72. Charvi MendirattaMar 11, 2021
  73. Eric SunshineMar 11, 2021
  74. Charvi MendirattaMar 11, 2021
  75. Junio C HamanoMar 14, 2021
  76. Charvi MendirattaMar 14, 2021
  77. Junio C HamanoMar 14, 2021
  78. Eric SunshineMar 14, 2021
  79. Charvi MendirattaMar 15, 2021
  80. Eric SunshineMar 15, 2021
  81. Charvi MendirattaMar 15, 2021
  82. 1/6 sequencer: export and rename subject_length()Charvi Mendiratta, Mar 10, 2021
  83. 3/6 commit: add a reword suboption to --fixupCharvi Mendiratta, Mar 10, 2021
  84. Junio C HamanoMar 11, 2021
  85. Charvi MendirattaMar 11, 2021
  86. Junio C HamanoMar 11, 2021
  87. Eric SunshineMar 11, 2021
  88. Charvi MendirattaMar 11, 2021
  89. 6/6 doc/git-commit: add documentation for fixup=[amend|reword] optionsCharvi Mendiratta, Mar 10, 2021
  90. Junio C HamanoMar 11, 2021
  91. Charvi MendirattaMar 11, 2021
  92. Eric SunshineMar 11, 2021
  93. Charvi MendirattaMar 11, 2021
  94. 5/6 t3437: use --fixup with options to create amend! commitCharvi Mendiratta, Mar 10, 2021
  95. 4/6 t7500: add tests for --fixup=[amend|reword] optionsCharvi Mendiratta, Mar 10, 2021

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.