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