]>
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" |
6d297f81 | 18 | |
6d297f81 JS |
19 | /* |
20 | * A virtual commit has | |
21 | * - (const char *)commit->util set to the name, and | |
22 | * - *(int *)commit->object.sha1 set to the virtual id. | |
23 | */ | |
6d297f81 JS |
24 | |
25 | static unsigned commit_list_count(const struct commit_list *l) | |
26 | { | |
27 | unsigned c = 0; | |
28 | for (; l; l = l->next ) | |
29 | c++; | |
30 | return c; | |
31 | } | |
32 | ||
33 | static struct commit *make_virtual_commit(struct tree *tree, const char *comment) | |
34 | { | |
35 | struct commit *commit = xcalloc(1, sizeof(struct commit)); | |
36 | static unsigned virtual_id = 1; | |
37 | commit->tree = tree; | |
38 | commit->util = (void*)comment; | |
39 | *(int*)commit->object.sha1 = virtual_id++; | |
c1f3089e JS |
40 | /* avoid warnings */ |
41 | commit->object.parsed = 1; | |
6d297f81 JS |
42 | return commit; |
43 | } | |
44 | ||
45 | /* | |
3058e933 JS |
46 | * Since we use get_tree_entry(), which does not put the read object into |
47 | * the object pool, we cannot rely on a == b. | |
6d297f81 JS |
48 | */ |
49 | static int sha_eq(const unsigned char *a, const unsigned char *b) | |
50 | { | |
3af244ca | 51 | if (!a && !b) |
6d297f81 | 52 | return 2; |
87cb004e | 53 | return a && b && hashcmp(a, b) == 0; |
6d297f81 JS |
54 | } |
55 | ||
6d297f81 | 56 | /* |
3058e933 JS |
57 | * Since we want to write the index eventually, we cannot reuse the index |
58 | * for these (temporary) data. | |
6d297f81 JS |
59 | */ |
60 | struct stage_data | |
61 | { | |
62 | struct | |
63 | { | |
64 | unsigned mode; | |
65 | unsigned char sha[20]; | |
66 | } stages[4]; | |
67 | unsigned processed:1; | |
68 | }; | |
69 | ||
66a155bc SP |
70 | struct output_buffer |
71 | { | |
72 | struct output_buffer *next; | |
73 | char *str; | |
74 | }; | |
75 | ||
5a753613 JS |
76 | static struct path_list current_file_set = {NULL, 0, 0, 1}; |
77 | static struct path_list current_directory_set = {NULL, 0, 0, 1}; | |
6d297f81 | 78 | |
63889639 | 79 | static int call_depth = 0; |
8c3275ab | 80 | static int verbosity = 2; |
66a155bc | 81 | static int buffer_output = 1; |
3f6ee2d1 SP |
82 | static int do_progress = 1; |
83 | static unsigned last_percent; | |
84 | static unsigned merged_cnt; | |
85 | static unsigned total_cnt; | |
86 | static volatile sig_atomic_t progress_update; | |
66a155bc | 87 | static struct output_buffer *output_list, *output_end; |
6d297f81 | 88 | |
8c3275ab SP |
89 | static int show (int v) |
90 | { | |
91 | return (!call_depth && verbosity >= v) || verbosity >= 5; | |
92 | } | |
93 | ||
94 | static void output(int v, const char *fmt, ...) | |
6d297f81 JS |
95 | { |
96 | va_list args; | |
6d297f81 | 97 | va_start(args, fmt); |
66a155bc SP |
98 | if (buffer_output && show(v)) { |
99 | struct output_buffer *b = xmalloc(sizeof(*b)); | |
100 | nfvasprintf(&b->str, fmt, args); | |
101 | b->next = NULL; | |
102 | if (output_end) | |
103 | output_end->next = b; | |
104 | else | |
105 | output_list = b; | |
106 | output_end = b; | |
107 | } else if (show(v)) { | |
8c3275ab SP |
108 | int i; |
109 | for (i = call_depth; i--;) | |
110 | fputs(" ", stdout); | |
111 | vfprintf(stdout, fmt, args); | |
112 | fputc('\n', stdout); | |
113 | } | |
6d297f81 | 114 | va_end(args); |
6d297f81 JS |
115 | } |
116 | ||
66a155bc SP |
117 | static void flush_output() |
118 | { | |
119 | struct output_buffer *b, *n; | |
120 | for (b = output_list; b; b = n) { | |
121 | int i; | |
122 | for (i = call_depth; i--;) | |
123 | fputs(" ", stdout); | |
124 | fputs(b->str, stdout); | |
125 | fputc('\n', stdout); | |
126 | n = b->next; | |
127 | free(b->str); | |
128 | free(b); | |
129 | } | |
130 | output_list = NULL; | |
131 | output_end = NULL; | |
132 | } | |
133 | ||
3af244ca JS |
134 | static void output_commit_title(struct commit *commit) |
135 | { | |
136 | int i; | |
66a155bc | 137 | flush_output(); |
63889639 | 138 | for (i = call_depth; i--;) |
3af244ca JS |
139 | fputs(" ", stdout); |
140 | if (commit->util) | |
141 | printf("virtual %s\n", (char *)commit->util); | |
142 | else { | |
9926ba98 | 143 | printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); |
3af244ca JS |
144 | if (parse_commit(commit) != 0) |
145 | printf("(bad commit)\n"); | |
146 | else { | |
147 | const char *s; | |
148 | int len; | |
149 | for (s = commit->buffer; *s; s++) | |
150 | if (*s == '\n' && s[1] == '\n') { | |
151 | s += 2; | |
152 | break; | |
153 | } | |
154 | for (len = 0; s[len] && '\n' != s[len]; len++) | |
155 | ; /* do nothing */ | |
156 | printf("%.*s\n", len, s); | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
3f6ee2d1 SP |
161 | static void progress_interval(int signum) |
162 | { | |
163 | progress_update = 1; | |
164 | } | |
165 | ||
166 | static void setup_progress_signal(void) | |
167 | { | |
168 | struct sigaction sa; | |
169 | struct itimerval v; | |
170 | ||
171 | memset(&sa, 0, sizeof(sa)); | |
172 | sa.sa_handler = progress_interval; | |
173 | sigemptyset(&sa.sa_mask); | |
174 | sa.sa_flags = SA_RESTART; | |
175 | sigaction(SIGALRM, &sa, NULL); | |
176 | ||
177 | v.it_interval.tv_sec = 1; | |
178 | v.it_interval.tv_usec = 0; | |
179 | v.it_value = v.it_interval; | |
180 | setitimer(ITIMER_REAL, &v, NULL); | |
181 | } | |
182 | ||
183 | static void display_progress() | |
184 | { | |
185 | unsigned percent = total_cnt ? merged_cnt * 100 / total_cnt : 0; | |
186 | if (progress_update || percent != last_percent) { | |
187 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
188 | percent, merged_cnt, total_cnt); | |
189 | progress_update = 0; | |
190 | last_percent = percent; | |
191 | } | |
192 | } | |
193 | ||
6d297f81 JS |
194 | static struct cache_entry *make_cache_entry(unsigned int mode, |
195 | const unsigned char *sha1, const char *path, int stage, int refresh) | |
196 | { | |
197 | int size, len; | |
198 | struct cache_entry *ce; | |
199 | ||
200 | if (!verify_path(path)) | |
201 | return NULL; | |
202 | ||
203 | len = strlen(path); | |
204 | size = cache_entry_size(len); | |
205 | ce = xcalloc(1, size); | |
206 | ||
8da71493 | 207 | hashcpy(ce->sha1, sha1); |
6d297f81 JS |
208 | memcpy(ce->name, path, len); |
209 | ce->ce_flags = create_ce_flags(len, stage); | |
210 | ce->ce_mode = create_ce_mode(mode); | |
211 | ||
212 | if (refresh) | |
213 | return refresh_cache_entry(ce, 0); | |
214 | ||
215 | return ce; | |
216 | } | |
217 | ||
218 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, | |
219 | const char *path, int stage, int refresh, int options) | |
220 | { | |
221 | struct cache_entry *ce; | |
6d297f81 JS |
222 | ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh); |
223 | if (!ce) | |
224 | return error("cache_addinfo failed: %s", strerror(cache_errno)); | |
225 | return add_cache_entry(ce, options); | |
226 | } | |
227 | ||
228 | /* | |
229 | * This is a global variable which is used in a number of places but | |
230 | * only written to in the 'merge' function. | |
231 | * | |
232 | * index_only == 1 => Don't leave any non-stage 0 entries in the cache and | |
233 | * don't update the working directory. | |
234 | * 0 => Leave unmerged entries in the cache and update | |
235 | * the working directory. | |
236 | */ | |
237 | static int index_only = 0; | |
238 | ||
7a85b848 | 239 | static int git_merge_trees(int index_only, |
6d297f81 JS |
240 | struct tree *common, |
241 | struct tree *head, | |
242 | struct tree *merge) | |
243 | { | |
3af244ca | 244 | int rc; |
7a85b848 JS |
245 | struct object_list *trees = NULL; |
246 | struct unpack_trees_options opts; | |
247 | ||
7a85b848 JS |
248 | memset(&opts, 0, sizeof(opts)); |
249 | if (index_only) | |
250 | opts.index_only = 1; | |
251 | else | |
252 | opts.update = 1; | |
253 | opts.merge = 1; | |
254 | opts.head_idx = 2; | |
255 | opts.fn = threeway_merge; | |
256 | ||
257 | object_list_append(&common->object, &trees); | |
258 | object_list_append(&head->object, &trees); | |
259 | object_list_append(&merge->object, &trees); | |
260 | ||
261 | rc = unpack_trees(trees, &opts); | |
262 | cache_tree_free(&active_cache_tree); | |
7a85b848 | 263 | return rc; |
6d297f81 JS |
264 | } |
265 | ||
8b944b56 JH |
266 | static int unmerged_index(void) |
267 | { | |
268 | int i; | |
269 | for (i = 0; i < active_nr; i++) { | |
270 | struct cache_entry *ce = active_cache[i]; | |
271 | if (ce_stage(ce)) | |
272 | return 1; | |
273 | } | |
274 | return 0; | |
275 | } | |
276 | ||
3af244ca | 277 | static struct tree *git_write_tree(void) |
6d297f81 | 278 | { |
5b982f84 JS |
279 | struct tree *result = NULL; |
280 | ||
a97e4075 AR |
281 | if (unmerged_index()) { |
282 | int i; | |
283 | output(0, "There are unmerged index entries:"); | |
284 | for (i = 0; i < active_nr; i++) { | |
285 | struct cache_entry *ce = active_cache[i]; | |
286 | if (ce_stage(ce)) | |
287 | output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name); | |
288 | } | |
8b944b56 | 289 | return NULL; |
a97e4075 | 290 | } |
5b982f84 JS |
291 | |
292 | if (!active_cache_tree) | |
293 | active_cache_tree = cache_tree(); | |
294 | ||
295 | if (!cache_tree_fully_valid(active_cache_tree) && | |
8b944b56 JH |
296 | cache_tree_update(active_cache_tree, |
297 | active_cache, active_nr, 0, 0) < 0) | |
5b982f84 JS |
298 | die("error building trees"); |
299 | ||
300 | result = lookup_tree(active_cache_tree->sha1); | |
301 | ||
5b982f84 | 302 | return result; |
6d297f81 JS |
303 | } |
304 | ||
6d297f81 JS |
305 | static int save_files_dirs(const unsigned char *sha1, |
306 | const char *base, int baselen, const char *path, | |
307 | unsigned int mode, int stage) | |
308 | { | |
309 | int len = strlen(path); | |
2d7320d0 | 310 | char *newpath = xmalloc(baselen + len + 1); |
6d297f81 JS |
311 | memcpy(newpath, base, baselen); |
312 | memcpy(newpath + baselen, path, len); | |
313 | newpath[baselen + len] = '\0'; | |
314 | ||
315 | if (S_ISDIR(mode)) | |
5a753613 | 316 | path_list_insert(newpath, ¤t_directory_set); |
6d297f81 | 317 | else |
5a753613 | 318 | path_list_insert(newpath, ¤t_file_set); |
6d297f81 JS |
319 | free(newpath); |
320 | ||
321 | return READ_TREE_RECURSIVE; | |
322 | } | |
323 | ||
3af244ca | 324 | static int get_files_dirs(struct tree *tree) |
6d297f81 JS |
325 | { |
326 | int n; | |
bd669986 | 327 | if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) |
6d297f81 | 328 | return 0; |
5a753613 | 329 | n = current_file_set.nr + current_directory_set.nr; |
6d297f81 JS |
330 | return n; |
331 | } | |
332 | ||
6d297f81 JS |
333 | /* |
334 | * Returns a index_entry instance which doesn't have to correspond to | |
335 | * a real cache entry in Git's index. | |
336 | */ | |
3af244ca JS |
337 | static struct stage_data *insert_stage_data(const char *path, |
338 | struct tree *o, struct tree *a, struct tree *b, | |
339 | struct path_list *entries) | |
6d297f81 | 340 | { |
3af244ca | 341 | struct path_list_item *item; |
6d297f81 JS |
342 | struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); |
343 | get_tree_entry(o->object.sha1, path, | |
344 | e->stages[1].sha, &e->stages[1].mode); | |
345 | get_tree_entry(a->object.sha1, path, | |
346 | e->stages[2].sha, &e->stages[2].mode); | |
347 | get_tree_entry(b->object.sha1, path, | |
348 | e->stages[3].sha, &e->stages[3].mode); | |
3af244ca JS |
349 | item = path_list_insert(path, entries); |
350 | item->util = e; | |
6d297f81 JS |
351 | return e; |
352 | } | |
353 | ||
6d297f81 | 354 | /* |
5a753613 | 355 | * Create a dictionary mapping file names to stage_data objects. The |
6d297f81 JS |
356 | * dictionary contains one entry for every path with a non-zero stage entry. |
357 | */ | |
3af244ca | 358 | static struct path_list *get_unmerged(void) |
6d297f81 JS |
359 | { |
360 | struct path_list *unmerged = xcalloc(1, sizeof(struct path_list)); | |
361 | int i; | |
362 | ||
363 | unmerged->strdup_paths = 1; | |
3f6ee2d1 | 364 | total_cnt += active_nr; |
8b944b56 | 365 | |
3f6ee2d1 | 366 | for (i = 0; i < active_nr; i++, merged_cnt++) { |
3af244ca JS |
367 | struct path_list_item *item; |
368 | struct stage_data *e; | |
6d297f81 | 369 | struct cache_entry *ce = active_cache[i]; |
3f6ee2d1 SP |
370 | if (do_progress) |
371 | display_progress(); | |
6d297f81 JS |
372 | if (!ce_stage(ce)) |
373 | continue; | |
374 | ||
3af244ca JS |
375 | item = path_list_lookup(ce->name, unmerged); |
376 | if (!item) { | |
377 | item = path_list_insert(ce->name, unmerged); | |
378 | item->util = xcalloc(1, sizeof(struct stage_data)); | |
379 | } | |
380 | e = item->util; | |
6d297f81 | 381 | e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode); |
8da71493 | 382 | hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1); |
6d297f81 JS |
383 | } |
384 | ||
6d297f81 JS |
385 | return unmerged; |
386 | } | |
387 | ||
388 | struct rename | |
389 | { | |
390 | struct diff_filepair *pair; | |
391 | struct stage_data *src_entry; | |
392 | struct stage_data *dst_entry; | |
393 | unsigned processed:1; | |
394 | }; | |
395 | ||
6d297f81 | 396 | /* |
3dff5379 | 397 | * Get information of all renames which occurred between 'o_tree' and |
5a753613 JS |
398 | * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and |
399 | * 'b_tree') to be able to associate the correct cache entries with | |
400 | * the rename information. 'tree' is always equal to either a_tree or b_tree. | |
6d297f81 JS |
401 | */ |
402 | static struct path_list *get_renames(struct tree *tree, | |
5a753613 JS |
403 | struct tree *o_tree, |
404 | struct tree *a_tree, | |
405 | struct tree *b_tree, | |
6d297f81 JS |
406 | struct path_list *entries) |
407 | { | |
3af244ca JS |
408 | int i; |
409 | struct path_list *renames; | |
410 | struct diff_options opts; | |
3af244ca JS |
411 | |
412 | renames = xcalloc(1, sizeof(struct path_list)); | |
6d297f81 JS |
413 | diff_setup(&opts); |
414 | opts.recursive = 1; | |
415 | opts.detect_rename = DIFF_DETECT_RENAME; | |
416 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
417 | if (diff_setup_done(&opts) < 0) | |
418 | die("diff setup failed"); | |
5a753613 | 419 | diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts); |
6d297f81 JS |
420 | diffcore_std(&opts); |
421 | for (i = 0; i < diff_queued_diff.nr; ++i) { | |
3af244ca | 422 | struct path_list_item *item; |
6d297f81 JS |
423 | struct rename *re; |
424 | struct diff_filepair *pair = diff_queued_diff.queue[i]; | |
425 | if (pair->status != 'R') { | |
426 | diff_free_filepair(pair); | |
427 | continue; | |
428 | } | |
429 | re = xmalloc(sizeof(*re)); | |
430 | re->processed = 0; | |
431 | re->pair = pair; | |
3af244ca JS |
432 | item = path_list_lookup(re->pair->one->path, entries); |
433 | if (!item) | |
434 | re->src_entry = insert_stage_data(re->pair->one->path, | |
5a753613 | 435 | o_tree, a_tree, b_tree, entries); |
3af244ca JS |
436 | else |
437 | re->src_entry = item->util; | |
438 | ||
439 | item = path_list_lookup(re->pair->two->path, entries); | |
440 | if (!item) | |
441 | re->dst_entry = insert_stage_data(re->pair->two->path, | |
5a753613 | 442 | o_tree, a_tree, b_tree, entries); |
3af244ca JS |
443 | else |
444 | re->dst_entry = item->util; | |
445 | item = path_list_insert(pair->one->path, renames); | |
6d297f81 JS |
446 | item->util = re; |
447 | } | |
448 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
449 | diff_queued_diff.nr = 0; | |
450 | diff_flush(&opts); | |
6d297f81 JS |
451 | return renames; |
452 | } | |
453 | ||
9fe0d87d JH |
454 | static int update_stages(const char *path, struct diff_filespec *o, |
455 | struct diff_filespec *a, struct diff_filespec *b, | |
456 | int clear) | |
6d297f81 JS |
457 | { |
458 | int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; | |
3af244ca JS |
459 | if (clear) |
460 | if (remove_file_from_cache(path)) | |
6d297f81 | 461 | return -1; |
3af244ca JS |
462 | if (o) |
463 | if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options)) | |
6d297f81 | 464 | return -1; |
3af244ca JS |
465 | if (a) |
466 | if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options)) | |
6d297f81 | 467 | return -1; |
3af244ca JS |
468 | if (b) |
469 | if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options)) | |
6d297f81 JS |
470 | return -1; |
471 | return 0; | |
472 | } | |
473 | ||
6d297f81 JS |
474 | static int remove_path(const char *name) |
475 | { | |
3af244ca JS |
476 | int ret, len; |
477 | char *slash, *dirs; | |
6d297f81 JS |
478 | |
479 | ret = unlink(name); | |
3af244ca | 480 | if (ret) |
6d297f81 | 481 | return ret; |
3af244ca | 482 | len = strlen(name); |
2d7320d0 | 483 | dirs = xmalloc(len+1); |
6d297f81 JS |
484 | memcpy(dirs, name, len); |
485 | dirs[len] = '\0'; | |
3af244ca | 486 | while ((slash = strrchr(name, '/'))) { |
6d297f81 JS |
487 | *slash = '\0'; |
488 | len = slash - name; | |
3af244ca | 489 | if (rmdir(name) != 0) |
6d297f81 JS |
490 | break; |
491 | } | |
492 | free(dirs); | |
493 | return ret; | |
494 | } | |
495 | ||
65ac6e9c | 496 | static int remove_file(int clean, const char *path, int no_wd) |
6d297f81 | 497 | { |
5a753613 | 498 | int update_cache = index_only || clean; |
65ac6e9c | 499 | int update_working_directory = !index_only && !no_wd; |
6d297f81 | 500 | |
5a753613 | 501 | if (update_cache) { |
6d297f81 JS |
502 | if (remove_file_from_cache(path)) |
503 | return -1; | |
504 | } | |
65ac6e9c | 505 | if (update_working_directory) { |
6d297f81 | 506 | unlink(path); |
3af244ca | 507 | if (errno != ENOENT || errno != EISDIR) |
6d297f81 JS |
508 | return -1; |
509 | remove_path(path); | |
510 | } | |
511 | return 0; | |
512 | } | |
513 | ||
514 | static char *unique_path(const char *path, const char *branch) | |
515 | { | |
516 | char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1); | |
3af244ca JS |
517 | int suffix = 0; |
518 | struct stat st; | |
f59aac47 | 519 | char *p = newpath + strlen(path); |
6d297f81 | 520 | strcpy(newpath, path); |
f59aac47 | 521 | *(p++) = '~'; |
6d297f81 | 522 | strcpy(p, branch); |
3af244ca JS |
523 | for (; *p; ++p) |
524 | if ('/' == *p) | |
6d297f81 | 525 | *p = '_'; |
5a753613 JS |
526 | while (path_list_has_path(¤t_file_set, newpath) || |
527 | path_list_has_path(¤t_directory_set, newpath) || | |
3af244ca | 528 | lstat(newpath, &st) == 0) |
6d297f81 | 529 | sprintf(p, "_%d", suffix++); |
3af244ca | 530 | |
5a753613 | 531 | path_list_insert(newpath, ¤t_file_set); |
6d297f81 JS |
532 | return newpath; |
533 | } | |
534 | ||
3af244ca | 535 | static int mkdir_p(const char *path, unsigned long mode) |
6d297f81 | 536 | { |
9befac47 SP |
537 | /* path points to cache entries, so xstrdup before messing with it */ |
538 | char *buf = xstrdup(path); | |
3af244ca | 539 | int result = safe_create_leading_directories(buf); |
6d297f81 | 540 | free(buf); |
3af244ca | 541 | return result; |
6d297f81 JS |
542 | } |
543 | ||
544 | static void flush_buffer(int fd, const char *buf, unsigned long size) | |
545 | { | |
546 | while (size > 0) { | |
93822c22 | 547 | long ret = write_in_full(fd, buf, size); |
6d297f81 JS |
548 | if (ret < 0) { |
549 | /* Ignore epipe */ | |
550 | if (errno == EPIPE) | |
551 | break; | |
552 | die("merge-recursive: %s", strerror(errno)); | |
553 | } else if (!ret) { | |
554 | die("merge-recursive: disk full?"); | |
555 | } | |
556 | size -= ret; | |
557 | buf += ret; | |
558 | } | |
559 | } | |
560 | ||
9fe0d87d JH |
561 | static void update_file_flags(const unsigned char *sha, |
562 | unsigned mode, | |
563 | const char *path, | |
564 | int update_cache, | |
565 | int update_wd) | |
6d297f81 | 566 | { |
3af244ca JS |
567 | if (index_only) |
568 | update_wd = 0; | |
6d297f81 | 569 | |
3af244ca | 570 | if (update_wd) { |
21666f1a | 571 | enum object_type type; |
6d297f81 JS |
572 | void *buf; |
573 | unsigned long size; | |
574 | ||
21666f1a | 575 | buf = read_sha1_file(sha, &type, &size); |
6d297f81 JS |
576 | if (!buf) |
577 | die("cannot read object %s '%s'", sha1_to_hex(sha), path); | |
21666f1a | 578 | if (type != OBJ_BLOB) |
6d297f81 JS |
579 | die("blob expected for %s '%s'", sha1_to_hex(sha), path); |
580 | ||
723024d6 | 581 | if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { |
3af244ca JS |
582 | int fd; |
583 | if (mkdir_p(path, 0777)) | |
6d297f81 JS |
584 | die("failed to create path %s: %s", path, strerror(errno)); |
585 | unlink(path); | |
3af244ca | 586 | if (mode & 0100) |
6d297f81 JS |
587 | mode = 0777; |
588 | else | |
589 | mode = 0666; | |
3af244ca JS |
590 | fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); |
591 | if (fd < 0) | |
6d297f81 JS |
592 | die("failed to open %s: %s", path, strerror(errno)); |
593 | flush_buffer(fd, buf, size); | |
594 | close(fd); | |
3af244ca | 595 | } else if (S_ISLNK(mode)) { |
2d7320d0 | 596 | char *lnk = xmalloc(size + 1); |
3af244ca JS |
597 | memcpy(lnk, buf, size); |
598 | lnk[size] = '\0'; | |
599 | mkdir_p(path, 0777); | |
17cd29b2 | 600 | unlink(path); |
3af244ca | 601 | symlink(lnk, path); |
723024d6 | 602 | free(lnk); |
6d297f81 JS |
603 | } else |
604 | die("do not know what to do with %06o %s '%s'", | |
605 | mode, sha1_to_hex(sha), path); | |
606 | } | |
3af244ca JS |
607 | if (update_cache) |
608 | add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); | |
6d297f81 JS |
609 | } |
610 | ||
9fe0d87d JH |
611 | static void update_file(int clean, |
612 | const unsigned char *sha, | |
613 | unsigned mode, | |
614 | const char *path) | |
6d297f81 JS |
615 | { |
616 | update_file_flags(sha, mode, path, index_only || clean, !index_only); | |
617 | } | |
618 | ||
619 | /* Low level file merging, update and removal */ | |
620 | ||
621 | struct merge_file_info | |
622 | { | |
623 | unsigned char sha[20]; | |
624 | unsigned mode; | |
625 | unsigned clean:1, | |
626 | merge:1; | |
627 | }; | |
628 | ||
c2b4faea | 629 | static void fill_mm(const unsigned char *sha1, mmfile_t *mm) |
6d297f81 | 630 | { |
6d297f81 | 631 | unsigned long size; |
21666f1a | 632 | enum object_type type; |
6d297f81 | 633 | |
f953831e JS |
634 | if (!hashcmp(sha1, null_sha1)) { |
635 | mm->ptr = xstrdup(""); | |
636 | mm->size = 0; | |
637 | return; | |
638 | } | |
6d297f81 | 639 | |
21666f1a NP |
640 | mm->ptr = read_sha1_file(sha1, &type, &size); |
641 | if (!mm->ptr || type != OBJ_BLOB) | |
6d297f81 | 642 | die("unable to read blob object %s", sha1_to_hex(sha1)); |
c2b4faea | 643 | mm->size = size; |
6d297f81 JS |
644 | } |
645 | ||
3af244ca JS |
646 | static struct merge_file_info merge_file(struct diff_filespec *o, |
647 | struct diff_filespec *a, struct diff_filespec *b, | |
c1d20846 | 648 | const char *branch1, const char *branch2) |
6d297f81 JS |
649 | { |
650 | struct merge_file_info result; | |
651 | result.merge = 0; | |
652 | result.clean = 1; | |
653 | ||
3af244ca | 654 | if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { |
6d297f81 | 655 | result.clean = 0; |
3af244ca JS |
656 | if (S_ISREG(a->mode)) { |
657 | result.mode = a->mode; | |
8da71493 | 658 | hashcpy(result.sha, a->sha1); |
6d297f81 | 659 | } else { |
3af244ca | 660 | result.mode = b->mode; |
8da71493 | 661 | hashcpy(result.sha, b->sha1); |
6d297f81 JS |
662 | } |
663 | } else { | |
3af244ca | 664 | if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1)) |
6d297f81 JS |
665 | result.merge = 1; |
666 | ||
3af244ca | 667 | result.mode = a->mode == o->mode ? b->mode: a->mode; |
6d297f81 | 668 | |
3af244ca | 669 | if (sha_eq(a->sha1, o->sha1)) |
8da71493 | 670 | hashcpy(result.sha, b->sha1); |
3af244ca | 671 | else if (sha_eq(b->sha1, o->sha1)) |
8da71493 | 672 | hashcpy(result.sha, a->sha1); |
3af244ca | 673 | else if (S_ISREG(a->mode)) { |
c2b4faea JH |
674 | mmfile_t orig, src1, src2; |
675 | mmbuffer_t result_buf; | |
676 | xpparam_t xpp; | |
677 | char *name1, *name2; | |
678 | int merge_status; | |
679 | ||
d9606e85 SP |
680 | name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); |
681 | name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); | |
c2b4faea JH |
682 | |
683 | fill_mm(o->sha1, &orig); | |
684 | fill_mm(a->sha1, &src1); | |
685 | fill_mm(b->sha1, &src2); | |
686 | ||
687 | memset(&xpp, 0, sizeof(xpp)); | |
688 | merge_status = xdl_merge(&orig, | |
689 | &src1, name1, | |
690 | &src2, name2, | |
691 | &xpp, XDL_MERGE_ZEALOUS, | |
692 | &result_buf); | |
693 | free(name1); | |
694 | free(name2); | |
695 | free(orig.ptr); | |
696 | free(src1.ptr); | |
697 | free(src2.ptr); | |
698 | ||
699 | if ((merge_status < 0) || !result_buf.ptr) | |
700 | die("Failed to execute internal merge"); | |
701 | ||
702 | if (write_sha1_file(result_buf.ptr, result_buf.size, | |
703 | blob_type, result.sha)) | |
704 | die("Unable to add %s to database", | |
705 | a->path); | |
706 | ||
707 | free(result_buf.ptr); | |
708 | result.clean = (merge_status == 0); | |
6d297f81 | 709 | } else { |
3af244ca | 710 | if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode))) |
6d297f81 JS |
711 | die("cannot merge modes?"); |
712 | ||
8da71493 | 713 | hashcpy(result.sha, a->sha1); |
6d297f81 | 714 | |
3af244ca | 715 | if (!sha_eq(a->sha1, b->sha1)) |
6d297f81 JS |
716 | result.clean = 0; |
717 | } | |
718 | } | |
719 | ||
720 | return result; | |
721 | } | |
722 | ||
723 | static void conflict_rename_rename(struct rename *ren1, | |
724 | const char *branch1, | |
725 | struct rename *ren2, | |
726 | const char *branch2) | |
727 | { | |
728 | char *del[2]; | |
729 | int delp = 0; | |
730 | const char *ren1_dst = ren1->pair->two->path; | |
731 | const char *ren2_dst = ren2->pair->two->path; | |
5a753613 JS |
732 | const char *dst_name1 = ren1_dst; |
733 | const char *dst_name2 = ren2_dst; | |
734 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { | |
735 | dst_name1 = del[delp++] = unique_path(ren1_dst, branch1); | |
89f40be2 | 736 | output(1, "%s is a directory in %s added as %s instead", |
5a753613 | 737 | ren1_dst, branch2, dst_name1); |
65ac6e9c | 738 | remove_file(0, ren1_dst, 0); |
6d297f81 | 739 | } |
5a753613 JS |
740 | if (path_list_has_path(¤t_directory_set, ren2_dst)) { |
741 | dst_name2 = del[delp++] = unique_path(ren2_dst, branch2); | |
89f40be2 | 742 | output(1, "%s is a directory in %s added as %s instead", |
5a753613 | 743 | ren2_dst, branch1, dst_name2); |
65ac6e9c | 744 | remove_file(0, ren2_dst, 0); |
6d297f81 | 745 | } |
a97e4075 AR |
746 | if (index_only) { |
747 | remove_file_from_cache(dst_name1); | |
748 | remove_file_from_cache(dst_name2); | |
749 | /* | |
750 | * Uncomment to leave the conflicting names in the resulting tree | |
751 | * | |
752 | * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1); | |
753 | * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2); | |
754 | */ | |
755 | } else { | |
756 | update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1); | |
757 | update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1); | |
758 | } | |
3af244ca | 759 | while (delp--) |
6d297f81 JS |
760 | free(del[delp]); |
761 | } | |
762 | ||
763 | static void conflict_rename_dir(struct rename *ren1, | |
764 | const char *branch1) | |
765 | { | |
5a753613 | 766 | char *new_path = unique_path(ren1->pair->two->path, branch1); |
89f40be2 | 767 | output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path); |
65ac6e9c | 768 | remove_file(0, ren1->pair->two->path, 0); |
5a753613 JS |
769 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path); |
770 | free(new_path); | |
6d297f81 JS |
771 | } |
772 | ||
773 | static void conflict_rename_rename_2(struct rename *ren1, | |
774 | const char *branch1, | |
775 | struct rename *ren2, | |
776 | const char *branch2) | |
777 | { | |
5a753613 JS |
778 | char *new_path1 = unique_path(ren1->pair->two->path, branch1); |
779 | char *new_path2 = unique_path(ren2->pair->two->path, branch2); | |
89f40be2 | 780 | output(1, "Renamed %s to %s and %s to %s instead", |
5a753613 JS |
781 | ren1->pair->one->path, new_path1, |
782 | ren2->pair->one->path, new_path2); | |
65ac6e9c | 783 | remove_file(0, ren1->pair->two->path, 0); |
5a753613 JS |
784 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1); |
785 | update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2); | |
786 | free(new_path2); | |
787 | free(new_path1); | |
6d297f81 JS |
788 | } |
789 | ||
5a753613 JS |
790 | static int process_renames(struct path_list *a_renames, |
791 | struct path_list *b_renames, | |
792 | const char *a_branch, | |
793 | const char *b_branch) | |
6d297f81 | 794 | { |
5a753613 JS |
795 | int clean_merge = 1, i, j; |
796 | struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0}; | |
6d297f81 JS |
797 | const struct rename *sre; |
798 | ||
5a753613 JS |
799 | for (i = 0; i < a_renames->nr; i++) { |
800 | sre = a_renames->items[i].util; | |
801 | path_list_insert(sre->pair->two->path, &a_by_dst)->util | |
6d297f81 JS |
802 | = sre->dst_entry; |
803 | } | |
5a753613 JS |
804 | for (i = 0; i < b_renames->nr; i++) { |
805 | sre = b_renames->items[i].util; | |
806 | path_list_insert(sre->pair->two->path, &b_by_dst)->util | |
6d297f81 JS |
807 | = sre->dst_entry; |
808 | } | |
809 | ||
5a753613 | 810 | for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { |
3af244ca JS |
811 | int compare; |
812 | char *src; | |
6d297f81 | 813 | struct path_list *renames1, *renames2, *renames2Dst; |
3af244ca | 814 | struct rename *ren1 = NULL, *ren2 = NULL; |
5a753613 | 815 | const char *branch1, *branch2; |
3af244ca JS |
816 | const char *ren1_src, *ren1_dst; |
817 | ||
5a753613 | 818 | if (i >= a_renames->nr) { |
3af244ca | 819 | compare = 1; |
5a753613 JS |
820 | ren2 = b_renames->items[j++].util; |
821 | } else if (j >= b_renames->nr) { | |
3af244ca | 822 | compare = -1; |
5a753613 | 823 | ren1 = a_renames->items[i++].util; |
3af244ca | 824 | } else { |
5a753613 JS |
825 | compare = strcmp(a_renames->items[i].path, |
826 | b_renames->items[j].path); | |
3d234d0a JS |
827 | if (compare <= 0) |
828 | ren1 = a_renames->items[i++].util; | |
829 | if (compare >= 0) | |
830 | ren2 = b_renames->items[j++].util; | |
3af244ca JS |
831 | } |
832 | ||
6d297f81 | 833 | /* TODO: refactor, so that 1/2 are not needed */ |
3af244ca | 834 | if (ren1) { |
5a753613 JS |
835 | renames1 = a_renames; |
836 | renames2 = b_renames; | |
837 | renames2Dst = &b_by_dst; | |
838 | branch1 = a_branch; | |
839 | branch2 = b_branch; | |
6d297f81 | 840 | } else { |
3af244ca | 841 | struct rename *tmp; |
5a753613 JS |
842 | renames1 = b_renames; |
843 | renames2 = a_renames; | |
844 | renames2Dst = &a_by_dst; | |
845 | branch1 = b_branch; | |
846 | branch2 = a_branch; | |
3af244ca | 847 | tmp = ren2; |
6d297f81 JS |
848 | ren2 = ren1; |
849 | ren1 = tmp; | |
850 | } | |
3af244ca | 851 | src = ren1->pair->one->path; |
6d297f81 JS |
852 | |
853 | ren1->dst_entry->processed = 1; | |
854 | ren1->src_entry->processed = 1; | |
855 | ||
3af244ca | 856 | if (ren1->processed) |
6d297f81 JS |
857 | continue; |
858 | ren1->processed = 1; | |
859 | ||
3af244ca JS |
860 | ren1_src = ren1->pair->one->path; |
861 | ren1_dst = ren1->pair->two->path; | |
6d297f81 | 862 | |
3af244ca | 863 | if (ren2) { |
6d297f81 JS |
864 | const char *ren2_src = ren2->pair->one->path; |
865 | const char *ren2_dst = ren2->pair->two->path; | |
866 | /* Renamed in 1 and renamed in 2 */ | |
867 | if (strcmp(ren1_src, ren2_src) != 0) | |
868 | die("ren1.src != ren2.src"); | |
869 | ren2->dst_entry->processed = 1; | |
870 | ren2->processed = 1; | |
871 | if (strcmp(ren1_dst, ren2_dst) != 0) { | |
5a753613 | 872 | clean_merge = 0; |
8c3275ab | 873 | output(1, "CONFLICT (rename/rename): " |
a97e4075 AR |
874 | "Rename \"%s\"->\"%s\" in branch \"%s\" " |
875 | "rename \"%s\"->\"%s\" in \"%s\"%s", | |
5a753613 | 876 | src, ren1_dst, branch1, |
a97e4075 AR |
877 | src, ren2_dst, branch2, |
878 | index_only ? " (left unresolved)": ""); | |
879 | if (index_only) { | |
880 | remove_file_from_cache(src); | |
881 | update_file(0, ren1->pair->one->sha1, | |
882 | ren1->pair->one->mode, src); | |
883 | } | |
5a753613 | 884 | conflict_rename_rename(ren1, branch1, ren2, branch2); |
6d297f81 | 885 | } else { |
6d297f81 | 886 | struct merge_file_info mfi; |
65ac6e9c | 887 | remove_file(1, ren1_src, 1); |
3af244ca JS |
888 | mfi = merge_file(ren1->pair->one, |
889 | ren1->pair->two, | |
890 | ren2->pair->two, | |
5a753613 JS |
891 | branch1, |
892 | branch2); | |
3af244ca | 893 | if (mfi.merge || !mfi.clean) |
89f40be2 | 894 | output(1, "Renamed %s->%s", src, ren1_dst); |
6d297f81 | 895 | |
3af244ca | 896 | if (mfi.merge) |
89f40be2 | 897 | output(2, "Auto-merged %s", ren1_dst); |
6d297f81 | 898 | |
3af244ca | 899 | if (!mfi.clean) { |
8c3275ab | 900 | output(1, "CONFLICT (content): merge conflict in %s", |
6d297f81 | 901 | ren1_dst); |
5a753613 | 902 | clean_merge = 0; |
6d297f81 | 903 | |
3af244ca | 904 | if (!index_only) |
6d297f81 | 905 | update_stages(ren1_dst, |
3af244ca JS |
906 | ren1->pair->one, |
907 | ren1->pair->two, | |
908 | ren2->pair->two, | |
6d297f81 JS |
909 | 1 /* clear */); |
910 | } | |
911 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); | |
912 | } | |
913 | } else { | |
914 | /* Renamed in 1, maybe changed in 2 */ | |
3af244ca JS |
915 | struct path_list_item *item; |
916 | /* we only use sha1 and mode of these */ | |
917 | struct diff_filespec src_other, dst_other; | |
5a753613 | 918 | int try_merge, stage = a_renames == renames1 ? 3: 2; |
6d297f81 | 919 | |
183d7972 | 920 | remove_file(1, ren1_src, index_only || stage == 3); |
6d297f81 | 921 | |
87cb004e | 922 | hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha); |
3af244ca | 923 | src_other.mode = ren1->src_entry->stages[stage].mode; |
87cb004e | 924 | hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha); |
3af244ca | 925 | dst_other.mode = ren1->dst_entry->stages[stage].mode; |
6d297f81 | 926 | |
5a753613 | 927 | try_merge = 0; |
6d297f81 | 928 | |
5a753613 JS |
929 | if (path_list_has_path(¤t_directory_set, ren1_dst)) { |
930 | clean_merge = 0; | |
89f40be2 | 931 | output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s " |
6d297f81 | 932 | " directory %s added in %s", |
5a753613 JS |
933 | ren1_src, ren1_dst, branch1, |
934 | ren1_dst, branch2); | |
935 | conflict_rename_dir(ren1, branch1); | |
3af244ca | 936 | } else if (sha_eq(src_other.sha1, null_sha1)) { |
5a753613 | 937 | clean_merge = 0; |
89f40be2 | 938 | output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s " |
6d297f81 | 939 | "and deleted in %s", |
5a753613 JS |
940 | ren1_src, ren1_dst, branch1, |
941 | branch2); | |
6d297f81 | 942 | update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst); |
3af244ca | 943 | } else if (!sha_eq(dst_other.sha1, null_sha1)) { |
5a753613 JS |
944 | const char *new_path; |
945 | clean_merge = 0; | |
946 | try_merge = 1; | |
89f40be2 | 947 | output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. " |
6d297f81 | 948 | "%s added in %s", |
5a753613 JS |
949 | ren1_src, ren1_dst, branch1, |
950 | ren1_dst, branch2); | |
951 | new_path = unique_path(ren1_dst, branch2); | |
89f40be2 | 952 | output(1, "Added as %s instead", new_path); |
5a753613 | 953 | update_file(0, dst_other.sha1, dst_other.mode, new_path); |
3af244ca JS |
954 | } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) { |
955 | ren2 = item->util; | |
5a753613 | 956 | clean_merge = 0; |
6d297f81 | 957 | ren2->processed = 1; |
89f40be2 SP |
958 | output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. " |
959 | "Renamed %s->%s in %s", | |
5a753613 JS |
960 | ren1_src, ren1_dst, branch1, |
961 | ren2->pair->one->path, ren2->pair->two->path, branch2); | |
962 | conflict_rename_rename_2(ren1, branch1, ren2, branch2); | |
6d297f81 | 963 | } else |
5a753613 | 964 | try_merge = 1; |
6d297f81 | 965 | |
5a753613 | 966 | if (try_merge) { |
3af244ca | 967 | struct diff_filespec *o, *a, *b; |
6d297f81 | 968 | struct merge_file_info mfi; |
3af244ca JS |
969 | src_other.path = (char *)ren1_src; |
970 | ||
971 | o = ren1->pair->one; | |
5a753613 | 972 | if (a_renames == renames1) { |
3af244ca JS |
973 | a = ren1->pair->two; |
974 | b = &src_other; | |
975 | } else { | |
976 | b = ren1->pair->two; | |
977 | a = &src_other; | |
978 | } | |
979 | mfi = merge_file(o, a, b, | |
5a753613 | 980 | a_branch, b_branch); |
6d297f81 | 981 | |
3af244ca | 982 | if (mfi.merge || !mfi.clean) |
89f40be2 | 983 | output(1, "Renamed %s => %s", ren1_src, ren1_dst); |
3af244ca | 984 | if (mfi.merge) |
89f40be2 | 985 | output(2, "Auto-merged %s", ren1_dst); |
3af244ca | 986 | if (!mfi.clean) { |
8c3275ab | 987 | output(1, "CONFLICT (rename/modify): Merge conflict in %s", |
6d297f81 | 988 | ren1_dst); |
5a753613 | 989 | clean_merge = 0; |
6d297f81 | 990 | |
3af244ca | 991 | if (!index_only) |
6d297f81 | 992 | update_stages(ren1_dst, |
3af244ca | 993 | o, a, b, 1); |
6d297f81 JS |
994 | } |
995 | update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst); | |
996 | } | |
997 | } | |
998 | } | |
5a753613 JS |
999 | path_list_clear(&a_by_dst, 0); |
1000 | path_list_clear(&b_by_dst, 0); | |
6d297f81 | 1001 | |
5a753613 | 1002 | return clean_merge; |
6d297f81 JS |
1003 | } |
1004 | ||
1005 | static unsigned char *has_sha(const unsigned char *sha) | |
1006 | { | |
87cb004e | 1007 | return is_null_sha1(sha) ? NULL: (unsigned char *)sha; |
6d297f81 JS |
1008 | } |
1009 | ||
1010 | /* Per entry merge function */ | |
1011 | static int process_entry(const char *path, struct stage_data *entry, | |
c1d20846 JS |
1012 | const char *branch1, |
1013 | const char *branch2) | |
6d297f81 JS |
1014 | { |
1015 | /* | |
1016 | printf("processing entry, clean cache: %s\n", index_only ? "yes": "no"); | |
1017 | print_index_entry("\tpath: ", entry); | |
1018 | */ | |
5a753613 JS |
1019 | int clean_merge = 1; |
1020 | unsigned char *o_sha = has_sha(entry->stages[1].sha); | |
1021 | unsigned char *a_sha = has_sha(entry->stages[2].sha); | |
1022 | unsigned char *b_sha = has_sha(entry->stages[3].sha); | |
1023 | unsigned o_mode = entry->stages[1].mode; | |
1024 | unsigned a_mode = entry->stages[2].mode; | |
1025 | unsigned b_mode = entry->stages[3].mode; | |
1026 | ||
1027 | if (o_sha && (!a_sha || !b_sha)) { | |
6d297f81 | 1028 | /* Case A: Deleted in one */ |
5a753613 JS |
1029 | if ((!a_sha && !b_sha) || |
1030 | (sha_eq(a_sha, o_sha) && !b_sha) || | |
1031 | (!a_sha && sha_eq(b_sha, o_sha))) { | |
6d297f81 JS |
1032 | /* Deleted in both or deleted in one and |
1033 | * unchanged in the other */ | |
5a753613 | 1034 | if (a_sha) |
89f40be2 | 1035 | output(2, "Removed %s", path); |
65ac6e9c JH |
1036 | /* do not touch working file if it did not exist */ |
1037 | remove_file(1, path, !a_sha); | |
6d297f81 JS |
1038 | } else { |
1039 | /* Deleted in one and changed in the other */ | |
5a753613 JS |
1040 | clean_merge = 0; |
1041 | if (!a_sha) { | |
8c3275ab | 1042 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
6d297f81 | 1043 | "and modified in %s. Version %s of %s left in tree.", |
c1d20846 JS |
1044 | path, branch1, |
1045 | branch2, branch2, path); | |
5a753613 | 1046 | update_file(0, b_sha, b_mode, path); |
6d297f81 | 1047 | } else { |
8c3275ab | 1048 | output(1, "CONFLICT (delete/modify): %s deleted in %s " |
6d297f81 | 1049 | "and modified in %s. Version %s of %s left in tree.", |
c1d20846 JS |
1050 | path, branch2, |
1051 | branch1, branch1, path); | |
5a753613 | 1052 | update_file(0, a_sha, a_mode, path); |
6d297f81 JS |
1053 | } |
1054 | } | |
1055 | ||
5a753613 JS |
1056 | } else if ((!o_sha && a_sha && !b_sha) || |
1057 | (!o_sha && !a_sha && b_sha)) { | |
6d297f81 | 1058 | /* Case B: Added in one. */ |
5a753613 JS |
1059 | const char *add_branch; |
1060 | const char *other_branch; | |
6d297f81 JS |
1061 | unsigned mode; |
1062 | const unsigned char *sha; | |
1063 | const char *conf; | |
1064 | ||
5a753613 | 1065 | if (a_sha) { |
c1d20846 JS |
1066 | add_branch = branch1; |
1067 | other_branch = branch2; | |
5a753613 JS |
1068 | mode = a_mode; |
1069 | sha = a_sha; | |
6d297f81 JS |
1070 | conf = "file/directory"; |
1071 | } else { | |
c1d20846 JS |
1072 | add_branch = branch2; |
1073 | other_branch = branch1; | |
5a753613 JS |
1074 | mode = b_mode; |
1075 | sha = b_sha; | |
6d297f81 JS |
1076 | conf = "directory/file"; |
1077 | } | |
5a753613 JS |
1078 | if (path_list_has_path(¤t_directory_set, path)) { |
1079 | const char *new_path = unique_path(path, add_branch); | |
1080 | clean_merge = 0; | |
8c3275ab | 1081 | output(1, "CONFLICT (%s): There is a directory with name %s in %s. " |
89f40be2 | 1082 | "Added %s as %s", |
5a753613 | 1083 | conf, path, other_branch, path, new_path); |
65ac6e9c | 1084 | remove_file(0, path, 0); |
5a753613 | 1085 | update_file(0, sha, mode, new_path); |
6d297f81 | 1086 | } else { |
89f40be2 | 1087 | output(2, "Added %s", path); |
6d297f81 JS |
1088 | update_file(1, sha, mode, path); |
1089 | } | |
f953831e JS |
1090 | } else if (a_sha && b_sha) { |
1091 | /* Case C: Added in both (check for same permissions) and */ | |
6d297f81 | 1092 | /* case D: Modified in both, but differently. */ |
f953831e | 1093 | const char *reason = "content"; |
6d297f81 | 1094 | struct merge_file_info mfi; |
3af244ca JS |
1095 | struct diff_filespec o, a, b; |
1096 | ||
f953831e JS |
1097 | if (!o_sha) { |
1098 | reason = "add/add"; | |
1099 | o_sha = (unsigned char *)null_sha1; | |
1100 | } | |
89f40be2 | 1101 | output(2, "Auto-merged %s", path); |
3af244ca | 1102 | o.path = a.path = b.path = (char *)path; |
8da71493 | 1103 | hashcpy(o.sha1, o_sha); |
5a753613 | 1104 | o.mode = o_mode; |
8da71493 | 1105 | hashcpy(a.sha1, a_sha); |
5a753613 | 1106 | a.mode = a_mode; |
8da71493 | 1107 | hashcpy(b.sha1, b_sha); |
5a753613 | 1108 | b.mode = b_mode; |
3af244ca JS |
1109 | |
1110 | mfi = merge_file(&o, &a, &b, | |
c1d20846 | 1111 | branch1, branch2); |
6d297f81 | 1112 | |
3af244ca | 1113 | if (mfi.clean) |
6d297f81 JS |
1114 | update_file(1, mfi.sha, mfi.mode, path); |
1115 | else { | |
5a753613 | 1116 | clean_merge = 0; |
8c3275ab | 1117 | output(1, "CONFLICT (%s): Merge conflict in %s", |
f953831e | 1118 | reason, path); |
6d297f81 | 1119 | |
3af244ca | 1120 | if (index_only) |
6d297f81 JS |
1121 | update_file(0, mfi.sha, mfi.mode, path); |
1122 | else | |
1123 | update_file_flags(mfi.sha, mfi.mode, path, | |
5a753613 | 1124 | 0 /* update_cache */, 1 /* update_working_directory */); |
6d297f81 JS |
1125 | } |
1126 | } else | |
1127 | die("Fatal merge failure, shouldn't happen."); | |
1128 | ||
5a753613 | 1129 | return clean_merge; |
6d297f81 JS |
1130 | } |
1131 | ||
3af244ca JS |
1132 | static int merge_trees(struct tree *head, |
1133 | struct tree *merge, | |
1134 | struct tree *common, | |
c1d20846 JS |
1135 | const char *branch1, |
1136 | const char *branch2, | |
3af244ca | 1137 | struct tree **result) |
6d297f81 | 1138 | { |
3af244ca JS |
1139 | int code, clean; |
1140 | if (sha_eq(common->object.sha1, merge->object.sha1)) { | |
8c3275ab | 1141 | output(0, "Already uptodate!"); |
3af244ca JS |
1142 | *result = head; |
1143 | return 1; | |
6d297f81 JS |
1144 | } |
1145 | ||
7a85b848 | 1146 | code = git_merge_trees(index_only, common, head, merge); |
6d297f81 | 1147 | |
3af244ca | 1148 | if (code != 0) |
6d297f81 JS |
1149 | die("merging of trees %s and %s failed", |
1150 | sha1_to_hex(head->object.sha1), | |
1151 | sha1_to_hex(merge->object.sha1)); | |
1152 | ||
8b944b56 | 1153 | if (unmerged_index()) { |
3af244ca JS |
1154 | struct path_list *entries, *re_head, *re_merge; |
1155 | int i; | |
5a753613 JS |
1156 | path_list_clear(¤t_file_set, 1); |
1157 | path_list_clear(¤t_directory_set, 1); | |
3af244ca JS |
1158 | get_files_dirs(head); |
1159 | get_files_dirs(merge); | |
6d297f81 | 1160 | |
3af244ca | 1161 | entries = get_unmerged(); |
6d297f81 JS |
1162 | re_head = get_renames(head, common, head, merge, entries); |
1163 | re_merge = get_renames(merge, common, head, merge, entries); | |
3af244ca | 1164 | clean = process_renames(re_head, re_merge, |
c1d20846 | 1165 | branch1, branch2); |
3f6ee2d1 SP |
1166 | total_cnt += entries->nr; |
1167 | for (i = 0; i < entries->nr; i++, merged_cnt++) { | |
6d297f81 JS |
1168 | const char *path = entries->items[i].path; |
1169 | struct stage_data *e = entries->items[i].util; | |
3f6ee2d1 SP |
1170 | if (!e->processed |
1171 | && !process_entry(path, e, branch1, branch2)) | |
3af244ca | 1172 | clean = 0; |
3f6ee2d1 SP |
1173 | if (do_progress) |
1174 | display_progress(); | |
6d297f81 JS |
1175 | } |
1176 | ||
3af244ca JS |
1177 | path_list_clear(re_merge, 0); |
1178 | path_list_clear(re_head, 0); | |
1179 | path_list_clear(entries, 1); | |
6d297f81 | 1180 | |
6d297f81 | 1181 | } |
1cf716a2 JH |
1182 | else |
1183 | clean = 1; | |
1184 | ||
8b944b56 JH |
1185 | if (index_only) |
1186 | *result = git_write_tree(); | |
6d297f81 | 1187 | |
3af244ca | 1188 | return clean; |
6d297f81 JS |
1189 | } |
1190 | ||
8918b0c9 JS |
1191 | static struct commit_list *reverse_commit_list(struct commit_list *list) |
1192 | { | |
1193 | struct commit_list *next = NULL, *current, *backup; | |
1194 | for (current = list; current; current = backup) { | |
1195 | backup = current->next; | |
1196 | current->next = next; | |
1197 | next = current; | |
1198 | } | |
1199 | return next; | |
1200 | } | |
1201 | ||
6d297f81 JS |
1202 | /* |
1203 | * Merge the commits h1 and h2, return the resulting virtual | |
3dff5379 | 1204 | * commit object and a flag indicating the cleanness of the merge. |
6d297f81 | 1205 | */ |
65ac6e9c JH |
1206 | static int merge(struct commit *h1, |
1207 | struct commit *h2, | |
1208 | const char *branch1, | |
1209 | const char *branch2, | |
8b944b56 | 1210 | struct commit_list *ca, |
65ac6e9c | 1211 | struct commit **result) |
6d297f81 | 1212 | { |
8b944b56 | 1213 | struct commit_list *iter; |
5a753613 | 1214 | struct commit *merged_common_ancestors; |
3af244ca JS |
1215 | struct tree *mrtree; |
1216 | int clean; | |
6d297f81 | 1217 | |
8c3275ab SP |
1218 | if (show(4)) { |
1219 | output(4, "Merging:"); | |
1220 | output_commit_title(h1); | |
1221 | output_commit_title(h2); | |
1222 | } | |
6d297f81 | 1223 | |
8b944b56 JH |
1224 | if (!ca) { |
1225 | ca = get_merge_bases(h1, h2, 1); | |
1226 | ca = reverse_commit_list(ca); | |
1227 | } | |
6d297f81 | 1228 | |
8c3275ab SP |
1229 | if (show(5)) { |
1230 | output(5, "found %u common ancestor(s):", commit_list_count(ca)); | |
1231 | for (iter = ca; iter; iter = iter->next) | |
1232 | output_commit_title(iter->item); | |
1233 | } | |
6d297f81 | 1234 | |
5a753613 | 1235 | merged_common_ancestors = pop_commit(&ca); |
934d9a24 JS |
1236 | if (merged_common_ancestors == NULL) { |
1237 | /* if there is no common ancestor, make an empty tree */ | |
1238 | struct tree *tree = xcalloc(1, sizeof(struct tree)); | |
934d9a24 JS |
1239 | |
1240 | tree->object.parsed = 1; | |
1241 | tree->object.type = OBJ_TREE; | |
21666f1a | 1242 | pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); |
934d9a24 JS |
1243 | merged_common_ancestors = make_virtual_commit(tree, "ancestor"); |
1244 | } | |
6d297f81 | 1245 | |
6d297f81 | 1246 | for (iter = ca; iter; iter = iter->next) { |
63889639 | 1247 | call_depth++; |
3af244ca JS |
1248 | /* |
1249 | * When the merge fails, the result contains files | |
1250 | * with conflict markers. The cleanness flag is | |
3dff5379 PR |
1251 | * ignored, it was never actually used, as result of |
1252 | * merge_trees has always overwritten it: the committed | |
3af244ca JS |
1253 | * "conflicts" were already resolved. |
1254 | */ | |
8b944b56 | 1255 | discard_cache(); |
5a753613 | 1256 | merge(merged_common_ancestors, iter->item, |
3af244ca JS |
1257 | "Temporary merge branch 1", |
1258 | "Temporary merge branch 2", | |
3af244ca | 1259 | NULL, |
5a753613 | 1260 | &merged_common_ancestors); |
63889639 | 1261 | call_depth--; |
6d297f81 | 1262 | |
5a753613 | 1263 | if (!merged_common_ancestors) |
6d297f81 JS |
1264 | die("merge returned no commit"); |
1265 | } | |
1266 | ||
8b944b56 | 1267 | discard_cache(); |
63889639 | 1268 | if (!call_depth) { |
8b944b56 | 1269 | read_cache(); |
6d297f81 | 1270 | index_only = 0; |
8b944b56 | 1271 | } else |
6d297f81 | 1272 | index_only = 1; |
6d297f81 | 1273 | |
5a753613 | 1274 | clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree, |
c1d20846 | 1275 | branch1, branch2, &mrtree); |
6d297f81 | 1276 | |
8b944b56 | 1277 | if (index_only) { |
3af244ca JS |
1278 | *result = make_virtual_commit(mrtree, "merged tree"); |
1279 | commit_list_insert(h1, &(*result)->parents); | |
1280 | commit_list_insert(h2, &(*result)->parents->next); | |
8b944b56 | 1281 | } |
3f6ee2d1 SP |
1282 | if (!call_depth && do_progress) { |
1283 | /* Make sure we end at 100% */ | |
1284 | if (!total_cnt) | |
1285 | total_cnt = 1; | |
1286 | merged_cnt = total_cnt; | |
1287 | progress_update = 1; | |
1288 | display_progress(); | |
1289 | fputc('\n', stderr); | |
1290 | } | |
66a155bc | 1291 | flush_output(); |
3af244ca | 1292 | return clean; |
6d297f81 JS |
1293 | } |
1294 | ||
7ba3c078 SP |
1295 | static const char *better_branch_name(const char *branch) |
1296 | { | |
1297 | static char githead_env[8 + 40 + 1]; | |
1298 | char *name; | |
1299 | ||
1300 | if (strlen(branch) != 40) | |
1301 | return branch; | |
1302 | sprintf(githead_env, "GITHEAD_%s", branch); | |
1303 | name = getenv(githead_env); | |
1304 | return name ? name : branch; | |
1305 | } | |
1306 | ||
6d297f81 JS |
1307 | static struct commit *get_ref(const char *ref) |
1308 | { | |
1309 | unsigned char sha1[20]; | |
1310 | struct object *object; | |
1311 | ||
1312 | if (get_sha1(ref, sha1)) | |
1313 | die("Could not resolve ref '%s'", ref); | |
1314 | object = deref_tag(parse_object(sha1), ref, strlen(ref)); | |
a970e84e SP |
1315 | if (object->type == OBJ_TREE) |
1316 | return make_virtual_commit((struct tree*)object, | |
1317 | better_branch_name(ref)); | |
bf6d324e | 1318 | if (object->type != OBJ_COMMIT) |
6d297f81 JS |
1319 | return NULL; |
1320 | if (parse_commit((struct commit *)object)) | |
1321 | die("Could not parse commit '%s'", sha1_to_hex(object->sha1)); | |
1322 | return (struct commit *)object; | |
1323 | } | |
1324 | ||
8c3275ab SP |
1325 | static int merge_config(const char *var, const char *value) |
1326 | { | |
1327 | if (!strcasecmp(var, "merge.verbosity")) { | |
1328 | verbosity = git_config_int(var, value); | |
1329 | return 0; | |
1330 | } | |
1331 | return git_default_config(var, value); | |
1332 | } | |
1333 | ||
6d297f81 JS |
1334 | int main(int argc, char *argv[]) |
1335 | { | |
8b944b56 | 1336 | static const char *bases[20]; |
6d297f81 | 1337 | static unsigned bases_count = 0; |
3af244ca JS |
1338 | int i, clean; |
1339 | const char *branch1, *branch2; | |
1340 | struct commit *result, *h1, *h2; | |
8b944b56 JH |
1341 | struct commit_list *ca = NULL; |
1342 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
1343 | int index_fd; | |
6d297f81 | 1344 | |
8c3275ab SP |
1345 | git_config(merge_config); |
1346 | if (getenv("GIT_MERGE_VERBOSITY")) | |
1347 | verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10); | |
6d297f81 JS |
1348 | |
1349 | if (argc < 4) | |
1350 | die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]); | |
1351 | ||
6d297f81 JS |
1352 | for (i = 1; i < argc; ++i) { |
1353 | if (!strcmp(argv[i], "--")) | |
1354 | break; | |
1355 | if (bases_count < sizeof(bases)/sizeof(*bases)) | |
1356 | bases[bases_count++] = argv[i]; | |
1357 | } | |
1358 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ | |
1359 | die("Not handling anything other than two heads merge."); | |
3f6ee2d1 SP |
1360 | if (verbosity >= 5) { |
1361 | buffer_output = 0; | |
1362 | do_progress = 0; | |
1363 | } | |
1364 | else | |
1365 | do_progress = isatty(1); | |
6d297f81 | 1366 | |
6d297f81 JS |
1367 | branch1 = argv[++i]; |
1368 | branch2 = argv[++i]; | |
6d297f81 | 1369 | |
3af244ca JS |
1370 | h1 = get_ref(branch1); |
1371 | h2 = get_ref(branch2); | |
6d297f81 | 1372 | |
e0ec1819 SP |
1373 | branch1 = better_branch_name(branch1); |
1374 | branch2 = better_branch_name(branch2); | |
3f6ee2d1 SP |
1375 | |
1376 | if (do_progress) | |
1377 | setup_progress_signal(); | |
8c3275ab SP |
1378 | if (show(3)) |
1379 | printf("Merging %s with %s\n", branch1, branch2); | |
e0ec1819 | 1380 | |
8b944b56 | 1381 | index_fd = hold_lock_file_for_update(lock, get_index_file(), 1); |
6d297f81 | 1382 | |
8b944b56 JH |
1383 | for (i = 0; i < bases_count; i++) { |
1384 | struct commit *ancestor = get_ref(bases[i]); | |
1385 | ca = commit_list_insert(ancestor, &ca); | |
1386 | } | |
63889639 | 1387 | clean = merge(h1, h2, branch1, branch2, ca, &result); |
8b944b56 JH |
1388 | |
1389 | if (active_cache_changed && | |
1390 | (write_cache(index_fd, active_cache, active_nr) || | |
1391 | close(index_fd) || commit_lock_file(lock))) | |
1392 | die ("unable to write %s", get_index_file()); | |
6d297f81 | 1393 | |
3af244ca | 1394 | return clean ? 0: 1; |
6d297f81 | 1395 | } |