Subject: Re: [PATCH v3 11/14] reset_head(): take struct rebase_head_opts

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Wed, 26 Jan 2022 14:35:11 +0100

On Wed, Jan 26 2022, Phillip Wood via GitGitGadget wrote:

> @@ -669,13 +672,15 @@ static int run_am(struct rebase_options *opts)
>  
>  	status = run_command(&format_patch);
>  	if (status) {
> +		struct reset_head_opts ropts = { 0 };
>  		unlink(rebased_patches);
>  		free(rebased_patches);
>  		strvec_clear(&am.args);
>  
> -		reset_head(the_repository, &opts->orig_head,
> -			   opts->head_name, 0,
> -			   NULL, NULL, DEFAULT_REFLOG_ACTION);
> +		ropts.oid = &opts->orig_head;
> +		ropts.branch = opts->head_name;
> +		ropts.default_reflog_action = DEFAULT_REFLOG_ACTION;
> +		reset_head(the_repository, &ropts);
>  		error(_("\ngit encountered an error while preparing the "
>  			"patches to replay\n"
>  			"these revisions:\n"

Wouldn't these and the rest be easier to read as:

	struct reset_head_opts ropts = {
		.oid = &opts->orig_head,
                .branch = opts->head_name,
                .default_reflog_action = DEFAULT_REFLOG_ACTION,
        };

....

> @@ -814,14 +819,17 @@ static int rebase_config(const char *var, const char *value, void *data)
>  static int checkout_up_to_date(struct rebase_options *options)
>  {
>  	struct strbuf buf = STRBUF_INIT;
> +	struct reset_head_opts ropts = { 0 };
>  	int ret = 0;
>  
>  	strbuf_addf(&buf, "%s: checkout %s",
>  		    getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
>  		    options->switch_to);
> -	if (reset_head(the_repository, &options->orig_head,
> -		       options->head_name, RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
> -		       NULL, buf.buf, NULL) < 0)
> +	ropts.oid = &options->orig_head;
> +	ropts.branch = options->head_name;
> +	ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
> +	ropts.head_msg = buf.buf;

...and then for some of the ones like this "ropts.head_msg = buf.buf"
assignment you just do that one immediately after the strbuf_addf() or
whatever modifies it.

That way it's clear what options we get from the function arguments and
can populate right away, and which ones we need to run some code in the
function before we can update "ropts".

[Ditto for the elided parts below]

>  #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
>  
> +/* Request a detached checkout */
>  #define RESET_HEAD_DETACH (1<<0)
> +/* Request a reset rather than a checkout */
>  #define RESET_HEAD_HARD (1<<1)
> +/* Run the post-checkout hook */
>  #define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
> +/* Only update refs, do not touch the worktree */
>  #define RESET_HEAD_REFS_ONLY (1<<3)
> +/* Update ORIG_HEAD as well as HEAD */
>  #define RESET_ORIG_HEAD (1<<4)
>  
> -int reset_head(struct repository *r, struct object_id *oid,
> -	       const char *switch_to_branch, unsigned flags,
> -	       const char *reflog_orig_head, const char *reflog_head,
> -	       const char *default_reflog_action);
> +struct reset_head_opts {
> +	/*
> +	 * The commit to checkout/reset to. Defaults to HEAD.
> +	 */
> +	const struct object_id *oid;
> +	/*
> +	 * Optional branch to switch to.
> +	 */
> +	const char *branch;
> +	/*
> +	 * Flags defined above.
> +	 */
> +	unsigned flags;

It's nice to make these sort of things an enum type for the reasons
explained in 3f9ab7ccdea (parse-options.[ch]: consistently use "enum
parse_opt_flags", 2021-10-08), i.e. gdb and the like will give you the
labels in the debugger.