]>
Commit | Line | Data |
---|---|---|
16da134b | 1 | #include "cache.h" |
f8a9d428 | 2 | #include "dir.h" |
16da134b JS |
3 | #include "tree.h" |
4 | #include "tree-walk.h" | |
076b0adc | 5 | #include "cache-tree.h" |
16da134b JS |
6 | #include "unpack-trees.h" |
7 | ||
076b0adc JS |
8 | #define DBRT_DEBUG 1 |
9 | ||
16da134b JS |
10 | struct tree_entry_list { |
11 | struct tree_entry_list *next; | |
12 | unsigned directory : 1; | |
13 | unsigned executable : 1; | |
14 | unsigned symlink : 1; | |
15 | unsigned int mode; | |
16 | const char *name; | |
17 | const unsigned char *sha1; | |
18 | }; | |
19 | ||
20 | static struct tree_entry_list *create_tree_entry_list(struct tree *tree) | |
21 | { | |
22 | struct tree_desc desc; | |
23 | struct name_entry one; | |
24 | struct tree_entry_list *ret = NULL; | |
25 | struct tree_entry_list **list_p = &ret; | |
26 | ||
076b0adc JS |
27 | if (!tree->object.parsed) |
28 | parse_tree(tree); | |
29 | ||
6fda5e51 | 30 | init_tree_desc(&desc, tree->buffer, tree->size); |
16da134b JS |
31 | |
32 | while (tree_entry(&desc, &one)) { | |
33 | struct tree_entry_list *entry; | |
34 | ||
35 | entry = xmalloc(sizeof(struct tree_entry_list)); | |
36 | entry->name = one.path; | |
37 | entry->sha1 = one.sha1; | |
38 | entry->mode = one.mode; | |
39 | entry->directory = S_ISDIR(one.mode) != 0; | |
40 | entry->executable = (one.mode & S_IXUSR) != 0; | |
41 | entry->symlink = S_ISLNK(one.mode) != 0; | |
42 | entry->next = NULL; | |
43 | ||
44 | *list_p = entry; | |
45 | list_p = &entry->next; | |
46 | } | |
47 | return ret; | |
48 | } | |
49 | ||
50 | static int entcmp(const char *name1, int dir1, const char *name2, int dir2) | |
51 | { | |
52 | int len1 = strlen(name1); | |
53 | int len2 = strlen(name2); | |
54 | int len = len1 < len2 ? len1 : len2; | |
55 | int ret = memcmp(name1, name2, len); | |
56 | unsigned char c1, c2; | |
57 | if (ret) | |
58 | return ret; | |
59 | c1 = name1[len]; | |
60 | c2 = name2[len]; | |
61 | if (!c1 && dir1) | |
62 | c1 = '/'; | |
63 | if (!c2 && dir2) | |
64 | c2 = '/'; | |
65 | ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
66 | if (c1 && c2 && !ret) | |
67 | ret = len1 - len2; | |
68 | return ret; | |
69 | } | |
70 | ||
71 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, | |
72 | const char *base, struct unpack_trees_options *o, | |
16da134b JS |
73 | struct tree_entry_list *df_conflict_list) |
74 | { | |
75 | int baselen = strlen(base); | |
76 | int src_size = len + 1; | |
f8a9d428 JH |
77 | int i_stk = i_stk; |
78 | int retval = 0; | |
79 | ||
80 | if (o->dir) | |
81 | i_stk = push_exclude_per_directory(o->dir, base, strlen(base)); | |
82 | ||
16da134b JS |
83 | do { |
84 | int i; | |
85 | const char *first; | |
86 | int firstdir = 0; | |
87 | int pathlen; | |
88 | unsigned ce_size; | |
89 | struct tree_entry_list **subposns; | |
90 | struct cache_entry **src; | |
91 | int any_files = 0; | |
92 | int any_dirs = 0; | |
93 | char *cache_name; | |
94 | int ce_stage; | |
95 | ||
96 | /* Find the first name in the input. */ | |
97 | ||
98 | first = NULL; | |
99 | cache_name = NULL; | |
100 | ||
101 | /* Check the cache */ | |
9a4d8fdc | 102 | if (o->merge && o->pos < active_nr) { |
16da134b JS |
103 | /* This is a bit tricky: */ |
104 | /* If the index has a subdirectory (with | |
105 | * contents) as the first name, it'll get a | |
106 | * filename like "foo/bar". But that's after | |
107 | * "foo", so the entry in trees will get | |
108 | * handled first, at which point we'll go into | |
109 | * "foo", and deal with "bar" from the index, | |
110 | * because the base will be "foo/". The only | |
111 | * way we can actually have "foo/bar" first of | |
112 | * all the things is if the trees don't | |
113 | * contain "foo" at all, in which case we'll | |
114 | * handle "foo/bar" without going into the | |
115 | * directory, but that's fine (and will return | |
116 | * an error anyway, with the added unknown | |
117 | * file case. | |
118 | */ | |
119 | ||
9a4d8fdc | 120 | cache_name = active_cache[o->pos]->name; |
16da134b JS |
121 | if (strlen(cache_name) > baselen && |
122 | !memcmp(cache_name, base, baselen)) { | |
123 | cache_name += baselen; | |
124 | first = cache_name; | |
125 | } else { | |
126 | cache_name = NULL; | |
127 | } | |
128 | } | |
129 | ||
130 | #if DBRT_DEBUG > 1 | |
131 | if (first) | |
132 | printf("index %s\n", first); | |
133 | #endif | |
134 | for (i = 0; i < len; i++) { | |
135 | if (!posns[i] || posns[i] == df_conflict_list) | |
136 | continue; | |
137 | #if DBRT_DEBUG > 1 | |
138 | printf("%d %s\n", i + 1, posns[i]->name); | |
139 | #endif | |
140 | if (!first || entcmp(first, firstdir, | |
141 | posns[i]->name, | |
142 | posns[i]->directory) > 0) { | |
143 | first = posns[i]->name; | |
144 | firstdir = posns[i]->directory; | |
145 | } | |
146 | } | |
147 | /* No name means we're done */ | |
148 | if (!first) | |
f8a9d428 | 149 | goto leave_directory; |
16da134b JS |
150 | |
151 | pathlen = strlen(first); | |
152 | ce_size = cache_entry_size(baselen + pathlen); | |
153 | ||
154 | src = xcalloc(src_size, sizeof(struct cache_entry *)); | |
155 | ||
156 | subposns = xcalloc(len, sizeof(struct tree_list_entry *)); | |
157 | ||
158 | if (cache_name && !strcmp(cache_name, first)) { | |
159 | any_files = 1; | |
9a4d8fdc JH |
160 | src[0] = active_cache[o->pos]; |
161 | remove_cache_entry_at(o->pos); | |
16da134b JS |
162 | } |
163 | ||
164 | for (i = 0; i < len; i++) { | |
165 | struct cache_entry *ce; | |
166 | ||
167 | if (!posns[i] || | |
168 | (posns[i] != df_conflict_list && | |
169 | strcmp(first, posns[i]->name))) { | |
170 | continue; | |
171 | } | |
172 | ||
173 | if (posns[i] == df_conflict_list) { | |
174 | src[i + o->merge] = o->df_conflict_entry; | |
175 | continue; | |
176 | } | |
177 | ||
178 | if (posns[i]->directory) { | |
179 | struct tree *tree = lookup_tree(posns[i]->sha1); | |
180 | any_dirs = 1; | |
181 | parse_tree(tree); | |
182 | subposns[i] = create_tree_entry_list(tree); | |
183 | posns[i] = posns[i]->next; | |
184 | src[i + o->merge] = o->df_conflict_entry; | |
185 | continue; | |
186 | } | |
187 | ||
188 | if (!o->merge) | |
189 | ce_stage = 0; | |
190 | else if (i + 1 < o->head_idx) | |
191 | ce_stage = 1; | |
192 | else if (i + 1 > o->head_idx) | |
193 | ce_stage = 3; | |
194 | else | |
195 | ce_stage = 2; | |
196 | ||
197 | ce = xcalloc(1, ce_size); | |
198 | ce->ce_mode = create_ce_mode(posns[i]->mode); | |
199 | ce->ce_flags = create_ce_flags(baselen + pathlen, | |
200 | ce_stage); | |
201 | memcpy(ce->name, base, baselen); | |
202 | memcpy(ce->name + baselen, first, pathlen + 1); | |
203 | ||
204 | any_files = 1; | |
205 | ||
e702496e | 206 | hashcpy(ce->sha1, posns[i]->sha1); |
16da134b JS |
207 | src[i + o->merge] = ce; |
208 | subposns[i] = df_conflict_list; | |
209 | posns[i] = posns[i]->next; | |
210 | } | |
211 | if (any_files) { | |
212 | if (o->merge) { | |
213 | int ret; | |
214 | ||
215 | #if DBRT_DEBUG > 1 | |
216 | printf("%s:\n", first); | |
217 | for (i = 0; i < src_size; i++) { | |
218 | printf(" %d ", i); | |
219 | if (src[i]) | |
220 | printf("%s\n", sha1_to_hex(src[i]->sha1)); | |
221 | else | |
222 | printf("\n"); | |
223 | } | |
224 | #endif | |
225 | ret = o->fn(src, o); | |
226 | ||
227 | #if DBRT_DEBUG > 1 | |
228 | printf("Added %d entries\n", ret); | |
229 | #endif | |
9a4d8fdc | 230 | o->pos += ret; |
16da134b JS |
231 | } else { |
232 | for (i = 0; i < src_size; i++) { | |
233 | if (src[i]) { | |
234 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); | |
235 | } | |
236 | } | |
237 | } | |
238 | } | |
239 | if (any_dirs) { | |
240 | char *newbase = xmalloc(baselen + 2 + pathlen); | |
241 | memcpy(newbase, base, baselen); | |
242 | memcpy(newbase + baselen, first, pathlen); | |
243 | newbase[baselen + pathlen] = '/'; | |
244 | newbase[baselen + pathlen + 1] = '\0'; | |
245 | if (unpack_trees_rec(subposns, len, newbase, o, | |
9a4d8fdc | 246 | df_conflict_list)) { |
f8a9d428 JH |
247 | retval = -1; |
248 | goto leave_directory; | |
249 | } | |
16da134b JS |
250 | free(newbase); |
251 | } | |
252 | free(subposns); | |
253 | free(src); | |
254 | } while (1); | |
f8a9d428 JH |
255 | |
256 | leave_directory: | |
257 | if (o->dir) | |
258 | pop_exclude_per_directory(o->dir, i_stk); | |
259 | return retval; | |
16da134b JS |
260 | } |
261 | ||
262 | /* Unlink the last component and attempt to remove leading | |
263 | * directories, in case this unlink is the removal of the | |
264 | * last entry in the directory -- empty directories are removed. | |
265 | */ | |
266 | static void unlink_entry(char *name) | |
267 | { | |
268 | char *cp, *prev; | |
269 | ||
270 | if (unlink(name)) | |
271 | return; | |
272 | prev = NULL; | |
273 | while (1) { | |
274 | int status; | |
275 | cp = strrchr(name, '/'); | |
276 | if (prev) | |
277 | *prev = '/'; | |
278 | if (!cp) | |
279 | break; | |
280 | ||
281 | *cp = 0; | |
282 | status = rmdir(name); | |
283 | if (status) { | |
284 | *cp = '/'; | |
285 | break; | |
286 | } | |
287 | prev = cp; | |
288 | } | |
289 | } | |
290 | ||
96f1e58f | 291 | static volatile sig_atomic_t progress_update; |
16da134b JS |
292 | |
293 | static void progress_interval(int signum) | |
294 | { | |
295 | progress_update = 1; | |
296 | } | |
297 | ||
298 | static void setup_progress_signal(void) | |
299 | { | |
300 | struct sigaction sa; | |
301 | struct itimerval v; | |
302 | ||
303 | memset(&sa, 0, sizeof(sa)); | |
304 | sa.sa_handler = progress_interval; | |
305 | sigemptyset(&sa.sa_mask); | |
306 | sa.sa_flags = SA_RESTART; | |
307 | sigaction(SIGALRM, &sa, NULL); | |
308 | ||
309 | v.it_interval.tv_sec = 1; | |
310 | v.it_interval.tv_usec = 0; | |
311 | v.it_value = v.it_interval; | |
312 | setitimer(ITIMER_REAL, &v, NULL); | |
313 | } | |
314 | ||
315 | static struct checkout state; | |
316 | static void check_updates(struct cache_entry **src, int nr, | |
317 | struct unpack_trees_options *o) | |
318 | { | |
319 | unsigned short mask = htons(CE_UPDATE); | |
320 | unsigned last_percent = 200, cnt = 0, total = 0; | |
321 | ||
322 | if (o->update && o->verbose_update) { | |
323 | for (total = cnt = 0; cnt < nr; cnt++) { | |
324 | struct cache_entry *ce = src[cnt]; | |
325 | if (!ce->ce_mode || ce->ce_flags & mask) | |
326 | total++; | |
327 | } | |
328 | ||
329 | /* Don't bother doing this for very small updates */ | |
330 | if (total < 250) | |
331 | total = 0; | |
332 | ||
333 | if (total) { | |
334 | fprintf(stderr, "Checking files out...\n"); | |
335 | setup_progress_signal(); | |
336 | progress_update = 1; | |
337 | } | |
338 | cnt = 0; | |
339 | } | |
340 | ||
341 | while (nr--) { | |
342 | struct cache_entry *ce = *src++; | |
343 | ||
344 | if (total) { | |
345 | if (!ce->ce_mode || ce->ce_flags & mask) { | |
346 | unsigned percent; | |
347 | cnt++; | |
348 | percent = (cnt * 100) / total; | |
349 | if (percent != last_percent || | |
350 | progress_update) { | |
351 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
352 | percent, cnt, total); | |
353 | last_percent = percent; | |
354 | progress_update = 0; | |
355 | } | |
356 | } | |
357 | } | |
358 | if (!ce->ce_mode) { | |
359 | if (o->update) | |
360 | unlink_entry(ce->name); | |
361 | continue; | |
362 | } | |
363 | if (ce->ce_flags & mask) { | |
364 | ce->ce_flags &= ~mask; | |
365 | if (o->update) | |
366 | checkout_entry(ce, &state, NULL); | |
367 | } | |
368 | } | |
369 | if (total) { | |
370 | signal(SIGALRM, SIG_IGN); | |
371 | fputc('\n', stderr); | |
372 | } | |
373 | } | |
374 | ||
375 | int unpack_trees(struct object_list *trees, struct unpack_trees_options *o) | |
376 | { | |
16da134b JS |
377 | unsigned len = object_list_length(trees); |
378 | struct tree_entry_list **posns; | |
379 | int i; | |
380 | struct object_list *posn = trees; | |
381 | struct tree_entry_list df_conflict_list; | |
0fb1eaa8 | 382 | static struct cache_entry *dfc; |
16da134b JS |
383 | |
384 | memset(&df_conflict_list, 0, sizeof(df_conflict_list)); | |
385 | df_conflict_list.next = &df_conflict_list; | |
076b0adc | 386 | memset(&state, 0, sizeof(state)); |
16da134b JS |
387 | state.base_dir = ""; |
388 | state.force = 1; | |
389 | state.quiet = 1; | |
390 | state.refresh_cache = 1; | |
391 | ||
392 | o->merge_size = len; | |
0fb1eaa8 JH |
393 | |
394 | if (!dfc) | |
395 | dfc = xcalloc(1, sizeof(struct cache_entry) + 1); | |
396 | o->df_conflict_entry = dfc; | |
16da134b JS |
397 | |
398 | if (len) { | |
399 | posns = xmalloc(len * sizeof(struct tree_entry_list *)); | |
400 | for (i = 0; i < len; i++) { | |
401 | posns[i] = create_tree_entry_list((struct tree *) posn->item); | |
402 | posn = posn->next; | |
403 | } | |
404 | if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "", | |
9a4d8fdc | 405 | o, &df_conflict_list)) |
16da134b JS |
406 | return -1; |
407 | } | |
408 | ||
409 | if (o->trivial_merges_only && o->nontrivial_merge) | |
410 | die("Merge requires file-level merging"); | |
411 | ||
412 | check_updates(active_cache, active_nr, o); | |
413 | return 0; | |
414 | } | |
076b0adc JS |
415 | |
416 | /* Here come the merge functions */ | |
417 | ||
418 | static void reject_merge(struct cache_entry *ce) | |
419 | { | |
420 | die("Entry '%s' would be overwritten by merge. Cannot merge.", | |
421 | ce->name); | |
422 | } | |
423 | ||
424 | static int same(struct cache_entry *a, struct cache_entry *b) | |
425 | { | |
426 | if (!!a != !!b) | |
427 | return 0; | |
428 | if (!a && !b) | |
429 | return 1; | |
430 | return a->ce_mode == b->ce_mode && | |
a89fccd2 | 431 | !hashcmp(a->sha1, b->sha1); |
076b0adc JS |
432 | } |
433 | ||
434 | ||
435 | /* | |
436 | * When a CE gets turned into an unmerged entry, we | |
437 | * want it to be up-to-date | |
438 | */ | |
439 | static void verify_uptodate(struct cache_entry *ce, | |
440 | struct unpack_trees_options *o) | |
441 | { | |
442 | struct stat st; | |
443 | ||
444 | if (o->index_only || o->reset) | |
445 | return; | |
446 | ||
447 | if (!lstat(ce->name, &st)) { | |
448 | unsigned changed = ce_match_stat(ce, &st, 1); | |
449 | if (!changed) | |
450 | return; | |
451 | errno = 0; | |
452 | } | |
453 | if (o->reset) { | |
454 | ce->ce_flags |= htons(CE_UPDATE); | |
455 | return; | |
456 | } | |
457 | if (errno == ENOENT) | |
458 | return; | |
459 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
460 | } | |
461 | ||
462 | static void invalidate_ce_path(struct cache_entry *ce) | |
463 | { | |
464 | if (ce) | |
465 | cache_tree_invalidate_path(active_cache_tree, ce->name); | |
466 | } | |
467 | ||
468 | /* | |
469 | * We do not want to remove or overwrite a working tree file that | |
f8a9d428 | 470 | * is not tracked, unless it is ignored. |
076b0adc JS |
471 | */ |
472 | static void verify_absent(const char *path, const char *action, | |
473 | struct unpack_trees_options *o) | |
474 | { | |
475 | struct stat st; | |
476 | ||
477 | if (o->index_only || o->reset || !o->update) | |
478 | return; | |
f8a9d428 | 479 | if (!lstat(path, &st) && !(o->dir && excluded(o->dir, path))) |
076b0adc JS |
480 | die("Untracked working tree file '%s' " |
481 | "would be %s by merge.", path, action); | |
482 | } | |
483 | ||
484 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old, | |
485 | struct unpack_trees_options *o) | |
486 | { | |
487 | merge->ce_flags |= htons(CE_UPDATE); | |
488 | if (old) { | |
489 | /* | |
490 | * See if we can re-use the old CE directly? | |
491 | * That way we get the uptodate stat info. | |
492 | * | |
493 | * This also removes the UPDATE flag on | |
494 | * a match. | |
495 | */ | |
496 | if (same(old, merge)) { | |
497 | *merge = *old; | |
498 | } else { | |
499 | verify_uptodate(old, o); | |
500 | invalidate_ce_path(old); | |
501 | } | |
502 | } | |
503 | else { | |
504 | verify_absent(merge->name, "overwritten", o); | |
505 | invalidate_ce_path(merge); | |
506 | } | |
507 | ||
508 | merge->ce_flags &= ~htons(CE_STAGEMASK); | |
509 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
510 | return 1; | |
511 | } | |
512 | ||
513 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, | |
514 | struct unpack_trees_options *o) | |
515 | { | |
516 | if (old) | |
517 | verify_uptodate(old, o); | |
518 | else | |
519 | verify_absent(ce->name, "removed", o); | |
520 | ce->ce_mode = 0; | |
521 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); | |
522 | invalidate_ce_path(ce); | |
523 | return 1; | |
524 | } | |
525 | ||
7f7932ab | 526 | static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o) |
076b0adc JS |
527 | { |
528 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); | |
529 | return 1; | |
530 | } | |
531 | ||
532 | #if DBRT_DEBUG | |
533 | static void show_stage_entry(FILE *o, | |
534 | const char *label, const struct cache_entry *ce) | |
535 | { | |
536 | if (!ce) | |
537 | fprintf(o, "%s (missing)\n", label); | |
538 | else | |
539 | fprintf(o, "%s%06o %s %d\t%s\n", | |
540 | label, | |
541 | ntohl(ce->ce_mode), | |
542 | sha1_to_hex(ce->sha1), | |
543 | ce_stage(ce), | |
544 | ce->name); | |
545 | } | |
546 | #endif | |
547 | ||
548 | int threeway_merge(struct cache_entry **stages, | |
549 | struct unpack_trees_options *o) | |
550 | { | |
551 | struct cache_entry *index; | |
552 | struct cache_entry *head; | |
553 | struct cache_entry *remote = stages[o->head_idx + 1]; | |
554 | int count; | |
555 | int head_match = 0; | |
556 | int remote_match = 0; | |
557 | const char *path = NULL; | |
558 | ||
559 | int df_conflict_head = 0; | |
560 | int df_conflict_remote = 0; | |
561 | ||
562 | int any_anc_missing = 0; | |
563 | int no_anc_exists = 1; | |
564 | int i; | |
565 | ||
566 | for (i = 1; i < o->head_idx; i++) { | |
567 | if (!stages[i]) | |
568 | any_anc_missing = 1; | |
569 | else { | |
570 | if (!path) | |
571 | path = stages[i]->name; | |
572 | no_anc_exists = 0; | |
573 | } | |
574 | } | |
575 | ||
576 | index = stages[0]; | |
577 | head = stages[o->head_idx]; | |
578 | ||
579 | if (head == o->df_conflict_entry) { | |
580 | df_conflict_head = 1; | |
581 | head = NULL; | |
582 | } | |
583 | ||
584 | if (remote == o->df_conflict_entry) { | |
585 | df_conflict_remote = 1; | |
586 | remote = NULL; | |
587 | } | |
588 | ||
589 | if (!path && index) | |
590 | path = index->name; | |
591 | if (!path && head) | |
592 | path = head->name; | |
593 | if (!path && remote) | |
594 | path = remote->name; | |
595 | ||
596 | /* First, if there's a #16 situation, note that to prevent #13 | |
597 | * and #14. | |
598 | */ | |
599 | if (!same(remote, head)) { | |
600 | for (i = 1; i < o->head_idx; i++) { | |
601 | if (same(stages[i], head)) { | |
602 | head_match = i; | |
603 | } | |
604 | if (same(stages[i], remote)) { | |
605 | remote_match = i; | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | /* We start with cases where the index is allowed to match | |
611 | * something other than the head: #14(ALT) and #2ALT, where it | |
612 | * is permitted to match the result instead. | |
613 | */ | |
614 | /* #14, #14ALT, #2ALT */ | |
615 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
616 | if (index && !same(index, remote) && !same(index, head)) | |
617 | reject_merge(index); | |
618 | return merged_entry(remote, index, o); | |
619 | } | |
620 | /* | |
621 | * If we have an entry in the index cache, then we want to | |
622 | * make sure that it matches head. | |
623 | */ | |
624 | if (index && !same(index, head)) { | |
625 | reject_merge(index); | |
626 | } | |
627 | ||
628 | if (head) { | |
629 | /* #5ALT, #15 */ | |
630 | if (same(head, remote)) | |
631 | return merged_entry(head, index, o); | |
632 | /* #13, #3ALT */ | |
633 | if (!df_conflict_remote && remote_match && !head_match) | |
634 | return merged_entry(head, index, o); | |
635 | } | |
636 | ||
637 | /* #1 */ | |
638 | if (!head && !remote && any_anc_missing) | |
639 | return 0; | |
640 | ||
641 | /* Under the new "aggressive" rule, we resolve mostly trivial | |
642 | * cases that we historically had git-merge-one-file resolve. | |
643 | */ | |
644 | if (o->aggressive) { | |
645 | int head_deleted = !head && !df_conflict_head; | |
646 | int remote_deleted = !remote && !df_conflict_remote; | |
647 | /* | |
648 | * Deleted in both. | |
649 | * Deleted in one and unchanged in the other. | |
650 | */ | |
651 | if ((head_deleted && remote_deleted) || | |
652 | (head_deleted && remote && remote_match) || | |
653 | (remote_deleted && head && head_match)) { | |
654 | if (index) | |
655 | return deleted_entry(index, index, o); | |
ed93b449 | 656 | else if (path && !head_deleted) |
076b0adc JS |
657 | verify_absent(path, "removed", o); |
658 | return 0; | |
659 | } | |
660 | /* | |
661 | * Added in both, identically. | |
662 | */ | |
663 | if (no_anc_exists && head && remote && same(head, remote)) | |
664 | return merged_entry(head, index, o); | |
665 | ||
666 | } | |
667 | ||
668 | /* Below are "no merge" cases, which require that the index be | |
669 | * up-to-date to avoid the files getting overwritten with | |
670 | * conflict resolution files. | |
671 | */ | |
672 | if (index) { | |
673 | verify_uptodate(index, o); | |
674 | } | |
076b0adc JS |
675 | |
676 | o->nontrivial_merge = 1; | |
677 | ||
678 | /* #2, #3, #4, #6, #7, #9, #11. */ | |
679 | count = 0; | |
680 | if (!head_match || !remote_match) { | |
681 | for (i = 1; i < o->head_idx; i++) { | |
682 | if (stages[i]) { | |
7f7932ab | 683 | keep_entry(stages[i], o); |
076b0adc JS |
684 | count++; |
685 | break; | |
686 | } | |
687 | } | |
688 | } | |
689 | #if DBRT_DEBUG | |
690 | else { | |
691 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
692 | show_stage_entry(stderr, "head ", stages[head_match]); | |
693 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
694 | } | |
695 | #endif | |
7f7932ab JH |
696 | if (head) { count += keep_entry(head, o); } |
697 | if (remote) { count += keep_entry(remote, o); } | |
076b0adc JS |
698 | return count; |
699 | } | |
700 | ||
701 | /* | |
702 | * Two-way merge. | |
703 | * | |
704 | * The rule is to "carry forward" what is in the index without losing | |
705 | * information across a "fast forward", favoring a successful merge | |
706 | * over a merge failure when it makes sense. For details of the | |
707 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
708 | * | |
709 | */ | |
710 | int twoway_merge(struct cache_entry **src, | |
711 | struct unpack_trees_options *o) | |
712 | { | |
713 | struct cache_entry *current = src[0]; | |
b8ba1535 JH |
714 | struct cache_entry *oldtree = src[1]; |
715 | struct cache_entry *newtree = src[2]; | |
076b0adc JS |
716 | |
717 | if (o->merge_size != 2) | |
718 | return error("Cannot do a twoway merge of %d trees", | |
719 | o->merge_size); | |
720 | ||
b8ba1535 JH |
721 | if (oldtree == o->df_conflict_entry) |
722 | oldtree = NULL; | |
723 | if (newtree == o->df_conflict_entry) | |
724 | newtree = NULL; | |
725 | ||
076b0adc JS |
726 | if (current) { |
727 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
728 | (!oldtree && newtree && | |
729 | same(current, newtree)) || /* 6 and 7 */ | |
730 | (oldtree && newtree && | |
731 | same(oldtree, newtree)) || /* 14 and 15 */ | |
732 | (oldtree && newtree && | |
b8ba1535 | 733 | !same(oldtree, newtree) && /* 18 and 19 */ |
076b0adc | 734 | same(current, newtree))) { |
7f7932ab | 735 | return keep_entry(current, o); |
076b0adc JS |
736 | } |
737 | else if (oldtree && !newtree && same(current, oldtree)) { | |
738 | /* 10 or 11 */ | |
739 | return deleted_entry(oldtree, current, o); | |
740 | } | |
741 | else if (oldtree && newtree && | |
742 | same(current, oldtree) && !same(current, newtree)) { | |
743 | /* 20 or 21 */ | |
744 | return merged_entry(newtree, current, o); | |
745 | } | |
746 | else { | |
747 | /* all other failures */ | |
748 | if (oldtree) | |
749 | reject_merge(oldtree); | |
750 | if (current) | |
751 | reject_merge(current); | |
752 | if (newtree) | |
753 | reject_merge(newtree); | |
754 | return -1; | |
755 | } | |
756 | } | |
757 | else if (newtree) | |
758 | return merged_entry(newtree, current, o); | |
759 | else | |
760 | return deleted_entry(oldtree, current, o); | |
761 | } | |
762 | ||
763 | /* | |
764 | * Bind merge. | |
765 | * | |
766 | * Keep the index entries at stage0, collapse stage1 but make sure | |
767 | * stage0 does not have anything there. | |
768 | */ | |
769 | int bind_merge(struct cache_entry **src, | |
770 | struct unpack_trees_options *o) | |
771 | { | |
772 | struct cache_entry *old = src[0]; | |
773 | struct cache_entry *a = src[1]; | |
774 | ||
775 | if (o->merge_size != 1) | |
776 | return error("Cannot do a bind merge of %d trees\n", | |
777 | o->merge_size); | |
778 | if (a && old) | |
779 | die("Entry '%s' overlaps. Cannot bind.", a->name); | |
780 | if (!a) | |
7f7932ab | 781 | return keep_entry(old, o); |
076b0adc JS |
782 | else |
783 | return merged_entry(a, NULL, o); | |
784 | } | |
785 | ||
786 | /* | |
787 | * One-way merge. | |
788 | * | |
789 | * The rule is: | |
790 | * - take the stat information from stage0, take the data from stage1 | |
791 | */ | |
792 | int oneway_merge(struct cache_entry **src, | |
793 | struct unpack_trees_options *o) | |
794 | { | |
795 | struct cache_entry *old = src[0]; | |
796 | struct cache_entry *a = src[1]; | |
797 | ||
798 | if (o->merge_size != 1) | |
799 | return error("Cannot do a oneway merge of %d trees", | |
800 | o->merge_size); | |
801 | ||
802 | if (!a) | |
803 | return deleted_entry(old, old, o); | |
804 | if (old && same(old, a)) { | |
805 | if (o->reset) { | |
806 | struct stat st; | |
807 | if (lstat(old->name, &st) || | |
808 | ce_match_stat(old, &st, 1)) | |
809 | old->ce_flags |= htons(CE_UPDATE); | |
810 | } | |
7f7932ab | 811 | return keep_entry(old, o); |
076b0adc JS |
812 | } |
813 | return merged_entry(a, old, o); | |
814 | } |