merge-recursive: introduce merge_options

This makes it possible to avoid passing the labels of branches as
arguments to merge_recursive(), merge_trees() and
merge_recursive_generic().

It also takes care of subtree merge, output buffering, verbosity, and
rename limits - these were global variables till now in
merge-recursive.c.

A new function, named init_merge_options(), is introduced as well, it
clears the struct merge_info, then initializes with default values,
finally updates the default values based on the config and environment
variables.

Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Miklos Vajna 2008-08-25 16:25:57 +02:00 committed by Junio C Hamano
parent 73118f89b8
commit 8a2fce1895
4 changed files with 192 additions and 169 deletions

View File

@ -264,6 +264,7 @@ static int merge_working_tree(struct checkout_opts *opts,
*/ */
struct tree *result; struct tree *result;
struct tree *work; struct tree *work;
struct merge_options o;
if (!opts->merge) if (!opts->merge)
return 1; return 1;
parse_commit(old->commit); parse_commit(old->commit);
@ -282,13 +283,17 @@ static int merge_working_tree(struct checkout_opts *opts,
*/ */
add_files_to_cache(NULL, NULL, 0); add_files_to_cache(NULL, NULL, 0);
work = write_tree_from_memory(); init_merge_options(&o);
o.verbosity = 0;
work = write_tree_from_memory(&o);
ret = reset_tree(new->commit->tree, opts, 1); ret = reset_tree(new->commit->tree, opts, 1);
if (ret) if (ret)
return ret; return ret;
merge_trees(new->commit->tree, work, old->commit->tree, o.branch1 = new->name;
new->name, "local", &result); o.branch2 = "local";
merge_trees(&o, new->commit->tree, work,
old->commit->tree, &result);
ret = reset_tree(new->commit->tree, opts, 0); ret = reset_tree(new->commit->tree, opts, 0);
if (ret) if (ret)
return ret; return ret;

View File

@ -17,32 +17,33 @@ static const char *better_branch_name(const char *branch)
int cmd_merge_recursive(int argc, const char **argv, const char *prefix) int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
{ {
const char *bases[21]; const unsigned char *bases[21];
unsigned bases_count = 0; unsigned bases_count = 0;
int i, failed; int i, failed;
const char *branch1, *branch2;
unsigned char h1[20], h2[20]; unsigned char h1[20], h2[20];
int subtree_merge = 0; struct merge_options o;
struct commit *result;
init_merge_options(&o);
if (argv[0]) { if (argv[0]) {
int namelen = strlen(argv[0]); int namelen = strlen(argv[0]);
if (8 < namelen && if (8 < namelen &&
!strcmp(argv[0] + namelen - 8, "-subtree")) !strcmp(argv[0] + namelen - 8, "-subtree"))
subtree_merge = 1; o.subtree_merge = 1;
} }
git_config(merge_recursive_config, NULL);
merge_recursive_setup(subtree_merge);
if (argc < 4) if (argc < 4)
die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]); die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "--")) { if (!strcmp(argv[i], "--"))
bases[bases_count] = NULL;
break; break;
if (bases_count < ARRAY_SIZE(bases)-1) {
unsigned char *sha = xmalloc(20);
if (get_sha1(argv[i], sha))
die("Could not parse object '%s'", argv[i]);
bases[bases_count++] = sha;
} }
if (bases_count < ARRAY_SIZE(bases)-1)
bases[bases_count++] = argv[i];
else else
warning("Cannot handle more than %zu bases. " warning("Cannot handle more than %zu bases. "
"Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]); "Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]);
@ -50,21 +51,21 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
if (argc - i != 3) /* "--" "<head>" "<remote>" */ if (argc - i != 3) /* "--" "<head>" "<remote>" */
die("Not handling anything other than two heads merge."); die("Not handling anything other than two heads merge.");
branch1 = argv[++i]; o.branch1 = argv[++i];
branch2 = argv[++i]; o.branch2 = argv[++i];
if (get_sha1(branch1, h1)) if (get_sha1(o.branch1, h1))
die("Could not resolve ref '%s'", branch1); die("Could not resolve ref '%s'", o.branch1);
if (get_sha1(branch2, h2)) if (get_sha1(o.branch2, h2))
die("Could not resolve ref '%s'", branch2); die("Could not resolve ref '%s'", o.branch2);
branch1 = better_branch_name(branch1); o.branch1 = better_branch_name(o.branch1);
branch2 = better_branch_name(branch2); o.branch2 = better_branch_name(o.branch2);
if (merge_recursive_verbosity >= 3) if (o.verbosity >= 3)
printf("Merging %s with %s\n", branch1, branch2); printf("Merging %s with %s\n", o.branch1, o.branch2);
failed = merge_recursive_generic(bases, h1, branch1, h2, branch2); failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
if (failed < 0) if (failed < 0)
return 128; /* die() error code */ return 128; /* die() error code */
return failed; return failed;

