git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Alex Henrie <alexhenrie24@gmail.com>
Cc: git@vger.kernel.org,  bert.wesarg@googlemail.com
Subject: Re: [PATCH] apply: support --ours, --theirs, and --union for three-way merges
Date: Fri, 06 Sep 2024 13:49:21 -0700	[thread overview]
Message-ID: <xmqqwmjov772.fsf@gitster.g> (raw)
In-Reply-To: <20240906044222.4881-1-alexhenrie24@gmail.com> (Alex Henrie's message of "Thu, 5 Sep 2024 22:22:36 -0600")

Alex Henrie <alexhenrie24@gmail.com> writes:

> --ours, --theirs, and --union are already supported in `git merge-file`
> for automatically resolving conflicts in favor of one version or the
> other, instead of leaving conflict markers in the file. Support them in
> `git apply -3` as well because the two commands do the same kind of
> file-level merges.

An unrelated #leftoverbits tangent.  

We probably should teach add "union" as a valid choice in
.recursive_variant in merge-recursive.c:parse_merge_opt(), together
with "ours" and "theirs" that are already supported.

> In case in the future --ours, --theirs, and --union gain a meaning
> outside of three-way-merges, they do not imply --3way but rather must be
> specified alongside it.

OK.  At least the code insists on having --3way specified, instead
of silently ignoring --ours given without --3way, so it is good.

> +static int apply_option_parse_favorite(const struct option *opt,
> +				       const char *arg, int unset)
> +{
> +	struct apply_state *state = opt->value;
> +
> +	BUG_ON_OPT_ARG(arg);
> +	BUG_ON_OPT_NEG(unset);
> +
> +	if (!strcmp(opt->long_name, "ours"))
> +		state->merge_opts.variant = XDL_MERGE_FAVOR_OURS;
> +	else if (!strcmp(opt->long_name, "theirs"))
> +		state->merge_opts.variant = XDL_MERGE_FAVOR_THEIRS;
> +	else
> +		state->merge_opts.variant = XDL_MERGE_FAVOR_UNION;
> +	return 0;
> +}

If you MUST use a opt-callback, then do not assume anything that is
not ours or theirs will always be union.  Help future developers by
making your assumption more explicit, i.e.

	if (..."ours"...)
		do ours thing;
	else if (... "theirs" ...)
		do theirs thing;
	else if (... "union" ...)
		do union thing;
	else
		BUG("unexpected option '--%s'", opt->long_name);

Having said that, I do not think you want or need a callback in this
case to begin with.

>  static int apply_option_parse_whitespace(const struct option *opt,
>  					 const char *arg, int unset)
>  {
> @@ -5151,6 +5177,18 @@ int apply_parse_options(int argc, const char **argv,
>  			N_("also apply the patch (use with --stat/--summary/--check)")),
>  		OPT_BOOL('3', "3way", &state->threeway,
>  			 N_( "attempt three-way merge, fall back on normal patch if that fails")),
> +		OPT_CALLBACK_F(0, "ours", state, NULL,
> +			N_("for conflicts, use our version"),
> +			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
> +			apply_option_parse_favorite),
> +		OPT_CALLBACK_F(0, "theirs", state, NULL,
> +			N_("for conflicts, use their version"),
> +			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
> +			apply_option_parse_favorite),
> +		OPT_CALLBACK_F(0, "union", state, NULL,
> +			N_("for conflicts, use a union version"),
> +			PARSE_OPT_NOARG | PARSE_OPT_NONEG,
> +			apply_option_parse_favorite),

Instead of embedding the whole ll_merge_options in apply_state, just
define a new integer member "merge_variant", and use OPT_SET_INT()
on that field.  That way, you won't have to do the callback interface
and worry about keeping the function's if/elseif cascade in sync
with these options that call the same function.

Then, instead of passing state->merge_opts, you can initialize
ll_merge_options instance in three_way_merge() with whatever is
needed from the apply_state (like the merge_variant mentioned
above).

> -	if (status) {
> +	if (state->merge_opts.variant) {
> +		/*
> +		 * XDL_MERGE_FAVOR_(OURS|THEIRS|UNION) automatically resolves
> +		 * conflicts, but the ll_merge function is not yet smart enough
> +		 * to report whether or not there were conflicts, so just print
> +		 * a generic message.
> +		 */
> +		fprintf(stderr, _("Applied patch to '%s'.\n"), patch->new_name);

I do not think this extra message or the comment is warranted.  When
you said "--ours" you told the machinery that you favor our version,
so there is no place to be "smart enough to report".  Instead of
normal 3-way merge, you told it there won't be any conflict.  There
is nothing to report.

The else clause of the original "if (status)" does report "applied
patch cleanly" when apply_verbosity is set to report it.  As far as
the command is concerned, if you told it to use "ours" and got a
merge result, that was also applied cleanly.  So you can just drop
this hunk and let the existing code take care of the rest (including
honoring the verbosity settings).

Other than that, looks fairly straight-forward.

I didn't read the tests, though.

Thanks.

  reply	other threads:[~2024-09-06 20:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-06  4:22 [PATCH] apply: support --ours, --theirs, and --union for three-way merges Alex Henrie
2024-09-06 20:49 ` Junio C Hamano [this message]
2024-09-09 14:10   ` Alex Henrie
2024-09-09 14:10 ` [PATCH v2 0/1] " Alex Henrie
2024-09-09 14:10   ` [PATCH v2 1/1] " Alex Henrie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqwmjov772.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=alexhenrie24@gmail.com \
    --cc=bert.wesarg@googlemail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).