commit: add a reword suboption to --fixup

`git commit --fixup=reword:<commit>` aliases `--fixup=amend:<commit> --only`, where it creates an empty "amend!" commit that will reword <commit> without changing its contents when it is rebased with `--autosquash`. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Charvi Mendiratta committed Mar 15, 2021 at 13:24 UTC 3270ae82acdec3d25491427e0ecff43c014ac4d9
1 file changed +25 -6
builtin/commit.c
+25 -6
@@ -1188,6 +1188,19 @@ static void finalize_deferred_config(struct wt_status *s)
1188 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
1189 }
1190
1191 +static void check_fixup_reword_options(int argc, const char *argv[]) {
1192 + if (whence != FROM_COMMIT) {
1193 + if (whence == FROM_MERGE)
1194 + die(_("You are in the middle of a merge -- cannot reword."));
1195 + else if (is_from_cherry_pick(whence))
1196 + die(_("You are in the middle of a cherry-pick -- cannot reword."));
1197 + }
1198 + if (argc)
1199 + die(_("cannot combine reword option of --fixup with path '%s'"), *argv);
1200 + if (patch_interactive || interactive || all || also || only)
1201 + die(_("reword option of --fixup is mutually exclusive with --patch/--interactive/--all/--include/--only"));
1202 +}
1203 +
1204 static int parse_and_validate_options(int argc, const char *argv[],
1205 const struct option *options,
1206 const char * const usage[],
@@ -1269,8 +1282,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
1282 * We limit --fixup's suboptions to only alpha characters.
1283 * If the first character after a run of alpha is colon,
1284 * then the part before the colon may be a known suboption
1272 - * name `amend` or a misspelt suboption name. In this case,
1273 - * we treat it as --fixup=<suboption>:<arg>.
1285 + * name like `amend` or `reword`, or a misspelt suboption
1286 + * name. In either case, we treat it as
1287 + * --fixup=<suboption>:<arg>.
1288 *
1289 * Otherwise, we are dealing with --fixup=<commit>.
1290 */
@@ -1280,9 +1294,14 @@ static int parse_and_validate_options(int argc, const char *argv[],
1294 if (p > fixup_message && *p == ':') {
1295 *p = '\0';
1296 fixup_commit = p + 1;
1283 - if (!strcmp("amend", fixup_message)) {
1297 + if (!strcmp("amend", fixup_message) ||
1298 + !strcmp("reword", fixup_message)) {
1299 fixup_prefix = "amend";
1300 allow_empty = 1;
1301 + if (*fixup_message == 'r') {
1302 + check_fixup_reword_options(argc, argv);
1303 + only = 1;
1304 + }
1305 } else {
1306 die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
1307 }
@@ -1571,10 +1590,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1590 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1591 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1592 /*
1574 - * TRANSLATORS: Leave "[amend:]" as-is, and
1575 - * only translate <commit>.
1593 + * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1594 + * and only translate <commit>.
1595 */
1577 - OPT_STRING(0, "fixup", &fixup_message, N_("[amend:]commit"), N_("use autosquash formatted message to fixup or amend specified commit")),
1596 + OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1597 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1598 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1599 OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")),