View File

@ -20,8 +20,6 @@
#include "attr.h" #include "attr.h"
#include "merge-recursive.h" #include "merge-recursive.h"
static int subtree_merge;
static struct tree *shift_tree_object(struct tree *one, struct tree *two) static struct tree *shift_tree_object(struct tree *one, struct tree *two)
{ {
unsigned char shifted[20]; unsigned char shifted[20];
@ -83,16 +81,11 @@ static struct string_list current_file_set = {NULL, 0, 0, 1};
static struct string_list current_directory_set = {NULL, 0, 0, 1}; static struct string_list current_directory_set = {NULL, 0, 0, 1};
static int call_depth = 0; static int call_depth = 0;
int merge_recursive_verbosity = 2;
static int diff_rename_limit = -1;
static int merge_rename_limit = -1;
static int buffer_output = 1;
static struct strbuf obuf = STRBUF_INIT; static struct strbuf obuf = STRBUF_INIT;
static int show(int v) static int show(struct merge_options *o, int v)
{ {
return (!call_depth && merge_recursive_verbosity >= v) || return (!call_depth && o->verbosity >= v) || o->verbosity >= 5;
merge_recursive_verbosity >= 5;
} }
static void flush_output(void) static void flush_output(void)
@ -103,12 +96,12 @@ static void flush_output(void)
} }
} }
static void output(int v, const char *fmt, ...) static void output(struct merge_options *o, int v, const char *fmt, ...)
{ {
int len; int len;
va_list ap; va_list ap;
if (!show(v)) if (!show(o, v))
return; return;
strbuf_grow(&obuf, call_depth * 2 + 2); strbuf_grow(&obuf, call_depth * 2 + 2);
@ -132,7 +125,7 @@ static void output(int v, const char *fmt, ...)
} }
strbuf_setlen(&obuf, obuf.len + len); strbuf_setlen(&obuf, obuf.len + len);
strbuf_add(&obuf, "\n", 1); strbuf_add(&obuf, "\n", 1);
if (!buffer_output) if (!o->buffer_output)
flush_output(); flush_output();
} }
@ -219,17 +212,17 @@ static int git_merge_trees(int index_only,
return rc; return rc;
} }
struct tree *write_tree_from_memory(void) struct tree *write_tree_from_memory(struct merge_options *o)
{ {
struct tree *result = NULL; struct tree *result = NULL;
if (unmerged_cache()) { if (unmerged_cache()) {
int i; int i;
output(0, "There are unmerged index entries:"); output(o, 0, "There are unmerged index entries:");
for (i = 0; i < active_nr; i++) { for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i]; struct cache_entry *ce = active_cache[i];
if (ce_stage(ce)) if (ce_stage(ce))
output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
} }
return NULL; return NULL;
} }
@ -341,11 +334,12 @@ struct rename
* 'b_tree') to be able to associate the correct cache entries with * 'b_tree') to be able to associate the correct cache entries with
* the rename information. 'tree' is always equal to either a_tree or b_tree. * the rename information. 'tree' is always equal to either a_tree or b_tree.
*/ */
static struct string_list *get_renames(struct tree *tree, static struct string_list *get_renames(struct merge_options *o,
struct tree *o_tree, struct tree *tree,
struct tree *a_tree, struct tree *o_tree,
struct tree *b_tree, struct tree *a_tree,
struct string_list *entries) struct tree *b_tree,
struct string_list *entries)
{ {
int i; int i;
struct string_list *renames; struct string_list *renames;
@ -355,8 +349,8 @@ static struct string_list *get_renames(struct tree *tree,
diff_setup(&opts); diff_setup(&opts);
DIFF_OPT_SET(&opts, RECURSIVE); DIFF_OPT_SET(&opts, RECURSIVE);
opts.detect_rename = DIFF_DETECT_RENAME; opts.detect_rename = DIFF_DETECT_RENAME;
opts.rename_limit = merge_rename_limit >= 0 ? merge_rename_limit : opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
diff_rename_limit >= 0 ? diff_rename_limit : o->diff_rename_limit >= 0 ? o->diff_rename_limit :
500; 500;
opts.warn_on_too_large_rename = 1; opts.warn_on_too_large_rename = 1;
opts.output_format = DIFF_FORMAT_NO_OUTPUT; opts.output_format = DIFF_FORMAT_NO_OUTPUT;
@ -717,7 +711,8 @@ static struct merge_file_info merge_file(struct diff_filespec *o,
return result; return result;
} }
static void conflict_rename_rename(struct rename *ren1, static void conflict_rename_rename(struct merge_options *o,
struct rename *ren1,
const char *branch1, const char *branch1,
struct rename *ren2, struct rename *ren2,
const char *branch2) const char *branch2)
@ -730,13 +725,13 @@ static void conflict_rename_rename(struct rename *ren1,
const char *dst_name2 = ren2_dst; const char *dst_name2 = ren2_dst;
if (string_list_has_string(&current_directory_set, ren1_dst)) { if (string_list_has_string(&current_directory_set, ren1_dst)) {
dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
output(1, "%s is a directory in %s adding as %s instead", output(o, 1, "%s is a directory in %s adding as %s instead",
ren1_dst, branch2, dst_name1); ren1_dst, branch2, dst_name1);
remove_file(0, ren1_dst, 0); remove_file(0, ren1_dst, 0);
} }
if (string_list_has_string(&current_directory_set, ren2_dst)) { if (string_list_has_string(&current_directory_set, ren2_dst)) {
dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
output(1, "%s is a directory in %s adding as %s instead", output(o, 1, "%s is a directory in %s adding as %s instead",
ren2_dst, branch1, dst_name2); ren2_dst, branch1, dst_name2);
remove_file(0, ren2_dst, 0); remove_file(0, ren2_dst, 0);
} }
@ -757,24 +752,26 @@ static void conflict_rename_rename(struct rename *ren1,
free(del[delp]); free(del[delp]);
} }
static void conflict_rename_dir(struct rename *ren1, static void conflict_rename_dir(struct merge_options *o,
struct rename *ren1,
const char *branch1) const char *branch1)
{ {
char *new_path = unique_path(ren1->pair->two->path, branch1); char *new_path = unique_path(ren1->pair->two->path, branch1);
output(1, "Renaming %s to %s instead", ren1->pair->one->path, new_path); output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
remove_file(0, ren1->pair->two->path, 0); remove_file(0, ren1->pair->two->path, 0);
update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
free(new_path); free(new_path);
} }
static void conflict_rename_rename_2(struct rename *ren1, static void conflict_rename_rename_2(struct merge_options *o,
struct rename *ren1,
const char *branch1, const char *branch1,
struct rename *ren2, struct rename *ren2,
const char *branch2) const char *branch2)
{ {
char *new_path1 = unique_path(ren1->pair->two->path, branch1); char *new_path1 = unique_path(ren1->pair->two->path, branch1);
char *new_path2 = unique_path(ren2->pair->two->path, branch2); char *new_path2 = unique_path(ren2->pair->two->path, branch2);
output(1, "Renaming %s to %s and %s to %s instead", output(o, 1, "Renaming %s to %s and %s to %s instead",
ren1->pair->one->path, new_path1, ren1->pair->one->path, new_path1,
ren2->pair->one->path, new_path2); ren2->pair->one->path, new_path2);
remove_file(0, ren1->pair->two->path, 0); remove_file(0, ren1->pair->two->path, 0);
@ -784,10 +781,9 @@ static void conflict_rename_rename_2(struct rename *ren1,
free(new_path1); free(new_path1);
} }
static int process_renames(struct string_list *a_renames, static int process_renames(struct merge_options *o,
struct string_list *b_renames, struct string_list *a_renames,
const char *a_branch, struct string_list *b_renames)
const char *b_branch)
{ {
int clean_merge = 1, i, j; int clean_merge = 1, i, j;
struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
@ -832,15 +828,15 @@ static int process_renames(struct string_list *a_renames,
renames1 = a_renames; renames1 = a_renames;
renames2 = b_renames; renames2 = b_renames;
renames2Dst = &b_by_dst; renames2Dst = &b_by_dst;
branch1 = a_branch; branch1 = o->branch1;
branch2 = b_branch; branch2 = o->branch2;
} else { } else {
struct rename *tmp; struct rename *tmp;
renames1 = b_renames; renames1 = b_renames;
renames2 = a_renames; renames2 = a_renames;
renames2Dst = &a_by_dst; renames2Dst = &a_by_dst;
branch1 = b_branch; branch1 = o->branch2;
branch2 = a_branch; branch2 = o->branch1;
tmp = ren2; tmp = ren2;
ren2 = ren1; ren2 = ren1;
ren1 = tmp; ren1 = tmp;
@ -867,7 +863,7 @@ static int process_renames(struct string_list *a_renames,
ren2->processed = 1; ren2->processed = 1;
if (strcmp(ren1_dst, ren2_dst) != 0) { if (strcmp(ren1_dst, ren2_dst) != 0) {
clean_merge = 0; clean_merge = 0;
output(1, "CONFLICT (rename/rename): " output(o, 1, "CONFLICT (rename/rename): "
"Rename \"%s\"->\"%s\" in branch \"%s\" " "Rename \"%s\"->\"%s\" in branch \"%s\" "
"rename \"%s\"->\"%s\" in \"%s\"%s", "rename \"%s\"->\"%s\" in \"%s\"%s",
src, ren1_dst, branch1, src, ren1_dst, branch1,
@ -878,7 +874,7 @@ static int process_renames(struct string_list *a_renames,
update_file(0, ren1->pair->one->sha1, update_file(0, ren1->pair->one->sha1,
ren1->pair->one->mode, src); ren1->pair->one->mode, src);
} }
conflict_rename_rename(ren1, branch1, ren2, branch2); conflict_rename_rename(o, ren1, branch1, ren2, branch2);
} else { } else {
struct merge_file_info mfi; struct merge_file_info mfi;
remove_file(1, ren1_src, 1); remove_file(1, ren1_src, 1);
@ -888,13 +884,13 @@ static int process_renames(struct string_list *a_renames,
branch1, branch1,
branch2); branch2);
if (mfi.merge || !mfi.clean) if (mfi.merge || !mfi.clean)
output(1, "Renaming %s->%s", src, ren1_dst); output(o, 1, "Renaming %s->%s", src, ren1_dst);
if (mfi.merge) if (mfi.merge)
output(2, "Auto-merging %s", ren1_dst); output(o, 2, "Auto-merging %s", ren1_dst);
if (!mfi.clean) { if (!mfi.clean) {
output(1, "CONFLICT (content): merge conflict in %s", output(o, 1, "CONFLICT (content): merge conflict in %s",
ren1_dst); ren1_dst);
clean_merge = 0; clean_merge = 0;
@ -925,14 +921,14 @@ static int process_renames(struct string_list *a_renames,
if (string_list_has_string(&current_directory_set, ren1_dst)) { if (string_list_has_string(&current_directory_set, ren1_dst)) {
clean_merge = 0; clean_merge = 0;
output(1, "CONFLICT (rename/directory): Rename %s->%s in %s " output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
" directory %s added in %s", " directory %s added in %s",
ren1_src, ren1_dst, branch1, ren1_src, ren1_dst, branch1,
ren1_dst, branch2); ren1_dst, branch2);
conflict_rename_dir(ren1, branch1); conflict_rename_dir(o, ren1, branch1);
} else if (sha_eq(src_other.sha1, null_sha1)) { } else if (sha_eq(src_other.sha1, null_sha1)) {
clean_merge = 0; clean_merge = 0;
output(1, "CONFLICT (rename/delete): Rename %s->%s in %s " output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
"and deleted in %s", "and deleted in %s",
ren1_src, ren1_dst, branch1, ren1_src, ren1_dst, branch1,
branch2); branch2);
@ -941,31 +937,32 @@ static int process_renames(struct string_list *a_renames,
const char *new_path; const char *new_path;
clean_merge = 0; clean_merge = 0;
try_merge = 1; try_merge = 1;
output(1, "CONFLICT (rename/add): Rename %s->%s in %s. " output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
"%s added in %s", "%s added in %s",
ren1_src, ren1_dst, branch1, ren1_src, ren1_dst, branch1,
ren1_dst, branch2); ren1_dst, branch2);
new_path = unique_path(ren1_dst, branch2); new_path = unique_path(ren1_dst, branch2);
output(1, "Adding as %s instead", new_path); output(o, 1, "Adding as %s instead", new_path);
update_file(0, dst_other.sha1, dst_other.mode, new_path); update_file(0, dst_other.sha1, dst_other.mode, new_path);
} else if ((item = string_list_lookup(ren1_dst, renames2Dst))) { } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
ren2 = item->util; ren2 = item->util;
clean_merge = 0; clean_merge = 0;
ren2->processed = 1; ren2->processed = 1;
output(1, "CONFLICT (rename/rename): Rename %s->%s in %s. " output(o, 1, "CONFLICT (rename/rename): "
"Rename %s->%s in %s. "
"Rename %s->%s in %s", "Rename %s->%s in %s",
ren1_src, ren1_dst, branch1, ren1_src, ren1_dst, branch1,
ren2->pair->one->path, ren2->pair->two->path, branch2); ren2->pair->one->path, ren2->pair->two->path, branch2);
conflict_rename_rename_2(ren1, branch1, ren2, branch2); conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
} else } else
try_merge = 1; try_merge = 1;
if (try_merge) { if (try_merge) {
struct diff_filespec *o, *a, *b; struct diff_filespec *one, *a, *b;
struct merge_file_info mfi; struct merge_file_info mfi;
src_other.path = (char *)ren1_src; src_other.path = (char *)ren1_src;
o = ren1->pair->one; one = ren1->pair->one;
if (a_renames == renames1) { if (a_renames == renames1) {
a = ren1->pair->two; a = ren1->pair->two;
b = &src_other; b = &src_other;
@ -973,8 +970,8 @@ static int process_renames(struct string_list *a_renames,
b = ren1->pair->two; b = ren1->pair->two;
a = &src_other; a = &src_other;
} }
mfi = merge_file(o, a, b, mfi = merge_file(one, a, b,
a_branch, b_branch); o->branch1, o->branch2);
if (mfi.clean && if (mfi.clean &&
sha_eq(mfi.sha, ren1->pair->two->sha1) && sha_eq(mfi.sha, ren1->pair->two->sha1) &&
@ -984,20 +981,20 @@ static int process_renames(struct string_list *a_renames,
* t6022 test. If you change * t6022 test. If you change
* it update the test too. * it update the test too.
*/ */
output(3, "Skipped %s (merged same as existing)", ren1_dst); output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
else { else {
if (mfi.merge || !mfi.clean) if (mfi.merge || !mfi.clean)
output(1, "Renaming %s => %s", ren1_src, ren1_dst); output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
if (mfi.merge) if (mfi.merge)
output(2, "Auto-merging %s", ren1_dst); output(o, 2, "Auto-merging %s", ren1_dst);
if (!mfi.clean) { if (!mfi.clean) {
output(1, "CONFLICT (rename/modify): Merge conflict in %s", output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
ren1_dst); ren1_dst);
clean_merge = 0; clean_merge = 0;
if (!index_only) if (!index_only)
update_stages(ren1_dst, update_stages(ren1_dst,
o, a, b, 1); one, a, b, 1);
} }
update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
} }
@ -1016,9 +1013,8 @@ static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
} }
/* Per entry merge function */ /* Per entry merge function */
static int process_entry(const char *path, struct stage_data *entry, static int process_entry(struct merge_options *o,
const char *branch1, const char *path, struct stage_data *entry)
const char *branch2)
{ {
/* /*
printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
@ -1040,23 +1036,23 @@ static int process_entry(const char *path, struct stage_data *entry,
/* Deleted in both or deleted in one and /* Deleted in both or deleted in one and
* unchanged in the other */ * unchanged in the other */
if (a_sha) if (a_sha)
output(2, "Removing %s", path); output(o, 2, "Removing %s", path);
/* do not touch working file if it did not exist */ /* do not touch working file if it did not exist */
remove_file(1, path, !a_sha); remove_file(1, path, !a_sha);
} else { } else {
/* Deleted in one and changed in the other */ /* Deleted in one and changed in the other */
clean_merge = 0; clean_merge = 0;
if (!a_sha) { if (!a_sha) {
output(1, "CONFLICT (delete/modify): %s deleted in %s " output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
"and modified in %s. Version %s of %s left in tree.", "and modified in %s. Version %s of %s left in tree.",
path, branch1, path, o->branch1,
branch2, branch2, path); o->branch2, o->branch2, path);
update_file(0, b_sha, b_mode, path); update_file(0, b_sha, b_mode, path);
} else { } else {
output(1, "CONFLICT (delete/modify): %s deleted in %s " output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
"and modified in %s. Version %s of %s left in tree.", "and modified in %s. Version %s of %s left in tree.",
path, branch2, path, o->branch2,
branch1, branch1, path); o->branch1, o->branch1, path);
update_file(0, a_sha, a_mode, path); update_file(0, a_sha, a_mode, path);
} }
} }
@ -1071,14 +1067,14 @@ static int process_entry(const char *path, struct stage_data *entry,
const char *conf; const char *conf;
if (a_sha) { if (a_sha) {
add_branch = branch1; add_branch = o->branch1;
other_branch = branch2; other_branch = o->branch2;
mode = a_mode; mode = a_mode;
sha = a_sha; sha = a_sha;
conf = "file/directory"; conf = "file/directory";
} else { } else {
add_branch = branch2; add_branch = o->branch2;
other_branch = branch1; other_branch = o->branch1;
mode = b_mode; mode = b_mode;
sha = b_sha; sha = b_sha;
conf = "directory/file"; conf = "directory/file";
@ -1086,13 +1082,13 @@ static int process_entry(const char *path, struct stage_data *entry,
if (string_list_has_string(&current_directory_set, path)) { if (string_list_has_string(&current_directory_set, path)) {
const char *new_path = unique_path(path, add_branch); const char *new_path = unique_path(path, add_branch);
clean_merge = 0; clean_merge = 0;
output(1, "CONFLICT (%s): There is a directory with name %s in %s. " output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
"Adding %s as %s", "Adding %s as %s",
conf, path, other_branch, path, new_path); conf, path, other_branch, path, new_path);
remove_file(0, path, 0); remove_file(0, path, 0);
update_file(0, sha, mode, new_path); update_file(0, sha, mode, new_path);
} else { } else {
output(2, "Adding %s", path); output(o, 2, "Adding %s", path);
update_file(1, sha, mode, path); update_file(1, sha, mode, path);
} }
} else if (a_sha && b_sha) { } else if (a_sha && b_sha) {
@ -1100,32 +1096,32 @@ static int process_entry(const char *path, struct stage_data *entry,
/* case D: Modified in both, but differently. */ /* case D: Modified in both, but differently. */
const char *reason = "content"; const char *reason = "content";
struct merge_file_info mfi; struct merge_file_info mfi;
struct diff_filespec o, a, b; struct diff_filespec one, a, b;
if (!o_sha) { if (!o_sha) {
reason = "add/add"; reason = "add/add";
o_sha = (unsigned char *)null_sha1; o_sha = (unsigned char *)null_sha1;
} }
output(2, "Auto-merging %s", path); output(o, 2, "Auto-merging %s", path);
o.path = a.path = b.path = (char *)path; one.path = a.path = b.path = (char *)path;
hashcpy(o.sha1, o_sha); hashcpy(one.sha1, o_sha);
o.mode = o_mode; one.mode = o_mode;
hashcpy(a.sha1, a_sha); hashcpy(a.sha1, a_sha);
a.mode = a_mode; a.mode = a_mode;
hashcpy(b.sha1, b_sha); hashcpy(b.sha1, b_sha);
b.mode = b_mode; b.mode = b_mode;
mfi = merge_file(&o, &a, &b, mfi = merge_file(&one, &a, &b,
branch1, branch2); o->branch1, o->branch2);
clean_merge = mfi.clean; clean_merge = mfi.clean;
if (mfi.clean) if (mfi.clean)
update_file(1, mfi.sha, mfi.mode, path); update_file(1, mfi.sha, mfi.mode, path);
else if (S_ISGITLINK(mfi.mode)) else if (S_ISGITLINK(mfi.mode))
output(1, "CONFLICT (submodule): Merge conflict in %s " output(o, 1, "CONFLICT (submodule): Merge conflict in %s "
"- needs %s", path, sha1_to_hex(b.sha1)); "- needs %s", path, sha1_to_hex(b.sha1));
else { else {
output(1, "CONFLICT (%s): Merge conflict in %s", output(o, 1, "CONFLICT (%s): Merge conflict in %s",
reason, path); reason, path);
if (index_only) if (index_only)
@ -1146,22 +1142,21 @@ static int process_entry(const char *path, struct stage_data *entry,
return clean_merge; return clean_merge;
} }
int merge_trees(struct tree *head, int merge_trees(struct merge_options *o,
struct tree *head,
struct tree *merge, struct tree *merge,
struct tree *common, struct tree *common,
const char *branch1,
const char *branch2,
struct tree **result) struct tree **result)
{ {
int code, clean; int code, clean;
if (subtree_merge) { if (o->subtree_merge) {
merge = shift_tree_object(head, merge); merge = shift_tree_object(head, merge);
common = shift_tree_object(head, common); common = shift_tree_object(head, common);
} }
if (sha_eq(common->object.sha1, merge->object.sha1)) { if (sha_eq(common->object.sha1, merge->object.sha1)) {
output(0, "Already uptodate!"); output(o, 0, "Already uptodate!");
*result = head; *result = head;
return 1; return 1;
} }
@ -1182,15 +1177,14 @@ int merge_trees(struct tree *head,
get_files_dirs(merge); get_files_dirs(merge);
entries = get_unmerged(); entries = get_unmerged();
re_head = get_renames(head, common, head, merge, entries); re_head = get_renames(o, head, common, head, merge, entries);
re_merge = get_renames(merge, common, head, merge, entries); re_merge = get_renames(o, merge, common, head, merge, entries);
clean = process_renames(re_head, re_merge, clean = process_renames(o, re_head, re_merge);
branch1, branch2);
for (i = 0; i < entries->nr; i++) { for (i = 0; i < entries->nr; i++) {
const char *path = entries->items[i].string; const char *path = entries->items[i].string;
struct stage_data *e = entries->items[i].util; struct stage_data *e = entries->items[i].util;
if (!e->processed if (!e->processed
&& !process_entry(path, e, branch1, branch2)) && !process_entry(o, path, e))
clean = 0; clean = 0;
} }
@ -1203,7 +1197,7 @@ int merge_trees(struct tree *head,
clean = 1; clean = 1;
if (index_only) if (index_only)
*result = write_tree_from_memory(); *result = write_tree_from_memory(o);
return clean; return clean;
} }
@ -1223,10 +1217,9 @@ static struct commit_list *reverse_commit_list(struct commit_list *list)
* Merge the commits h1 and h2, return the resulting virtual * Merge the commits h1 and h2, return the resulting virtual
* commit object and a flag indicating the cleanness of the merge. * commit object and a flag indicating the cleanness of the merge.
*/ */
int merge_recursive(struct commit *h1, int merge_recursive(struct merge_options *o,
struct commit *h1,
struct commit *h2, struct commit *h2,
const char *branch1,
const char *branch2,
struct commit_list *ca, struct commit_list *ca,
struct commit **result) struct commit **result)
{ {
@ -1235,8 +1228,8 @@ int merge_recursive(struct commit *h1,
struct tree *mrtree = mrtree; struct tree *mrtree = mrtree;
int clean; int clean;
if (show(4)) { if (show(o, 4)) {
output(4, "Merging:"); output(o, 4, "Merging:");
output_commit_title(h1); output_commit_title(h1);
output_commit_title(h2); output_commit_title(h2);
} }
@ -1246,8 +1239,8 @@ int merge_recursive(struct commit *h1,
ca = reverse_commit_list(ca); ca = reverse_commit_list(ca);
} }
if (show(5)) { if (show(o, 5)) {
output(5, "found %u common ancestor(s):", commit_list_count(ca)); output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
for (iter = ca; iter; iter = iter->next) for (iter = ca; iter; iter = iter->next)
output_commit_title(iter->item); output_commit_title(iter->item);
} }
@ -1264,6 +1257,7 @@ int merge_recursive(struct commit *h1,
} }
for (iter = ca; iter; iter = iter->next) { for (iter = ca; iter; iter = iter->next) {
const char *saved_b1, *saved_b2;
call_depth++; call_depth++;
/* /*
* When the merge fails, the result contains files * When the merge fails, the result contains files
@ -1273,11 +1267,14 @@ int merge_recursive(struct commit *h1,
* "conflicts" were already resolved. * "conflicts" were already resolved.
*/ */
discard_cache(); discard_cache();
merge_recursive(merged_common_ancestors, iter->item, saved_b1 = o->branch1;
"Temporary merge branch 1", saved_b2 = o->branch2;
"Temporary merge branch 2", o->branch1 = "Temporary merge branch 1";
NULL, o->branch2 = "Temporary merge branch 2";
&merged_common_ancestors); merge_recursive(o, merged_common_ancestors, iter->item,
NULL, &merged_common_ancestors);
o->branch1 = saved_b1;
o->branch2 = saved_b2;
call_depth--; call_depth--;
if (!merged_common_ancestors) if (!merged_common_ancestors)
@ -1291,8 +1288,8 @@ int merge_recursive(struct commit *h1,
} else } else
index_only = 1; index_only = 1;
clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
branch1, branch2, &mrtree); &mrtree);
if (index_only) { if (index_only) {
*result = make_virtual_commit(mrtree, "merged tree"); *result = make_virtual_commit(mrtree, "merged tree");
@ -1319,35 +1316,33 @@ static struct commit *get_ref(const unsigned char *sha1, const char *name)
return (struct commit *)object; return (struct commit *)object;
} }
int merge_recursive_generic(const char **base_list, int merge_recursive_generic(struct merge_options *o,
const unsigned char *head_sha1, const char *head_name, const unsigned char *head,
const unsigned char *next_sha1, const char *next_name) const unsigned char *merge,
int num_base_list,
const unsigned char **base_list,
struct commit **result)
{ {
int clean, index_fd; int clean, index_fd;
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
struct commit *result; struct commit *head_commit = get_ref(head, o->branch1);
struct commit *head_commit = get_ref(head_sha1, head_name); struct commit *next_commit = get_ref(merge, o->branch2);
struct commit *next_commit = get_ref(next_sha1, next_name);
struct commit_list *ca = NULL; struct commit_list *ca = NULL;
if (base_list) { if (base_list) {
int i; int i;
for (i = 0; base_list[i]; ++i) { for (i = 0; i < num_base_list; ++i) {
unsigned char sha[20];
struct commit *base; struct commit *base;
if (get_sha1(base_list[i], sha)) if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
return error("Could not resolve ref '%s'",
base_list[i]);
if (!(base = get_ref(sha, base_list[i])))
return error("Could not parse object '%s'", return error("Could not parse object '%s'",
base_list[i]); sha1_to_hex(base_list[i]));
commit_list_insert(base, &ca); commit_list_insert(base, &ca);
} }
} }
index_fd = hold_locked_index(lock, 1); index_fd = hold_locked_index(lock, 1);
clean = merge_recursive(head_commit, next_commit, clean = merge_recursive(o, head_commit, next_commit, ca,
head_name, next_name, ca, &result); result);
if (active_cache_changed && if (active_cache_changed &&
(write_cache(index_fd, active_cache, active_nr) || (write_cache(index_fd, active_cache, active_nr) ||
commit_locked_index(lock))) commit_locked_index(lock)))
@ -1356,29 +1351,35 @@ int merge_recursive_generic(const char **base_list,
return clean ? 0 : 1; return clean ? 0 : 1;
} }
int merge_recursive_config(const char *var, const char *value, void *cb) static int merge_recursive_config(const char *var, const char *value, void *cb)
{ {
struct merge_options *o = cb;
if (!strcasecmp(var, "merge.verbosity")) { if (!strcasecmp(var, "merge.verbosity")) {
merge_recursive_verbosity = git_config_int(var, value); o->verbosity = git_config_int(var, value);
return 0; return 0;
} }
if (!strcasecmp(var, "diff.renamelimit")) { if (!strcasecmp(var, "diff.renamelimit")) {
diff_rename_limit = git_config_int(var, value); o->diff_rename_limit = git_config_int(var, value);
return 0; return 0;
} }
if (!strcasecmp(var, "merge.renamelimit")) { if (!strcasecmp(var, "merge.renamelimit")) {
merge_rename_limit = git_config_int(var, value); o->merge_rename_limit = git_config_int(var, value);
return 0; return 0;
} }
return git_default_config(var, value, cb); return git_default_config(var, value, cb);
} }
void merge_recursive_setup(int is_subtree_merge) void init_merge_options(struct merge_options *o)
{ {
memset(o, 0, sizeof(struct merge_options));
o->verbosity = 2;
o->buffer_output = 1;
o->diff_rename_limit = -1;
o->merge_rename_limit = -1;
git_config(merge_recursive_config, o);
if (getenv("GIT_MERGE_VERBOSITY")) if (getenv("GIT_MERGE_VERBOSITY"))
merge_recursive_verbosity = o->verbosity =
strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
if (merge_recursive_verbosity >= 5) if (o->verbosity >= 5)
buffer_output = 0; o->buffer_output = 0;
subtree_merge = is_subtree_merge;
} }

View File

@ -1,26 +1,42 @@
#ifndef MERGE_RECURSIVE_H #ifndef MERGE_RECURSIVE_H
#define MERGE_RECURSIVE_H #define MERGE_RECURSIVE_H
int merge_recursive(struct commit *h1, struct merge_options {
const char *branch1;
const char *branch2;
unsigned subtree_merge : 1;
unsigned buffer_output : 1;
int verbosity;
int diff_rename_limit;
int merge_rename_limit;
};
/* merge_trees() but with recursive ancestor consolidation */
int merge_recursive(struct merge_options *o,
struct commit *h1,
struct commit *h2, struct commit *h2,
const char *branch1,
const char *branch2,
struct commit_list *ancestors, struct commit_list *ancestors,
struct commit **result); struct commit **result);
int merge_trees(struct tree *head, /* rename-detecting three-way merge, no recursion */
int merge_trees(struct merge_options *o,
struct tree *head,
struct tree *merge, struct tree *merge,
struct tree *common, struct tree *common,
const char *branch1,
const char *branch2,
struct tree **result); struct tree **result);
extern int merge_recursive_generic(const char **base_list,
const unsigned char *head_sha1, const char *head_name,
const unsigned char *next_sha1, const char *next_name);
int merge_recursive_config(const char *var, const char *value, void *cb);
void merge_recursive_setup(int is_subtree_merge);
struct tree *write_tree_from_memory(void);
extern int merge_recursive_verbosity; /*
* "git-merge-recursive" can be fed trees; wrap them into
* virtual commits and call merge_recursive() proper.
*/
int merge_recursive_generic(struct merge_options *o,
const unsigned char *head,
const unsigned char *merge,
int num_ca,
const unsigned char **ca,
struct commit **result);
void init_merge_options(struct merge_options *o);
struct tree *write_tree_from_memory(struct merge_options *o);
#endif #endif