show, log: provide a --remerge-diff capability

When this option is specified, we remerge all (two parent) merge commits and diff the actual merge commit to the automatically created version, in order to show how users removed conflict markers, resolved the different conflict versions, and potentially added new changes outside of conflict regions in order to resolve semantic merge problems (or, possibly, just to hide other random changes). This capability works by creating a temporary object directory and marking it as the primary object store. This makes it so that any blobs or trees created during the automatic merge are easily removable afterwards by just deleting all objects from the temporary object directory. There are a few ways that this implementation is suboptimal: * `log --remerge-diff` becomes slow, because the temporary object directory can fill with many loose objects while running * the log output can be muddied with misplaced "warning: cannot merge binary files" messages, since ll-merge.c unconditionally writes those messages to stderr while running instead of allowing callers to manage them. * important conflict and warning messages are simply dropped; thus for conflicts like modify/delete or rename/rename or file/directory which are not representable with content conflict markers, there may be no way for a user of --remerge-diff to know that there had been a conflict which was resolved (and which possibly motivated other changes in the merge commit). * when fixing the previous issue, note that some unimportant conflict and warning messages might start being included. We should instead make sure these remain dropped. Subsequent commits will address these issues. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Feb 2, 2022 at 02:37 UTC db757e8b8d5527c195c461a04ec35d141ddea48e
6 files changed +191 -2
Documentation/diff-options.txt
+13 -1
@@ -34,7 +34,7 @@ endif::git-diff[]
34 endif::git-format-patch[]
35
36 ifdef::git-log[]
37 ---diff-merges=(off|none|on|first-parent|1|separate|m|combined|c|dense-combined|cc)::
37 +--diff-merges=(off|none|on|first-parent|1|separate|m|combined|c|dense-combined|cc|remerge|r)::
38 --no-diff-merges::
39 Specify diff format to be used for merge commits. Default is
40 {diff-merges-default} unless `--first-parent` is in use, in which case
@@ -64,6 +64,18 @@ ifdef::git-log[]
64 each of the parents. Separate log entry and diff is generated
65 for each parent.
66 +
67 +--diff-merges=remerge:::
68 +--diff-merges=r:::
69 +--remerge-diff:::
70 + With this option, two-parent merge commits are remerged to
71 + create a temporary tree object -- potentially containing files
72 + with conflict markers and such. A diff is then shown between
73 + that temporary tree and the actual merge commit.
74 ++
75 +The output emitted when this option is used is subject to change, and
76 +so is its interaction with other options (unless explicitly
77 +documented).
78 ++
79 --diff-merges=combined:::
80 --diff-merges=c:::
81 -c:::
builtin/log.c
+14
@@ -35,6 +35,7 @@
35 #include "repository.h"
36 #include "commit-reach.h"
37 #include "range-diff.h"
38 +#include "tmp-objdir.h"
39
40 #define MAIL_DEFAULT_WRAP 72
41 #define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
@@ -421,6 +422,14 @@ static int cmd_log_walk(struct rev_info *rev)
422 struct commit *commit;
423 int saved_nrl = 0;
424 int saved_dcctc = 0;
425 + struct tmp_objdir *remerge_objdir = NULL;
426 +
427 + if (rev->remerge_diff) {
428 + remerge_objdir = tmp_objdir_create("remerge-diff");
429 + if (!remerge_objdir)
430 + die(_("unable to create temporary object directory"));
431 + tmp_objdir_replace_primary_odb(remerge_objdir, 1);
432 + }
433
434 if (rev->early_output)
435 setup_early_output();
@@ -464,6 +473,9 @@ static int cmd_log_walk(struct rev_info *rev)
473 rev->diffopt.no_free = 0;
474 diff_free(&rev->diffopt);
475
476 + if (rev->remerge_diff)
477 + tmp_objdir_destroy(remerge_objdir);
478 +
479 if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
480 rev->diffopt.flags.check_failed) {
481 return 02;
@@ -1958,6 +1970,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1970 die(_("--name-status does not make sense"));
1971 if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
1972 die(_("--check does not make sense"));
1973 + if (rev.remerge_diff)
1974 + die(_("--remerge-diff does not make sense"));
1975
1976 if (!use_patch_format &&
1977 (!rev.diffopt.output_format ||
diff-merges.c
+12
@@ -17,6 +17,7 @@ static void suppress(struct rev_info *revs)
17 revs->combined_all_paths = 0;
18 revs->merges_imply_patch = 0;
19 revs->merges_need_diff = 0;
20 + revs->remerge_diff = 0;
21 }
22
23 static void set_separate(struct rev_info *revs)
@@ -45,6 +46,12 @@ static void set_dense_combined(struct rev_info *revs)
46 revs->dense_combined_merges = 1;
47 }
48
49 +static void set_remerge_diff(struct rev_info *revs)
50 +{
51 + suppress(revs);
52 + revs->remerge_diff = 1;
53 +}
54 +
55 static diff_merges_setup_func_t func_by_opt(const char *optarg)
56 {
57 if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
@@ -57,6 +64,8 @@ static diff_merges_setup_func_t func_by_opt(const char *optarg)
64 return set_combined;
65 else if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
66 return set_dense_combined;
67 + else if (!strcmp(optarg, "r") || !strcmp(optarg, "remerge"))
68 + return set_remerge_diff;
69 else if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
70 return set_to_default;
71 return NULL;
@@ -110,6 +119,9 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
119 } else if (!strcmp(arg, "--cc")) {
120 set_dense_combined(revs);
121 revs->merges_imply_patch = 1;
122 + } else if (!strcmp(arg, "--remerge-diff")) {
123 + set_remerge_diff(revs);
124 + revs->merges_imply_patch = 1;
125 } else if (!strcmp(arg, "--no-diff-merges")) {
126 suppress(revs);
127 } else if (!strcmp(arg, "--combined-all-paths")) {
log-tree.c
+59
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "commit-reach.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "object-store.h"
@@ -7,6 +8,7 @@
8 #include "tag.h"
9 #include "graph.h"
10 #include "log-tree.h"
11 +#include "merge-ort.h"
12 #include "reflog-walk.h"
13 #include "refs.h"
14 #include "string-list.h"
@@ -904,6 +906,51 @@ static int do_diff_combined(struct rev_info *opt, struct commit *commit)
906 return !opt->loginfo;
907 }
908
909 +static int do_remerge_diff(struct rev_info *opt,
910 + struct commit_list *parents,
911 + struct object_id *oid,
912 + struct commit *commit)
913 +{
914 + struct merge_options o;
915 + struct commit_list *bases;
916 + struct merge_result res = {0};
917 + struct pretty_print_context ctx = {0};
918 + struct commit *parent1 = parents->item;
919 + struct commit *parent2 = parents->next->item;
920 + struct strbuf parent1_desc = STRBUF_INIT;
921 + struct strbuf parent2_desc = STRBUF_INIT;
922 +
923 + /* Setup merge options */
924 + init_merge_options(&o, the_repository);
925 + o.show_rename_progress = 0;
926 +
927 + ctx.abbrev = DEFAULT_ABBREV;
928 + format_commit_message(parent1, "%h (%s)", &parent1_desc, &ctx);
929 + format_commit_message(parent2, "%h (%s)", &parent2_desc, &ctx);
930 + o.branch1 = parent1_desc.buf;
931 + o.branch2 = parent2_desc.buf;
932 +
933 + /* Parse the relevant commits and get the merge bases */
934 + parse_commit_or_die(parent1);
935 + parse_commit_or_die(parent2);
936 + bases = get_merge_bases(parent1, parent2);
937 +
938 + /* Re-merge the parents */
939 + merge_incore_recursive(&o, bases, parent1, parent2, &res);
940 +
941 + /* Show the diff */
942 + diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
943 + log_tree_diff_flush(opt);
944 +
945 + /* Cleanup */
946 + strbuf_release(&parent1_desc);
947 + strbuf_release(&parent2_desc);
948 + merge_finalize(&o, &res);
949 + /* TODO: clean up the temporary object directory */
950 +
951 + return !opt->loginfo;
952 +}
953 +
954 /*
955 * Show the diff of a commit.
956 *
@@ -938,6 +985,18 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
985 }
986
987 if (is_merge) {
988 + int octopus = (parents->next->next != NULL);
989 +
990 + if (opt->remerge_diff) {
991 + if (octopus) {
992 + show_log(opt);
993 + fprintf(opt->diffopt.file,
994 + "diff: warning: Skipping remerge-diff "
995 + "for octopus merges.\n");
996 + return 1;
997 + }
998 + return do_remerge_diff(opt, parents, oid, commit);
999 + }
1000 if (opt->combine_merges)
1001 return do_diff_combined(opt, commit);
1002 if (opt->separate_merges) {
revision.h
+2 -1
@@ -195,7 +195,8 @@ struct rev_info {
195 combine_merges:1,
196 combined_all_paths:1,
197 dense_combined_merges:1,
198 - first_parent_merges:1;
198 + first_parent_merges:1,
199 + remerge_diff:1;
200
201 /* Format info */
202 int show_notes;
t/t4069-remerge-diff.sh new
+91
@@ -0,0 +1,91 @@
1 +#!/bin/sh
2 +
3 +test_description='remerge-diff handling'
4 +
5 +. ./test-lib.sh
6 +
7 +# This test is ort-specific
8 +if test "${GIT_TEST_MERGE_ALGORITHM}" != ort
9 +then
10 + skip_all="GIT_TEST_MERGE_ALGORITHM != ort"
11 + test_done
12 +fi
13 +
14 +test_expect_success 'setup basic merges' '
15 + test_write_lines 1 2 3 4 5 6 7 8 9 >numbers &&
16 + git add numbers &&
17 + git commit -m base &&
18 +
19 + git branch feature_a &&
20 + git branch feature_b &&
21 + git branch feature_c &&
22 +
23 + git branch ab_resolution &&
24 + git branch bc_resolution &&
25 +
26 + git checkout feature_a &&
27 + test_write_lines 1 2 three 4 5 6 7 eight 9 >numbers &&
28 + git commit -a -m change_a &&
29 +
30 + git checkout feature_b &&
31 + test_write_lines 1 2 tres 4 5 6 7 8 9 >numbers &&
32 + git commit -a -m change_b &&
33 +
34 + git checkout feature_c &&
35 + test_write_lines 1 2 3 4 5 6 7 8 9 10 >numbers &&
36 + git commit -a -m change_c &&
37 +
38 + git checkout bc_resolution &&
39 + git merge --ff-only feature_b &&
40 + # no conflict
41 + git merge feature_c &&
42 +
43 + git checkout ab_resolution &&
44 + git merge --ff-only feature_a &&
45 + # conflicts!
46 + test_must_fail git merge feature_b &&
47 + # Resolve conflict...and make another change elsewhere
48 + test_write_lines 1 2 drei 4 5 6 7 acht 9 >numbers &&
49 + git add numbers &&
50 + git merge --continue
51 +'
52 +
53 +test_expect_success 'remerge-diff on a clean merge' '
54 + git log -1 --oneline bc_resolution >expect &&
55 + git show --oneline --remerge-diff bc_resolution >actual &&
56 + test_cmp expect actual
57 +'
58 +
59 +test_expect_success 'remerge-diff with both a resolved conflict and an unrelated change' '
60 + git log -1 --oneline ab_resolution >tmp &&
61 + cat <<-EOF >>tmp &&
62 + diff --git a/numbers b/numbers
63 + index a1fb731..6875544 100644
64 + --- a/numbers
65 + +++ b/numbers
66 + @@ -1,13 +1,9 @@
67 + 1
68 + 2
69 + -<<<<<<< b0ed5cb (change_a)
70 + -three
71 + -=======
72 + -tres
73 + ->>>>>>> 6cd3f82 (change_b)
74 + +drei
75 + 4
76 + 5
77 + 6
78 + 7
79 + -eight
80 + +acht
81 + 9
82 + EOF
83 + # Hashes above are sha1; rip them out so test works with sha256
84 + sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
85 +
86 + git show --oneline --remerge-diff ab_resolution >tmp &&
87 + sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
88 + test_cmp expect actual
89 +'
90 +
91 +test_done