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>
This commit is contained in:
Junio C Hamano 2008-06-30 22:18:57 -07:00
Родитель 14e5d40ca4
Коммит 85e51b783c
6 изменённых файлов: 91 добавлений и 18 удалений

Просмотреть файл

@ -25,10 +25,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
struct commit *result; struct commit *result;
init_merge_options(&o); init_merge_options(&o);
if (argv[0]) { if (argv[0] && !suffixcmp(argv[0], "-subtree"))
if (!suffixcmp(argv[0], "-subtree")) o.subtree_shift = "";
o.recursive_variant = MERGE_RECURSIVE_SUBTREE;
}
if (argc < 4) if (argc < 4)
usagef("%s <base>... -- <head> <remote> ...", argv[0]); usagef("%s <base>... -- <head> <remote> ...", argv[0]);
@ -44,7 +42,9 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
else if (!strcmp(arg+2, "theirs")) else if (!strcmp(arg+2, "theirs"))
o.recursive_variant = MERGE_RECURSIVE_THEIRS; o.recursive_variant = MERGE_RECURSIVE_THEIRS;
else if (!strcmp(arg+2, "subtree")) else if (!strcmp(arg+2, "subtree"))
o.recursive_variant = MERGE_RECURSIVE_SUBTREE; o.subtree_shift = "";
else if (!prefixcmp(arg+2, "subtree="))
o.subtree_shift = arg + 10;
else else
die("Unknown option %s", arg); die("Unknown option %s", arg);
continue; continue;

Просмотреть файл

@ -578,7 +578,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
init_merge_options(&o); init_merge_options(&o);
if (!strcmp(strategy, "subtree")) if (!strcmp(strategy, "subtree"))
o.recursive_variant = MERGE_RECURSIVE_SUBTREE; o.subtree_shift = "";
for (x = 0; x < xopts_nr; x++) { for (x = 0; x < xopts_nr; x++) {
if (!strcmp(xopts[x], "ours")) if (!strcmp(xopts[x], "ours"))
@ -586,7 +586,9 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
else if (!strcmp(xopts[x], "theirs")) else if (!strcmp(xopts[x], "theirs"))
o.recursive_variant = MERGE_RECURSIVE_THEIRS; o.recursive_variant = MERGE_RECURSIVE_THEIRS;
else if (!strcmp(xopts[x], "subtree")) else if (!strcmp(xopts[x], "subtree"))
o.recursive_variant = MERGE_RECURSIVE_SUBTREE; o.subtree_shift = "";
else if (!prefixcmp(xopts[x], "subtree="))
o.subtree_shift = xopts[x]+8;
else else
die("Unknown option for merge-recursive: -X%s", xopts[x]); die("Unknown option for merge-recursive: -X%s", xopts[x]);
} }

Просмотреть файл

