merge-recursive: porcelain messages for checkout
[git/git.git] / unpack-trees.c
CommitLineData
bc052d7f 1#define NO_THE_INDEX_COMPATIBILITY_MACROS
16da134b 2#include "cache.h"
f8a9d428 3#include "dir.h"
16da134b
JS
4#include "tree.h"
5#include "tree-walk.h"
076b0adc 6#include "cache-tree.h"
16da134b 7#include "unpack-trees.h"
96a02f8f 8#include "progress.h"
0cf73755 9#include "refs.h"
06f33c17 10#include "attr.h"
16da134b 11
8ccba008
JH
12/*
13 * Error messages expected by scripts out of plumbing commands such as
14 * read-tree. Non-scripted Porcelain is not required to use these messages
15 * and in fact are encouraged to reword them to better suit their particular
23cbf11b
DG
16 * situation better. See how "git checkout" and "git merge" replaces
17 * them using set_porcelain_error_msgs(), for example.
8ccba008 18 */
08353ebb
MM
19const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
20 /* ERROR_WOULD_OVERWRITE */
8ccba008
JH
21 "Entry '%s' would be overwritten by merge. Cannot merge.",
22
08353ebb 23 /* ERROR_NOT_UPTODATE_FILE */
8ccba008
JH
24 "Entry '%s' not uptodate. Cannot merge.",
25
08353ebb 26 /* ERROR_NOT_UPTODATE_DIR */
8ccba008
JH
27 "Updating '%s' would lose untracked files in it",
28
08353ebb 29 /* ERROR_WOULD_LOSE_UNTRACKED */
8ccba008
JH
30 "Untracked working tree file '%s' would be %s by merge.",
31
08353ebb 32 /* ERROR_BIND_OVERLAP */
8ccba008 33 "Entry '%s' overlaps with '%s'. Cannot bind.",
e800ec9d 34
08353ebb 35 /* ERROR_SPARSE_NOT_UPTODATE_FILE */
e800ec9d
NTND
36 "Entry '%s' not uptodate. Cannot update sparse checkout.",
37
08353ebb 38 /* ERROR_WOULD_LOSE_ORPHANED */
e800ec9d 39 "Working tree file '%s' would be %s by sparse checkout update.",
8ccba008
JH
40};
41
08353ebb
MM
42#define ERRORMSG(o,type) \
43 ( ((o) && (o)->msgs[(type)]) \
44 ? ((o)->msgs[(type)]) \
45 : (unpack_plumbing_errors[(type)]) )
8ccba008 46
34110cd4
LT
47static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
48 unsigned int set, unsigned int clear)
b48d5a05 49{
34110cd4
LT
50 unsigned int size = ce_size(ce);
51 struct cache_entry *new = xmalloc(size);
52
53 clear |= CE_HASHED | CE_UNHASHED;
54
55 memcpy(new, ce, size);
56 new->next = NULL;
57 new->ce_flags = (new->ce_flags & ~clear) | set;
aab3b9a1 58 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
b48d5a05
LT
59}
60
78478927
KB
61/*
62 * Unlink the last component and schedule the leading directories for
63 * removal, such that empty directories get removed.
16da134b 64 */
c40641b7 65static void unlink_entry(struct cache_entry *ce)
16da134b 66{
57199892 67 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
16a4c617 68 return;
80d706af
PC
69 if (remove_or_warn(ce->ce_mode, ce->name))
70 return;
78478927 71 schedule_dir_for_removal(ce->name, ce_namelen(ce));
16da134b
JS
72}
73
16da134b 74static struct checkout state;
c4758d3c 75static int check_updates(struct unpack_trees_options *o)
16da134b 76{
96a02f8f 77 unsigned cnt = 0, total = 0;
dc6a0757 78 struct progress *progress = NULL;
34110cd4 79 struct index_state *index = &o->result;
33ecf7eb 80 int i;
c4758d3c 81 int errs = 0;
16da134b
JS
82
83 if (o->update && o->verbose_update) {
34110cd4
LT
84 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
85 struct cache_entry *ce = index->cache[cnt];
e663db2f 86 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE | CE_WT_REMOVE))
16da134b
JS
87 total++;
88 }
89
dc6a0757 90 progress = start_progress_delay("Checking out files",
e8548645 91 total, 50, 1);
16da134b
JS
92 cnt = 0;
93 }
94
66985e66
JH
95 if (o->update)
96 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
34110cd4
LT
97 for (i = 0; i < index->cache_nr; i++) {
98 struct cache_entry *ce = index->cache[i];
16da134b 99
e663db2f
NTND
100 if (ce->ce_flags & CE_WT_REMOVE) {
101 display_progress(progress, ++cnt);
102 if (o->update)
103 unlink_entry(ce);
104 continue;
105 }
106
7a51ed66 107 if (ce->ce_flags & CE_REMOVE) {
1fa6ead4 108 display_progress(progress, ++cnt);
16da134b 109 if (o->update)
c40641b7 110 unlink_entry(ce);
16da134b 111 }
1fa6ead4 112 }
36419c8e 113 remove_marked_cache_entries(&o->result);
78478927 114 remove_scheduled_dirs();
1fa6ead4
LT
115
116 for (i = 0; i < index->cache_nr; i++) {
117 struct cache_entry *ce = index->cache[i];
118
7a51ed66 119 if (ce->ce_flags & CE_UPDATE) {
1fa6ead4 120 display_progress(progress, ++cnt);
7a51ed66 121 ce->ce_flags &= ~CE_UPDATE;
16a4c617 122 if (o->update) {
c4758d3c 123 errs |= checkout_entry(ce, &state, NULL);
16a4c617 124 }
16da134b
JS
125 }
126 }
4d4fcc54 127 stop_progress(&progress);
66985e66
JH
128 if (o->update)
129 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
c4758d3c 130 return errs != 0;
16da134b
JS
131}
132
e800ec9d
NTND
133static int verify_uptodate_sparse(struct cache_entry *ce, struct unpack_trees_options *o);
134static int verify_absent_sparse(struct cache_entry *ce, const char *action, struct unpack_trees_options *o);
135
136static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_trees_options *o)
137{
138 const char *basename;
139
140 if (ce_stage(ce))
141 return 0;
142
143 basename = strrchr(ce->name, '/');
144 basename = basename ? basename+1 : ce->name;
145 return excluded_from_list(ce->name, ce_namelen(ce), basename, NULL, o->el) <= 0;
146}
147
148static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_options *o)
149{
150 int was_skip_worktree = ce_skip_worktree(ce);
151
152 if (will_have_skip_worktree(ce, o))
153 ce->ce_flags |= CE_SKIP_WORKTREE;
154 else
155 ce->ce_flags &= ~CE_SKIP_WORKTREE;
156
157 /*
158 * We only care about files getting into the checkout area
159 * If merge strategies want to remove some, go ahead, this
160 * flag will be removed eventually in unpack_trees() if it's
161 * outside checkout area.
162 */
163 if (ce->ce_flags & CE_REMOVE)
164 return 0;
165
166 if (!was_skip_worktree && ce_skip_worktree(ce)) {
167 /*
168 * If CE_UPDATE is set, verify_uptodate() must be called already
169 * also stat info may have lost after merged_entry() so calling
170 * verify_uptodate() again may fail
171 */
172 if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
173 return -1;
174 ce->ce_flags |= CE_WT_REMOVE;
175 }
176 if (was_skip_worktree && !ce_skip_worktree(ce)) {
177 if (verify_absent_sparse(ce, "overwritten", o))
178 return -1;
179 ce->ce_flags |= CE_UPDATE;
180 }
181 return 0;
182}
183
34110cd4 184static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
01904572 185{
34110cd4
LT
186 int ret = o->fn(src, o);
187 if (ret > 0)
01904572 188 ret = 0;
01904572
LT
189 return ret;
190}
191
da165f47
JH
192static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
193{
194 ce->ce_flags |= CE_UNPACKED;
195
196 if (o->cache_bottom < o->src_index->cache_nr &&
197 o->src_index->cache[o->cache_bottom] == ce) {
198 int bottom = o->cache_bottom;
199 while (bottom < o->src_index->cache_nr &&
200 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
201 bottom++;
202 o->cache_bottom = bottom;
203 }
204}
205
206static void mark_all_ce_unused(struct index_state *index)
207{
208 int i;
209 for (i = 0; i < index->cache_nr; i++)
210 index->cache[i]->ce_flags &= ~CE_UNPACKED;
211}
212
213static int locate_in_src_index(struct cache_entry *ce,
214 struct unpack_trees_options *o)
215{
216 struct index_state *index = o->src_index;
217 int len = ce_namelen(ce);
218 int pos = index_name_pos(index, ce->name, len);
219 if (pos < 0)
220 pos = -1 - pos;
221 return pos;
222}
223
224/*
225 * We call unpack_index_entry() with an unmerged cache entry
226 * only in diff-index, and it wants a single callback. Skip
227 * the other unmerged entry with the same name.
228 */
229static void mark_ce_used_same_name(struct cache_entry *ce,
230 struct unpack_trees_options *o)
231{
232 struct index_state *index = o->src_index;
233 int len = ce_namelen(ce);
234 int pos;
235
236 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
237 struct cache_entry *next = index->cache[pos];
238 if (len != ce_namelen(next) ||
239 memcmp(ce->name, next->name, len))
240 break;
241 mark_ce_used(next, o);
242 }
243}
244
245static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
246{
247 const struct index_state *index = o->src_index;
248 int pos = o->cache_bottom;
249
250 while (pos < index->cache_nr) {
251 struct cache_entry *ce = index->cache[pos];
252 if (!(ce->ce_flags & CE_UNPACKED))
253 return ce;
254 pos++;
255 }
256 return NULL;
257}
258
259static void add_same_unmerged(struct cache_entry *ce,
260 struct unpack_trees_options *o)
261{
262 struct index_state *index = o->src_index;
263 int len = ce_namelen(ce);
264 int pos = index_name_pos(index, ce->name, len);
265
266 if (0 <= pos)
267 die("programming error in a caller of mark_ce_used_same_name");
268 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
269 struct cache_entry *next = index->cache[pos];
270 if (len != ce_namelen(next) ||
271 memcmp(ce->name, next->name, len))
272 break;
273 add_entry(o, next, 0, 0);
274 mark_ce_used(next, o);
275 }
276}
277
278static int unpack_index_entry(struct cache_entry *ce,
279 struct unpack_trees_options *o)
01904572 280{
66dbfd55 281 struct cache_entry *src[5] = { NULL };
da165f47 282 int ret;
34110cd4 283
66dbfd55
GV
284 src[0] = ce;
285
da165f47 286 mark_ce_used(ce, o);
01904572
LT
287 if (ce_stage(ce)) {
288 if (o->skip_unmerged) {
34110cd4
LT
289 add_entry(o, ce, 0, 0);
290 return 0;
01904572 291 }
01904572 292 }
da165f47
JH
293 ret = call_unpack_fn(src, o);
294 if (ce_stage(ce))
295 mark_ce_used_same_name(ce, o);
296 return ret;
01904572
LT
297}
298
730f7284
JH
299static int find_cache_pos(struct traverse_info *, const struct name_entry *);
300
301static void restore_cache_bottom(struct traverse_info *info, int bottom)
302{
303 struct unpack_trees_options *o = info->data;
304
305 if (o->diff_index_cached)
306 return;
307 o->cache_bottom = bottom;
308}
309
310static int switch_cache_bottom(struct traverse_info *info)
311{
312 struct unpack_trees_options *o = info->data;
313 int ret, pos;
314
315 if (o->diff_index_cached)
316 return 0;
317 ret = o->cache_bottom;
318 pos = find_cache_pos(info->prev, &info->name);
319
320 if (pos < -1)
321 o->cache_bottom = -2 - pos;
322 else if (pos < 0)
323 o->cache_bottom = o->src_index->cache_nr;
324 return ret;
01904572
LT
325}
326
2af202be 327static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
16da134b 328{
730f7284 329 int i, ret, bottom;
ca885a4f 330 struct tree_desc t[MAX_UNPACK_TREES];
01904572
LT
331 struct traverse_info newinfo;
332 struct name_entry *p;
333
334 p = names;
335 while (!p->mode)
336 p++;
337
338 newinfo = *info;
339 newinfo.prev = info;
340 newinfo.name = *p;
341 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
342 newinfo.conflicts |= df_conflicts;
343
344 for (i = 0; i < n; i++, dirmask >>= 1) {
345 const unsigned char *sha1 = NULL;
346 if (dirmask & 1)
347 sha1 = names[i].sha1;
348 fill_tree_descriptor(t+i, sha1);
349 }
730f7284
JH
350
351 bottom = switch_cache_bottom(&newinfo);
352 ret = traverse_trees(n, t, &newinfo);
353 restore_cache_bottom(&newinfo, bottom);
354 return ret;
01904572
LT
355}
356
357/*
358 * Compare the traverse-path to the cache entry without actually
359 * having to generate the textual representation of the traverse
360 * path.
361 *
362 * NOTE! This *only* compares up to the size of the traverse path
363 * itself - the caller needs to do the final check for the cache
364 * entry having more data at the end!
365 */
366static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
367{
368 int len, pathlen, ce_len;
369 const char *ce_name;
370
371 if (info->prev) {
372 int cmp = do_compare_entry(ce, info->prev, &info->name);
373 if (cmp)
374 return cmp;
375 }
376 pathlen = info->pathlen;
377 ce_len = ce_namelen(ce);
378
379 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
380 if (ce_len < pathlen)
381 return -1;
382
383 ce_len -= pathlen;
384 ce_name = ce->name + pathlen;
385
386 len = tree_entry_len(n->path, n->sha1);
387 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
388}
389
390static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
391{
392 int cmp = do_compare_entry(ce, info, n);
393 if (cmp)
394 return cmp;
395
396 /*
397 * Even if the beginning compared identically, the ce should
398 * compare as bigger than a directory leading up to it!
399 */
400 return ce_namelen(ce) > traverse_path_len(info, n);
401}
402
da165f47
JH
403static int ce_in_traverse_path(const struct cache_entry *ce,
404 const struct traverse_info *info)
405{
406 if (!info->prev)
407 return 1;
408 if (do_compare_entry(ce, info->prev, &info->name))
409 return 0;
410 /*
411 * If ce (blob) is the same name as the path (which is a tree
412 * we will be descending into), it won't be inside it.
413 */
414 return (info->pathlen < ce_namelen(ce));
415}
416
01904572
LT
417static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
418{
419 int len = traverse_path_len(info, n);
420 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
421
422 ce->ce_mode = create_ce_mode(n->mode);
423 ce->ce_flags = create_ce_flags(len, stage);
424 hashcpy(ce->sha1, n->sha1);
425 make_traverse_path(ce->name, info, n);
426
427 return ce;
428}
429
c7cddc1a
RS
430static int unpack_nondirectories(int n, unsigned long mask,
431 unsigned long dirmask,
432 struct cache_entry **src,
433 const struct name_entry *names,
434 const struct traverse_info *info)
01904572
LT
435{
436 int i;
437 struct unpack_trees_options *o = info->data;
438 unsigned long conflicts;
439
440 /* Do we have *only* directories? Nothing to do */
441 if (mask == dirmask && !src[0])
442 return 0;
443
444 conflicts = info->conflicts;
445 if (o->merge)
446 conflicts >>= 1;
447 conflicts |= dirmask;
448
449 /*
450 * Ok, we've filled in up to any potential index entry in src[0],
451 * now do the rest.
452 */
453 for (i = 0; i < n; i++) {
454 int stage;
455 unsigned int bit = 1ul << i;
456 if (conflicts & bit) {
457 src[i + o->merge] = o->df_conflict_entry;
458 continue;
459 }
460 if (!(mask & bit))
461 continue;
462 if (!o->merge)
463 stage = 0;
464 else if (i + 1 < o->head_idx)
465 stage = 1;
466 else if (i + 1 > o->head_idx)
467 stage = 3;
468 else
469 stage = 2;
470 src[i + o->merge] = create_ce_entry(info, names + i, stage);
471 }
472
473 if (o->merge)
34110cd4 474 return call_unpack_fn(src, o);
01904572 475
01904572 476 for (i = 0; i < n; i++)
aab3b9a1
JH
477 if (src[i] && src[i] != o->df_conflict_entry)
478 add_entry(o, src[i], 0, 0);
01904572
LT
479 return 0;
480}
481
353c5eeb
JH
482static int unpack_failed(struct unpack_trees_options *o, const char *message)
483{
484 discard_index(&o->result);
485 if (!o->gently) {
486 if (message)
487 return error("%s", message);
488 return -1;
489 }
490 return -1;
491}
492
730f7284
JH
493/* NEEDSWORK: give this a better name and share with tree-walk.c */
494static int name_compare(const char *a, int a_len,
495 const char *b, int b_len)
496{
497 int len = (a_len < b_len) ? a_len : b_len;
498 int cmp = memcmp(a, b, len);
499 if (cmp)
500 return cmp;
501 return (a_len - b_len);
502}
503
504/*
505 * The tree traversal is looking at name p. If we have a matching entry,
506 * return it. If name p is a directory in the index, do not return
507 * anything, as we will want to match it when the traversal descends into
508 * the directory.
509 */
510static int find_cache_pos(struct traverse_info *info,
511 const struct name_entry *p)
512{
513 int pos;
514 struct unpack_trees_options *o = info->data;
515 struct index_state *index = o->src_index;
516 int pfxlen = info->pathlen;
517 int p_len = tree_entry_len(p->path, p->sha1);
518
519 for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
520 struct cache_entry *ce = index->cache[pos];
521 const char *ce_name, *ce_slash;
522 int cmp, ce_len;
523
e53e6b44
BD
524 if (ce->ce_flags & CE_UNPACKED) {
525 /*
526 * cache_bottom entry is already unpacked, so
527 * we can never match it; don't check it
528 * again.
529 */
530 if (pos == o->cache_bottom)
531 ++o->cache_bottom;
730f7284 532 continue;
e53e6b44
BD
533 }
534 if (!ce_in_traverse_path(ce, info))
730f7284
JH
535 continue;
536 ce_name = ce->name + pfxlen;
537 ce_slash = strchr(ce_name, '/');
538 if (ce_slash)
539 ce_len = ce_slash - ce_name;
540 else
541 ce_len = ce_namelen(ce) - pfxlen;
542 cmp = name_compare(p->path, p_len, ce_name, ce_len);
543 /*
544 * Exact match; if we have a directory we need to
545 * delay returning it.
546 */
547 if (!cmp)
548 return ce_slash ? -2 - pos : pos;
549 if (0 < cmp)
550 continue; /* keep looking */
551 /*
552 * ce_name sorts after p->path; could it be that we
553 * have files under p->path directory in the index?
554 * E.g. ce_name == "t-i", and p->path == "t"; we may
555 * have "t/a" in the index.
556 */
557 if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&
558 ce_name[p_len] < '/')
559 continue; /* keep looking */
560 break;
561 }
562 return -1;
563}
564
565static struct cache_entry *find_cache_entry(struct traverse_info *info,
566 const struct name_entry *p)
567{
568 int pos = find_cache_pos(info, p);
569 struct unpack_trees_options *o = info->data;
570
571 if (0 <= pos)
572 return o->src_index->cache[pos];
573 else
574 return NULL;
575}
576
ba655da5
JH
577static void debug_path(struct traverse_info *info)
578{
579 if (info->prev) {
580 debug_path(info->prev);
581 if (*info->prev->name.path)
582 putchar('/');
583 }
584 printf("%s", info->name.path);
585}
586
587static void debug_name_entry(int i, struct name_entry *n)
588{
589 printf("ent#%d %06o %s\n", i,
590 n->path ? n->mode : 0,
591 n->path ? n->path : "(missing)");
592}
593
594static void debug_unpack_callback(int n,
595 unsigned long mask,
596 unsigned long dirmask,
597 struct name_entry *names,
598 struct traverse_info *info)
599{
600 int i;
601 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
602 mask, dirmask, n);
603 debug_path(info);
604 putchar('\n');
605 for (i = 0; i < n; i++)
606 debug_name_entry(i, names + i);
607}
608
01904572
LT
609static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
610{
c7cddc1a 611 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
01904572 612 struct unpack_trees_options *o = info->data;
01904572
LT
613 const struct name_entry *p = names;
614
615 /* Find first entry with a real name (we could use "mask" too) */
616 while (!p->mode)
617 p++;
618
ba655da5
JH
619 if (o->debug_unpack)
620 debug_unpack_callback(n, mask, dirmask, names, info);
621
01904572
LT
622 /* Are we supposed to look at the index too? */
623 if (o->merge) {
da165f47 624 while (1) {
da165f47 625 int cmp;
730f7284
JH
626 struct cache_entry *ce;
627
628 if (o->diff_index_cached)
629 ce = next_cache_entry(o);
630 else
631 ce = find_cache_entry(info, p);
632
da165f47
JH
633 if (!ce)
634 break;
635 cmp = compare_entry(ce, info, p);
01904572
LT
636 if (cmp < 0) {
637 if (unpack_index_entry(ce, o) < 0)
353c5eeb 638 return unpack_failed(o, NULL);
01904572
LT
639 continue;
640 }
641 if (!cmp) {
642 if (ce_stage(ce)) {
643 /*
da165f47
JH
644 * If we skip unmerged index
645 * entries, we'll skip this
646 * entry *and* the tree
647 * entries associated with it!
01904572 648 */
34110cd4 649 if (o->skip_unmerged) {
da165f47 650 add_same_unmerged(ce, o);
01904572 651 return mask;
34110cd4 652 }
01904572
LT
653 }
654 src[0] = ce;
01904572
LT
655 }
656 break;
657 }
658 }
659
34110cd4 660 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
01904572
LT
661 return -1;
662
da165f47
JH
663 if (src[0]) {
664 if (ce_stage(src[0]))
665 mark_ce_used_same_name(src[0], o);
666 else
667 mark_ce_used(src[0], o);
668 }
669
01904572
LT
670 /* Now handle any directories.. */
671 if (dirmask) {
672 unsigned long conflicts = mask & ~dirmask;
673 if (o->merge) {
674 conflicts <<= 1;
675 if (src[0])
676 conflicts |= 1;
677 }
b65982b6
JH
678
679 /* special case: "diff-index --cached" looking at a tree */
680 if (o->diff_index_cached &&
681 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
682 int matches;
683 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
684 names, info);
685 /*
da165f47
JH
686 * Everything under the name matches; skip the
687 * entire hierarchy. diff_index_cached codepath
688 * special cases D/F conflicts in such a way that
689 * it does not do any look-ahead, so this is safe.
b65982b6
JH
690 */
691 if (matches) {
da165f47 692 o->cache_bottom += matches;
b65982b6
JH
693 return mask;
694 }
695 }
696
542c264b
JH
697 if (traverse_trees_recursive(n, dirmask, conflicts,
698 names, info) < 0)
699 return -1;
01904572
LT
700 return mask;
701 }
702
703 return mask;
704}
705
2e2b887d
JH
706/*
707 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
708 * resulting index, -2 on failure to reflect the changes to the work tree.
709 */
01904572
LT
710int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
711{
e800ec9d 712 int i, ret;
0fb1eaa8 713 static struct cache_entry *dfc;
08aefc9e 714 struct exclude_list el;
16da134b 715
ca885a4f
JH
716 if (len > MAX_UNPACK_TREES)
717 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
076b0adc 718 memset(&state, 0, sizeof(state));
16da134b
JS
719 state.base_dir = "";
720 state.force = 1;
721 state.quiet = 1;
722 state.refresh_cache = 1;
723
08aefc9e
NTND
724 memset(&el, 0, sizeof(el));
725 if (!core_apply_sparse_checkout || !o->update)
726 o->skip_sparse_checkout = 1;
727 if (!o->skip_sparse_checkout) {
728 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0)
729 o->skip_sparse_checkout = 1;
730 else
731 o->el = &el;
732 }
733
34110cd4 734 memset(&o->result, 0, sizeof(o->result));
913e0e99 735 o->result.initialized = 1;
da165f47
JH
736 o->result.timestamp.sec = o->src_index->timestamp.sec;
737 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
16da134b 738 o->merge_size = len;
da165f47 739 mark_all_ce_unused(o->src_index);
0fb1eaa8
JH
740
741 if (!dfc)
13494ed1 742 dfc = xcalloc(1, cache_entry_size(0));
0fb1eaa8 743 o->df_conflict_entry = dfc;
16da134b
JS
744
745 if (len) {
01904572
LT
746 const char *prefix = o->prefix ? o->prefix : "";
747 struct traverse_info info;
748
749 setup_traverse_info(&info, prefix);
750 info.fn = unpack_callback;
751 info.data = o;
752
da165f47
JH
753 if (o->prefix) {
754 /*
755 * Unpack existing index entries that sort before the
756 * prefix the tree is spliced into. Note that o->merge
757 * is always true in this case.
758 */
759 while (1) {
760 struct cache_entry *ce = next_cache_entry(o);
761 if (!ce)
762 break;
763 if (ce_in_traverse_path(ce, &info))
764 break;
765 if (unpack_index_entry(ce, o) < 0)
766 goto return_failed;
767 }
08aefc9e 768 }
da165f47 769
01904572 770 if (traverse_trees(len, t, &info) < 0)
da165f47 771 goto return_failed;
16da134b
JS
772 }
773
01904572
LT
774 /* Any left-over entries in the index? */
775 if (o->merge) {
da165f47
JH
776 while (1) {
777 struct cache_entry *ce = next_cache_entry(o);
778 if (!ce)
779 break;
01904572 780 if (unpack_index_entry(ce, o) < 0)
da165f47 781 goto return_failed;
17e46426 782 }
17e46426 783 }
da165f47 784 mark_all_ce_unused(o->src_index);
16da134b 785
08aefc9e
NTND
786 if (o->trivial_merges_only && o->nontrivial_merge) {
787 ret = unpack_failed(o, "Merge requires file-level merging");
788 goto done;
789 }
01904572 790
e800ec9d 791 if (!o->skip_sparse_checkout) {
9e1afb16 792 int empty_worktree = 1;
e800ec9d
NTND
793 for (i = 0;i < o->result.cache_nr;i++) {
794 struct cache_entry *ce = o->result.cache[i];
795
796 if (apply_sparse_checkout(ce, o)) {
797 ret = -1;
798 goto done;
799 }
f1f523ea
NTND
800 /*
801 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
802 * area as a result of ce_skip_worktree() shortcuts in
803 * verify_absent() and verify_uptodate(). Clear them.
804 */
805 if (ce_skip_worktree(ce))
806 ce->ce_flags &= ~(CE_UPDATE | CE_REMOVE);
9e1afb16
NTND
807 else
808 empty_worktree = 0;
f1f523ea 809
e800ec9d 810 }
9e1afb16
NTND
811 if (o->result.cache_nr && empty_worktree) {
812 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
813 goto done;
814 }
e800ec9d 815 }
01904572 816
34110cd4 817 o->src_index = NULL;
2e2b887d 818 ret = check_updates(o) ? (-2) : 0;
34110cd4
LT
819 if (o->dst_index)
820 *o->dst_index = o->result;
08aefc9e
NTND
821
822done:
823 for (i = 0;i < el.nr;i++)
824 free(el.excludes[i]);
825 if (el.excludes)
826 free(el.excludes);
827
2e2b887d 828 return ret;
da165f47
JH
829
830return_failed:
831 mark_all_ce_unused(o->src_index);
026680f8
JH
832 ret = unpack_failed(o, NULL);
833 goto done;
16da134b 834}
076b0adc
JS
835
836/* Here come the merge functions */
837
8ccba008 838static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
076b0adc 839{
08353ebb 840 return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
076b0adc
JS
841}
842
843static int same(struct cache_entry *a, struct cache_entry *b)
844{
845 if (!!a != !!b)
846 return 0;
847 if (!a && !b)
848 return 1;
e11d7b59
JH
849 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
850 return 0;
076b0adc 851 return a->ce_mode == b->ce_mode &&
a89fccd2 852 !hashcmp(a->sha1, b->sha1);
076b0adc
JS
853}
854
855
856/*
857 * When a CE gets turned into an unmerged entry, we
858 * want it to be up-to-date
859 */
35a5aa79
NTND
860static int verify_uptodate_1(struct cache_entry *ce,
861 struct unpack_trees_options *o,
862 const char *error_msg)
076b0adc
JS
863{
864 struct stat st;
865
aecda37c 866 if (o->index_only || (!((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) && (o->reset || ce_uptodate(ce))))
203a2fe1 867 return 0;
076b0adc
JS
868
869 if (!lstat(ce->name, &st)) {
56cac48c 870 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
076b0adc 871 if (!changed)
203a2fe1 872 return 0;
936492d3
JH
873 /*
874 * NEEDSWORK: the current default policy is to allow
875 * submodule to be out of sync wrt the supermodule
876 * index. This needs to be tightened later for
877 * submodules that are marked to be automatically
878 * checked out.
879 */
7a51ed66 880 if (S_ISGITLINK(ce->ce_mode))
203a2fe1 881 return 0;
076b0adc
JS
882 errno = 0;
883 }
076b0adc 884 if (errno == ENOENT)
203a2fe1 885 return 0;
17e46426 886 return o->gently ? -1 :
35a5aa79
NTND
887 error(error_msg, ce->name);
888}
889
890static int verify_uptodate(struct cache_entry *ce,
891 struct unpack_trees_options *o)
892{
f1f523ea
NTND
893 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
894 return 0;
08353ebb 895 return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_NOT_UPTODATE_FILE));
076b0adc
JS
896}
897
e800ec9d
NTND
898static int verify_uptodate_sparse(struct cache_entry *ce,
899 struct unpack_trees_options *o)
900{
08353ebb 901 return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_SPARSE_NOT_UPTODATE_FILE));
076b0adc
JS
902}
903
bc052d7f 904static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
076b0adc
JS
905{
906 if (ce)
34110cd4 907 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
076b0adc
JS
908}
909
0cf73755
SV
910/*
911 * Check that checking out ce->sha1 in subdir ce->name is not
912 * going to overwrite any working files.
913 *
914 * Currently, git does not checkout subprojects during a superproject
915 * checkout, so it is not going to overwrite anything.
916 */
917static int verify_clean_submodule(struct cache_entry *ce, const char *action,
918 struct unpack_trees_options *o)
919{
920 return 0;
921}
922
923static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
c8193534
JH
924 struct unpack_trees_options *o)
925{
926 /*
0cf73755 927 * we are about to extract "ce->name"; we would not want to lose
c8193534
JH
928 * anything in the existing directory there.
929 */
930 int namelen;
7b9e3ce0 931 int i;
c8193534
JH
932 struct dir_struct d;
933 char *pathbuf;
934 int cnt = 0;
0cf73755
SV
935 unsigned char sha1[20];
936
7a51ed66 937 if (S_ISGITLINK(ce->ce_mode) &&
0cf73755
SV
938 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
939 /* If we are not going to update the submodule, then
940 * we don't care.
941 */
942 if (!hashcmp(sha1, ce->sha1))
943 return 0;
944 return verify_clean_submodule(ce, action, o);
945 }
c8193534
JH
946
947 /*
948 * First let's make sure we do not have a local modification
949 * in that directory.
950 */
0cf73755 951 namelen = strlen(ce->name);
da165f47
JH
952 for (i = locate_in_src_index(ce, o);
953 i < o->src_index->cache_nr;
954 i++) {
837e5fe9
CB
955 struct cache_entry *ce2 = o->src_index->cache[i];
956 int len = ce_namelen(ce2);
c8193534 957 if (len < namelen ||
837e5fe9
CB
958 strncmp(ce->name, ce2->name, namelen) ||
959 ce2->name[namelen] != '/')
c8193534
JH
960 break;
961 /*
da165f47
JH
962 * ce2->name is an entry in the subdirectory to be
963 * removed.
c8193534 964 */
837e5fe9
CB
965 if (!ce_stage(ce2)) {
966 if (verify_uptodate(ce2, o))
203a2fe1 967 return -1;
837e5fe9 968 add_entry(o, ce2, CE_REMOVE, 0);
da165f47 969 mark_ce_used(ce2, o);
c8193534
JH
970 }
971 cnt++;
972 }
973
974 /*
975 * Then we need to make sure that we do not lose a locally
976 * present file that is not ignored.
977 */
978 pathbuf = xmalloc(namelen + 2);
0cf73755 979 memcpy(pathbuf, ce->name, namelen);
c8193534
JH
980 strcpy(pathbuf+namelen, "/");
981
982 memset(&d, 0, sizeof(d));
983 if (o->dir)
984 d.exclude_per_dir = o->dir->exclude_per_dir;
dba2e203 985 i = read_directory(&d, pathbuf, namelen+1, NULL);
c8193534 986 if (i)
17e46426 987 return o->gently ? -1 :
08353ebb 988 error(ERRORMSG(o, ERROR_NOT_UPTODATE_DIR), ce->name);
c8193534
JH
989 free(pathbuf);
990 return cnt;
991}
992
32260ad5
LT
993/*
994 * This gets called when there was no index entry for the tree entry 'dst',
995 * but we found a file in the working tree that 'lstat()' said was fine,
996 * and we're on a case-insensitive filesystem.
997 *
998 * See if we can find a case-insensitive match in the index that also
999 * matches the stat information, and assume it's that other file!
1000 */
1001static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
1002{
1003 struct cache_entry *src;
1004
1005 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
56cac48c 1006 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
32260ad5
LT
1007}
1008
076b0adc
JS
1009/*
1010 * We do not want to remove or overwrite a working tree file that
f8a9d428 1011 * is not tracked, unless it is ignored.
076b0adc 1012 */
35a5aa79
NTND
1013static int verify_absent_1(struct cache_entry *ce, const char *action,
1014 struct unpack_trees_options *o,
1015 const char *error_msg)
076b0adc
JS
1016{
1017 struct stat st;
1018
1019 if (o->index_only || o->reset || !o->update)
203a2fe1 1020 return 0;
c8193534 1021
57199892 1022 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
203a2fe1 1023 return 0;
ec0603e1 1024
0cf73755 1025 if (!lstat(ce->name, &st)) {
6831a88a 1026 int dtype = ce_to_dtype(ce);
df292c79 1027 struct cache_entry *result;
c8193534 1028
32260ad5
LT
1029 /*
1030 * It may be that the 'lstat()' succeeded even though
1031 * target 'ce' was absent, because there is an old
1032 * entry that is different only in case..
1033 *
1034 * Ignore that lstat() if it matches.
1035 */
1036 if (ignore_case && icase_exists(o, ce, &st))
1037 return 0;
1038
6831a88a 1039 if (o->dir && excluded(o->dir, ce->name, &dtype))
c8193534 1040 /*
0cf73755 1041 * ce->name is explicitly excluded, so it is Ok to
c8193534
JH
1042 * overwrite it.
1043 */
203a2fe1 1044 return 0;
c8193534
JH
1045 if (S_ISDIR(st.st_mode)) {
1046 /*
1047 * We are checking out path "foo" and
1048 * found "foo/." in the working tree.
1049 * This is tricky -- if we have modified
1050 * files that are in "foo/" we would lose
6caa7b55 1051 * them.
c8193534 1052 */
da165f47
JH
1053 if (verify_clean_subdirectory(ce, action, o) < 0)
1054 return -1;
203a2fe1 1055 return 0;
c8193534
JH
1056 }
1057
1058 /*
1059 * The previous round may already have decided to
1060 * delete this path, which is in a subdirectory that
1061 * is being replaced with a blob.
1062 */
cd2fef59 1063 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
df292c79
LT
1064 if (result) {
1065 if (result->ce_flags & CE_REMOVE)
203a2fe1 1066 return 0;
c8193534
JH
1067 }
1068
17e46426 1069 return o->gently ? -1 :
08353ebb 1070 error(ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED), ce->name, action);
c8193534 1071 }
203a2fe1 1072 return 0;
076b0adc 1073}
35a5aa79
NTND
1074static int verify_absent(struct cache_entry *ce, const char *action,
1075 struct unpack_trees_options *o)
1076{
f1f523ea
NTND
1077 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
1078 return 0;
08353ebb 1079 return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED));
35a5aa79 1080}
076b0adc 1081
e800ec9d
NTND
1082static int verify_absent_sparse(struct cache_entry *ce, const char *action,
1083 struct unpack_trees_options *o)
1084{
08353ebb 1085 return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_ORPHANED));
e800ec9d 1086}
076b0adc
JS
1087
1088static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1089 struct unpack_trees_options *o)
1090{
7f8ab8dc
LT
1091 int update = CE_UPDATE;
1092
e11d7b59
JH
1093 if (!old) {
1094 if (verify_absent(merge, "overwritten", o))
1095 return -1;
1096 invalidate_ce_path(merge, o);
1097 } else if (!(old->ce_flags & CE_CONFLICTED)) {
076b0adc
JS
1098 /*
1099 * See if we can re-use the old CE directly?
1100 * That way we get the uptodate stat info.
1101 *
7f8ab8dc
LT
1102 * This also removes the UPDATE flag on a match; otherwise
1103 * we will end up overwriting local changes in the work tree.
076b0adc
JS
1104 */
1105 if (same(old, merge)) {
eb7a2f1d 1106 copy_cache_entry(merge, old);
7f8ab8dc 1107 update = 0;
076b0adc 1108 } else {
203a2fe1
DB
1109 if (verify_uptodate(old, o))
1110 return -1;
32f54ca3
NTND
1111 if (ce_skip_worktree(old))
1112 update |= CE_SKIP_WORKTREE;
bc052d7f 1113 invalidate_ce_path(old, o);
076b0adc 1114 }
e11d7b59
JH
1115 } else {
1116 /*
1117 * Previously unmerged entry left as an existence
1118 * marker by read_index_unmerged();
1119 */
1120 invalidate_ce_path(old, o);
076b0adc
JS
1121 }
1122
7f8ab8dc 1123 add_entry(o, merge, update, CE_STAGEMASK);
076b0adc
JS
1124 return 1;
1125}
1126
1127static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
1128 struct unpack_trees_options *o)
1129{
34110cd4
LT
1130 /* Did it exist in the index? */
1131 if (!old) {
203a2fe1
DB
1132 if (verify_absent(ce, "removed", o))
1133 return -1;
34110cd4
LT
1134 return 0;
1135 }
e11d7b59 1136 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
34110cd4
LT
1137 return -1;
1138 add_entry(o, ce, CE_REMOVE, 0);
bc052d7f 1139 invalidate_ce_path(ce, o);
076b0adc
JS
1140 return 1;
1141}
1142
7f7932ab 1143static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
076b0adc 1144{
34110cd4 1145 add_entry(o, ce, 0, 0);
076b0adc
JS
1146 return 1;
1147}
1148
1149#if DBRT_DEBUG
1150static void show_stage_entry(FILE *o,
1151 const char *label, const struct cache_entry *ce)
1152{
1153 if (!ce)
1154 fprintf(o, "%s (missing)\n", label);
1155 else
1156 fprintf(o, "%s%06o %s %d\t%s\n",
1157 label,
7a51ed66 1158 ce->ce_mode,
076b0adc
JS
1159 sha1_to_hex(ce->sha1),
1160 ce_stage(ce),
1161 ce->name);
1162}
1163#endif
1164
34110cd4 1165int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
076b0adc
JS
1166{
1167 struct cache_entry *index;
1168 struct cache_entry *head;
1169 struct cache_entry *remote = stages[o->head_idx + 1];
1170 int count;
1171 int head_match = 0;
1172 int remote_match = 0;
076b0adc
JS
1173
1174 int df_conflict_head = 0;
1175 int df_conflict_remote = 0;
1176
1177 int any_anc_missing = 0;
1178 int no_anc_exists = 1;
1179 int i;
1180
1181 for (i = 1; i < o->head_idx; i++) {
4c4caafc 1182 if (!stages[i] || stages[i] == o->df_conflict_entry)
076b0adc 1183 any_anc_missing = 1;
4c4caafc 1184 else
076b0adc 1185 no_anc_exists = 0;
076b0adc
JS
1186 }
1187
1188 index = stages[0];
1189 head = stages[o->head_idx];
1190
1191 if (head == o->df_conflict_entry) {
1192 df_conflict_head = 1;
1193 head = NULL;
1194 }
1195
1196 if (remote == o->df_conflict_entry) {
1197 df_conflict_remote = 1;
1198 remote = NULL;
1199 }
1200
cee2d6ae
JH
1201 /*
1202 * First, if there's a #16 situation, note that to prevent #13
076b0adc
JS
1203 * and #14.
1204 */
1205 if (!same(remote, head)) {
1206 for (i = 1; i < o->head_idx; i++) {
1207 if (same(stages[i], head)) {
1208 head_match = i;
1209 }
1210 if (same(stages[i], remote)) {
1211 remote_match = i;
1212 }
1213 }
1214 }
1215
cee2d6ae
JH
1216 /*
1217 * We start with cases where the index is allowed to match
076b0adc
JS
1218 * something other than the head: #14(ALT) and #2ALT, where it
1219 * is permitted to match the result instead.
1220 */
1221 /* #14, #14ALT, #2ALT */
1222 if (remote && !df_conflict_head && head_match && !remote_match) {
1223 if (index && !same(index, remote) && !same(index, head))
8ccba008 1224 return o->gently ? -1 : reject_merge(index, o);
076b0adc
JS
1225 return merged_entry(remote, index, o);
1226 }
1227 /*
1228 * If we have an entry in the index cache, then we want to
1229 * make sure that it matches head.
1230 */
4e7c4571 1231 if (index && !same(index, head))
8ccba008 1232 return o->gently ? -1 : reject_merge(index, o);
076b0adc
JS
1233
1234 if (head) {
1235 /* #5ALT, #15 */
1236 if (same(head, remote))
1237 return merged_entry(head, index, o);
1238 /* #13, #3ALT */
1239 if (!df_conflict_remote && remote_match && !head_match)
1240 return merged_entry(head, index, o);
1241 }
1242
1243 /* #1 */
34110cd4 1244 if (!head && !remote && any_anc_missing)
076b0adc
JS
1245 return 0;
1246
cee2d6ae
JH
1247 /*
1248 * Under the "aggressive" rule, we resolve mostly trivial
076b0adc
JS
1249 * cases that we historically had git-merge-one-file resolve.
1250 */
1251 if (o->aggressive) {
cee2d6ae
JH
1252 int head_deleted = !head;
1253 int remote_deleted = !remote;
0cf73755 1254 struct cache_entry *ce = NULL;
4c4caafc
JH
1255
1256 if (index)
0cf73755 1257 ce = index;
4c4caafc 1258 else if (head)
0cf73755 1259 ce = head;
4c4caafc 1260 else if (remote)
0cf73755 1261 ce = remote;
4c4caafc
JH
1262 else {
1263 for (i = 1; i < o->head_idx; i++) {
1264 if (stages[i] && stages[i] != o->df_conflict_entry) {
0cf73755 1265 ce = stages[i];
4c4caafc
JH
1266 break;
1267 }
1268 }
1269 }
1270
076b0adc
JS
1271 /*
1272 * Deleted in both.
1273 * Deleted in one and unchanged in the other.
1274 */
1275 if ((head_deleted && remote_deleted) ||
1276 (head_deleted && remote && remote_match) ||
1277 (remote_deleted && head && head_match)) {
1278 if (index)
1279 return deleted_entry(index, index, o);
34110cd4 1280 if (ce && !head_deleted) {
203a2fe1
DB
1281 if (verify_absent(ce, "removed", o))
1282 return -1;
1283 }
076b0adc
JS
1284 return 0;
1285 }
1286 /*
1287 * Added in both, identically.
1288 */
1289 if (no_anc_exists && head && remote && same(head, remote))
1290 return merged_entry(head, index, o);
1291
1292 }
1293
1294 /* Below are "no merge" cases, which require that the index be
1295 * up-to-date to avoid the files getting overwritten with
1296 * conflict resolution files.
1297 */
1298 if (index) {
203a2fe1
DB
1299 if (verify_uptodate(index, o))
1300 return -1;
076b0adc 1301 }
076b0adc
JS
1302
1303 o->nontrivial_merge = 1;
1304
ea4b52a8 1305 /* #2, #3, #4, #6, #7, #9, #10, #11. */
076b0adc
JS
1306 count = 0;
1307 if (!head_match || !remote_match) {
1308 for (i = 1; i < o->head_idx; i++) {
4c4caafc 1309 if (stages[i] && stages[i] != o->df_conflict_entry) {
7f7932ab 1310 keep_entry(stages[i], o);
076b0adc
JS
1311 count++;
1312 break;
1313 }
1314 }
1315 }
1316#if DBRT_DEBUG
1317 else {
1318 fprintf(stderr, "read-tree: warning #16 detected\n");
1319 show_stage_entry(stderr, "head ", stages[head_match]);
1320 show_stage_entry(stderr, "remote ", stages[remote_match]);
1321 }
1322#endif
7f7932ab
JH
1323 if (head) { count += keep_entry(head, o); }
1324 if (remote) { count += keep_entry(remote, o); }
076b0adc
JS
1325 return count;
1326}
1327
1328/*
1329 * Two-way merge.
1330 *
1331 * The rule is to "carry forward" what is in the index without losing
a75d7b54 1332 * information across a "fast-forward", favoring a successful merge
076b0adc
JS
1333 * over a merge failure when it makes sense. For details of the
1334 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1335 *
1336 */
34110cd4 1337int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
076b0adc
JS
1338{
1339 struct cache_entry *current = src[0];
b8ba1535
JH
1340 struct cache_entry *oldtree = src[1];
1341 struct cache_entry *newtree = src[2];
076b0adc
JS
1342
1343 if (o->merge_size != 2)
1344 return error("Cannot do a twoway merge of %d trees",
1345 o->merge_size);
1346
b8ba1535
JH
1347 if (oldtree == o->df_conflict_entry)
1348 oldtree = NULL;
1349 if (newtree == o->df_conflict_entry)
1350 newtree = NULL;
1351
076b0adc
JS
1352 if (current) {
1353 if ((!oldtree && !newtree) || /* 4 and 5 */
1354 (!oldtree && newtree &&
1355 same(current, newtree)) || /* 6 and 7 */
1356 (oldtree && newtree &&
1357 same(oldtree, newtree)) || /* 14 and 15 */
1358 (oldtree && newtree &&
b8ba1535 1359 !same(oldtree, newtree) && /* 18 and 19 */
076b0adc 1360 same(current, newtree))) {
7f7932ab 1361 return keep_entry(current, o);
076b0adc
JS
1362 }
1363 else if (oldtree && !newtree && same(current, oldtree)) {
1364 /* 10 or 11 */
1365 return deleted_entry(oldtree, current, o);
1366 }
1367 else if (oldtree && newtree &&
1368 same(current, oldtree) && !same(current, newtree)) {
1369 /* 20 or 21 */
1370 return merged_entry(newtree, current, o);
1371 }
1372 else {
1373 /* all other failures */
1374 if (oldtree)
8ccba008 1375 return o->gently ? -1 : reject_merge(oldtree, o);
076b0adc 1376 if (current)
8ccba008 1377 return o->gently ? -1 : reject_merge(current, o);
076b0adc 1378 if (newtree)
8ccba008 1379 return o->gently ? -1 : reject_merge(newtree, o);
076b0adc
JS
1380 return -1;
1381 }
1382 }
55218834
JH
1383 else if (newtree) {
1384 if (oldtree && !o->initial_checkout) {
1385 /*
1386 * deletion of the path was staged;
1387 */
1388 if (same(oldtree, newtree))
1389 return 1;
1390 return reject_merge(oldtree, o);
1391 }
076b0adc 1392 return merged_entry(newtree, current, o);
55218834 1393 }
d699676d 1394 return deleted_entry(oldtree, current, o);
076b0adc
JS
1395}
1396
1397/*
1398 * Bind merge.
1399 *
1400 * Keep the index entries at stage0, collapse stage1 but make sure
1401 * stage0 does not have anything there.
1402 */
1403int bind_merge(struct cache_entry **src,
34110cd4 1404 struct unpack_trees_options *o)
076b0adc
JS
1405{
1406 struct cache_entry *old = src[0];
1407 struct cache_entry *a = src[1];
1408
1409 if (o->merge_size != 1)
1410 return error("Cannot do a bind merge of %d trees\n",
1411 o->merge_size);
1412 if (a && old)
17e46426 1413 return o->gently ? -1 :
08353ebb 1414 error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);
076b0adc 1415 if (!a)
7f7932ab 1416 return keep_entry(old, o);
076b0adc
JS
1417 else
1418 return merged_entry(a, NULL, o);
1419}
1420
1421/*
1422 * One-way merge.
1423 *
1424 * The rule is:
1425 * - take the stat information from stage0, take the data from stage1
1426 */
34110cd4 1427int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
076b0adc
JS
1428{
1429 struct cache_entry *old = src[0];
1430 struct cache_entry *a = src[1];
1431
1432 if (o->merge_size != 1)
1433 return error("Cannot do a oneway merge of %d trees",
1434 o->merge_size);
1435
78d3b06e 1436 if (!a || a == o->df_conflict_entry)
076b0adc 1437 return deleted_entry(old, old, o);
34110cd4 1438
076b0adc 1439 if (old && same(old, a)) {
34110cd4 1440 int update = 0;
52030836 1441 if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) {
076b0adc
JS
1442 struct stat st;
1443 if (lstat(old->name, &st) ||
56cac48c 1444 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
34110cd4 1445 update |= CE_UPDATE;
076b0adc 1446 }
34110cd4
LT
1447 add_entry(o, old, update, 0);
1448 return 0;
076b0adc
JS
1449 }
1450 return merged_entry(a, old, o);
1451}