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