diff mbox series

[v4,8/9] rebase -i: teach --autosquash to work with amend!

Message ID 20210129182050.26143-9-charvi077@gmail.com (mailing list archive)
State Superseded
Commit bae5b4aea523388faedd3b850c862fe45198c6b2
Headers show
Series rebase -i: add options to fixup command | expand

Commit Message

Charvi Mendiratta Jan. 29, 2021, 6:20 p.m. UTC
If the commit subject starts with "amend!" then rearrange it like a
"fixup!" commit and replace `pick` command with `fixup -C` command,
which is used to fixup up the content if any and replaces the original
commit message with amend! commit's message.

Original-patch-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
---
 sequencer.c                     | 23 ++++++++++++++++-------
 t/t3437-rebase-fixup-options.sh | 12 ++++++++++++
 2 files changed, 28 insertions(+), 7 deletions(-)

Comments

Eric Sunshine Feb. 2, 2021, 3:20 a.m. UTC | #1
On Fri, Jan 29, 2021 at 1:25 PM Charvi Mendiratta <charvi077@gmail.com> wrote:
> If the commit subject starts with "amend!" then rearrange it like a
> "fixup!" commit and replace `pick` command with `fixup -C` command,
> which is used to fixup up the content if any and replaces the original
> commit message with amend! commit's message.
>
> Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
> ---
>  sequencer.c                     | 23 ++++++++++++++++-------
>  t/t3437-rebase-fixup-options.sh | 12 ++++++++++++
>  2 files changed, 28 insertions(+), 7 deletions(-)

Is this new behavior of recognizing "amend!" in the subject documented
anywhere? I checked the documentation patch [9/9] but didn't see any
mention of it.

