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