]>
Commit | Line | Data |
---|---|---|
6d297f81 JS |
1 | /* |
2 | * Recursive Merge algorithm stolen from git-merge-recursive.py by | |
3 | * Fredrik Kuivinen. | |
4 | * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006 | |
5 | */ | |
6d297f81 JS |
6 | #include "cache.h" |
7 | #include "cache-tree.h" | |
8 | #include "commit.h" | |
9 | #include "blob.h" | |
10 | #include "tree-walk.h" | |
11 | #include "diff.h" | |
12 | #include "diffcore.h" | |
13 | #include "run-command.h" | |
14 | #include "tag.h" | |
7a85b848 | 15 | #include "unpack-trees.h" |
6d297f81 | 16 | #include "path-list.h" |
c2b4faea | 17 | #include "xdiff-interface.h" |
f3ef6b6b | 18 | #include "interpolate.h" |
a129d96f | 19 | #include "attr.h" |
6d297f81 | 20 | |
68faf689 JH |
21 | static int subtree_merge; |
22 | ||
23 | static struct tree *shift_tree_object(struct tree *one, struct tree *two) | |
24 | { | |
25 | unsigned char shifted[20]; | |
26 | ||
27 | /* | |
28 | * NEEDSWORK: this limits the recursion depth to hardcoded | |
29 | * value '2' to avoid excessive overhead. | |
30 | */ | |
31 | shift_tree(one->object.sha1, two->object.sha1, shifted, 2); | |
32 | if (!hashcmp(two->object.sha1, shifted)) | |
33 | return two; | |
34 | return lookup_tree(shifted); | |
35 | } | |
36 | ||
6d297f81 JS |
37 | /* |
38 | * A virtual commit has | |
39 | * - (const char *)commit->util set to the name, and | |
40 | * - *(int *)commit->object.sha1 set to the virtual id. | |
41 | */ | |
6d297f81 JS |
42 | |
43 | static unsigned commit_list_count(const struct commit_list *l) | |
44 | { | |
45 | unsigned c = 0; | |
46 | for (; l; l = l->next ) | |
47 | c++; | |
48 | return c; | |
49 | } | |
50 | ||
51 | static struct commit *make_virtual_commit(struct tree *tree, const char *comment) | |
52 | { | |
53 | struct commit *commit = xcalloc(1, sizeof(struct commit)); | |
54 | static unsigned virtual_id = 1; | |
55 | commit->tree = tree; | |
56 | commit->util = (void*)comment; | |
57 | *(int*)commit->object.sha1 = virtual_id++; | |
c1f3089e JS |
58 | /* avoid warnings */ |
59 | commit->object.parsed = 1; | |
6d297f81 JS |
60 | return commit; |
61 | } | |
62 | ||
63 | /* | |
3058e933 JS |
64 | * Since we use get_tree_entry(), which does not put the read object into |
65 | * the object pool, we cannot rely on a == b. | |
6d297f81 JS |
66 | */ |
67 | static int sha_eq(const unsigned char *a, const unsigned char *b) | |
68 | { | |
3af244ca | 69 | if (!a && !b) |
6d297f81 | 70 | return 2; |
87cb004e | 71 | return a && b && hashcmp(a, b) == 0; |
6d297f81 JS |
72 | } |
73 | ||
6d297f81 | 74 | /* |
3058e933 JS |
75 | * Since we want to write the index eventually, we cannot reuse the index |
76 | * for these (temporary) data. | |
6d297f81 JS |
77 | */ |
78 | struct stage_data | |
79 | { | |
80 | struct | |
81 | { | |
82 | unsigned mode; | |
83 | unsigned char sha[20]; | |
84 | } stages[4]; | |
85 | unsigned processed:1; | |
86 | }; | |
87 | ||
66a155bc SP |
88 | struct output_buffer |
89 | { | |
90 | struct output_buffer *next; | |
91 | char *str; | |
92 | }; | |
93 | ||
5a753613 JS |
94 | static struct path_list current_file_set = {NULL, 0, 0, 1}; |
95 | static struct path_list current_directory_set = {NULL, 0, 0, 1}; | |
6d297f81 | 96 | |
63889639 | 97 | static int call_depth = 0; |
8c3275ab | 98 | static int verbosity = 2; |
66a155bc SP |
99 | static int buffer_output = 1; |
100 | static struct output_buffer *output_list, *output_end; | |
6d297f81 | 101 | |
8c3275ab SP |
102 | static int show (int v) |
103 | { | |
104 | return (!call_depth && verbosity >= v) || verbosity >= 5; | |
105 | } | |
106 | ||
107 | static void output(int v, const char *fmt, ...) | |
6d297f81 JS |
108 | { |
109 | va_list args; | |
6d297f81 | 110 | va_start(args, fmt); |
66a155bc SP |
111 | if (buffer_output && show(v)) { |
112 | struct output_buffer *b = xmalloc(sizeof(*b)); | |
113 | nfvasprintf(&b->str, fmt, args); | |
114 | b->next = NULL; | |
115 | if (output_end) | |
116 | output_end->next = b; | |
117 | else | |
118 | output_list = b; | |
119 | output_end = b; | |
120 | } else if (show(v)) { | |
8c3275ab SP |
121 | int i; |
122 | for (i = call_depth; i--;) | |
123 | fputs(" ", stdout); | |
124 | vfprintf(stdout, fmt, args); | |
125 | fputc('\n', stdout); | |
126 | } | |
6d297f81 | 127 | va_end(args); |
6d297f81 JS |
128 | } |
129 | ||
66a155bc SP |
130 | static void flush_output() |
131 | { | |
132 | struct output_buffer *b, *n; | |
133 | for (b = output_list; b; b = n) { | |
134 | int i; | |
135 | for (i = call_depth; i--;) | |
136 | fputs(" ", stdout); | |
137 | fputs(b->str, stdout); | |
138 | fputc('\n', stdout); | |
139 | n = b->next; | |
140 | free(b->str); | |
141 | free(b); | |
142 | } | |
143 | output_list = NULL; | |
144 | output_end = NULL; | |
145 | } | |
146 | ||
3af244ca JS |
147 | static void output_commit_title(struct commit *commit) |
148 | { | |
149 | int i; | |
66a155bc | 150 | flush_output(); |
63889639 | 151 | for (i = call_depth; i--;) |
3af244ca JS |
152 | fputs(" ", stdout); |
153 | if (commit->util) | |
154 | printf("virtual %s\n", (char *)commit->util); | |
155 | else { | |
9926ba98 | 156 | printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); |
3af244ca JS |
157 | if (parse_commit(commit) != 0) |
158 | printf("(bad commit)\n"); | |
159 | else { | |
160 | const char *s; | |
161 | int len; | |
162 | for (s = commit->buffer; *s; s++) | |
163 | if (*s == '\n' && s[1] == '\n') { | |
164 | s += 2; | |
165 | break; | |
166 | } | |
167 | for (len = 0; s[len] && '\n' != s[len]; len++) | |
168 | ; /* do nothing */ | |
169 | printf("%.*s\n", len, s); | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
6d297f81 JS |
174 | static struct cache_entry *make_cache_entry(unsigned int mode, |
175 | const unsigned char *sha1, const char *path, int stage, int refresh) | |
176 | { | |
177 | int size, len; | |
178 | struct cache_entry *ce; | |
179 | ||
180 | if (!verify_path(path)) | |
181 | return NULL; | |
182 | ||
183 | len = strlen(path); | |
184 | size = cache_entry_size(len); | |
185 | ce = xcalloc(1, size); | |
186 | ||
8da71493 | 187 | hashcpy(ce->sha1, sha1); |
6d297f81 JS |
188 | memcpy(ce->name, path, len); |
189 | ce->ce_flags = create_ce_flags(len, stage); | |
190 | ce->ce_mode = create_ce_mode(mode); | |
191 | ||
192 | if (refresh) | |
193 | return refresh_cache_entry(ce, 0); | |
194 | ||
195 | return ce; | |
196 | } | |
197 | ||
198 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, | |
199 | const char *path, int stage, int refresh, int options) | |
200 | { | |
201 | struct cache_entry *ce; | |
6d297f81 JS |
202 | ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh); |
203 | if (!ce) | |
0424138d | 204 | return error("addinfo_cache failed for path '%s'", path); |
6d297f81 JS |
205 | return add_cache_entry(ce, options); |
206 | } | |
207 | ||
208 | /* | |
209 | * This is a global variable which is used in a number of places but | |
210 | * only written to in the 'merge' function. | |
211 | * | |
212 | * index_only == 1 => Don't leave any non-stage 0 entries in the cache and | |
213 | * don't update the working directory. | |
214 | * 0 => Leave unmerged entries in the cache and update | |
215 | * the working directory. | |
216 | */ | |
217 | static int index_only = 0; | |
218 | ||
7a85b848 | 219 | static int git_merge_trees(int index_only, |
6d297f81 JS |
220 | struct tree *common, |
221 | struct tree *head, | |
222 | struct tree *merge) | |
223 | { | |
3af244ca | 224 | int rc; |
7a85b848 JS |
225 | struct object_list *trees = NULL; |
226 | struct unpack_trees_options opts; | |
227 | ||
7a85b848 JS |
228 | memset(&opts, 0, sizeof(opts)); |
229 | if (index_only) | |
230 | opts.index_only = 1; | |
231 | else | |
232 | opts.update = 1; | |
233 | opts.merge = 1; | |
234 | opts.head_idx = 2; | |
235 | opts.fn = threeway_merge; | |
236 | ||
237 | object_list_append(&common->object, &trees); | |
238 | object_list_append(&head->object, &trees); | |
239 | object_list_append(&merge->object, &trees); | |
240 | ||
241 | rc = unpack_trees(trees, &opts); | |
242 | cache_tree_free(&active_cache_tree); | |
7a85b848 | 243 | return rc; |
6d297f81 JS |
244 | } |
245 | ||
8b944b56 JH |
246 | static int unmerged_index(void) |
247 | { | |
248 | int i; | |
249 | for (i = 0; i < active_nr; i++) { | |
250 | struct cache_entry *ce = active_cache[i]; | |
251 | if (ce_stage(ce)) | |
252 | return 1; | |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
3af244ca | 257 | static struct tree *git_write_tree(void) |
6d297f81 | 258 | { |
5b982f84 JS |
259 | struct tree *result = NULL; |
260 | ||
a97e4075 AR |
261 | if (unmerged_index()) { |
262 | int i; | |
263 | output(0, "There are unmerged index entries:"); | |
264 | for (i = 0; i < active_nr; i++) { | |
265 | struct cache_entry *ce = active_cache[i]; | |
266 | if (ce_stage(ce)) | |
267 | output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); | |
268 | } | |
8b944b56 | 269 | return NULL; |
a97e4075 | 270 | } |
5b982f84 JS |
271 | |
272 | if (!active_cache_tree) | |
273 | active_cache_tree = cache_tree(); | |
274 | ||
275 | if (!cache_tree_fully_valid(active_cache_tree) && | |
8b944b56 JH |
276 | cache_tree_update(active_cache_tree, |
277 | active_cache, active_nr, 0, 0) < 0) | |
5b982f84 JS |
278 | die("error building trees"); |
279 | ||
280 | result = lookup_tree(active_cache_tree->sha1); | |
281 | ||
5b982f84 | 282 | return result; |
6d297f81 JS |
283 | } |
284 | ||
6d297f81 JS |
285 | static int save_files_dirs(const unsigned char *sha1, |
286 | const char *base, int baselen, const char *path, | |
287 | unsigned int mode, int stage) | |
288 | { | |
289 | int len = strlen(path); | |
2d7320d0 | 290 | char *newpath = xmalloc(baselen + len + 1); |
6d297f81 JS |
291 | memcpy(newpath, base, baselen); |
292 | memcpy(newpath + baselen, path, len); | |
293 | newpath[baselen + len] = '\0'; | |
294 | ||
295 | if (S_ISDIR(mode)) | |
5a753613 | 296 | path_list_insert(newpath, ¤t_directory_set); |
6d297f81 | 297 | else |
5a753613 | 298 | path_list_insert(newpath, ¤t_file_set); |
6d297f81 JS |
299 | free(newpath); |
300 | ||
301 | return READ_TREE_RECURSIVE; | |
302 | } | |
303 | ||
3af244ca | 304 | static int get_files_dirs(struct tree *tree) |
6d297f81 JS |
305 | { |
306 | int n; | |
bd669986 | 307 | if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) |
6d297f81 | 308 | return 0; |
5a753613 | 309 | n = current_file_set.nr + current_directory_set.nr; |
6d297f81 JS |
310 | return n; |
311 | } | |
312 | ||
6d297f81 JS |
313 | /* |
314 | * Returns a index_entry instance which doesn't have to correspond to | |
315 | * a real cache entry in Git's index. | |
316 | */ | |
3af244ca JS |
317 | static struct stage_data *insert_stage_data(const char *path, |
318 | struct tree *o, struct tree *a, struct tree *b, | |
319 | struct path_list *entries) | |
6d297f81 | 320 | { |
3af244ca | 321 | struct path_list_item *item; |
6d297f81 JS |
322 | struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); |
323 | get_tree_entry(o->object.sha1, path, | |
324 | e->stages[1].sha, &e->stages[1].mode); | |
325 | get_tree_entry(a->object.sha1, path, | |
326 | e->stages[2].sha, &e->stages[2].mode); | |
327 | get_tree_entry(b->object.sha1, path, | |
328 | e->stages[3].sha, &e->stages[3].mode); | |
3af244ca JS |
329 | item = path_list_insert(path, entries); |
330 | item->util = e; | |
6d297f81 JS |
331 | return e; |
332 | } | |
333 | ||
6d297f81 | 334 | /* |
5a753613 | 335 | * Create a dictionary mapping file names to stage_data objects. The |
6d297f81 JS |
336 | * dictionary contains one entry for every path with a non-zero stage entry. |
337 | */ | |
3af244ca | 338 | static struct path_list *get_unmerged(void) |
6d297f81 JS |
339 | { |
340 | struct path_list *unmerged = xcalloc(1, sizeof(struct path_list)); | |
341 | int i; | |
342 | ||
343 | unmerged->strdup_paths = 1; | |
8b944b56 | 344 | |
ad57cbca | 345 | for (i = 0; i < active_nr; i++) { |
3af244ca JS |
346 | struct path_list_item *item; |
347 | struct stage_data *e; | |
6d297f81 JS |
348 | struct cache_entry *ce = active_cache[i]; |
349 | if (!ce_stage(ce)) | |
350 | continue; | |
351 | ||
3af244ca JS |
352 | item = path_list_lookup(ce->name, unmerged); |
353 | if (!item) { | |
354 | item = path_list_insert(ce->name, unmerged); | |
355 | item->util = xcalloc(1, sizeof(struct stage_data)); | |
356 | } | |
357 | e = item->util; | |
6d297f81 | 358 | e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode); |
8da71493 | 359 | hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); |
6d297f81 JS |
360 | } |
361 | ||
6d297f81 JS |
362 | return unmerged; |
363 | } | |
364 | ||
365 | struct rename | |
366 | { | |
367 | struct diff_filepair *pair; | |
368 | struct stage_data *src_entry; | |
369 | struct stage_data *dst_entry; | |
370 | unsigned processed:1; | |
371 | }; | |
372 | ||
6d297f81 | 373 | /* |
3dff5379 | 374 | * Get information of all renames which occurred between 'o_tree' and |
5a753613 JS |
375 | * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and |
376 | * 'b_tree') to be able to associate the correct cache entries with | |
377 | * the rename information. 'tree' is always equal to either a_tree or b_tree. | |
6d297f81 JS |
378 | */ |
379 | static struct path_list *get_renames(struct tree *tree, | |
5a753613 JS |
380 | struct tree *o_tree, |
381 | struct tree *a_tree, | |
382 | struct tree *b_tree, | |
6d297f81 JS |
383 | struct path_list *entries) |
384 | { | |
3af244ca JS |
385 | int i; |
386 | struct path_list *renames; | |
387 | struct diff_options opts; | |
3af244ca JS |
388 | |
389 | renames = xcalloc(1, sizeof(struct path_list)); | |
6d297f81 JS |
390 | diff_setup(&opts); |
391 | opts.recursive = 1; | |
392 | opts.detect_rename = DIFF_DETECT_RENAME; | |
393 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
394 | if (diff_setup_done(&opts) < 0) | |
395 | die("diff setup failed"); | |
5a753613 | 396 | diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts); |
6d297f81 JS |
397 | diffcore_std(&opts); |
398 | for (i = 0; i < diff_queued_diff.nr; ++i) { | |
3af244ca | 399 | struct path_list_item *item; |
6d297f81 JS |
400 | struct rename *re; |
401 | struct diff_filepair *pair = diff_queued_diff.queue[i]; | |
402 | if (pair->status != 'R') { | |
403 | diff_free_filepair(pair); | |
404 | continue; | |
405 | } | |
406 | re = xmalloc(sizeof(*re)); | |
407 | re->processed = 0; | |
408 | re->pair = pair; | |
3af244ca JS |
409 | item = path_list_lookup(re->pair->one->path, entries); |
410 | if (!item) | |
411 | re->src_entry = insert_stage_data(re->pair->one->path, | |
5a753613 | 412 | o_tree, a_tree, b_tree, entries); |
3af244ca JS |
413 | else |
414 | re->src_entry = item->util; | |
415 | ||
416 | item = path_list_lookup(re->pair->two->path, entries); | |
417 | if (!item) | |
418 | re->dst_entry = insert_stage_data(re->pair->two->path, | |
5a753613 | 419 | o_tree, a_tree, b_tree, entries); |
3af244ca JS |
420 | else |
421 | re->dst_entry = item->util; | |
422 | item = path_list_insert(pair->one->path, renames); | |
6d297f81 JS |
423 | item->util = re; |
424 | } | |
425 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
426 | diff_queued_diff.nr = 0; | |
427 | diff_flush(&opts); | |
6d297f81 JS |
428 | return renames; |
429 | } | |
430 | ||
9fe0d87d JH |
431 | static int update_stages(const char *path, struct diff_filespec *o, |
432 | struct diff_filespec *a, struct diff_filespec *b, | |
433 | int clear) | |
6d297f81 JS |
434 | { |
435 | int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; | |
3af244ca JS |
436 | if (clear) |
437 | if (remove_file_from_cache(path)) | |
6d297f81 | 438 | return -1; |
3af244ca JS |
439 | if (o) |
440 | if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options)) | |
6d297f81 | 441 | return -1; |
3af244ca JS |
442 | if (a) |
443 | if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options)) | |
6d297f81 | 444 | return -1; |
3af244ca JS |
445 | if (b) |
446 | if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options)) | |
6d297f81 JS |
447 | return -1; |
448 | return 0; | |
449 | } | |
450 | ||
6d297f81 JS |
451 | static int remove_path(const char *name) |
452 | { | |
3af244ca JS |
453 | int ret, len; |
454 | char *slash, *dirs; | |
6d297f81 JS |
455 | |
456 | ret = unlink(name); | |
3af244ca | 457 | if (ret) |
6d297f81 | 458 | return ret; |
3af244ca | 459 | len = strlen(name); |
2d7320d0 | 460 | dirs = xmalloc(len+1); |
6d297f81 JS |
461 | memcpy(dirs, name, len); |
462 | dirs[len] = '\0'; | |
3af244ca | 463 | while ((slash = strrchr(name, '/'))) { |
6d297f81 JS |
464 | *slash = '\0'; |
465 | len = slash - name; | |
3af244ca | 466 | if (rmdir(name) != 0) |
6d297f81 JS |
467 | break; |
468 | } | |
469 | free(dirs); | |
470 | return ret; | |
471 | } | |
472 | ||
65ac6e9c | 473 | static int remove_file(int clean, const char *path, int no_wd) |
6d297f81 | 474 | { |
5a753613 | 475 | int update_cache = index_only || clean; |
65ac6e9c | 476 | int update_working_directory = !index_only && !no_wd; |
6d297f81 | 477 | |
5a753613 | 478 | if (update_cache) { |
6d297f81 JS |
479 | if (remove_file_from_cache(path)) |
480 | return -1; | |
481 | } | |
65ac6e9c | 482 | if (update_working_directory) { |
6d297f81 | 483 | unlink(path); |
3af244ca | 484 | if (errno != ENOENT || errno != EISDIR) |
6d297f81 JS |
485 | return -1; |
486 | remove_path(path); | |
487 | } | |
488 | return 0; | |
489 | } | |
490 | ||
491 | static char *unique_path(const char *path, const char *branch) | |
492 | { | |
493 | char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); | |
3af244ca JS |
494 | int suffix = 0; |
495 | struct stat st; | |
f59aac47 | 496 | char *p = newpath + strlen(path); |
6d297f81 | 497 | strcpy(newpath, path); |
f59aac47 | 498 | *(p++) = '~'; |
6d297f81 | 499 | strcpy(p, branch); |
3af244ca JS |
500 | for (; *p; ++p) |
501 | if ('/' == *p) | |
6d297f81 | 502 | *p = '_'; |
5a753613 JS |
503 | while (path_list_has_path(¤t_file_set, newpath) || |
504 | path_list_has_path(¤t_directory_set, newpath) || | |
3af244ca | 505 | lstat(newpath, &st) == 0) |
6d297f81 | 506 | sprintf(p, "_%d", suffix++); |
3af244ca | 507 | |
5a753613 | 508 | path_list_insert(newpath, ¤t_file_set); |
6d297f81 JS |
509 | return newpath; |
510 | } | |
511 | ||
3af244ca | 512 | static int mkdir_p(const char *path, unsigned long mode) |
6d297f81 | 513 | { |
9befac47 SP |
514 | /* path points to cache entries, so xstrdup before messing with it */ |
515 | char *buf = xstrdup(path); | |
3af244ca | 516 | int result = safe_create_leading_directories(buf); |
6d297f81 | 517 | free(buf); |
3af244ca | 518 | return result; |
6d297f81 JS |
519 | } |
520 | ||
521 | static void flush_buffer(int fd, const char *buf, unsigned long size) | |
522 | { | |
523 | while (size > 0) { | |
93822c22 | 524 | long ret = write_in_full(fd, buf, size); |
6d297f81 JS |
525 | if (ret < 0) { |
526 | /* Ignore epipe */ | |
527 | if (errno == EPIPE) | |
528 | break; | |
529 | die("merge-recursive: %s", strerror(errno)); | |
530 | } else if (!ret) { | |
531 | die("merge-recursive: disk full?"); | |
532 | } | |
533 | size -= ret; | |
534 | buf += ret; | |
535 | } | |
536 | } | |
537 | ||
851c603e LT |
538 | static int make_room_for_path(const char *path) |
539 | { | |
540 | int status; | |
541 | const char *msg = "failed to create path '%s'%s"; | |
542 | ||
543 | status = mkdir_p(path, 0777); | |
544 | if (status) { | |
545 | if (status == -3) { | |
546 | /* something else exists */ | |
547 | error(msg, path, ": perhaps a D/F conflict?"); | |
548 | return -1; | |
549 | } | |
550 | die(msg, path, ""); | |
551 | } | |
552 | ||
553 | /* Successful unlink is good.. */ | |
554 | if (!unlink(path)) | |
555 | return 0; | |
556 | /* .. and so is no existing file */ | |
557 | if (errno == ENOENT) | |
558 | return 0; | |
559 | /* .. but not some other error (who really cares what?) */ | |
560 | return error(msg, path, ": perhaps a D/F conflict?"); | |
561 | } | |
562 | ||
9fe0d87d JH |
563 | static void update_file_flags(const unsigned char *sha, |
564 | unsigned mode, | |
565 | const char *path, | |
566 | int update_cache, | |
567 | int update_wd) | |
6d297f81 | 568 | { |
3af244ca JS |
569 | if (index_only) |
570 | update_wd = 0; | |
6d297f81 | 571 | |
3af244ca | 572 | if (update_wd) { |
21666f1a | 573 | enum object_type type; |
6d297f81 JS |
574 | void *buf; |
575 | unsigned long size; | |
576 | ||
21666f1a | 577 | buf = read_sha1_file(sha, &type, &size); |
6d297f81 JS |
578 | if (!buf) |
579 | die("cannot read object %s '%s'", sha1_to_hex(sha), path); | |
21666f1a | 580 | if (type != OBJ_BLOB) |
6d297f81 JS |
581 | die("blob expected for %s '%s'", sha1_to_hex(sha), path); |
582 | ||
851c603e LT |
583 | if (make_room_for_path(path) < 0) { |
584 | update_wd = 0; | |
585 | goto update_index; | |
586 | } | |
723024d6 | 587 | if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { |
3af244ca | 588 | int fd; |
3af244ca | 589 | if (mode & 0100) |
6d297f81 JS |
590 | mode = 0777; |
591 | else | |
592 | mode = 0666; | |
3af244ca JS |
593 | fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); |
594 | if (fd < 0) | |
6d297f81 JS |
595 | die("failed to open %s: %s", path, strerror(errno)); |
596 | flush_buffer(fd, buf, size); | |
597 | close(fd); | |
3af244ca | 598 | } else if (S_ISLNK(mode)) { |
2d7320d0 | 599 | char *lnk = xmalloc(size + 1); |
3af244ca JS |
600 | memcpy(lnk, buf, size); |
601 | lnk[size] = '\0'; | |
602 | mkdir_p(path, 0777); | |
17cd29b2 | 603 | unlink(path); |
3af244ca | 604 | symlink(lnk, path); |
723024d6 | 605 | free(lnk); |
6d297f81 JS |
606 | } else |
607 | die("do not know what to do with %06o %s '%s'", | |
608 | mode, sha1_to_hex(sha), path); | |
609 | } | |
4d50895a | 610 | update_index: |
3af244ca JS |
611 | if (update_cache) |
612 | add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); | |
6d297f81 JS |
613 | } |
614 | ||
9fe0d87d JH |
615 | static void update_file(int clean, |
616 | const unsigned char *sha, | |
617 | unsigned mode, | |
618 | const char *path) | |
6d297f81 JS |
619 | { |
620 | update_file_flags(sha, mode, path, index_only || clean, !index_only); | |
621 | } | |
622 | ||
623 | /* Low level file merging, update and removal */ | |
624 | ||
625 | struct merge_file_info | |
626 | { | |
627 | unsigned char sha[20]; | |
628 | unsigned mode; | |
629 | unsigned clean:1, | |
630 | merge:1; | |
631 | }; | |
632 | ||
c2b4faea | 633 | static void fill_mm(const unsigned char *sha1, mmfile_t *mm) |
6d297f81 | 634 | { |
6d297f81 | 635 | unsigned long size; |
21666f1a | 636 | enum object_type type; |
6d297f81 | 637 | |
f953831e JS |
638 | if (!hashcmp(sha1, null_sha1)) { |
639 | mm->ptr = xstrdup(""); | |
640 | mm->size = 0; | |
641 | return; | |
642 | } | |
6d297f81 | 643 | |
21666f1a NP |
644 | mm->ptr = read_sha1_file(sha1, &type, &size); |
645 | if (!mm->ptr || type != OBJ_BLOB) | |
6d297f81 | 646 | die("unable to read blob object %s", sha1_to_hex(sha1)); |
c2b4faea | 647 | mm->size = size; |
6d297f81 JS |
648 | } |
649 | ||
153920da JH |
650 | /* |
651 | * Customizable low-level merge drivers support. | |
652 | */ | |
653 | ||
654 | struct ll_merge_driver; | |
655 | typedef int (*ll_merge_fn)(const struct ll_merge_driver *, | |
656 | const char *path, | |
f3ef6b6b | 657 | mmfile_t *orig, |
a129d96f JH |
658 | mmfile_t *src1, const char *name1, |
659 | mmfile_t *src2, const char *name2, | |
660 | mmbuffer_t *result); | |
661 | ||
153920da JH |
662 | struct ll_merge_driver { |
663 | const char *name; | |
664 | const char *description; | |
665 | ll_merge_fn fn; | |
3086486d | 666 | const char *recursive; |
153920da JH |
667 | struct ll_merge_driver *next; |
668 | char *cmdline; | |
669 | }; | |
670 | ||
671 | /* | |
672 | * Built-in low-levels | |
673 | */ | |
674 | static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, | |
675 | const char *path_unused, | |
f3ef6b6b | 676 | mmfile_t *orig, |
a129d96f JH |
677 | mmfile_t *src1, const char *name1, |
678 | mmfile_t *src2, const char *name2, | |
679 | mmbuffer_t *result) | |
680 | { | |
681 | xpparam_t xpp; | |
682 | ||
683 | memset(&xpp, 0, sizeof(xpp)); | |
684 | return xdl_merge(orig, | |
685 | src1, name1, | |
686 | src2, name2, | |
687 | &xpp, XDL_MERGE_ZEALOUS, | |
688 | result); | |
689 | } | |
690 | ||
153920da JH |
691 | static int ll_union_merge(const struct ll_merge_driver *drv_unused, |
692 | const char *path_unused, | |
f3ef6b6b | 693 | mmfile_t *orig, |
a129d96f JH |
694 | mmfile_t *src1, const char *name1, |
695 | mmfile_t *src2, const char *name2, | |
696 | mmbuffer_t *result) | |
697 | { | |
698 | char *src, *dst; | |
699 | long size; | |
700 | const int marker_size = 7; | |
701 | ||
153920da JH |
702 | int status = ll_xdl_merge(drv_unused, path_unused, |
703 | orig, src1, NULL, src2, NULL, result); | |
a129d96f JH |
704 | if (status <= 0) |
705 | return status; | |
706 | size = result->size; | |
707 | src = dst = result->ptr; | |
708 | while (size) { | |
709 | char ch; | |
710 | if ((marker_size < size) && | |
711 | (*src == '<' || *src == '=' || *src == '>')) { | |
712 | int i; | |
713 | ch = *src; | |
714 | for (i = 0; i < marker_size; i++) | |
715 | if (src[i] != ch) | |
716 | goto not_a_marker; | |
717 | if (src[marker_size] != '\n') | |
718 | goto not_a_marker; | |
719 | src += marker_size + 1; | |
720 | size -= marker_size + 1; | |
721 | continue; | |
722 | } | |
723 | not_a_marker: | |
724 | do { | |
725 | ch = *src++; | |
726 | *dst++ = ch; | |
727 | size--; | |
728 | } while (ch != '\n' && size); | |
729 | } | |
730 | result->size = dst - result->ptr; | |
731 | return 0; | |
732 | } | |
733 | ||
153920da JH |
734 | static int ll_binary_merge(const struct ll_merge_driver *drv_unused, |
735 | const char *path_unused, | |
f3ef6b6b | 736 | mmfile_t *orig, |
a129d96f JH |
737 | mmfile_t *src1, const char *name1, |
738 | mmfile_t *src2, const char *name2, | |
739 | mmbuffer_t *result) | |
740 | { | |
741 | /* | |
742 | * The tentative merge result is "ours" for the final round, | |
743 | * or common ancestor for an internal merge. Still return | |
744 | * "conflicted merge" status. | |
745 | */ | |
746 | mmfile_t *stolen = index_only ? orig : src1; | |
747 | ||
748 | result->ptr = stolen->ptr; | |
749 | result->size = stolen->size; | |
750 | stolen->ptr = NULL; | |
751 | return 1; | |
752 | } | |
753 | ||
153920da JH |
754 | #define LL_BINARY_MERGE 0 |
755 | #define LL_TEXT_MERGE 1 | |
756 | #define LL_UNION_MERGE 2 | |
757 | static struct ll_merge_driver ll_merge_drv[] = { | |
758 | { "binary", "built-in binary merge", ll_binary_merge }, | |
759 | { "text", "built-in 3-way text merge", ll_xdl_merge }, | |
760 | { "union", "built-in union merge", ll_union_merge }, | |
a129d96f JH |
761 | }; |
762 | ||
f3ef6b6b JH |
763 | static void create_temp(mmfile_t *src, char *path) |
764 | { | |
765 | int fd; | |
766 | ||
767 | strcpy(path, ".merge_file_XXXXXX"); | |
768 | fd = mkstemp(path); | |
769 | if (fd < 0) | |
770 | die("unable to create temp-file"); | |
771 | if (write_in_full(fd, src->ptr, src->size) != src->size) | |
772 | die("unable to write temp-file"); | |
773 | close(fd); | |
774 | } | |
775 | ||
153920da JH |
776 | /* |
777 | * User defined low-level merge driver support. | |
778 | */ | |
779 | static int ll_ext_merge(const struct ll_merge_driver *fn, | |
780 | const char *path, | |
f3ef6b6b JH |
781 | mmfile_t *orig, |
782 | mmfile_t *src1, const char *name1, | |
783 | mmfile_t *src2, const char *name2, | |
784 | mmbuffer_t *result) | |
785 | { | |
786 | char temp[3][50]; | |
787 | char cmdbuf[2048]; | |
788 | struct interp table[] = { | |
789 | { "%O" }, | |
790 | { "%A" }, | |
791 | { "%B" }, | |
792 | }; | |
793 | struct child_process child; | |
794 | const char *args[20]; | |
795 | int status, fd, i; | |
796 | struct stat st; | |
797 | ||
15ba3af2 JH |
798 | if (fn->cmdline == NULL) |
799 | die("custom merge driver %s lacks command line.", fn->name); | |
800 | ||
f3ef6b6b JH |
801 | result->ptr = NULL; |
802 | result->size = 0; | |
803 | create_temp(orig, temp[0]); | |
804 | create_temp(src1, temp[1]); | |
805 | create_temp(src2, temp[2]); | |
806 | ||
807 | interp_set_entry(table, 0, temp[0]); | |
808 | interp_set_entry(table, 1, temp[1]); | |
809 | interp_set_entry(table, 2, temp[2]); | |
810 | ||
153920da JH |
811 | output(1, "merging %s using %s", path, |
812 | fn->description ? fn->description : fn->name); | |
813 | ||
814 | interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3); | |
f3ef6b6b JH |
815 | |
816 | memset(&child, 0, sizeof(child)); | |
817 | child.argv = args; | |
818 | args[0] = "sh"; | |
819 | args[1] = "-c"; | |
820 | args[2] = cmdbuf; | |
821 | args[3] = NULL; | |
822 | ||
823 | status = run_command(&child); | |
824 | if (status < -ERR_RUN_COMMAND_FORK) | |
825 | ; /* failure in run-command */ | |
826 | else | |
827 | status = -status; | |
828 | fd = open(temp[1], O_RDONLY); | |
829 | if (fd < 0) | |
830 | goto bad; | |
831 | if (fstat(fd, &st)) | |
832 | goto close_bad; | |
833 | result->size = st.st_size; | |
834 | result->ptr = xmalloc(result->size + 1); | |
835 | if (read_in_full(fd, result->ptr, result->size) != result->size) { | |
836 | free(result->ptr); | |
837 | result->ptr = NULL; | |
838 | result->size = 0; | |
839 | } | |
840 | close_bad: | |
841 | close(fd); | |
842 | bad: | |
843 | for (i = 0; i < 3; i++) | |
844 | unlink(temp[i]); | |
845 | return status; | |
846 | } | |
847 | ||
848 | /* | |
849 | * merge.default and merge.driver configuration items | |
850 | */ | |
153920da | 851 | static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; |
be89cb23 | 852 | static const char *default_ll_merge; |
f3ef6b6b JH |
853 | |
854 | static int read_merge_config(const char *var, const char *value) | |
855 | { | |
153920da JH |
856 | struct ll_merge_driver *fn; |
857 | const char *ep, *name; | |
858 | int namelen; | |
f3ef6b6b | 859 | |
be89cb23 | 860 | if (!strcmp(var, "merge.default")) { |
153920da JH |
861 | if (value) |
862 | default_ll_merge = strdup(value); | |
be89cb23 JH |
863 | return 0; |
864 | } | |
865 | ||
153920da JH |
866 | /* |
867 | * We are not interested in anything but "merge.<name>.variable"; | |
868 | * especially, we do not want to look at variables such as | |
869 | * "merge.summary", "merge.tool", and "merge.verbosity". | |
870 | */ | |
15ba3af2 | 871 | if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) |
f3ef6b6b | 872 | return 0; |
153920da | 873 | |
f3ef6b6b | 874 | /* |
153920da JH |
875 | * Find existing one as we might be processing merge.<name>.var2 |
876 | * after seeing merge.<name>.var1. | |
f3ef6b6b | 877 | */ |
153920da JH |
878 | name = var + 6; |
879 | namelen = ep - name; | |
880 | for (fn = ll_user_merge; fn; fn = fn->next) | |
881 | if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) | |
882 | break; | |
883 | if (!fn) { | |
884 | char *namebuf; | |
885 | fn = xcalloc(1, sizeof(struct ll_merge_driver)); | |
886 | namebuf = xmalloc(namelen + 1); | |
887 | memcpy(namebuf, name, namelen); | |
888 | namebuf[namelen] = 0; | |
889 | fn->name = namebuf; | |
890 | fn->fn = ll_ext_merge; | |
e87b1c94 | 891 | fn->next = NULL; |
153920da | 892 | *ll_user_merge_tail = fn; |
e87b1c94 | 893 | ll_user_merge_tail = &(fn->next); |
153920da JH |
894 | } |
895 | ||
896 | ep++; | |
897 | ||
898 | if (!strcmp("name", ep)) { | |
899 | if (!value) | |
900 | return error("%s: lacks value", var); | |
901 | fn->description = strdup(value); | |
902 | return 0; | |
903 | } | |
904 | ||
905 | if (!strcmp("driver", ep)) { | |
906 | if (!value) | |
907 | return error("%s: lacks value", var); | |
908 | /* | |
909 | * merge.<name>.driver specifies the command line: | |
910 | * | |
911 | * command-line | |
912 | * | |
913 | * The command-line will be interpolated with the following | |
914 | * tokens and is given to the shell: | |
915 | * | |
916 | * %O - temporary file name for the merge base. | |
917 | * %A - temporary file name for our version. | |
918 | * %B - temporary file name for the other branches' version. | |
919 | * | |
920 | * The external merge driver should write the results in the | |
921 | * file named by %A, and signal that it has done with zero exit | |
922 | * status. | |
923 | */ | |
924 | fn->cmdline = strdup(value); | |
925 | return 0; | |
926 | } | |
927 | ||
3086486d JH |
928 | if (!strcmp("recursive", ep)) { |
929 | if (!value) | |
930 | return error("%s: lacks value", var); | |
931 | fn->recursive = strdup(value); | |
932 | return 0; | |
933 | } | |
934 | ||
f3ef6b6b JH |
935 | return 0; |
936 | } | |
937 | ||
938 | static void initialize_ll_merge(void) | |
a129d96f | 939 | { |
153920da | 940 | if (ll_user_merge_tail) |
f3ef6b6b | 941 | return; |
153920da | 942 | ll_user_merge_tail = &ll_user_merge; |
f3ef6b6b JH |
943 | git_config(read_merge_config); |
944 | } | |
945 | ||
a5e92abd | 946 | static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) |
f3ef6b6b | 947 | { |
153920da | 948 | struct ll_merge_driver *fn; |
a129d96f JH |
949 | const char *name; |
950 | int i; | |
951 | ||
f3ef6b6b JH |
952 | initialize_ll_merge(); |
953 | ||
954 | if (ATTR_TRUE(merge_attr)) | |
153920da | 955 | return &ll_merge_drv[LL_TEXT_MERGE]; |
a129d96f | 956 | else if (ATTR_FALSE(merge_attr)) |
153920da | 957 | return &ll_merge_drv[LL_BINARY_MERGE]; |
be89cb23 JH |
958 | else if (ATTR_UNSET(merge_attr)) { |
959 | if (!default_ll_merge) | |
153920da | 960 | return &ll_merge_drv[LL_TEXT_MERGE]; |
be89cb23 JH |
961 | else |
962 | name = default_ll_merge; | |
963 | } | |
f3ef6b6b JH |
964 | else |
965 | name = merge_attr; | |
966 | ||
153920da JH |
967 | for (fn = ll_user_merge; fn; fn = fn->next) |
968 | if (!strcmp(fn->name, name)) | |
969 | return fn; | |
a129d96f | 970 | |
153920da JH |
971 | for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) |
972 | if (!strcmp(ll_merge_drv[i].name, name)) | |
973 | return &ll_merge_drv[i]; | |
a129d96f JH |
974 | |
975 | /* default to the 3-way */ | |
153920da | 976 | return &ll_merge_drv[LL_TEXT_MERGE]; |
a129d96f JH |
977 | } |
978 | ||
a5e92abd | 979 | static const char *git_path_check_merge(const char *path) |
a129d96f JH |
980 | { |
981 | static struct git_attr_check attr_merge_check; | |
982 | ||
983 | if (!attr_merge_check.attr) | |
984 | attr_merge_check.attr = git_attr("merge", 5); | |
985 | ||
986 | if (git_checkattr(path, 1, &attr_merge_check)) | |
a5e92abd | 987 | return NULL; |
a129d96f JH |
988 | return attr_merge_check.value; |
989 | } | |
990 | ||
3e5261a2 JH |
991 | static int ll_merge(mmbuffer_t *result_buf, |
992 | struct diff_filespec *o, | |
993 | struct diff_filespec *a, | |
994 | struct diff_filespec *b, | |
995 | const char *branch1, | |
996 | const char *branch2) | |
997 | { | |
998 | mmfile_t orig, src1, src2; | |
3e5261a2 JH |
999 | char *name1, *name2; |
1000 | int merge_status; | |
a5e92abd | 1001 | const char *ll_driver_name; |
153920da | 1002 | const struct ll_merge_driver *driver; |
3e5261a2 JH |
1003 | |
1004 | name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); | |
1005 | name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); | |
1006 | ||
1007 | fill_mm(o->sha1, &orig); | |
1008 | fill_mm(a->sha1, &src1); | |
1009 | fill_mm(b->sha1, &src2); | |
1010 | ||
a5e92abd JH |
1011 | ll_driver_name = git_path_check_merge(a->path); |
1012 | driver = find_ll_merge_driver(ll_driver_name); | |
a129d96f | 1013 | |
d56dbd67 JH |
1014 | if (index_only && driver->recursive) |
1015 | driver = find_ll_merge_driver(driver->recursive); | |
153920da JH |
1016 | merge_status = driver->fn(driver, a->path, |
1017 | &orig, &src1, name1, &src2, name2, | |
1018 | result_buf); | |
a129d96f | 1019 | |
3e5261a2 JH |
1020 | free(name1); |
1021 | free(name2); | |
1022 | free(orig.ptr); | |
1023 | free(src1.ptr); | |
1024 | free(src2.ptr); | |
1025 | return merge_status; | |
1026 | } | |
1027 | ||
3af244ca JS |
1028 | static struct merge_file_info merge_file(struct diff_filespec *o, |
1029 | struct diff_filespec *a, struct diff_filespec *b, | |
c1d20846 | 1030 | const char *branch1, const char *branch2) |
6d297f81 JS |
1031 | { |
1032 | struct merge_file_info result; | |
1033 | result.merge = 0; | |
1034 | result.clean = 1; | |
1035 | ||
3af244ca | 1036 | if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { |
6d297f81 | 1037 | result.clean = 0; |
3af244ca JS |
1038 | if (S_ISREG(a->mode)) { |
1039 | result.mode = a->mode; | |
8da71493 | 1040 | hashcpy(result.sha, a->sha1); |
6d297f81 | 1041 | } else { |
3af244ca | 1042 | result.mode = b->mode; |
8da71493 | 1043 | hashcpy(result.sha, b->sha1); |
6d297f81 JS |
1044 | } |
1045 | } else { | |
3af244ca | 1046 | if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) |
6d297f81 JS |
1047 | result.merge = 1; |
1048 | ||
3af244ca | 1049 | result.mode = a->mode == o->mode ? b->mode: a->mode; |
6d297f81 | 1050 | |
3af244ca | 1051 | if (sha_eq(a->sha1, o->sha1)) |
8da71493 | 1052 | hashcpy(result.sha, b->sha1); |
3af244ca | 1053 | else if (sha_eq(b->sha1, o->sha1)) |
8da71493 | 1054 | hashcpy(result.sha, a->sha1); |
3af244ca | 1055 | else if (S_ISREG(a->mode)) { |
c2b4faea | 1056 | mmbuffer_t result_buf; |
c2b4faea JH |
1057 | int merge_status; |
1058 | ||
3e5261a2 JH |
1059 | merge_status = ll_merge(&result_buf, o, a, b, |
1060 | branch1, branch2); | |
c2b4faea JH |
1061 | |
1062 | if ((merge_status < 0) || !result_buf.ptr) | |
1063 | die("Failed to execute internal merge"); | |
1064 | ||
1065 | if (write_sha1_file(result_buf.ptr, result_buf.size, | |
1066 | blob_type, result.sha)) | |
1067 | die("Unable to add %s to database", | |
1068 | a->path); | |
1069 | ||
1070 | free(result_buf.ptr); | |
1071 | result.clean = (merge_status == 0); | |
6d297f81 | 1072 | } else { |
3af244ca | 1073 | if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode))) |
6d297f81 JS |
1074 | die("cannot merge modes?"); |
1075 | ||
8da71493 | 1076 | hashcpy(result.sha, a->sha1); |
6d297f81 | 1077 | |
3af244ca | 1078 | if (!sha_eq(a->sha1, b->sha1)) |
6d297f81 JS |
1079 | result.clean = 0; |
1080 | } | |
1081 | } | |
1082 | ||
1083 | return result; | |
1084 | } | |
1085 | ||
1086 | static void conflict_rename_rename(struct rename *ren1, | |
1087 | const char *branch1, | |
1088 | struct rename *ren2, | |
1089 | const char *branch2) | |
1090 | { | |
1091 | char *del[2]; | |
1092 | int delp = 0; | |
1093 | const char *ren1_dst = ren1->pair->two->path; | |
1094 | const char *ren2_dst = ren2->pair->two->path; | |
5a753613 JS |
1095 | const char *dst_name1 = ren1_dst; |
1096 | const char *dst_name2 = ren2_dst; | |
1097 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { | |
1098 | dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); | |
89f40be2 | 1099 | output(1, "%s is a directory in %s added as %s instead", |
5a753613 | 1100 | ren1_dst, branch2, dst_name1); |
65ac6e9c | 1101 | remove_file(0, ren1_dst, 0); |
6d297f81 | 1102 | } |
5a753613 JS |
1103 | if (path_list_has_path(¤t_directory_set, ren2_dst)) { |
1104 | dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); | |
89f40be2 | 1105 | output(1, "%s is a directory in %s added as %s instead", |
5a753613 | 1106 | ren2_dst, branch1, dst_name2); |
65ac6e9c | 1107 | remove_file(0, ren2_dst, 0); |
6d297f81 | 1108 | } |
a97e4075 AR |
1109 | if (index_only) { |
1110 | remove_file_from_cache(dst_name1); | |
1111 | remove_file_from_cache(dst_name2); | |
1112 | /* | |
1113 | * Uncomment to leave the conflicting names in the resulting tree | |
1114 | * | |
1115 | * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); | |
1116 | * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); | |
1117 | */ | |
1118 | } else { | |
1119 | update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); | |
1120 | update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1); | |
1121 | } | |
3af244ca | 1122 | while (delp--) |
6d297f81 JS |
1123 | free(del[delp]); |
1124 | } | |
1125 | ||
1126 | static void conflict_rename_dir(struct rename *ren1, | |
1127 | const char *branch1) | |
1128 | { | |
5a753613 | 1129 | char *new_path = unique_path(ren1->pair->two->path, branch1); |
89f40be2 | 1130 | output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path); |
65ac6e9c | 1131 | remove_file(0, ren1->pair->two->path, 0); |
5a753613 JS |
1132 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); |
1133 | free(new_path); | |
6d297f81 JS |
1134 | } |
1135 | ||
1136 | static void conflict_rename_rename_2(struct rename *ren1, | |
1137 | const char *branch1, | |
1138 | struct rename *ren2, | |
1139 | const char *branch2) | |
1140 | { | |
5a753613 JS |
1141 | char *new_path1 = unique_path(ren1->pair->two->path, branch1); |
1142 | char *new_path2 = unique_path(ren2->pair->two->path, branch2); | |
89f40be2 | 1143 | output(1, "Renamed %s to %s and %s to %s instead", |
5a753613 JS |
1144 | ren1->pair->one->path, new_path1, |
1145 | ren2->pair->one->path, new_path2); | |
65ac6e9c | 1146 | remove_file(0, ren1->pair->two->path, 0); |
5a753613 JS |
1147 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); |
1148 | update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); | |
1149 | free(new_path2); | |
1150 | free(new_path1); | |
6d297f81 JS |
1151 | } |
1152 | ||
5a753613 JS |
1153 | static int process_renames(struct path_list *a_renames, |
1154 | struct path_list *b_renames, | |
1155 | const char *a_branch, | |
1156 | const char *b_branch) | |
6d297f81 | 1157 | { |
5a753613 JS |
1158 | int clean_merge = 1, i, j; |
1159 | struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; | |
6d297f81 JS |
1160 | const struct rename *sre; |
1161 | ||
5a753613 JS |
1162 | for (i = 0; i < a_renames->nr; i++) { |
1163 | sre = a_renames->items[i].util; | |
1164 | path_list_insert(sre->pair->two->path, &a_by_dst)->util | |
6d297f81 JS |
1165 | = sre->dst_entry; |
1166 | } | |
5a753613 JS |
1167 | for (i = 0; i < b_renames->nr; i++) { |
1168 | sre = b_renames->items[i].util; | |
1169 | path_list_insert(sre->pair->two->path, &b_by_dst)->util | |
6d297f81 JS |
1170 | = sre->dst_entry; |
1171 | } | |
1172 | ||
5a753613 | 1173 | for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { |
3af244ca JS |
1174 | int compare; |
1175 | char *src; | |
6d297f81 | 1176 | struct path_list *renames1, *renames2, *renames2Dst; |
3af244ca | 1177 | struct rename *ren1 = NULL, *ren2 = NULL; |
5a753613 | 1178 | const char *branch1, *branch2; |
3af244ca JS |
1179 | const char *ren1_src, *ren1_dst; |
1180 | ||
5a753613 | 1181 | if (i >= a_renames->nr) { |
3af244ca | 1182 | compare = 1; |
5a753613 JS |
1183 | ren2 = b_renames->items[j++].util; |
1184 | } else if (j >= b_renames->nr) { | |
3af244ca | 1185 | compare = -1; |
5a753613 | 1186 | ren1 = a_renames->items[i++].util; |
3af244ca | 1187 | } else { |
5a753613 JS |
1188 | compare = strcmp(a_renames->items[i].path, |
1189 | b_renames->items[j].path); | |
3d234d0a JS |
1190 | if (compare <= 0) |
1191 | ren1 = a_renames->items[i++].util; | |
1192 | if (compare >= 0) | |
1193 | ren2 = b_renames->items[j++].util; | |
3af244ca JS |
1194 | } |
1195 | ||
6d297f81 | 1196 | /* TODO: refactor, so that 1/2 are not needed */ |
3af244ca | 1197 | if (ren1) { |
5a753613 JS |
1198 | renames1 = a_renames; |
1199 | renames2 = b_renames; | |
1200 | renames2Dst = &b_by_dst; | |
1201 | branch1 = a_branch; | |
1202 | branch2 = b_branch; | |
6d297f81 | 1203 | } else { |
3af244ca | 1204 | struct rename *tmp; |
5a753613 JS |
1205 | renames1 = b_renames; |
1206 | renames2 = a_renames; | |
1207 | renames2Dst = &a_by_dst; | |
1208 | branch1 = b_branch; | |
1209 | branch2 = a_branch; | |
3af244ca | 1210 | tmp = ren2; |
6d297f81 JS |
1211 | ren2 = ren1; |
1212 | ren1 = tmp; | |
1213 | } | |
3af244ca | 1214 | src = ren1->pair->one->path; |
6d297f81 JS |
1215 | |
1216 | ren1->dst_entry->processed = 1; | |
1217 | ren1->src_entry->processed = 1; | |
1218 | ||
3af244ca | 1219 | if (ren1->processed) |
6d297f81 JS |
1220 | continue; |
1221 | ren1->processed = 1; | |
1222 | ||
3af244ca JS |
1223 | ren1_src = ren1->pair->one->path; |
1224 | ren1_dst = ren1->pair->two->path; | |
6d297f81 | 1225 | |
3af244ca | 1226 | if (ren2) { |
6d297f81 JS |
1227 | const char *ren2_src = ren2->pair->one->path; |
1228 | const char *ren2_dst = ren2->pair->two->path; | |
1229 | /* Renamed in 1 and renamed in 2 */ | |
1230 | if (strcmp(ren1_src, ren2_src) != 0) | |
1231 | die("ren1.src != ren2.src"); | |
1232 | ren2->dst_entry->processed = 1; | |
1233 | ren2->processed = 1; | |
1234 | if (strcmp(ren1_dst, ren2_dst) != 0) { | |
5a753613 | 1235 | clean_merge = 0; |
8c3275ab | 1236 | output(1, "CONFLICT (rename/rename): " |
a97e4075 AR |
1237 | "Rename \"%s\"->\"%s\" in branch \"%s\" " |
1238 | "rename \"%s\"->\"%s\" in \"%s\"%s", | |
5a753613 | 1239 | src, ren1_dst, branch1, |
a97e4075 AR |
1240 | src, ren2_dst, branch2, |
1241 | index_only ? " (left unresolved)": ""); | |
1242 | if (index_only) { | |
1243 | remove_file_from_cache(src); | |
1244 | update_file(0, ren1->pair->one->sha1, | |
1245 | ren1->pair->one->mode, src); | |
1246 | } | |
5a753613 | 1247 | conflict_rename_rename(ren1, branch1, ren2, branch2); |
6d297f81 | 1248 | } else { |
6d297f81 | 1249 | struct merge_file_info mfi; |
65ac6e9c | 1250 | remove_file(1, ren1_src, 1); |
3af244ca JS |
1251 | mfi = merge_file(ren1->pair->one, |
1252 | ren1->pair->two, | |
1253 | ren2->pair->two, | |
5a753613 JS |
1254 | branch1, |
1255 | branch2); | |
3af244ca | 1256 | if (mfi.merge || !mfi.clean) |
89f40be2 | 1257 | output(1, "Renamed %s->%s", src, ren1_dst); |
6d297f81 | 1258 | |
3af244ca | 1259 | if (mfi.merge) |
89f40be2 | 1260 | output(2, "Auto-merged %s", ren1_dst); |
6d297f81 | 1261 | |
3af244ca | 1262 | if (!mfi.clean) { |
8c3275ab | 1263 | output(1, "CONFLICT (content): merge conflict in %s", |
6d297f81 | 1264 | ren1_dst); |
5a753613 | 1265 | clean_merge = 0; |
6d297f81 | 1266 | |
3af244ca | 1267 | if (!index_only) |
6d297f81 | 1268 | update_stages(ren1_dst, |
3af244ca JS |
1269 | ren1->pair->one, |
1270 | ren1->pair->two, | |
1271 | ren2->pair->two, | |
6d297f81 JS |
1272 | 1 /* clear */); |
1273 | } | |
1274 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); | |
1275 | } | |
1276 | } else { | |
1277 | /* Renamed in 1, maybe changed in 2 */ | |
3af244ca JS |
1278 | struct path_list_item *item; |
1279 | /* we only use sha1 and mode of these */ | |
1280 | struct diff_filespec src_other, dst_other; | |
5a753613 | 1281 | int try_merge, stage = a_renames == renames1 ? 3: 2; |
6d297f81 | 1282 | |
183d7972 | 1283 | remove_file(1, ren1_src, index_only || stage == 3); |
6d297f81 | 1284 | |
87cb004e | 1285 | hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); |
3af244ca | 1286 | src_other.mode = ren1->src_entry->stages[stage].mode; |
87cb004e | 1287 | hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha); |
3af244ca | 1288 | dst_other.mode = ren1->dst_entry->stages[stage].mode; |
6d297f81 | 1289 | |
5a753613 | 1290 | try_merge = 0; |
6d297f81 | 1291 | |
5a753613 JS |
1292 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { |
1293 | clean_merge = 0; | |
89f40be2 | 1294 | output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s " |
6d297f81 | 1295 | " directory %s added in %s", |
5a753613 JS |
1296 | ren1_src, ren1_dst, branch1, |
1297 | ren1_dst, branch2); | |
1298 | conflict_rename_dir(ren1, branch1); | |
3af244ca | 1299 | } else if (sha_eq(src_other.sha1, null_sha1)) { |
5a753613 | 1300 | clean_merge = 0; |
89f40be2 | 1301 | output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s " |
6d297f81 | 1302 | "and deleted in %s", |
5a753613 JS |
1303 | ren1_src, ren1_dst, branch1, |
1304 | branch2); | |
6d297f81 | 1305 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); |
3af244ca | 1306 | } else if (!sha_eq(dst_other.sha1, null_sha1)) { |
5a753613 JS |
1307 | const char *new_path; |
1308 | clean_merge = 0; | |
1309 | try_merge = 1; | |
89f40be2 | 1310 | output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. " |
6d297f81 | 1311 | "%s added in %s", |
5a753613 JS |
1312 | ren1_src, ren1_dst, branch1, |
1313 | ren1_dst, branch2); | |
1314 | new_path = unique_path(ren1_dst, branch2); | |
89f40be2 | 1315 | output(1, "Added as %s instead", new_path); |
5a753613 | 1316 | update_file(0, dst_other.sha1, dst_other.mode, new_path); |
3af244ca JS |
1317 | } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) { |
1318 | ren2 = item->util; | |
5a753613 | 1319 | clean_merge = 0; |
6d297f81 | 1320 | ren2->processed = 1; |
89f40be2 SP |
1321 | output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. " |
1322 | "Renamed %s->%s in %s", | |
5a753613 JS |
1323 | ren1_src, ren1_dst, branch1, |
1324 | ren2->pair->one->path, ren2->pair->two->path, branch2); | |
1325 | conflict_rename_rename_2(ren1, branch1, ren2, branch2); | |
6d297f81 | 1326 | } else |
5a753613 | 1327 | try_merge = 1; |
6d297f81 | 1328 | |
5a753613 | 1329 | if (try_merge) { |
3af244ca | 1330 | struct diff_filespec *o, *a, *b; |
6d297f81 | 1331 | struct merge_file_info mfi; |
3af244ca JS |
1332 | src_other.path = (char *)ren1_src; |
1333 | ||
1334 | o = ren1->pair->one; | |
5a753613 | 1335 | if (a_renames == renames1) { |
3af244ca JS |
1336 | a = ren1->pair->two; |
1337 | b = &src_other; | |
1338 | } else { | |
1339 | b = ren1->pair->two; | |
1340 | a = &src_other; | |
1341 | } | |
1342 | mfi = merge_file(o, a, b, | |
5a753613 | 1343 | a_branch, b_branch); |
6d297f81 | 1344 | |
0d5e6c97 | 1345 | if (mfi.clean && |
c135ee88 AR |
1346 | sha_eq(mfi.sha, ren1->pair->two->sha1) && |
1347 | mfi.mode == ren1->pair->two->mode) | |
8a359819 AR |
1348 | /* |
1349 | * This messaged is part of | |
1350 | * t6022 test. If you change | |
1351 | * it update the test too. | |
1352 | */ | |
c135ee88 AR |
1353 | output(3, "Skipped %s (merged same as existing)", ren1_dst); |
1354 | else { | |
1355 | if (mfi.merge || !mfi.clean) | |
1356 | output(1, "Renamed %s => %s", ren1_src, ren1_dst); | |
1357 | if (mfi.merge) | |
1358 | output(2, "Auto-merged %s", ren1_dst); | |
1359 | if (!mfi.clean) { | |
1360 | output(1, "CONFLICT (rename/modify): Merge conflict in %s", | |
1361 | ren1_dst); | |
1362 | clean_merge = 0; | |
1363 | ||
1364 | if (!index_only) | |
1365 | update_stages(ren1_dst, | |
1366 | o, a, b, 1); | |
1367 | } | |
1368 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); | |
6d297f81 | 1369 | } |
6d297f81 JS |
1370 | } |
1371 | } | |
1372 | } | |
5a753613 JS |
1373 | path_list_clear(&a_by_dst, 0); |
1374 | path_list_clear(&b_by_dst, 0); | |
6d297f81 | 1375 | |
5a753613 | 1376 | return clean_merge; |
6d297f81 JS |
1377 | } |
1378 | ||
ac7f0f43 | 1379 | static unsigned char *stage_sha(const unsigned char *sha, unsigned mode) |
6d297f81 | 1380 | { |
ac7f0f43 | 1381 | return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha; |
6d297f81 JS |
1382 | } |
1383 | ||
1384 | /* Per entry merge function */ | |
1385 | static int process_entry(const char *path, struct stage_data *entry, | |
c1d20846 JS |
1386 | const char *branch1, |
1387 | const char *branch2) | |
6d297f81 JS |
1388 | { |
1389 | /* | |
1390 | printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); | |
1391 | print_index_entry("\tpath: ", entry); | |
1392 | */ | |
5a753613 | 1393 | int clean_merge = 1; |
5a753613 JS |
1394 | unsigned o_mode = entry->stages[1].mode; |
1395 | unsigned a_mode = entry->stages[2].mode; | |
1396 | unsigned b_mode = entry->stages[3].mode; | |
ac7f0f43 JH |
1397 | unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode); |
1398 | unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode); | |
1399 | unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode); | |
5a753613 JS |
1400 | |
1401 | if (o_sha && (!a_sha || !b_sha)) { | |
6d297f81 | 1402 | /* Case A: Deleted in one */ |
5a753613 JS |
1403 | if ((!a_sha && !b_sha) || |
1404 | (sha_eq(a_sha, o_sha) && !b_sha) || | |
1405 | (!a_sha && sha_eq(b_sha, o_sha))) { | |
6d297f81 JS |
1406 | /* Deleted in both or deleted in one and |
1407 | * unchanged in the other */ | |
5a753613 | 1408 | if (a_sha) |
89f40be2 | 1409 | output(2, "Removed %s", path); |
65ac6e9c JH |
1410 | /* do not touch working file if it did not exist */ |
1411 | remove_file(1, path, !a_sha); | |
6d297f81 JS |
1412 | } else { |
1413 | /* Deleted in one and changed in the other */ | |
5a753613 JS |
1414 | clean_merge = 0; |
1415 | if (!a_sha) { | |
8c3275ab | 1416 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
6d297f81 | 1417 | "and modified in %s. Version %s of %s left in tree.", |
c1d20846 JS |
1418 | path, branch1, |
1419 | branch2, branch2, path); | |
5a753613 | 1420 | update_file(0, b_sha, b_mode, path); |
6d297f81 | 1421 | } else { |
8c3275ab | 1422 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
6d297f81 | 1423 | "and modified in %s. Version %s of %s left in tree.", |
c1d20846 JS |
1424 | path, branch2, |
1425 | branch1, branch1, path); | |
5a753613 | 1426 | update_file(0, a_sha, a_mode, path); |
6d297f81 JS |
1427 | } |
1428 | } | |
1429 | ||
5a753613 JS |
1430 | } else if ((!o_sha && a_sha && !b_sha) || |
1431 | (!o_sha && !a_sha && b_sha)) { | |
6d297f81 | 1432 | /* Case B: Added in one. */ |
5a753613 JS |
1433 | const char *add_branch; |
1434 | const char *other_branch; | |
6d297f81 JS |
1435 | unsigned mode; |
1436 | const unsigned char *sha; | |
1437 | const char *conf; | |
1438 | ||
5a753613 | 1439 | if (a_sha) { |
c1d20846 JS |
1440 | add_branch = branch1; |
1441 | other_branch = branch2; | |
5a753613 JS |
1442 | mode = a_mode; |
1443 | sha = a_sha; | |
6d297f81 JS |
1444 | conf = "file/directory"; |
1445 | } else { | |
c1d20846 JS |
1446 | add_branch = branch2; |
1447 | other_branch = branch1; | |
5a753613 JS |
1448 | mode = b_mode; |
1449 | sha = b_sha; | |
6d297f81 JS |
1450 | conf = "directory/file"; |
1451 | } | |
5a753613 JS |
1452 | if (path_list_has_path(¤t_directory_set, path)) { |
1453 | const char *new_path = unique_path(path, add_branch); | |
1454 | clean_merge = 0; | |
8c3275ab | 1455 | output(1, "CONFLICT (%s): There is a directory with name %s in %s. " |
89f40be2 | 1456 | "Added %s as %s", |
5a753613 | 1457 | conf, path, other_branch, path, new_path); |
65ac6e9c | 1458 | remove_file(0, path, 0); |
5a753613 | 1459 | update_file(0, sha, mode, new_path); |
6d297f81 | 1460 | } else { |
89f40be2 | 1461 | output(2, "Added %s", path); |
6d297f81 JS |
1462 | update_file(1, sha, mode, path); |
1463 | } | |
f953831e JS |
1464 | } else if (a_sha && b_sha) { |
1465 | /* Case C: Added in both (check for same permissions) and */ | |
6d297f81 | 1466 | /* case D: Modified in both, but differently. */ |
f953831e | 1467 | const char *reason = "content"; |
6d297f81 | 1468 | struct merge_file_info mfi; |
3af244ca JS |
1469 | struct diff_filespec o, a, b; |
1470 | ||
f953831e JS |
1471 | if (!o_sha) { |
1472 | reason = "add/add"; | |
1473 | o_sha = (unsigned char *)null_sha1; | |
1474 | } | |
89f40be2 | 1475 | output(2, "Auto-merged %s", path); |
3af244ca | 1476 | o.path = a.path = b.path = (char *)path; |
8da71493 | 1477 | hashcpy(o.sha1, o_sha); |
5a753613 | 1478 | o.mode = o_mode; |
8da71493 | 1479 | hashcpy(a.sha1, a_sha); |
5a753613 | 1480 | a.mode = a_mode; |
8da71493 | 1481 | hashcpy(b.sha1, b_sha); |
5a753613 | 1482 | b.mode = b_mode; |
3af244ca JS |
1483 | |
1484 | mfi = merge_file(&o, &a, &b, | |
c1d20846 | 1485 | branch1, branch2); |
6d297f81 | 1486 | |
3af244ca | 1487 | if (mfi.clean) |
6d297f81 JS |
1488 | update_file(1, mfi.sha, mfi.mode, path); |
1489 | else { | |
5a753613 | 1490 | clean_merge = 0; |
8c3275ab | 1491 | output(1, "CONFLICT (%s): Merge conflict in %s", |
f953831e | 1492 | reason, path); |
6d297f81 | 1493 | |
3af244ca | 1494 | if (index_only) |
6d297f81 JS |
1495 | update_file(0, mfi.sha, mfi.mode, path); |
1496 | else | |
1497 | update_file_flags(mfi.sha, mfi.mode, path, | |
5a753613 | 1498 | 0 /* update_cache */, 1 /* update_working_directory */); |
6d297f81 | 1499 | } |
ac7f0f43 JH |
1500 | } else if (!o_sha && !a_sha && !b_sha) { |
1501 | /* | |
1502 | * this entry was deleted altogether. a_mode == 0 means | |
1503 | * we had that path and want to actively remove it. | |
1504 | */ | |
1505 | remove_file(1, path, !a_mode); | |
6d297f81 JS |
1506 | } else |
1507 | die("Fatal merge failure, shouldn't happen."); | |
1508 | ||
5a753613 | 1509 | return clean_merge; |
6d297f81 JS |
1510 | } |
1511 | ||
3af244ca JS |
1512 | static int merge_trees(struct tree *head, |
1513 | struct tree *merge, | |
1514 | struct tree *common, | |
c1d20846 JS |
1515 | const char *branch1, |
1516 | const char *branch2, | |
3af244ca | 1517 | struct tree **result) |
6d297f81 | 1518 | { |
3af244ca | 1519 | int code, clean; |
68faf689 JH |
1520 | |
1521 | if (subtree_merge) { | |
1522 | merge = shift_tree_object(head, merge); | |
1523 | common = shift_tree_object(head, common); | |
1524 | } | |
1525 | ||
3af244ca | 1526 | if (sha_eq(common->object.sha1, merge->object.sha1)) { |
8c3275ab | 1527 | output(0, "Already uptodate!"); |
3af244ca JS |
1528 | *result = head; |
1529 | return 1; | |
6d297f81 JS |
1530 | } |
1531 | ||
7a85b848 | 1532 | code = git_merge_trees(index_only, common, head, merge); |
6d297f81 | 1533 | |
3af244ca | 1534 | if (code != 0) |
6d297f81 JS |
1535 | die("merging of trees %s and %s failed", |
1536 | sha1_to_hex(head->object.sha1), | |
1537 | sha1_to_hex(merge->object.sha1)); | |
1538 | ||
8b944b56 | 1539 | if (unmerged_index()) { |
3af244ca JS |
1540 | struct path_list *entries, *re_head, *re_merge; |
1541 | int i; | |
5a753613 JS |
1542 | path_list_clear(¤t_file_set, 1); |
1543 | path_list_clear(¤t_directory_set, 1); | |
3af244ca JS |
1544 | get_files_dirs(head); |
1545 | get_files_dirs(merge); | |
6d297f81 | 1546 | |
3af244ca | 1547 | entries = get_unmerged(); |
6d297f81 JS |
1548 | re_head = get_renames(head, common, head, merge, entries); |
1549 | re_merge = get_renames(merge, common, head, merge, entries); | |
3af244ca | 1550 | clean = process_renames(re_head, re_merge, |
c1d20846 | 1551 | branch1, branch2); |
ad57cbca | 1552 | for (i = 0; i < entries->nr; i++) { |
6d297f81 JS |
1553 | const char *path = entries->items[i].path; |
1554 | struct stage_data *e = entries->items[i].util; | |
3f6ee2d1 SP |
1555 | if (!e->processed |
1556 | && !process_entry(path, e, branch1, branch2)) | |
3af244ca | 1557 | clean = 0; |
6d297f81 JS |
1558 | } |
1559 | ||
3af244ca JS |
1560 | path_list_clear(re_merge, 0); |
1561 | path_list_clear(re_head, 0); | |
1562 | path_list_clear(entries, 1); | |
6d297f81 | 1563 | |
6d297f81 | 1564 | } |
1cf716a2 JH |
1565 | else |
1566 | clean = 1; | |
1567 | ||
8b944b56 JH |
1568 | if (index_only) |
1569 | *result = git_write_tree(); | |
6d297f81 | 1570 | |
3af244ca | 1571 | return clean; |
6d297f81 JS |
1572 | } |
1573 | ||
8918b0c9 JS |
1574 | static struct commit_list *reverse_commit_list(struct commit_list *list) |
1575 | { | |
1576 | struct commit_list *next = NULL, *current, *backup; | |
1577 | for (current = list; current; current = backup) { | |
1578 | backup = current->next; | |
1579 | current->next = next; | |
1580 | next = current; | |
1581 | } | |
1582 | return next; | |
1583 | } | |
1584 | ||
6d297f81 JS |
1585 | /* |
1586 | * Merge the commits h1 and h2, return the resulting virtual | |
3dff5379 | 1587 | * commit object and a flag indicating the cleanness of the merge. |
6d297f81 | 1588 | */ |
65ac6e9c JH |
1589 | static int merge(struct commit *h1, |
1590 | struct commit *h2, | |
1591 | const char *branch1, | |
1592 | const char *branch2, | |
8b944b56 | 1593 | struct commit_list *ca, |
65ac6e9c | 1594 | struct commit **result) |
6d297f81 | 1595 | { |
8b944b56 | 1596 | struct commit_list *iter; |
5a753613 | 1597 | struct commit *merged_common_ancestors; |
3af244ca JS |
1598 | struct tree *mrtree; |
1599 | int clean; | |
6d297f81 | 1600 | |
8c3275ab SP |
1601 | if (show(4)) { |
1602 | output(4, "Merging:"); | |
1603 | output_commit_title(h1); | |
1604 | output_commit_title(h2); | |
1605 | } | |
6d297f81 | 1606 | |
8b944b56 JH |
1607 | if (!ca) { |
1608 | ca = get_merge_bases(h1, h2, 1); | |
1609 | ca = reverse_commit_list(ca); | |
1610 | } | |
6d297f81 | 1611 | |
8c3275ab SP |
1612 | if (show(5)) { |
1613 | output(5, "found %u common ancestor(s):", commit_list_count(ca)); | |
1614 | for (iter = ca; iter; iter = iter->next) | |
1615 | output_commit_title(iter->item); | |
1616 | } | |
6d297f81 | 1617 | |
5a753613 | 1618 | merged_common_ancestors = pop_commit(&ca); |
934d9a24 JS |
1619 | if (merged_common_ancestors == NULL) { |
1620 | /* if there is no common ancestor, make an empty tree */ | |
1621 | struct tree *tree = xcalloc(1, sizeof(struct tree)); | |
934d9a24 JS |
1622 | |
1623 | tree->object.parsed = 1; | |
1624 | tree->object.type = OBJ_TREE; | |
21666f1a | 1625 | pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); |
934d9a24 JS |
1626 | merged_common_ancestors = make_virtual_commit(tree, "ancestor"); |
1627 | } | |
6d297f81 | 1628 | |
6d297f81 | 1629 | for (iter = ca; iter; iter = iter->next) { |
63889639 | 1630 | call_depth++; |
3af244ca JS |
1631 | /* |
1632 | * When the merge fails, the result contains files | |
1633 | * with conflict markers. The cleanness flag is | |
3dff5379 PR |
1634 | * ignored, it was never actually used, as result of |
1635 | * merge_trees has always overwritten it: the committed | |
3af244ca JS |
1636 | * "conflicts" were already resolved. |
1637 | */ | |
8b944b56 | 1638 | discard_cache(); |
5a753613 | 1639 | merge(merged_common_ancestors, iter->item, |
3af244ca JS |
1640 | "Temporary merge branch 1", |
1641 | "Temporary merge branch 2", | |
3af244ca | 1642 | NULL, |
5a753613 | 1643 | &merged_common_ancestors); |
63889639 | 1644 | call_depth--; |
6d297f81 | 1645 | |
5a753613 | 1646 | if (!merged_common_ancestors) |
6d297f81 JS |
1647 | die("merge returned no commit"); |
1648 | } | |
1649 | ||
8b944b56 | 1650 | discard_cache(); |
63889639 | 1651 | if (!call_depth) { |
8b944b56 | 1652 | read_cache(); |
6d297f81 | 1653 | index_only = 0; |
8b944b56 | 1654 | } else |
6d297f81 | 1655 | index_only = 1; |
6d297f81 | 1656 | |
5a753613 | 1657 | clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, |
c1d20846 | 1658 | branch1, branch2, &mrtree); |
6d297f81 | 1659 | |
8b944b56 | 1660 | if (index_only) { |
3af244ca JS |
1661 | *result = make_virtual_commit(mrtree, "merged tree"); |
1662 | commit_list_insert(h1, &(*result)->parents); | |
1663 | commit_list_insert(h2, &(*result)->parents->next); | |
8b944b56 | 1664 | } |
66a155bc | 1665 | flush_output(); |
3af244ca | 1666 | return clean; |
6d297f81 JS |
1667 | } |
1668 | ||
7ba3c078 SP |
1669 | static const char *better_branch_name(const char *branch) |
1670 | { | |
1671 | static char githead_env[8 + 40 + 1]; | |
1672 | char *name; | |
1673 | ||
1674 | if (strlen(branch) != 40) | |
1675 | return branch; | |
1676 | sprintf(githead_env, "GITHEAD_%s", branch); | |
1677 | name = getenv(githead_env); | |
1678 | return name ? name : branch; | |
1679 | } | |
1680 | ||
6d297f81 JS |
1681 | static struct commit *get_ref(const char *ref) |
1682 | { | |
1683 | unsigned char sha1[20]; | |
1684 | struct object *object; | |
1685 | ||
1686 | if (get_sha1(ref, sha1)) | |
1687 | die("Could not resolve ref '%s'", ref); | |
1688 | object = deref_tag(parse_object(sha1), ref, strlen(ref)); | |
a970e84e SP |
1689 | if (object->type == OBJ_TREE) |
1690 | return make_virtual_commit((struct tree*)object, | |
1691 | better_branch_name(ref)); | |
bf6d324e | 1692 | if (object->type != OBJ_COMMIT) |
6d297f81 JS |
1693 | return NULL; |
1694 | if (parse_commit((struct commit *)object)) | |
1695 | die("Could not parse commit '%s'", sha1_to_hex(object->sha1)); | |
1696 | return (struct commit *)object; | |
1697 | } | |
1698 | ||
8c3275ab SP |
1699 | static int merge_config(const char *var, const char *value) |
1700 | { | |
1701 | if (!strcasecmp(var, "merge.verbosity")) { | |
1702 | verbosity = git_config_int(var, value); | |
1703 | return 0; | |
1704 | } | |
1705 | return git_default_config(var, value); | |
1706 | } | |
1707 | ||
6d297f81 JS |
1708 | int main(int argc, char *argv[]) |
1709 | { | |
8b944b56 | 1710 | static const char *bases[20]; |
6d297f81 | 1711 | static unsigned bases_count = 0; |
3af244ca JS |
1712 | int i, clean; |
1713 | const char *branch1, *branch2; | |
1714 | struct commit *result, *h1, *h2; | |
8b944b56 JH |
1715 | struct commit_list *ca = NULL; |
1716 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
1717 | int index_fd; | |
6d297f81 | 1718 | |
68faf689 JH |
1719 | if (argv[0]) { |
1720 | int namelen = strlen(argv[0]); | |
1721 | if (8 < namelen && | |
1722 | !strcmp(argv[0] + namelen - 8, "-subtree")) | |
1723 | subtree_merge = 1; | |
1724 | } | |
1725 | ||
8c3275ab SP |
1726 | git_config(merge_config); |
1727 | if (getenv("GIT_MERGE_VERBOSITY")) | |
1728 | verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); | |
6d297f81 JS |
1729 | |
1730 | if (argc < 4) | |
1731 | die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]); | |
1732 | ||
6d297f81 JS |
1733 | for (i = 1; i < argc; ++i) { |
1734 | if (!strcmp(argv[i], "--")) | |
1735 | break; | |
1736 | if (bases_count < sizeof(bases)/sizeof(*bases)) | |
1737 | bases[bases_count++] = argv[i]; | |
1738 | } | |
1739 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ | |
1740 | die("Not handling anything other than two heads merge."); | |
ad57cbca | 1741 | if (verbosity >= 5) |
3f6ee2d1 | 1742 | buffer_output = 0; |
6d297f81 | 1743 | |
6d297f81 JS |
1744 | branch1 = argv[++i]; |
1745 | branch2 = argv[++i]; | |
6d297f81 | 1746 | |
3af244ca JS |
1747 | h1 = get_ref(branch1); |
1748 | h2 = get_ref(branch2); | |
6d297f81 | 1749 | |
e0ec1819 SP |
1750 | branch1 = better_branch_name(branch1); |
1751 | branch2 = better_branch_name(branch2); | |
3f6ee2d1 | 1752 | |
8c3275ab SP |
1753 | if (show(3)) |
1754 | printf("Merging %s with %s\n", branch1, branch2); | |
e0ec1819 | 1755 | |
30ca07a2 | 1756 | index_fd = hold_locked_index(lock, 1); |
6d297f81 | 1757 | |
8b944b56 JH |
1758 | for (i = 0; i < bases_count; i++) { |
1759 | struct commit *ancestor = get_ref(bases[i]); | |
1760 | ca = commit_list_insert(ancestor, &ca); | |
1761 | } | |
63889639 | 1762 | clean = merge(h1, h2, branch1, branch2, ca, &result); |
8b944b56 JH |
1763 | |
1764 | if (active_cache_changed && | |
1765 | (write_cache(index_fd, active_cache, active_nr) || | |
30ca07a2 | 1766 | close(index_fd) || commit_locked_index(lock))) |
8b944b56 | 1767 | die ("unable to write %s", get_index_file()); |
6d297f81 | 1768 | |
3af244ca | 1769 | return clean ? 0: 1; |
6d297f81 | 1770 | } |