Support bash completion of refs/remote.
[git/gitweb-caching.git] / merge-tree.c
blob692ede0e3db7df66b2e5943dc58224f34c59fd8c
1 #include "cache.h"
2 #include "tree-walk.h"
3 #include "xdiff-interface.h"
4 #include "blob.h"
6 static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
7 static int resolve_directories = 1;
9 struct merge_list {
10 struct merge_list *next;
11 struct merge_list *link; /* other stages for this object */
13 unsigned int stage : 2,
14 flags : 30;
15 unsigned int mode;
16 const char *path;
17 struct blob *blob;
20 static struct merge_list *merge_result, **merge_result_end = &merge_result;
22 static void add_merge_entry(struct merge_list *entry)
24 *merge_result_end = entry;
25 merge_result_end = &entry->next;
28 static void merge_trees(struct tree_desc t[3], const char *base);
30 static const char *explanation(struct merge_list *entry)
32 switch (entry->stage) {
33 case 0:
34 return "merged";
35 case 3:
36 return "added in remote";
37 case 2:
38 if (entry->link)
39 return "added in both";
40 return "added in local";
43 /* Existed in base */
44 entry = entry->link;
45 if (!entry)
46 return "removed in both";
48 if (entry->link)
49 return "changed in both";
51 if (entry->stage == 3)
52 return "removed in local";
53 return "removed in remote";
56 extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned long *);
58 static void *result(struct merge_list *entry, unsigned long *size)
60 char type[20];
61 struct blob *base, *our, *their;
63 if (!entry->stage)
64 return read_sha1_file(entry->blob->object.sha1, type, size);
65 base = NULL;
66 if (entry->stage == 1) {
67 base = entry->blob;
68 entry = entry->link;
70 our = NULL;
71 if (entry && entry->stage == 2) {
72 our = entry->blob;
73 entry = entry->link;
75 their = NULL;
76 if (entry)
77 their = entry->blob;
78 return merge_file(base, our, their, size);
81 static void *origin(struct merge_list *entry, unsigned long *size)
83 char type[20];
84 while (entry) {
85 if (entry->stage == 2)
86 return read_sha1_file(entry->blob->object.sha1, type, size);
87 entry = entry->link;
89 return NULL;
92 static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
94 int i;
95 for (i = 0; i < nbuf; i++)
96 printf("%.*s", (int) mb[i].size, mb[i].ptr);
97 return 0;
100 static void show_diff(struct merge_list *entry)
102 unsigned long size;
103 mmfile_t src, dst;
104 xpparam_t xpp;
105 xdemitconf_t xecfg;
106 xdemitcb_t ecb;
108 xpp.flags = XDF_NEED_MINIMAL;
109 xecfg.ctxlen = 3;
110 xecfg.flags = 0;
111 ecb.outf = show_outf;
112 ecb.priv = NULL;
114 src.ptr = origin(entry, &size);
115 if (!src.ptr)
116 size = 0;
117 src.size = size;
118 dst.ptr = result(entry, &size);
119 if (!dst.ptr)
120 size = 0;
121 dst.size = size;
122 xdl_diff(&src, &dst, &xpp, &xecfg, &ecb);
123 free(src.ptr);
124 free(dst.ptr);
127 static void show_result_list(struct merge_list *entry)
129 printf("%s\n", explanation(entry));
130 do {
131 struct merge_list *link = entry->link;
132 static const char *desc[4] = { "result", "base", "our", "their" };
133 printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path);
134 entry = link;
135 } while (entry);
138 static void show_result(void)
140 struct merge_list *walk;
142 walk = merge_result;
143 while (walk) {
144 show_result_list(walk);
145 show_diff(walk);
146 walk = walk->next;
150 /* An empty entry never compares same, not even to another empty entry */
151 static int same_entry(struct name_entry *a, struct name_entry *b)
153 return a->sha1 &&
154 b->sha1 &&
155 !hashcmp(a->sha1, b->sha1) &&
156 a->mode == b->mode;
159 static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
161 struct merge_list *res = xmalloc(sizeof(*res));
163 memset(res, 0, sizeof(*res));
164 res->stage = stage;
165 res->path = path;
166 res->mode = mode;
167 res->blob = lookup_blob(sha1);
168 return res;
171 static void resolve(const char *base, struct name_entry *branch1, struct name_entry *result)
173 struct merge_list *orig, *final;
174 const char *path;
176 /* If it's already branch1, don't bother showing it */
177 if (!branch1)
178 return;
180 path = xstrdup(mkpath("%s%s", base, result->path));
181 orig = create_entry(2, branch1->mode, branch1->sha1, path);
182 final = create_entry(0, result->mode, result->sha1, path);
184 final->link = orig;
186 add_merge_entry(final);
189 static int unresolved_directory(const char *base, struct name_entry n[3])
191 int baselen;
192 char *newbase;
193 struct name_entry *p;
194 struct tree_desc t[3];
195 void *buf0, *buf1, *buf2;
197 if (!resolve_directories)
198 return 0;
199 p = n;
200 if (!p->mode) {
201 p++;
202 if (!p->mode)
203 p++;
205 if (!S_ISDIR(p->mode))
206 return 0;
207 baselen = strlen(base);
208 newbase = xmalloc(baselen + p->pathlen + 2);
209 memcpy(newbase, base, baselen);
210 memcpy(newbase + baselen, p->path, p->pathlen);
211 memcpy(newbase + baselen + p->pathlen, "/", 2);
213 buf0 = fill_tree_descriptor(t+0, n[0].sha1);
214 buf1 = fill_tree_descriptor(t+1, n[1].sha1);
215 buf2 = fill_tree_descriptor(t+2, n[2].sha1);
216 merge_trees(t, newbase);
218 free(buf0);
219 free(buf1);
220 free(buf2);
221 free(newbase);
222 return 1;
226 static struct merge_list *link_entry(unsigned stage, const char *base, struct name_entry *n, struct merge_list *entry)
228 const char *path;
229 struct merge_list *link;
231 if (!n->mode)
232 return entry;
233 if (entry)
234 path = entry->path;
235 else
236 path = xstrdup(mkpath("%s%s", base, n->path));
237 link = create_entry(stage, n->mode, n->sha1, path);
238 link->link = entry;
239 return link;
242 static void unresolved(const char *base, struct name_entry n[3])
244 struct merge_list *entry = NULL;
246 if (unresolved_directory(base, n))
247 return;
250 * Do them in reverse order so that the resulting link
251 * list has the stages in order - link_entry adds new
252 * links at the front.
254 entry = link_entry(3, base, n + 2, entry);
255 entry = link_entry(2, base, n + 1, entry);
256 entry = link_entry(1, base, n + 0, entry);
258 add_merge_entry(entry);
262 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
263 * as the origin.
265 * This walks the (sorted) trees in lock-step, checking every possible
266 * name. Note that directories automatically sort differently from other
267 * files (see "base_name_compare"), so you'll never see file/directory
268 * conflicts, because they won't ever compare the same.
270 * IOW, if a directory changes to a filename, it will automatically be
271 * seen as the directory going away, and the filename being created.
273 * Think of this as a three-way diff.
275 * The output will be either:
276 * - successful merge
277 * "0 mode sha1 filename"
278 * NOTE NOTE NOTE! FIXME! We really really need to walk the index
279 * in parallel with this too!
281 * - conflict:
282 * "1 mode sha1 filename"
283 * "2 mode sha1 filename"
284 * "3 mode sha1 filename"
285 * where not all of the 1/2/3 lines may exist, of course.
287 * The successful merge rules are the same as for the three-way merge
288 * in git-read-tree.
290 static void threeway_callback(int n, unsigned long mask, struct name_entry *entry, const char *base)
292 /* Same in both? */
293 if (same_entry(entry+1, entry+2)) {
294 if (entry[0].sha1) {
295 resolve(base, NULL, entry+1);
296 return;
300 if (same_entry(entry+0, entry+1)) {
301 if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
302 resolve(base, entry+1, entry+2);
303 return;
307 if (same_entry(entry+0, entry+2)) {
308 if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
309 resolve(base, NULL, entry+1);
310 return;
314 unresolved(base, entry);
317 static void merge_trees(struct tree_desc t[3], const char *base)
319 traverse_trees(3, t, base, threeway_callback);
322 static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
324 unsigned char sha1[20];
325 void *buf;
327 if (get_sha1(rev, sha1))
328 die("unknown rev %s", rev);
329 buf = fill_tree_descriptor(desc, sha1);
330 if (!buf)
331 die("%s is not a tree", rev);
332 return buf;
335 int main(int argc, char **argv)
337 struct tree_desc t[3];
338 void *buf1, *buf2, *buf3;
340 if (argc != 4)
341 usage(merge_tree_usage);
343 setup_git_directory();
345 buf1 = get_tree_descriptor(t+0, argv[1]);
346 buf2 = get_tree_descriptor(t+1, argv[2]);
347 buf3 = get_tree_descriptor(t+2, argv[3]);
348 merge_trees(t, "");
349 free(buf1);
350 free(buf2);
351 free(buf3);
353 show_result();
354 return 0;