@matheusbsbs / matheus-bagliano / commits / 85e51b783c
Fork 0

Make "subtree" part more orthogonal to the rest of merge-recursive.

This makes "subtree" more orthogonal to the rest of recursive merge, so that you can use subtree and ours/theirs features at the same time. For example, you can now say: git merge -s subtree -Xtheirs other to merge with "other" branch while shifting it up or down to match the shape of the tree of the current branch, and resolving conflicts favoring the changes "other" branch made over changes made in the current branch. It also allows the prefix used to shift the trees to be specified using the "-Xsubtree=$prefix" option. Giving an empty prefix tells the command to figure out how much to shift trees automatically as we have always done. "merge -s subtree" is the same as "merge -s recursive -Xsubtree=" (or "merge -s recursive -Xsubtree"). Based on an old patch done back in the days when git-merge was a script; Avery ported the script part to builtin-merge.c. Bugs in shift_tree() is mine. Signed-off-by: Avery Pennarun <apenwarr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 30, 2008 at 22:18 UTC 85e51b783c3a92b42275442147e7a27e17d42276
6 files changed +91 -18
builtin-merge-recursive.c
+5 -5
@@ -25,10 +25,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
25 struct commit *result;
26
27 init_merge_options(&o);
28 - if (argv[0]) {
29 - if (!suffixcmp(argv[0], "-subtree"))
30 - o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
31 - }
28 + if (argv[0] && !suffixcmp(argv[0], "-subtree"))
29 + o.subtree_shift = "";
30
31 if (argc < 4)
32 usagef("%s <base>... -- <head> <remote> ...", argv[0]);
@@ -44,7 +42,9 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
42 else if (!strcmp(arg+2, "theirs"))
43 o.recursive_variant = MERGE_RECURSIVE_THEIRS;
44 else if (!strcmp(arg+2, "subtree"))
47 - o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
45 + o.subtree_shift = "";
46 + else if (!prefixcmp(arg+2, "subtree="))
47 + o.subtree_shift = arg + 10;
48 else
49 die("Unknown option %s", arg);
50 continue;
builtin-merge.c
+4 -2
@@ -578,7 +578,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
578
579 init_merge_options(&o);
580 if (!strcmp(strategy, "subtree"))
581 - o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
581 + o.subtree_shift = "";
582
583 for (x = 0; x < xopts_nr; x++) {
584 if (!strcmp(xopts[x], "ours"))
@@ -586,7 +586,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
586 else if (!strcmp(xopts[x], "theirs"))
587 o.recursive_variant = MERGE_RECURSIVE_THEIRS;
588 else if (!strcmp(xopts[x], "subtree"))
589 - o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
589 + o.subtree_shift = "";
590 + else if (!prefixcmp(xopts[x], "subtree="))
591 + o.subtree_shift = xopts[x]+8;
592 else
593 die("Unknown option for merge-recursive: -X%s", xopts[x]);
594 }
cache.h
+1
@@ -993,6 +993,7 @@ extern int diff_auto_refresh_index;
993
994 /* match-trees.c */
995 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
996 +void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *, const char *);
997
998 /*
999 * whitespace rules.
match-trees.c
+68 -1
@@ -185,7 +185,7 @@ static void match_trees(const unsigned char *hash1,
185 * tree object by replacing it with another tree "hash2".
186 */
187 static int splice_tree(const unsigned char *hash1,
188 - char *prefix,
188 + const char *prefix,
189 const unsigned char *hash2,
190 unsigned char *result)
191 {
@@ -264,6 +264,13 @@ void shift_tree(const unsigned char *hash1,
264 char *del_prefix;
265 int add_score, del_score;
266
267 + /*
268 + * NEEDSWORK: this limits the recursion depth to hardcoded
269 + * value '2' to avoid excessive overhead.
270 + */
271 + if (!depth_limit)
272 + depth_limit = 2;
273 +
274 add_score = del_score = score_trees(hash1, hash2);
275 add_prefix = xcalloc(1, 1);
276 del_prefix = xcalloc(1, 1);
@@ -301,3 +308,63 @@ void shift_tree(const unsigned char *hash1,
308
309 splice_tree(hash1, add_prefix, hash2, shifted);
310 }
311 +
312 +/*
313 + * The user says the trees will be shifted by this much.
314 + * Unfortunately we cannot fundamentally tell which one to
315 + * be prefixed, as recursive merge can work in either direction.
316 + */
317 +void shift_tree_by(const unsigned char *hash1,
318 + const unsigned char *hash2,
319 + unsigned char *shifted,
320 + const char *shift_prefix)
321 +{
322 + unsigned char sub1[20], sub2[20];
323 + unsigned mode1, mode2;
324 + unsigned candidate = 0;
325 +
326 + /* Can hash2 be a tree at shift_prefix in tree hash1? */
327 + if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
328 + S_ISDIR(mode1))
329 + candidate |= 1;
330 +
331 + /* Can hash1 be a tree at shift_prefix in tree hash2? */
332 + if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
333 + S_ISDIR(mode2))
334 + candidate |= 2;
335 +
336 + if (candidate == 3) {
337 + /* Both are plausible -- we need to evaluate the score */
338 + int best_score = score_trees(hash1, hash2);
339 + int score;
340 +
341 + candidate = 0;
342 + score = score_trees(sub1, hash2);
343 + if (score > best_score) {
344 + candidate = 1;
345 + best_score = score;
346 + }
347 + score = score_trees(sub2, hash1);
348 + if (score > best_score)
349 + candidate = 2;
350 + }
351 +
352 + if (!candidate) {
353 + /* Neither is plausible -- do not shift */
354 + hashcpy(shifted, hash2);
355 + return;
356 + }
357 +
358 + if (candidate == 1)
359 + /*
360 + * shift tree2 down by adding shift_prefix above it
361 + * to match tree1.
362 + */
363 + splice_tree(hash1, shift_prefix, hash2, shifted);
364 + else
365 + /*
366 + * shift tree2 up by removing shift_prefix from it
367 + * to match tree1.
368 + */
369 + hashcpy(shifted, sub2);
370 +}
merge-recursive.c
+11 -9
@@ -21,15 +21,17 @@
21 #include "merge-recursive.h"
22 #include "dir.h"
23
24 -static struct tree *shift_tree_object(struct tree *one, struct tree *two)
24 +static struct tree *shift_tree_object(struct tree *one, struct tree *two,
25 + const char *subtree_shift)
26 {
27 unsigned char shifted[20];
28
28 - /*
29 - * NEEDSWORK: this limits the recursion depth to hardcoded
30 - * value '2' to avoid excessive overhead.
31 - */
32 - shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
29 + if (!*subtree_shift) {
30 + shift_tree(one->object.sha1, two->object.sha1, shifted, 0);
31 + } else {
32 + shift_tree_by(one->object.sha1, two->object.sha1, shifted,
33 + subtree_shift);
34 + }
35 if (!hashcmp(two->object.sha1, shifted))
36 return two;
37 return lookup_tree(shifted);
@@ -1213,9 +1215,9 @@ int merge_trees(struct merge_options *o,
1215 {
1216 int code, clean;
1217
1216 - if (o->recursive_variant == MERGE_RECURSIVE_SUBTREE) {
1217 - merge = shift_tree_object(head, merge);
1218 - common = shift_tree_object(head, common);
1218 + if (o->subtree_shift) {
1219 + merge = shift_tree_object(head, merge, o->subtree_shift);
1220 + common = shift_tree_object(head, common, o->subtree_shift);
1221 }
1222
1223 if (sha_eq(common->object.sha1, merge->object.sha1)) {
merge-recursive.h
+2 -1
@@ -7,10 +7,11 @@ struct merge_options {
7 const char *branch1;
8 const char *branch2;
9 enum {
10 - MERGE_RECURSIVE_SUBTREE = 1,
10 + MERGE_RECURSIVE_NORMAL = 0,
11 MERGE_RECURSIVE_OURS,
12 MERGE_RECURSIVE_THEIRS,
13 } recursive_variant;
14 + const char *subtree_shift;
15 unsigned buffer_output : 1;
16 int verbosity;
17 int diff_rename_limit;