> diff --git a/sequencer.c b/sequencer.c
> @@ -5662,6 +5662,12 @@ static int subject2item_cmp(const void *fndata,
> +static inline int skip_fixup_amend_squash(const char *subject, const char **p) {
> +       return skip_prefix(subject, "fixup! ", p) ||
> +              skip_prefix(subject, "amend! ", p) ||
> +              skip_prefix(subject, "squash! ", p);
> +}

While the function name skip_fixup_amend_squash() may be accurate, it
won't scale well. What happens when additional fixup-like prefixes are
added in the future? Does the function name get extended to name them,
as well? How about choosing a more generic, yet still meaningful,
function name which doesn't suffer from this scaling problem. Perhaps
skip_fixupish() or fix_squashlike() or something.

Also, making this function `inline` seems like a case of premature optimization.
Charvi Mendiratta Feb. 2, 2021, 3:29 p.m. UTC | #2
On Tue, 2 Feb 2021 at 08:50, Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Fri, Jan 29, 2021 at 1:25 PM Charvi Mendiratta <charvi077@gmail.com> wrote:
> > If the commit subject starts with "amend!" then rearrange it like a
> > "fixup!" commit and replace `pick` command with `fixup -C` command,
> > which is used to fixup up the content if any and replaces the original
> > commit message with amend! commit's message.
> >
> > Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
> > ---
> >  sequencer.c                     | 23 ++++++++++++++++-------
> >  t/t3437-rebase-fixup-options.sh | 12 ++++++++++++
> >  2 files changed, 28 insertions(+), 7 deletions(-)
>
> Is this new behavior of recognizing "amend!" in the subject documented
> anywhere? I checked the documentation patch [9/9] but didn't see any
> mention of it.
>

No this patch series does not include that but included in the patch series of
amend! commit implementation that is not sent to the mailing list yet.
(Apology for the confusion)

> > diff --git a/sequencer.c b/sequencer.c
> > @@ -5662,6 +5662,12 @@ static int subject2item_cmp(const void *fndata,
> > +static inline int skip_fixup_amend_squash(const char *subject, const char **p) {
> > +       return skip_prefix(subject, "fixup! ", p) ||
> > +              skip_prefix(subject, "amend! ", p) ||
> > +              skip_prefix(subject, "squash! ", p);
> > +}
>
> While the function name skip_fixup_amend_squash() may be accurate, it
> won't scale well. What happens when additional fixup-like prefixes are
> added in the future? Does the function name get extended to name them,
> as well? How about choosing a more generic, yet still meaningful,
> function name which doesn't suffer from this scaling problem. Perhaps
> skip_fixupish() or fix_squashlike() or something.
>

I agree completely, and will change the function name  to skip_fixupish().

> Also, making this function `inline` seems like a case of premature optimization.

Okay, will remove `inline`.

Thanks and Regards,
Charvi
diff mbox series

Patch

diff --git a/sequencer.c b/sequencer.c
index 46e11d20e8..d09ce446b6 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -5662,6 +5662,12 @@  static int subject2item_cmp(const void *fndata,
 
 define_commit_slab(commit_todo_item, struct todo_item *);
 
+static inline int skip_fixup_amend_squash(const char *subject, const char **p) {
+	return skip_prefix(subject, "fixup! ", p) ||
+	       skip_prefix(subject, "amend! ", p) ||
+	       skip_prefix(subject, "squash! ", p);
+}
+
 /*
  * Rearrange the todo list that has both "pick commit-id msg" and "pick
  * commit-id fixup!/squash! msg" in it so that the latter is put immediately
@@ -5720,15 +5726,13 @@  int todo_list_rearrange_squash(struct todo_list *todo_list)
 		format_subject(&buf, subject, " ");
 		subject = subjects[i] = strbuf_detach(&buf, &subject_len);
 		unuse_commit_buffer(item->commit, commit_buffer);
-		if ((skip_prefix(subject, "fixup! ", &p) ||
-		     skip_prefix(subject, "squash! ", &p))) {
+		if (skip_fixup_amend_squash(subject, &p)) {
 			struct commit *commit2;
 
 			for (;;) {
 				while (isspace(*p))
 					p++;
-				if (!skip_prefix(p, "fixup! ", &p) &&
-				    !skip_prefix(p, "squash! ", &p))
+				if (!skip_fixup_amend_squash(p, &p))
 					break;
 			}
 
@@ -5758,9 +5762,14 @@  int todo_list_rearrange_squash(struct todo_list *todo_list)
 		}
 		if (i2 >= 0) {
 			rearranged = 1;
-			todo_list->items[i].command =
-				starts_with(subject, "fixup!") ?
-				TODO_FIXUP : TODO_SQUASH;
+			if (starts_with(subject, "fixup!")) {
+				todo_list->items[i].command = TODO_FIXUP;
+			} else if (starts_with(subject, "amend!")) {
+				todo_list->items[i].command = TODO_FIXUP;
+				todo_list->items[i].flags = TODO_REPLACE_FIXUP_MSG;
+			} else {
+				todo_list->items[i].command = TODO_SQUASH;
+			}
 			if (tail[i2] < 0) {
 				next[i] = next[i2];
 				next[i2] = i;
diff --git a/t/t3437-rebase-fixup-options.sh b/t/t3437-rebase-fixup-options.sh
index 832971ffdb..945df2555b 100755
--- a/t/t3437-rebase-fixup-options.sh
+++ b/t/t3437-rebase-fixup-options.sh
@@ -210,4 +210,16 @@  test_expect_success 'sequence squash, fixup & fixup -c gives combined message' '
 	test_cmp_rev HEAD^ A
 '
 
+test_expect_success 'fixup -C works upon --autosquash with amend!' '
+	git checkout --detach branch &&
+	FAKE_COMMIT_AMEND=squashed \
+		FAKE_MESSAGE_COPY=actual-squash-message \
+		git -c commit.status=false rebase -ik --autosquash \
+						--signoff A &&
+	git diff-tree --exit-code --patch HEAD branch -- &&
+	test_cmp_rev HEAD^ A &&
+	test_i18ncmp "$TEST_DIRECTORY/t3437/expected-squash-message" \
+		actual-squash-message
+'
+
 test_done