@ -993,6 +993,7 @@ extern int diff_auto_refresh_index;
/* match-trees.c */ /* match-trees.c */
void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int); void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *, const char *);
/* /*
* whitespace rules. * whitespace rules.

Просмотреть файл

@ -185,7 +185,7 @@ static void match_trees(const unsigned char *hash1,
* tree object by replacing it with another tree "hash2". * tree object by replacing it with another tree "hash2".
*/ */
static int splice_tree(const unsigned char *hash1, static int splice_tree(const unsigned char *hash1,
char *prefix, const char *prefix,
const unsigned char *hash2, const unsigned char *hash2,
unsigned char *result) unsigned char *result)
{ {
@ -264,6 +264,13 @@ void shift_tree(const unsigned char *hash1,
char *del_prefix; char *del_prefix;
int add_score, del_score; int add_score, del_score;
/*
* NEEDSWORK: this limits the recursion depth to hardcoded
* value '2' to avoid excessive overhead.
*/
if (!depth_limit)
depth_limit = 2;
add_score = del_score = score_trees(hash1, hash2); add_score = del_score = score_trees(hash1, hash2);
add_prefix = xcalloc(1, 1); add_prefix = xcalloc(1, 1);
del_prefix = xcalloc(1, 1); del_prefix = xcalloc(1, 1);
@ -301,3 +308,63 @@ void shift_tree(const unsigned char *hash1,
splice_tree(hash1, add_prefix, hash2, shifted); splice_tree(hash1, add_prefix, hash2, shifted);
} }
/*
* The user says the trees will be shifted by this much.
* Unfortunately we cannot fundamentally tell which one to
* be prefixed, as recursive merge can work in either direction.
*/
void shift_tree_by(const unsigned char *hash1,
const unsigned char *hash2,
unsigned char *shifted,
const char *shift_prefix)
{
unsigned char sub1[20], sub2[20];
unsigned mode1, mode2;
unsigned candidate = 0;
/* Can hash2 be a tree at shift_prefix in tree hash1? */
if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
S_ISDIR(mode1))
candidate |= 1;
/* Can hash1 be a tree at shift_prefix in tree hash2? */
if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
S_ISDIR(mode2))
candidate |= 2;
if (candidate == 3) {
/* Both are plausible -- we need to evaluate the score */
int best_score = score_trees(hash1, hash2);
int score;
candidate = 0;
score = score_trees(sub1, hash2);
if (score > best_score) {
candidate = 1;
best_score = score;
}
score = score_trees(sub2, hash1);
if (score > best_score)
candidate = 2;
}
if (!candidate) {
/* Neither is plausible -- do not shift */
hashcpy(shifted, hash2);
return;
}
if (candidate == 1)
/*
* shift tree2 down by adding shift_prefix above it
* to match tree1.
*/
splice_tree(hash1, shift_prefix, hash2, shifted);
else
/*
* shift tree2 up by removing shift_prefix from it
* to match tree1.
*/
hashcpy(shifted, sub2);
}

Просмотреть файл

@ -21,15 +21,17 @@
#include "merge-recursive.h" #include "merge-recursive.h"
#include "dir.h" #include "dir.h"
static struct tree *shift_tree_object(struct tree *one, struct tree *two) static struct tree *shift_tree_object(struct tree *one, struct tree *two,
const char *subtree_shift)
{ {
unsigned char shifted[20]; unsigned char shifted[20];
/* if (!*subtree_shift) {
* NEEDSWORK: this limits the recursion depth to hardcoded shift_tree(one->object.sha1, two->object.sha1, shifted, 0);
* value '2' to avoid excessive overhead. } else {
*/ shift_tree_by(one->object.sha1, two->object.sha1, shifted,
shift_tree(one->object.sha1, two->object.sha1, shifted, 2); subtree_shift);
}
if (!hashcmp(two->object.sha1, shifted)) if (!hashcmp(two->object.sha1, shifted))
return two; return two;
return lookup_tree(shifted); return lookup_tree(shifted);
@ -1213,9 +1215,9 @@ int merge_trees(struct merge_options *o,
{ {
int code, clean; int code, clean;
if (o->recursive_variant == MERGE_RECURSIVE_SUBTREE) { if (o->subtree_shift) {
merge = shift_tree_object(head, merge); merge = shift_tree_object(head, merge, o->subtree_shift);
common = shift_tree_object(head, common); common = shift_tree_object(head, common, o->subtree_shift);
} }
if (sha_eq(common->object.sha1, merge->object.sha1)) { if (sha_eq(common->object.sha1, merge->object.sha1)) {

Просмотреть файл

@ -7,10 +7,11 @@ struct merge_options {
const char *branch1; const char *branch1;
const char *branch2; const char *branch2;
enum { enum {
MERGE_RECURSIVE_SUBTREE = 1, MERGE_RECURSIVE_NORMAL = 0,
MERGE_RECURSIVE_OURS, MERGE_RECURSIVE_OURS,
MERGE_RECURSIVE_THEIRS, MERGE_RECURSIVE_THEIRS,
} recursive_variant; } recursive_variant;
const char *subtree_shift;
unsigned buffer_output : 1; unsigned buffer_output : 1;
int verbosity; int verbosity;
int diff_rename_limit; int diff_rename_limit;