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