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

Re: [PATCH v2 1/8] rebase --apply: remove duplicated code

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 20, 2022, 10:10 UTC
Message-ID
<220420.86a6cg83b0.gmgdl@evledraar.gmail.com>
In-Reply-To
<a4320f2fcf35f180e1c585be4105194f1555a874.1650448612.git.gitgitgadget@gmail.com>
On Wed, Apr 20 2022, Phillip Wood via GitGitGadget wrote:
Show 11 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> When we are reattaching HEAD after a fast-forward we can use
> move_to_original_branch() rather than open coding a copy of that
> code. The old code appears to handle the case where the rebase is
> started from a detached HEAD but in fact in that case there is nothing
> to do as we have already updated HEAD.
>
> Note that the removal of "strbuf_release(&msg)" is safe as there is an
> identical call just above this hunk which can be seen by viewing the
> diff with -U6.
Instead of assuring the reader that this is OK, perhaps just squash this
in:
	
	diff --git a/builtin/rebase.c b/builtin/rebase.c
	index 4a664964c30..5108679e816 100644
	--- a/builtin/rebase.c
	+++ b/builtin/rebase.c
	@@ -1029,7 +1029,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 	int ret, flags, total_argc, in_progress = 0;
	 	int keep_base = 0;
	 	int ok_to_skip_pre_rebase = 0;
	-	struct strbuf msg = STRBUF_INIT;
	 	struct strbuf revisions = STRBUF_INIT;
	 	struct strbuf buf = STRBUF_INIT;
	 	struct object_id merge_base;
	@@ -1769,17 +1768,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 		printf(_("First, rewinding head to replay your work on top of "
	 			 "it...\n"));
	 
	-	strbuf_addf(&msg, "%s: checkout %s",
	-		    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
	-	ropts.oid = &options.onto->object.oid;
	-	ropts.orig_head = &options.orig_head,
	-	ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
	+	{
	+		struct strbuf msg = STRBUF_INIT;
	+
	+		strbuf_addf(&msg, "%s: checkout %s",
	+			    getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
	+			    options.onto_name);
	+		ropts.oid = &options.onto->object.oid;
	+		ropts.orig_head = &options.orig_head,
	+			ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
	 			RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
	-	ropts.head_msg = msg.buf;
	-	ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
	-	if (reset_head(the_repository, &ropts))
	-		die(_("Could not detach HEAD"));
	-	strbuf_release(&msg);
	+		ropts.head_msg = msg.buf;
	+		ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
	+		if (reset_head(the_repository, &ropts))
	+			die(_("Could not detach HEAD"));
	+		strbuf_release(&msg);
	+	}
	 
	 	/*
	 	 * If the onto is a proper descendant of the tip of the branch, then

That's a 2-line change (aside from the scope addition) if viewed with -w. I.e. before your change below we needed &msg in two places, now that we only need it in one it seems better just to move the variable to be scoped with its only user.

And maybe it's getting too much into unrelated cleanup, but this change also leaves the loose end that the "ropts" variable no longer needs to be shared. You added it in 6ae8086161d (reset_head(): take struct rebase_head_opts, 2022-01-26) along whith the memset() removed here, but (on top of the proposed squash) we could also do this:

	
	diff --git a/builtin/rebase.c b/builtin/rebase.c
	index 5108679e816..8e2dc74c834 100644
	--- a/builtin/rebase.c
	+++ b/builtin/rebase.c
	@@ -1043,7 +1043,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 	int reschedule_failed_exec = -1;
	 	int allow_preemptive_ff = 1;
	 	int preserve_merges_selected = 0;
	-	struct reset_head_opts ropts = { 0 };
	 	struct option builtin_rebase_options[] = {
	 		OPT_STRING(0, "onto", &options.onto_name,
	 			   N_("revision"),
	@@ -1275,7 +1274,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 	}
	 	case ACTION_SKIP: {
	 		struct string_list merge_rr = STRING_LIST_INIT_DUP;
	-
	+		struct reset_head_opts ropts = { 0 };
	+	
	 		options.action = "skip";
	 		set_reflog_action(&options);
	 
	@@ -1291,6 +1291,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 	}
	 	case ACTION_ABORT: {
	 		struct string_list merge_rr = STRING_LIST_INIT_DUP;
	+		struct reset_head_opts ropts = { 0 };
	+
	 		options.action = "abort";
	 		set_reflog_action(&options);
	 
	@@ -1770,6 +1772,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
	 
	 	{
	 		struct strbuf msg = STRBUF_INIT;
	+		struct reset_head_opts ropts = { 0 };
	 
	 		strbuf_addf(&msg, "%s: checkout %s",
	 			    getenv(GIT_REFLOG_ACTION_ENVIRONMENT),

I.e. the "ropts" earlier in the function wasn't shared with the code below, we'd "goto" past it...

Show 31 quoted lines
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  builtin/rebase.c | 11 +----------
>  1 file changed, 1 insertion(+), 10 deletions(-)
>
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> index e942c300f8c..4832f16e675 100644
> --- a/builtin/rebase.c
> +++ b/builtin/rebase.c
> @@ -1782,19 +1782,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
>  	 * If the onto is a proper descendant of the tip of the branch, then
>  	 * we just fast-forwarded.
>  	 */
> -	strbuf_reset(&msg);
>  	if (oideq(&merge_base, &options.orig_head)) {
>  		printf(_("Fast-forwarded %s to %s.\n"),
>  			branch_name, options.onto_name);
> -		strbuf_addf(&msg, "rebase finished: %s onto %s",
> -			options.head_name ? options.head_name : "detached HEAD",
> -			oid_to_hex(&options.onto->object.oid));
> -		memset(&ropts, 0, sizeof(ropts));
> -		ropts.branch = options.head_name;
> -		ropts.flags = RESET_HEAD_REFS_ONLY;
> -		ropts.head_msg = msg.buf;
> -		reset_head(the_repository, &ropts);
> -		strbuf_release(&msg);
> +		move_to_original_branch(&options);
>  		ret = finish_rebase(&options);
>  		goto cleanup;
>  	}
Previous: Phillip Wood via GitGitGadgetNext: Junio C Hamano
Message 29 of 73 in “rebase: make reflog messages independent of the backend”
  1. 0/7 rebase: make reflog messages independent of the backendPhillip Wood via GitGitGadget, Feb 21, 2022
  2. 3/7 rebase --merge: fix reflog message after skippingPhillip Wood via GitGitGadget, Feb 21, 2022
  3. Elijah NewrenApr 17, 2022
  4. 4/7 rebase --apply: respect GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Feb 21, 2022
  5. Christian CouderApr 7, 2022
  6. Phillip WoodApr 15, 2022
  7. 7/7 rebase: cleanup action handlingPhillip Wood via GitGitGadget, Feb 21, 2022
  8. Elijah NewrenApr 17, 2022
  9. 5/7 rebase --apply: make reflog messages match rebase --mergePhillip Wood via GitGitGadget, Feb 21, 2022
  10. Elijah NewrenApr 17, 2022
  11. 2/7 rebase --merge: fix reflog when continuingPhillip Wood via GitGitGadget, Feb 21, 2022
  12. Christian CouderApr 7, 2022
  13. Phillip WoodApr 15, 2022
  14. Elijah NewrenApr 17, 2022
  15. 1/7 rebase --apply: remove duplicated codePhillip Wood via GitGitGadget, Feb 21, 2022
  16. Christian CouderApr 7, 2022
  17. Elijah NewrenApr 17, 2022
  18. 6/7 rebase --abort: improve reflog messagePhillip Wood via GitGitGadget, Feb 21, 2022
  19. Elijah NewrenApr 17, 2022
  20. Review Request (was Re: [PATCH 0/7] rebase: make reflog messages independent of the backend)Phillip Wood, Apr 4, 2022
  21. Elijah NewrenApr 17, 2022
  22. Phillip WoodApr 18, 2022
  23. Christian CouderApr 7, 2022
  24. Elijah NewrenApr 17, 2022
  25. 0/8 rebase: make reflog messages independent of the backendPhillip Wood via GitGitGadget, Apr 20, 2022
  26. 2/8 t3406: rework rebase reflog testsPhillip Wood via GitGitGadget, Apr 20, 2022
  27. Junio C HamanoApr 20, 2022
  28. 1/8 rebase --apply: remove duplicated codePhillip Wood via GitGitGadget, Apr 20, 2022
  29. Ævar Arnfjörð BjarmasonApr 20, 2022
  30. Junio C HamanoApr 20, 2022
  31. Junio C HamanoApr 20, 2022
  32. 3/8 rebase --merge: fix reflog when continuingPhillip Wood via GitGitGadget, Apr 20, 2022
  33. 4/8 rebase --merge: fix reflog message after skippingPhillip Wood via GitGitGadget, Apr 20, 2022
  34. 5/8 rebase --apply: respect GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Apr 20, 2022
  35. 6/8 rebase --apply: make reflog messages match rebase --mergePhillip Wood via GitGitGadget, Apr 20, 2022
  36. Ævar Arnfjörð BjarmasonApr 20, 2022
  37. Junio C HamanoApr 20, 2022
  38. 7/8 rebase --abort: improve reflog messagePhillip Wood via GitGitGadget, Apr 20, 2022
  39. 8/8 rebase: cleanup action handlingPhillip Wood via GitGitGadget, Apr 20, 2022
  40. Ævar Arnfjörð BjarmasonApr 20, 2022
  41. 0/8 rebase: make reflog messages independent of the backendPhillip Wood via GitGitGadget, Oct 12, 2022
  42. 1/8 rebase --apply: remove duplicated codePhillip Wood via GitGitGadget, Oct 12, 2022
  43. Junio C HamanoOct 12, 2022
  44. Junio C HamanoOct 13, 2022
  45. Phillip WoodOct 20, 2022
  46. 2/8 t3406: rework rebase reflog testsPhillip Wood via GitGitGadget, Oct 12, 2022
  47. 3/8 rebase --merge: fix reflog when continuingPhillip Wood via GitGitGadget, Oct 12, 2022
  48. 4/8 rebase --merge: fix reflog message after skippingPhillip Wood via GitGitGadget, Oct 12, 2022
  49. 5/8 rebase --apply: respect GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Oct 12, 2022
  50. 6/8 rebase --apply: make reflog messages match rebase --mergePhillip Wood via GitGitGadget, Oct 12, 2022
  51. 7/8 rebase --abort: improve reflog messagePhillip Wood via GitGitGadget, Oct 12, 2022
  52. 8/8 rebase: cleanup action handlingPhillip Wood via GitGitGadget, Oct 12, 2022
  53. Junio C HamanoOct 12, 2022
  54. Phillip WoodOct 13, 2022
  55. Junio C HamanoOct 13, 2022
  56. 0/8 rebase: make reflog messages independent of the backendPhillip Wood via GitGitGadget, Oct 21, 2022
  57. 1/8 t3406: rework rebase reflog testsPhillip Wood via GitGitGadget, Oct 21, 2022
  58. 2/8 rebase --apply: improve fast-forward reflog messagePhillip Wood via GitGitGadget, Oct 21, 2022
  59. 3/8 rebase --merge: fix reflog when continuingPhillip Wood via GitGitGadget, Oct 21, 2022
  60. Junio C HamanoOct 21, 2022
  61. Phillip WoodOct 25, 2022
  62. Junio C HamanoOct 25, 2022
  63. Phillip WoodOct 26, 2022
  64. Junio C HamanoOct 26, 2022
  65. 4/8 rebase --merge: fix reflog message after skippingPhillip Wood via GitGitGadget, Oct 21, 2022
  66. 5/8 rebase --apply: respect GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Oct 21, 2022
  67. Junio C HamanoOct 21, 2022
  68. 6/8 rebase --apply: make reflog messages match rebase --mergePhillip Wood via GitGitGadget, Oct 21, 2022
  69. Junio C HamanoOct 21, 2022
  70. 7/8 rebase --abort: improve reflog messagePhillip Wood via GitGitGadget, Oct 21, 2022
  71. Junio C HamanoOct 21, 2022
  72. 8/8 rebase: cleanup action handlingPhillip Wood via GitGitGadget, Oct 21, 2022
  73. Junio C HamanoOct 21, 2022

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.