Skip to content

Commit 85e51b7

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 14e5d40 commit 85e51b7

File tree

6 files changed

+91
-18
lines changed

6 files changed

+91
-18
lines changed

builtin-merge-recursive.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
2525
struct commit *result;
2626

2727
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 = "";
3230

3331
if (argc < 4)
3432
usagef("%s <base>... -- <head> <remote> ...", argv[0]);
@@ -44,7 +42,9 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
4442
else if (!strcmp(arg+2, "theirs"))
4543
o.recursive_variant = MERGE_RECURSIVE_THEIRS;
4644
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;
4848
else
4949
die("Unknown option %s", arg);
5050
continue;

builtin-merge.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,15 +578,17 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
578578

579579
init_merge_options(&o);
580580
if (!strcmp(strategy, "subtree"))
581-
o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
581+
o.subtree_shift = "";
582582

583583
for (x = 0; x < xopts_nr; x++) {
584584
if (!strcmp(xopts[x], "ours"))
585585
o.recursive_variant = MERGE_RECURSIVE_OURS;
586586
else if (!strcmp(xopts[x], "theirs"))
587587
o.recursive_variant = MERGE_RECURSIVE_THEIRS;
588588
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;
590592
else
591593
die("Unknown option for merge-recursive: -X%s", xopts[x]);
592594
}

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ extern int diff_auto_refresh_index;
993993

994994
/* match-trees.c */
995995
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 *);
996997

997998
/*
998999
* whitespace rules.

match-trees.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void match_trees(const unsigned char *hash1,
185185
* tree object by replacing it with another tree "hash2".
186186
*/
187187
static int splice_tree(const unsigned char *hash1,
188-
char *prefix,
188+
const char *prefix,
189189
const unsigned char *hash2,
190190
unsigned char *result)
191191
{
@@ -264,6 +264,13 @@ void shift_tree(const unsigned char *hash1,
264264
char *del_prefix;
265265
int add_score, del_score;
266266

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+
267274
add_score = del_score = score_trees(hash1, hash2);
268275
add_prefix = xcalloc(1, 1);
269276
del_prefix = xcalloc(1, 1);
@@ -301,3 +308,63 @@ void shift_tree(const unsigned char *hash1,
301308

302309
splice_tree(hash1, add_prefix, hash2, shifted);
303310
}
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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@
2121
#include "merge-recursive.h"
2222
#include "dir.h"
2323

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)
2526
{
2627
unsigned char shifted[20];
2728

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+
}
3335
if (!hashcmp(two->object.sha1, shifted))
3436
return two;
3537
return lookup_tree(shifted);
@@ -1213,9 +1215,9 @@ int merge_trees(struct merge_options *o,
12131215
{
12141216
int code, clean;
12151217

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);
12191221
}
12201222

12211223
if (sha_eq(common->object.sha1, merge->object.sha1)) {

merge-recursive.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ struct merge_options {
77
const char *branch1;
88
const char *branch2;
99
enum {
10-
MERGE_RECURSIVE_SUBTREE = 1,
10+
MERGE_RECURSIVE_NORMAL = 0,
1111
MERGE_RECURSIVE_OURS,
1212
MERGE_RECURSIVE_THEIRS,
1313
} recursive_variant;
14+
const char *subtree_shift;
1415
unsigned buffer_output : 1;
1516
int verbosity;
1617
int diff_rename_limit;

0 commit comments

Comments
 (0)