4 static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
5 static int resolve_directories = 1;
7 static void merge_trees(struct tree_desc t[3], const char *base);
9 static void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
11 unsigned long size = 0;
15 buf = read_object_with_reference(sha1, "tree", &size, NULL);
17 die("unable to read tree %s", sha1_to_hex(sha1));
25 const unsigned char *sha1;
31 static void entry_clear(struct name_entry *a)
33 memset(a, 0, sizeof(*a));
36 static int entry_compare(struct name_entry *a, struct name_entry *b)
38 return base_name_compare(
39 a->path, a->pathlen, a->mode,
40 b->path, b->pathlen, b->mode);
43 static void entry_extract(struct tree_desc *t, struct name_entry *a)
45 a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
46 a->pathlen = strlen(a->path);
49 /* An empty entry never compares same, not even to another empty entry */
50 static int same_entry(struct name_entry *a, struct name_entry *b)
54 !memcmp(a->sha1, b->sha1, 20) &&
58 static const char *sha1_to_hex_zero(const unsigned char *sha1)
61 return sha1_to_hex(sha1);
62 return "0000000000000000000000000000000000000000";
65 static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
67 char branch1_sha1[50];
69 /* If it's already branch1, don't bother showing it */
72 memcpy(branch1_sha1, sha1_to_hex_zero(branch1->sha1), 41);
74 printf("0 %06o->%06o %s->%s %s%s\n",
75 branch1->mode, result->mode,
76 branch1_sha1, sha1_to_hex_zero(result->sha1),
80 static int unresolved_directory(const char *base, struct name_entry n[3])
85 struct tree_desc t[3];
86 void *buf0, *buf1, *buf2;
88 if (!resolve_directories)
96 if (!S_ISDIR(p->mode))
98 baselen = strlen(base);
99 newbase = xmalloc(baselen + p->pathlen + 2);
100 memcpy(newbase, base, baselen);
101 memcpy(newbase + baselen, p->path, p->pathlen);
102 memcpy(newbase + baselen + p->pathlen, "/", 2);
104 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
105 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
106 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
107 merge_trees(t, newbase);
116 static void unresolved(const char *base, struct name_entry n[3])
118 if (unresolved_directory(base, n))
121 printf("1 %06o %s %s%s\n", n[0].mode, sha1_to_hex(n[0].sha1), base, n[0].path);
123 printf("2 %06o %s %s%s\n", n[1].mode, sha1_to_hex(n[1].sha1), base, n[1].path);
125 printf("3 %06o %s %s%s\n", n[2].mode, sha1_to_hex(n[2].sha1), base, n[2].path);
128 typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
130 static void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
132 struct name_entry *entry = xmalloc(n*sizeof(*entry));
135 struct name_entry entry[3];
136 unsigned long mask = 0;
140 for (i = 0; i < n; i++) {
143 entry_extract(t+i, entry+i);
145 int cmp = entry_compare(entry+i, entry+last);
148 * Is the new name bigger than the old one?
154 * Is the new name smaller than the old one?
155 * Ignore all old ones
167 * Update the tree entries we've walked, and clear
168 * all the unused name-entries.
170 for (i = 0; i < n; i++) {
171 if (mask & (1ul << i)) {
172 update_tree_entry(t+i);
175 entry_clear(entry + i);
177 callback(n, mask, entry, base);
183 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
186 * This walks the (sorted) trees in lock-step, checking every possible
187 * name. Note that directories automatically sort differently from other
188 * files (see "base_name_compare"), so you'll never see file/directory
189 * conflicts, because they won't ever compare the same.
191 * IOW, if a directory changes to a filename, it will automatically be
192 * seen as the directory going away, and the filename being created.
194 * Think of this as a three-way diff.
196 * The output will be either:
198 * "0 mode sha1 filename"
199 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
200 * in parallel with this too!
203 * "1 mode sha1 filename"
204 * "2 mode sha1 filename"
205 * "3 mode sha1 filename"
206 * where not all of the 1/2/3 lines may exist, of course.
208 * The successful merge rules are the same as for the three-way merge
211 static void threeway_callback(int n, unsigned long mask, struct name_entry *entry, const char *base)
214 if (same_entry(entry+1, entry+2)) {
216 resolve(base, NULL, entry+1);
221 if (same_entry(entry+0, entry+1)) {
222 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
223 resolve(base, entry+1, entry+2);
228 if (same_entry(entry+0, entry+2)) {
229 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
230 resolve(base, NULL, entry+1);
235 unresolved(base, entry);
238 static void merge_trees(struct tree_desc t[3], const char *base)
240 traverse_trees(3, t, base, threeway_callback);
243 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
245 unsigned char sha1[20];
248 if (get_sha1(rev, sha1) < 0)
249 die("unknown rev %s", rev);
250 buf = fill_tree_descriptor(desc, sha1);
252 die("%s is not a tree", rev);
256 int main(int argc, char **argv)
258 struct tree_desc t[3];
259 void *buf1, *buf2, *buf3;
262 usage(merge_tree_usage);
264 buf1 = get_tree_descriptor(t+0, argv[1]);
265 buf2 = get_tree_descriptor(t+1, argv[2]);
266 buf3 = get_tree_descriptor(t+2, argv[3]);