diff mbox series

[v3,3/6] commit: add a reword suboption to --fixup

Message ID 20210301084512.27170-4-charvi077@gmail.com (mailing list archive)
State Superseded
Headers show
Series commit: Implementation of "amend!" commit | expand

Commit Message

Charvi Mendiratta March 1, 2021, 8:45 a.m. UTC
`git commit --fixup=reword:<commit>` creates an empty "amend!" commit
that will reword <commit> without changing its contents when it is
rebased with --autosquash.

Apart from ignoring staged changes it works similarly to
`--fixup=amend:<commit>`.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
---
 builtin/commit.c | 44 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 8 deletions(-)

--
2.29.0.rc1

Comments

Junio C Hamano March 1, 2021, 6:41 p.m. UTC | #1
Charvi Mendiratta <charvi077@gmail.com> writes:

> `git commit --fixup=reword:<commit>` creates an empty "amend!" commit
> that will reword <commit> without changing its contents when it is
> rebased with --autosquash.
>
> Apart from ignoring staged changes it works similarly to
> `--fixup=amend:<commit>`.
>
> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> Helped-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
> ---
>  builtin/commit.c | 44 ++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 36 insertions(+), 8 deletions(-)
>
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 200ef83cc0..a4d18d96df 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -1186,6 +1186,27 @@ static void finalize_deferred_config(struct wt_status *s)
>  		s->ahead_behind_flags = AHEAD_BEHIND_FULL;
>  }
>
> +static void check_fixup_reword_options(int argc, const char *argv[]) {
> +	if (whence != FROM_COMMIT) {
> +		if (whence == FROM_MERGE)
> +			die(_("You are in the middle of a merge -- cannot reword."));
> +		else if (is_from_cherry_pick(whence))
> +			die(_("You are in the middle of a cherry-pick -- cannot reword."));
> +	}
> +	if (argc)
> +		die(_("cannot combine reword option of --fixup with path %s"), *argv);

I think our convention is to quote '%s' with a single-quote pair.
See other error messages.

commit.c:			die_errno(_("could not read '%s'"), templat...
commit.c:		die_errno(_("could not open '%s'"), git_path_commit...
commit.c:	die(_("--author '%s' is not 'Name <email>' and matches no e...
commit.c:		die(_("Invalid ignored mode '%s'"), ignored_arg);
commit.c:		die(_("Invalid untracked files mode '%s'"), untrack...
...
Eric Sunshine March 1, 2021, 10:36 p.m. UTC | #2
On Mon, Mar 1, 2021 at 3:50 AM Charvi Mendiratta <charvi077@gmail.com> wrote:
> `git commit --fixup=reword:<commit>` creates an empty "amend!" commit
> that will reword <commit> without changing its contents when it is
> rebased with --autosquash.
>
> Apart from ignoring staged changes it works similarly to
> `--fixup=amend:<commit>`.
>
> Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
> ---
> diff --git a/builtin/commit.c b/builtin/commit.c
> @@ -1186,6 +1186,27 @@ static void finalize_deferred_config(struct wt_status *s)
> +static void check_fixup_reword_options(int argc, const char *argv[]) {
> +       if (whence != FROM_COMMIT) {
> +               if (whence == FROM_MERGE)
> +                       die(_("You are in the middle of a merge -- cannot reword."));
> +               else if (is_from_cherry_pick(whence))
> +                       die(_("You are in the middle of a cherry-pick -- cannot reword."));
> +       }
> +       if (argc)
> +               die(_("cannot combine reword option of --fixup with path %s"), *argv);
> +       if (patch_interactive)
> +               die(_("cannot combine reword option of --fixup with --patch"));
> +       if (interactive)
> +               die(_("cannot combine reword option of --fixup with --interactive"));
> +       if (all)
> +               die(_("cannot combine reword option of --fixup with --all"));
> +       if (also)
> +               die(_("cannot combine reword option of --fixup with --include"));
> +       if (only)
> +               die(_("cannot combine reword option of --fixup with --only"));
> +}

Or, more concisely:

    if (argc)
        die(_("--fixup mutually exclusive with path '%s'), ...);
    if (patch_interactive || interactive || all || also || only)
        die(_("--fixup mutually exclusive with
--patch/--interactive/--all/--include/--only);

The mix of two different error message styles (capitalized with
full-stop vs. lowercase no-full-stop) is a bit jarring, but minor.

Not worth re-roll.
Charvi Mendiratta March 3, 2021, 7:33 a.m. UTC | #3
On Tue, 2 Mar 2021 at 00:11, Junio C Hamano <gitster@pobox.com> wrote:
>
[...]
> > +     if (argc)
> > +             die(_("cannot combine reword option of --fixup with path %s"), *argv);
>
> I think our convention is to quote '%s' with a single-quote pair.
> See other error messages.
>
> commit.c:                       die_errno(_("could not read '%s'"), templat...
> commit.c:               die_errno(_("could not open '%s'"), git_path_commit...
> commit.c:       die(_("--author '%s' is not 'Name <email>' and matches no e...
> commit.c:               die(_("Invalid ignored mode '%s'"), ignored_arg);
> commit.c:               die(_("Invalid untracked files mode '%s'"), untrack...
> ...

I admit I forgot to add it. I will fix it.
Charvi Mendiratta March 3, 2021, 7:41 a.m. UTC | #4
On Tue, 2 Mar 2021 at 04:06, Eric Sunshine <sunshine@sunshineco.com> wrote:
>

> > +       if (argc)
> > +               die(_("cannot combine reword option of --fixup with path %s"), *argv);
> > +       if (patch_interactive)
> > +               die(_("cannot combine reword option of --fixup with --patch"));
> > +       if (interactive)
> > +               die(_("cannot combine reword option of --fixup with --interactive"));
> > +       if (all)
> > +               die(_("cannot combine reword option of --fixup with --all"));
> > +       if (also)
> > +               die(_("cannot combine reword option of --fixup with --include"));
> > +       if (only)
> > +               die(_("cannot combine reword option of --fixup with --only"));
> > +}
>
> Or, more concisely:
>
>     if (argc)
>         die(_("--fixup mutually exclusive with path '%s'), ...);
>     if (patch_interactive || interactive || all || also || only)
>         die(_("--fixup mutually exclusive with
> --patch/--interactive/--all/--include/--only);
>

Got it ! Its seems more clear, I will fix it.

> The mix of two different error message styles (capitalized with
> full-stop vs. lowercase no-full-stop) is a bit jarring, but minor.
>

Okay, I will fix it.
diff mbox series

Patch

diff --git a/builtin/commit.c b/builtin/commit.c
index 200ef83cc0..a4d18d96df 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1186,6 +1186,27 @@  static void finalize_deferred_config(struct wt_status *s)
 		s->ahead_behind_flags = AHEAD_BEHIND_FULL;
 }

+static void check_fixup_reword_options(int argc, const char *argv[]) {
+	if (whence != FROM_COMMIT) {
+		if (whence == FROM_MERGE)
+			die(_("You are in the middle of a merge -- cannot reword."));
+		else if (is_from_cherry_pick(whence))
+			die(_("You are in the middle of a cherry-pick -- cannot reword."));
+	}
+	if (argc)
+		die(_("cannot combine reword option of --fixup with path %s"), *argv);
+	if (patch_interactive)
+		die(_("cannot combine reword option of --fixup with --patch"));
+	if (interactive)
+		die(_("cannot combine reword option of --fixup with --interactive"));
+	if (all)
+		die(_("cannot combine reword option of --fixup with --all"));
+	if (also)
+		die(_("cannot combine reword option of --fixup with --include"));
+	if (only)
+		die(_("cannot combine reword option of --fixup with --only"));
+}
+
 /* returns the length of intial segment of alpha characters only */
 static size_t get_alpha_len(char *fixup_message) {
 	const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
@@ -1270,18 +1291,25 @@  static int parse_and_validate_options(int argc, const char *argv[],

 	if (fixup_message) {
 		/*
-		 * As `amend` suboption contains only alpha
-		 * character. So check if first non alpha
-		 * character in fixup_message is ':'.
+		 * As `amend`/`reword` suboptions contains only alpha
+		 * characters. So check if first non alpha character
+		 * in fixup_message is ':'.
 		 */
 		size_t len = get_alpha_len(fixup_message);
 		if (len && fixup_message[len] == ':') {
 			fixup_message[len++] = '\0';
 			fixup_commit = fixup_message + len;
-			if (starts_with("amend", fixup_message))
+			if (starts_with("amend", fixup_message) ||
+			    starts_with("reword", fixup_message)) {
 				fixup_prefix = "amend";
-			else
+				if (*fixup_message == 'r') {
+					check_fixup_reword_options(argc, argv);
+					allow_empty = 1;
+					only = 1;
+				}
+			} else {
 				die(_("unknown option: --fixup=%s:%s"), fixup_message, fixup_commit);
+			}
 		} else {
 			fixup_commit = fixup_message;
 			fixup_prefix = "fixup";
@@ -1567,10 +1595,10 @@  int cmd_commit(int argc, const char **argv, const char *prefix)
 		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")),
 		/*
-		 * TRANSLATORS: Leave "[amend:]" as-is, and
-		 * only translate <commit>.
+		 * TRANSLATORS: Leave "[(amend|reword):]" 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, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword 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")),