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