merge-tree: support including merge messages in output

When running `git merge-tree --write-tree`, we previously would only
return an exit status reflecting the cleanness of a merge, and print out
the toplevel tree of the resulting merge.  Merges also have
informational messages, such as:
  * "Auto-merging <PATH>"
  * "CONFLICT (content): ..."
  * "CONFLICT (file/directory)"
  * etc.
In fact, when non-content conflicts occur (such as file/directory,
modify/delete, add/add with differing modes, rename/rename (1to2),
etc.), these informational messages may be the only notification the
user gets since these conflicts are not representable in the contents
of the file.

Add a --[no-]messages option so that callers can request these messages
be included at the end of the output.  Include such messages by default
when there are conflicts, and omit them by default when the merge is
clean.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren
2022-06-18 00:20:49 +00:00
committed by Junio C Hamano
parent a34edae68a
commit a1a7811975
3 changed files with 97 additions and 8 deletions

View File

@ -396,6 +396,7 @@ enum mode {
struct merge_tree_options {
int mode;
int show_messages;
};
static int real_merge(struct merge_tree_options *o,
@ -435,18 +436,27 @@ static int real_merge(struct merge_tree_options *o,
merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result);
if (result.clean < 0)
die(_("failure to merge"));
if (o->show_messages == -1)
o->show_messages = !result.clean;
puts(oid_to_hex(&result.tree->object.oid));
if (o->show_messages) {
printf("\n");
merge_display_update_messages(&opt, &result);
}
merge_finalize(&opt, &result);
return !result.clean; /* result.clean < 0 handled above */
}
int cmd_merge_tree(int argc, const char **argv, const char *prefix)
{
struct merge_tree_options o = { 0 };
struct merge_tree_options o = { .show_messages = -1 };
int expected_remaining_argc;
int original_argc;
const char * const merge_tree_usage[] = {
N_("git merge-tree [--write-tree] <branch1> <branch2>"),
N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"),
N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"),
NULL
};
@ -456,10 +466,13 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
MODE_REAL),
OPT_CMDMODE(0, "trivial-merge", &o.mode,
N_("do a trivial merge only"), MODE_TRIVIAL),
OPT_BOOL(0, "messages", &o.show_messages,
N_("also show informational/conflict messages")),
OPT_END()
};
/* Parse arguments */
original_argc = argc - 1; /* ignoring argv[0] */
argc = parse_options(argc, argv, prefix, mt_options,
merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION);
switch (o.mode) {
@ -483,8 +496,12 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
break;
case MODE_TRIVIAL:
expected_remaining_argc = 3;
/* Removal of `--trivial-merge` is expected */
original_argc--;
break;
}
if (o.mode == MODE_TRIVIAL && argc < original_argc)
die(_("--trivial-merge is incompatible with all other options"));
if (argc != expected_remaining_argc)
usage_with_options(merge_tree_usage, mt_options);