Skip to content

Commit db757e8

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5046831 commit db757e8

File tree

6 files changed

+191
-2
lines changed

6 files changed

+191
-2
lines changed

Documentation/diff-options.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ endif::git-diff[]
3434
endif::git-format-patch[]
3535

3636
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)::
3838
--no-diff-merges::
3939
Specify diff format to be used for merge commits. Default is
4040
{diff-merges-default} unless `--first-parent` is in use, in which case
@@ -64,6 +64,18 @@ ifdef::git-log[]
6464
each of the parents. Separate log entry and diff is generated
6565
for each parent.
6666
+
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+
+
6779
--diff-merges=combined:::
6880
--diff-merges=c:::
6981
-c:::

builtin/log.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "repository.h"
3636
#include "commit-reach.h"
3737
#include "range-diff.h"
38+
#include "tmp-objdir.h"
3839

3940
#define MAIL_DEFAULT_WRAP 72
4041
#define COVER_FROM_AUTO_MAX_SUBJECT_LEN 100
@@ -421,6 +422,14 @@ static int cmd_log_walk(struct rev_info *rev)
421422
struct commit *commit;
422423
int saved_nrl = 0;
423424
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+
}
424433

425434
if (rev->early_output)
426435
setup_early_output();
@@ -464,6 +473,9 @@ static int cmd_log_walk(struct rev_info *rev)
464473
rev->diffopt.no_free = 0;
465474
diff_free(&rev->diffopt);
466475

476+
if (rev->remerge_diff)
477+
tmp_objdir_destroy(remerge_objdir);
478+
467479
if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
468480
rev->diffopt.flags.check_failed) {
469481
return 02;
@@ -1958,6 +1970,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19581970
die(_("--name-status does not make sense"));
19591971
if (rev.diffopt.output_format & DIFF_FORMAT_CHECKDIFF)
19601972
die(_("--check does not make sense"));
1973+
if (rev.remerge_diff)
1974+
die(_("--remerge-diff does not make sense"));
19611975

19621976
if (!use_patch_format &&
19631977
(!rev.diffopt.output_format ||

diff-merges.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static void suppress(struct rev_info *revs)
1717
revs->combined_all_paths = 0;
1818
revs->merges_imply_patch = 0;
1919
revs->merges_need_diff = 0;
20+
revs->remerge_diff = 0;
2021
}
2122

2223
static void set_separate(struct rev_info *revs)
@@ -45,6 +46,12 @@ static void set_dense_combined(struct rev_info *revs)
4546
revs->dense_combined_merges = 1;
4647
}
4748

49+
static void set_remerge_diff(struct rev_info *revs)
50+
{
51+
suppress(revs);
52+
revs->remerge_diff = 1;
53+
}
54+
4855
static diff_merges_setup_func_t func_by_opt(const char *optarg)
4956
{
5057
if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
@@ -57,6 +64,8 @@ static diff_merges_setup_func_t func_by_opt(const char *optarg)
5764
return set_combined;
5865
else if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
5966
return set_dense_combined;
67+
else if (!strcmp(optarg, "r") || !strcmp(optarg, "remerge"))
68+
return set_remerge_diff;
6069
else if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
6170
return set_to_default;
6271
return NULL;
@@ -110,6 +119,9 @@ int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
110119
} else if (!strcmp(arg, "--cc")) {
111120
set_dense_combined(revs);
112121
revs->merges_imply_patch = 1;
122+
} else if (!strcmp(arg, "--remerge-diff")) {
123+
set_remerge_diff(revs);
124+
revs->merges_imply_patch = 1;
113125
} else if (!strcmp(arg, "--no-diff-merges")) {
114126
suppress(revs);
115127
} else if (!strcmp(arg, "--combined-all-paths")) {

log-tree.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "commit-reach.h"
23
#include "config.h"
34
#include "diff.h"
45
#include "object-store.h"
@@ -7,6 +8,7 @@
78
#include "tag.h"
89
#include "graph.h"
910
#include "log-tree.h"
11+
#include "merge-ort.h"
1012
#include "reflog-walk.h"
1113
#include "refs.h"
1214
#include "string-list.h"
@@ -904,6 +906,51 @@ static int do_diff_combined(struct rev_info *opt, struct commit *commit)
904906
return !opt->loginfo;
905907
}
906908

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+
907954
/*
908955
* Show the diff of a commit.
909956
*
@@ -938,6 +985,18 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
938985
}
939986

940987
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+
}
9411000
if (opt->combine_merges)
9421001
return do_diff_combined(opt, commit);
9431002
if (opt->separate_merges) {

revision.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ struct rev_info {
195195
combine_merges:1,
196196
combined_all_paths:1,
197197
dense_combined_merges:1,
198-
first_parent_merges:1;
198+
first_parent_merges:1,
199+
remerge_diff:1;
199200

200201
/* Format info */
201202
int show_notes;

t/t4069-remerge-diff.sh

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)