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