@matheusbsbs / matheus-bagliano / commits / 73118f89b8
Fork 0

merge-recursive.c: Add more generic merge_recursive_generic()

merge_recursive_generic() takes, in comparison to to merge_recursive(), no commit ("struct commit *") arguments but SHA ids ("unsigned char *"), and no commit list of bases but an array of refs ("const char **"). This makes it more generic in the case that it can also take the SHA of a tree to merge trees without commits, for the bases, the head and the remote. merge_recursive_generic() also handles locking and updating of the index, which is a common use case of merge_recursive(). This patch also rewrites builtin-merge-recursive.c to make use of merge_recursive_generic(). By doing this, I stumbled over the limitation of 20 bases and I've added a warning if this limitation is exceeded. This patch qualifies make_virtual_commit() as static again because this function is not needed anymore outside merge-recursive.c. Signed-off-by: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephan Beyer committed Aug 12, 2008 at 22:13 UTC 73118f89b81f5a3ed1bb56e2517627d56e9ebdfb
3 files changed +75 -46
builtin-merge-recursive.c
+19 -45
@@ -15,36 +15,13 @@ static const char *better_branch_name(const char *branch)
15 return name ? name : branch;
16 }
17
18 -static struct commit *get_ref(const char *ref)
19 -{
20 - unsigned char sha1[20];
21 - struct object *object;
22 -
23 - if (get_sha1(ref, sha1))
24 - die("Could not resolve ref '%s'", ref);
25 - object = deref_tag(parse_object(sha1), ref, strlen(ref));
26 - if (!object)
27 - return NULL;
28 - if (object->type == OBJ_TREE)
29 - return make_virtual_commit((struct tree*)object,
30 - better_branch_name(ref));
31 - if (object->type != OBJ_COMMIT)
32 - return NULL;
33 - if (parse_commit((struct commit *)object))
34 - die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
35 - return (struct commit *)object;
36 -}
37 -
18 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
19 {
40 - static const char *bases[20];
41 - static unsigned bases_count = 0;
42 - int i, clean;
20 + const char *bases[21];
21 + unsigned bases_count = 0;
22 + int i, failed;
23 const char *branch1, *branch2;
44 - struct commit *result, *h1, *h2;
45 - struct commit_list *ca = NULL;
46 - struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
47 - int index_fd;
24 + unsigned char h1[20], h2[20];
25 int subtree_merge = 0;
26
27 if (argv[0]) {
@@ -60,10 +37,15 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
37 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
38
39 for (i = 1; i < argc; ++i) {
63 - if (!strcmp(argv[i], "--"))
40 + if (!strcmp(argv[i], "--")) {
41 + bases[bases_count] = NULL;
42 break;
65 - if (bases_count < sizeof(bases)/sizeof(*bases))
43 + }
44 + if (bases_count < ARRAY_SIZE(bases)-1)
45 bases[bases_count++] = argv[i];
46 + else
47 + warning("Cannot handle more than %zu bases. "
48 + "Ignoring %s.", ARRAY_SIZE(bases)-1, argv[i]);
49 }
50 if (argc - i != 3) /* "--" "<head>" "<remote>" */
51 die("Not handling anything other than two heads merge.");
@@ -71,8 +53,10 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
53 branch1 = argv[++i];
54 branch2 = argv[++i];
55
74 - h1 = get_ref(branch1);
75 - h2 = get_ref(branch2);
56 + if (get_sha1(branch1, h1))
57 + die("Could not resolve ref '%s'", branch1);
58 + if (get_sha1(branch2, h2))
59 + die("Could not resolve ref '%s'", branch2);
60
61 branch1 = better_branch_name(branch1);
62 branch2 = better_branch_name(branch2);
@@ -80,18 +64,8 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
64 if (merge_recursive_verbosity >= 3)
65 printf("Merging %s with %s\n", branch1, branch2);
66
83 - index_fd = hold_locked_index(lock, 1);
84 -
85 - for (i = 0; i < bases_count; i++) {
86 - struct commit *ancestor = get_ref(bases[i]);
87 - ca = commit_list_insert(ancestor, &ca);
88 - }
89 - clean = merge_recursive(h1, h2, branch1, branch2, ca, &result);
90 -
91 - if (active_cache_changed &&
92 - (write_cache(index_fd, active_cache, active_nr) ||
93 - commit_locked_index(lock)))
94 - die ("unable to write %s", get_index_file());
95 -
96 - return clean ? 0: 1;
67 + failed = merge_recursive_generic(bases, h1, branch1, h2, branch2);
68 + if (failed < 0)
69 + return 128; /* die() error code */
70 + return failed;
71 }
merge-recursive.c
+53
@@ -1303,6 +1303,59 @@ int merge_recursive(struct commit *h1,
1303 return clean;
1304 }
1305
1306 +static struct commit *get_ref(const unsigned char *sha1, const char *name)
1307 +{
1308 + struct object *object;
1309 +
1310 + object = deref_tag(parse_object(sha1), name, strlen(name));
1311 + if (!object)
1312 + return NULL;
1313 + if (object->type == OBJ_TREE)
1314 + return make_virtual_commit((struct tree*)object, name);
1315 + if (object->type != OBJ_COMMIT)
1316 + return NULL;
1317 + if (parse_commit((struct commit *)object))
1318 + return NULL;
1319 + return (struct commit *)object;
1320 +}
1321 +
1322 +int merge_recursive_generic(const char **base_list,
1323 + const unsigned char *head_sha1, const char *head_name,
1324 + const unsigned char *next_sha1, const char *next_name)
1325 +{
1326 + int clean, index_fd;
1327 + struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1328 + struct commit *result;
1329 + struct commit *head_commit = get_ref(head_sha1, head_name);
1330 + struct commit *next_commit = get_ref(next_sha1, next_name);
1331 + struct commit_list *ca = NULL;
1332 +
1333 + if (base_list) {
1334 + int i;
1335 + for (i = 0; base_list[i]; ++i) {
1336 + unsigned char sha[20];
1337 + struct commit *base;
1338 + if (get_sha1(base_list[i], sha))
1339 + return error("Could not resolve ref '%s'",
1340 + base_list[i]);
1341 + if (!(base = get_ref(sha, base_list[i])))
1342 + return error("Could not parse object '%s'",
1343 + base_list[i]);
1344 + commit_list_insert(base, &ca);
1345 + }
1346 + }
1347 +
1348 + index_fd = hold_locked_index(lock, 1);
1349 + clean = merge_recursive(head_commit, next_commit,
1350 + head_name, next_name, ca, &result);
1351 + if (active_cache_changed &&
1352 + (write_cache(index_fd, active_cache, active_nr) ||
1353 + commit_locked_index(lock)))
1354 + return error("Unable to write index.");
1355 +
1356 + return clean ? 0 : 1;
1357 +}
1358 +
1359 int merge_recursive_config(const char *var, const char *value, void *cb)
1360 {
1361 if (!strcasecmp(var, "merge.verbosity")) {
merge-recursive.h
+3 -1
@@ -14,7 +14,9 @@ int merge_trees(struct tree *head,
14 const char *branch1,
15 const char *branch2,
16 struct tree **result);
17 -struct commit *make_virtual_commit(struct tree *tree, const char *comment);
17 +extern int merge_recursive_generic(const char **base_list,
18 + const unsigned char *head_sha1, const char *head_name,
19 + const unsigned char *next_sha1, const char *next_name);
20 int merge_recursive_config(const char *var, const char *value, void *cb);
21 void merge_recursive_setup(int is_subtree_merge);
22 struct tree *write_tree_from_memory(void);