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