1#define NO_THE_INDEX_COMPATIBILITY_MACROS
2#include "cache.h"
3#include "dir.h"
4#include "tree.h"
5#include "tree-walk.h"
6#include "cache-tree.h"
7#include "unpack-trees.h"
8#include "progress.h"
9#include "refs.h"
10#include "attr.h"
11
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
16 * situation better. See how "git checkout" replaces not_uptodate_file to
17 * explain why it does not allow switching between branches when you have
18 * local changes, for example.
19 */
20static struct unpack_trees_error_msgs unpack_plumbing_errors = {
21 /* would_overwrite */
22 "Entry '%s' would be overwritten by merge. Cannot merge.",
23
24 /* not_uptodate_file */
25 "Entry '%s' not uptodate. Cannot merge.",
26
27 /* not_uptodate_dir */
28 "Updating '%s' would lose untracked files in it",
29
30 /* would_lose_untracked */
31 "Untracked working tree file '%s' would be %s by merge.",
32
33 /* bind_overlap */
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
35};
36
37#define ERRORMSG(o,fld) \
38 ( ((o) && (o)->msgs.fld) \
39 ? ((o)->msgs.fld) \
40 : (unpack_plumbing_errors.fld) )
41
42static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
43 unsigned int set, unsigned int clear)
44{
45 unsigned int size = ce_size(ce);
46 struct cache_entry *new = xmalloc(size);
47
48 clear |= CE_HASHED | CE_UNHASHED;
49
50 memcpy(new, ce, size);
51 new->next = NULL;
52 new->ce_flags = (new->ce_flags & ~clear) | set;
53 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
54}
55
56/* Unlink the last component and attempt to remove leading
57 * directories, in case this unlink is the removal of the
58 * last entry in the directory -- empty directories are removed.
59 */
60static void unlink_entry(struct cache_entry *ce)
61{
62 char *cp, *prev;
63 char *name = ce->name;
64
65 if (has_symlink_or_noent_leading_path(ce_namelen(ce), ce->name))
66 return;
67 if (unlink(name))
68 return;
69 prev = NULL;
70 while (1) {
71 int status;
72 cp = strrchr(name, '/');
73 if (prev)
74 *prev = '/';
75 if (!cp)
76 break;
77
78 *cp = 0;
79 status = rmdir(name);
80 if (status) {
81 *cp = '/';
82 break;
83 }
84 prev = cp;
85 }
86}
87
88static struct checkout state;
89static int check_updates(struct unpack_trees_options *o)
90{
91 unsigned cnt = 0, total = 0;
92 struct progress *progress = NULL;
93 struct index_state *index = &o->result;
94 int i;
95 int errs = 0;
96
97 if (o->update && o->verbose_update) {
98 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
99 struct cache_entry *ce = index->cache[cnt];
100 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
101 total++;
102 }
103
104 progress = start_progress_delay("Checking out files",
105 total, 50, 1);
106 cnt = 0;
107 }
108
109 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
110 for (i = 0; i < index->cache_nr; i++) {
111 struct cache_entry *ce = index->cache[i];
112
113 if (ce->ce_flags & CE_REMOVE) {
114 display_progress(progress, ++cnt);
115 if (o->update)
116 unlink_entry(ce);
117 remove_index_entry_at(&o->result, i);
118 i--;
119 continue;
120 }
121 }
122
123 for (i = 0; i < index->cache_nr; i++) {
124 struct cache_entry *ce = index->cache[i];
125
126 if (ce->ce_flags & CE_UPDATE) {
127 display_progress(progress, ++cnt);
128 ce->ce_flags &= ~CE_UPDATE;
129 if (o->update) {
130 errs |= checkout_entry(ce, &state, NULL);
131 }
132 }
133 }
134 stop_progress(&progress);
135 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
136 return errs != 0;
137}
138
139static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
140{
141 int ret = o->fn(src, o);
142 if (ret > 0)
143 ret = 0;
144 return ret;
145}
146
147static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
148{
149 struct cache_entry *src[5] = { ce, };
150
151 o->pos++;
152 if (ce_stage(ce)) {
153 if (o->skip_unmerged) {
154 add_entry(o, ce, 0, 0);
155 return 0;
156 }
157 }
158 return call_unpack_fn(src, o);
159}
160
161int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
162{
163 int i;
164 struct tree_desc t[MAX_UNPACK_TREES];
165 struct traverse_info newinfo;
166 struct name_entry *p;
167
168 p = names;
169 while (!p->mode)
170 p++;
171
172 newinfo = *info;
173 newinfo.prev = info;
174 newinfo.name = *p;
175 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
176 newinfo.conflicts |= df_conflicts;
177
178 for (i = 0; i < n; i++, dirmask >>= 1) {
179 const unsigned char *sha1 = NULL;
180 if (dirmask & 1)
181 sha1 = names[i].sha1;
182 fill_tree_descriptor(t+i, sha1);
183 }
184 return traverse_trees(n, t, &newinfo);
185}
186
187/*
188 * Compare the traverse-path to the cache entry without actually
189 * having to generate the textual representation of the traverse
190 * path.
191 *
192 * NOTE! This *only* compares up to the size of the traverse path
193 * itself - the caller needs to do the final check for the cache
194 * entry having more data at the end!
195 */
196static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
197{
198 int len, pathlen, ce_len;
199 const char *ce_name;
200
201 if (info->prev) {
202 int cmp = do_compare_entry(ce, info->prev, &info->name);
203 if (cmp)
204 return cmp;
205 }
206 pathlen = info->pathlen;
207 ce_len = ce_namelen(ce);
208
209 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
210 if (ce_len < pathlen)
211 return -1;
212
213 ce_len -= pathlen;
214 ce_name = ce->name + pathlen;
215
216 len = tree_entry_len(n->path, n->sha1);
217 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
218}
219
220static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
221{
222 int cmp = do_compare_entry(ce, info, n);
223 if (cmp)
224 return cmp;
225
226 /*
227 * Even if the beginning compared identically, the ce should
228 * compare as bigger than a directory leading up to it!
229 */
230 return ce_namelen(ce) > traverse_path_len(info, n);
231}
232
233static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
234{
235 int len = traverse_path_len(info, n);
236 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
237
238 ce->ce_mode = create_ce_mode(n->mode);
239 ce->ce_flags = create_ce_flags(len, stage);
240 hashcpy(ce->sha1, n->sha1);
241 make_traverse_path(ce->name, info, n);
242
243 return ce;
244}
245
246static int unpack_nondirectories(int n, unsigned long mask,
247 unsigned long dirmask,
248 struct cache_entry **src,
249 const struct name_entry *names,
250 const struct traverse_info *info)
251{
252 int i;
253 struct unpack_trees_options *o = info->data;
254 unsigned long conflicts;
255
256 /* Do we have *only* directories? Nothing to do */
257 if (mask == dirmask && !src[0])
258 return 0;
259
260 conflicts = info->conflicts;
261 if (o->merge)
262 conflicts >>= 1;
263 conflicts |= dirmask;
264
265 /*
266 * Ok, we've filled in up to any potential index entry in src[0],
267 * now do the rest.
268 */
269 for (i = 0; i < n; i++) {
270 int stage;
271 unsigned int bit = 1ul << i;
272 if (conflicts & bit) {
273 src[i + o->merge] = o->df_conflict_entry;
274 continue;
275 }
276 if (!(mask & bit))
277 continue;
278 if (!o->merge)
279 stage = 0;
280 else if (i + 1 < o->head_idx)
281 stage = 1;
282 else if (i + 1 > o->head_idx)
283 stage = 3;
284 else
285 stage = 2;
286 src[i + o->merge] = create_ce_entry(info, names + i, stage);
287 }
288
289 if (o->merge)
290 return call_unpack_fn(src, o);
291
292 n += o->merge;
293 for (i = 0; i < n; i++)
294 add_entry(o, src[i], 0, 0);
295 return 0;
296}
297
298static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
299{
300 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
301 struct unpack_trees_options *o = info->data;
302 const struct name_entry *p = names;
303
304 /* Find first entry with a real name (we could use "mask" too) */
305 while (!p->mode)
306 p++;
307
308 /* Are we supposed to look at the index too? */
309 if (o->merge) {
310 while (o->pos < o->src_index->cache_nr) {
311 struct cache_entry *ce = o->src_index->cache[o->pos];
312 int cmp = compare_entry(ce, info, p);
313 if (cmp < 0) {
314 if (unpack_index_entry(ce, o) < 0)
315 return -1;
316 continue;
317 }
318 if (!cmp) {
319 o->pos++;
320 if (ce_stage(ce)) {
321 /*
322 * If we skip unmerged index entries, we'll skip this
323 * entry *and* the tree entries associated with it!
324 */
325 if (o->skip_unmerged) {
326 add_entry(o, ce, 0, 0);
327 return mask;
328 }
329 }
330 src[0] = ce;
331 }
332 break;
333 }
334 }
335
336 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
337 return -1;
338
339 /* Now handle any directories.. */
340 if (dirmask) {
341 unsigned long conflicts = mask & ~dirmask;
342 if (o->merge) {
343 conflicts <<= 1;
344 if (src[0])
345 conflicts |= 1;
346 }
347 if (traverse_trees_recursive(n, dirmask, conflicts,
348 names, info) < 0)
349 return -1;
350 return mask;
351 }
352
353 return mask;
354}
355
356static int unpack_failed(struct unpack_trees_options *o, const char *message)
357{
358 discard_index(&o->result);
359 if (!o->gently) {
360 if (message)
361 return error("%s", message);
362 return -1;
363 }
364 return -1;
365}
366
367/*
368 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
369 * resulting index, -2 on failure to reflect the changes to the work tree.
370 */
371int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
372{
373 int ret;
374 static struct cache_entry *dfc;
375
376 if (len > MAX_UNPACK_TREES)
377 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
378 memset(&state, 0, sizeof(state));
379 state.base_dir = "";
380 state.force = 1;
381 state.quiet = 1;
382 state.refresh_cache = 1;
383
384 memset(&o->result, 0, sizeof(o->result));
385 o->result.initialized = 1;
386 if (o->src_index)
387 o->result.timestamp = o->src_index->timestamp;
388 o->merge_size = len;
389
390 if (!dfc)
391 dfc = xcalloc(1, cache_entry_size(0));
392 o->df_conflict_entry = dfc;
393
394 if (len) {
395 const char *prefix = o->prefix ? o->prefix : "";
396 struct traverse_info info;
397
398 setup_traverse_info(&info, prefix);
399 info.fn = unpack_callback;
400 info.data = o;
401
402 if (traverse_trees(len, t, &info) < 0)
403 return unpack_failed(o, NULL);
404 }
405
406 /* Any left-over entries in the index? */
407 if (o->merge) {
408 while (o->pos < o->src_index->cache_nr) {
409 struct cache_entry *ce = o->src_index->cache[o->pos];
410 if (unpack_index_entry(ce, o) < 0)
411 return unpack_failed(o, NULL);
412 }
413 }
414
415 if (o->trivial_merges_only && o->nontrivial_merge)
416 return unpack_failed(o, "Merge requires file-level merging");
417
418 o->src_index = NULL;
419 ret = check_updates(o) ? (-2) : 0;
420 if (o->dst_index)
421 *o->dst_index = o->result;
422 return ret;
423}
424
425/* Here come the merge functions */
426
427static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
428{
429 return error(ERRORMSG(o, would_overwrite), ce->name);
430}
431
432static int same(struct cache_entry *a, struct cache_entry *b)
433{
434 if (!!a != !!b)
435 return 0;
436 if (!a && !b)
437 return 1;
438 return a->ce_mode == b->ce_mode &&
439 !hashcmp(a->sha1, b->sha1);
440}
441
442
443/*
444 * When a CE gets turned into an unmerged entry, we
445 * want it to be up-to-date
446 */
447static int verify_uptodate(struct cache_entry *ce,
448 struct unpack_trees_options *o)
449{
450 struct stat st;
451
452 if (o->index_only || o->reset)
453 return 0;
454
455 if (!lstat(ce->name, &st)) {
456 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
457 if (!changed)
458 return 0;
459 /*
460 * NEEDSWORK: the current default policy is to allow
461 * submodule to be out of sync wrt the supermodule
462 * index. This needs to be tightened later for
463 * submodules that are marked to be automatically
464 * checked out.
465 */
466 if (S_ISGITLINK(ce->ce_mode))
467 return 0;
468 errno = 0;
469 }
470 if (errno == ENOENT)
471 return 0;
472 return o->gently ? -1 :
473 error(ERRORMSG(o, not_uptodate_file), ce->name);
474}
475
476static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
477{
478 if (ce)
479 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
480}
481
482/*
483 * Check that checking out ce->sha1 in subdir ce->name is not
484 * going to overwrite any working files.
485 *
486 * Currently, git does not checkout subprojects during a superproject
487 * checkout, so it is not going to overwrite anything.
488 */
489static int verify_clean_submodule(struct cache_entry *ce, const char *action,
490 struct unpack_trees_options *o)
491{
492 return 0;
493}
494
495static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
496 struct unpack_trees_options *o)
497{
498 /*
499 * we are about to extract "ce->name"; we would not want to lose
500 * anything in the existing directory there.
501 */
502 int namelen;
503 int i;
504 struct dir_struct d;
505 char *pathbuf;
506 int cnt = 0;
507 unsigned char sha1[20];
508
509 if (S_ISGITLINK(ce->ce_mode) &&
510 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
511 /* If we are not going to update the submodule, then
512 * we don't care.
513 */
514 if (!hashcmp(sha1, ce->sha1))
515 return 0;
516 return verify_clean_submodule(ce, action, o);
517 }
518
519 /*
520 * First let's make sure we do not have a local modification
521 * in that directory.
522 */
523 namelen = strlen(ce->name);
524 for (i = o->pos; i < o->src_index->cache_nr; i++) {
525 struct cache_entry *ce2 = o->src_index->cache[i];
526 int len = ce_namelen(ce2);
527 if (len < namelen ||
528 strncmp(ce->name, ce2->name, namelen) ||
529 ce2->name[namelen] != '/')
530 break;
531 /*
532 * ce2->name is an entry in the subdirectory.
533 */
534 if (!ce_stage(ce2)) {
535 if (verify_uptodate(ce2, o))
536 return -1;
537 add_entry(o, ce2, CE_REMOVE, 0);
538 }
539 cnt++;
540 }
541
542 /*
543 * Then we need to make sure that we do not lose a locally
544 * present file that is not ignored.
545 */
546 pathbuf = xmalloc(namelen + 2);
547 memcpy(pathbuf, ce->name, namelen);
548 strcpy(pathbuf+namelen, "/");
549
550 memset(&d, 0, sizeof(d));
551 if (o->dir)
552 d.exclude_per_dir = o->dir->exclude_per_dir;
553 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
554 if (i)
555 return o->gently ? -1 :
556 error(ERRORMSG(o, not_uptodate_dir), ce->name);
557 free(pathbuf);
558 return cnt;
559}
560
561/*
562 * This gets called when there was no index entry for the tree entry 'dst',
563 * but we found a file in the working tree that 'lstat()' said was fine,
564 * and we're on a case-insensitive filesystem.
565 *
566 * See if we can find a case-insensitive match in the index that also
567 * matches the stat information, and assume it's that other file!
568 */
569static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
570{
571 struct cache_entry *src;
572
573 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
574 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
575}
576
577/*
578 * We do not want to remove or overwrite a working tree file that
579 * is not tracked, unless it is ignored.
580 */
581static int verify_absent(struct cache_entry *ce, const char *action,
582 struct unpack_trees_options *o)
583{
584 struct stat st;
585
586 if (o->index_only || o->reset || !o->update)
587 return 0;
588
589 if (has_symlink_or_noent_leading_path(ce_namelen(ce), ce->name))
590 return 0;
591
592 if (!lstat(ce->name, &st)) {
593 int ret;
594 int dtype = ce_to_dtype(ce);
595 struct cache_entry *result;
596
597 /*
598 * It may be that the 'lstat()' succeeded even though
599 * target 'ce' was absent, because there is an old
600 * entry that is different only in case..
601 *
602 * Ignore that lstat() if it matches.
603 */
604 if (ignore_case && icase_exists(o, ce, &st))
605 return 0;
606
607 if (o->dir && excluded(o->dir, ce->name, &dtype))
608 /*
609 * ce->name is explicitly excluded, so it is Ok to
610 * overwrite it.
611 */
612 return 0;
613 if (S_ISDIR(st.st_mode)) {
614 /*
615 * We are checking out path "foo" and
616 * found "foo/." in the working tree.
617 * This is tricky -- if we have modified
618 * files that are in "foo/" we would lose
619 * it.
620 */
621 ret = verify_clean_subdirectory(ce, action, o);
622 if (ret < 0)
623 return ret;
624
625 /*
626 * If this removed entries from the index,
627 * what that means is:
628 *
629 * (1) the caller unpack_callback() saw path/foo
630 * in the index, and it has not removed it because
631 * it thinks it is handling 'path' as blob with
632 * D/F conflict;
633 * (2) we will return "ok, we placed a merged entry
634 * in the index" which would cause o->pos to be
635 * incremented by one;
636 * (3) however, original o->pos now has 'path/foo'
637 * marked with "to be removed".
638 *
639 * We need to increment it by the number of
640 * deleted entries here.
641 */
642 o->pos += ret;
643 return 0;
644 }
645
646 /*
647 * The previous round may already have decided to
648 * delete this path, which is in a subdirectory that
649 * is being replaced with a blob.
650 */
651 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
652 if (result) {
653 if (result->ce_flags & CE_REMOVE)
654 return 0;
655 }
656
657 return o->gently ? -1 :
658 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
659 }
660 return 0;
661}
662
663static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
664 struct unpack_trees_options *o)
665{
666 int update = CE_UPDATE;
667
668 if (old) {
669 /*
670 * See if we can re-use the old CE directly?
671 * That way we get the uptodate stat info.
672 *
673 * This also removes the UPDATE flag on a match; otherwise
674 * we will end up overwriting local changes in the work tree.
675 */
676 if (same(old, merge)) {
677 copy_cache_entry(merge, old);
678 update = 0;
679 } else {
680 if (verify_uptodate(old, o))
681 return -1;
682 invalidate_ce_path(old, o);
683 }
684 }
685 else {
686 if (verify_absent(merge, "overwritten", o))
687 return -1;
688 invalidate_ce_path(merge, o);
689 }
690
691 add_entry(o, merge, update, CE_STAGEMASK);
692 return 1;
693}
694
695static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
696 struct unpack_trees_options *o)
697{
698 /* Did it exist in the index? */
699 if (!old) {
700 if (verify_absent(ce, "removed", o))
701 return -1;
702 return 0;
703 }
704 if (verify_uptodate(old, o))
705 return -1;
706 add_entry(o, ce, CE_REMOVE, 0);
707 invalidate_ce_path(ce, o);
708 return 1;
709}
710
711static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
712{
713 add_entry(o, ce, 0, 0);
714 return 1;
715}
716
717#if DBRT_DEBUG
718static void show_stage_entry(FILE *o,
719 const char *label, const struct cache_entry *ce)
720{
721 if (!ce)
722 fprintf(o, "%s (missing)\n", label);
723 else
724 fprintf(o, "%s%06o %s %d\t%s\n",
725 label,
726 ce->ce_mode,
727 sha1_to_hex(ce->sha1),
728 ce_stage(ce),
729 ce->name);
730}
731#endif
732
733int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
734{
735 struct cache_entry *index;
736 struct cache_entry *head;
737 struct cache_entry *remote = stages[o->head_idx + 1];
738 int count;
739 int head_match = 0;
740 int remote_match = 0;
741
742 int df_conflict_head = 0;
743 int df_conflict_remote = 0;
744
745 int any_anc_missing = 0;
746 int no_anc_exists = 1;
747 int i;
748
749 for (i = 1; i < o->head_idx; i++) {
750 if (!stages[i] || stages[i] == o->df_conflict_entry)
751 any_anc_missing = 1;
752 else
753 no_anc_exists = 0;
754 }
755
756 index = stages[0];
757 head = stages[o->head_idx];
758
759 if (head == o->df_conflict_entry) {
760 df_conflict_head = 1;
761 head = NULL;
762 }
763
764 if (remote == o->df_conflict_entry) {
765 df_conflict_remote = 1;
766 remote = NULL;
767 }
768
769 /* First, if there's a #16 situation, note that to prevent #13
770 * and #14.
771 */
772 if (!same(remote, head)) {
773 for (i = 1; i < o->head_idx; i++) {
774 if (same(stages[i], head)) {
775 head_match = i;
776 }
777 if (same(stages[i], remote)) {
778 remote_match = i;
779 }
780 }
781 }
782
783 /* We start with cases where the index is allowed to match
784 * something other than the head: #14(ALT) and #2ALT, where it
785 * is permitted to match the result instead.
786 */
787 /* #14, #14ALT, #2ALT */
788 if (remote && !df_conflict_head && head_match && !remote_match) {
789 if (index && !same(index, remote) && !same(index, head))
790 return o->gently ? -1 : reject_merge(index, o);
791 return merged_entry(remote, index, o);
792 }
793 /*
794 * If we have an entry in the index cache, then we want to
795 * make sure that it matches head.
796 */
797 if (index && !same(index, head))
798 return o->gently ? -1 : reject_merge(index, o);
799
800 if (head) {
801 /* #5ALT, #15 */
802 if (same(head, remote))
803 return merged_entry(head, index, o);
804 /* #13, #3ALT */
805 if (!df_conflict_remote && remote_match && !head_match)
806 return merged_entry(head, index, o);
807 }
808
809 /* #1 */
810 if (!head && !remote && any_anc_missing)
811 return 0;
812
813 /* Under the new "aggressive" rule, we resolve mostly trivial
814 * cases that we historically had git-merge-one-file resolve.
815 */
816 if (o->aggressive) {
817 int head_deleted = !head && !df_conflict_head;
818 int remote_deleted = !remote && !df_conflict_remote;
819 struct cache_entry *ce = NULL;
820
821 if (index)
822 ce = index;
823 else if (head)
824 ce = head;
825 else if (remote)
826 ce = remote;
827 else {
828 for (i = 1; i < o->head_idx; i++) {
829 if (stages[i] && stages[i] != o->df_conflict_entry) {
830 ce = stages[i];
831 break;
832 }
833 }
834 }
835
836 /*
837 * Deleted in both.
838 * Deleted in one and unchanged in the other.
839 */
840 if ((head_deleted && remote_deleted) ||
841 (head_deleted && remote && remote_match) ||
842 (remote_deleted && head && head_match)) {
843 if (index)
844 return deleted_entry(index, index, o);
845 if (ce && !head_deleted) {
846 if (verify_absent(ce, "removed", o))
847 return -1;
848 }
849 return 0;
850 }
851 /*
852 * Added in both, identically.
853 */
854 if (no_anc_exists && head && remote && same(head, remote))
855 return merged_entry(head, index, o);
856
857 }
858
859 /* Below are "no merge" cases, which require that the index be
860 * up-to-date to avoid the files getting overwritten with
861 * conflict resolution files.
862 */
863 if (index) {
864 if (verify_uptodate(index, o))
865 return -1;
866 }
867
868 o->nontrivial_merge = 1;
869
870 /* #2, #3, #4, #6, #7, #9, #10, #11. */
871 count = 0;
872 if (!head_match || !remote_match) {
873 for (i = 1; i < o->head_idx; i++) {
874 if (stages[i] && stages[i] != o->df_conflict_entry) {
875 keep_entry(stages[i], o);
876 count++;
877 break;
878 }
879 }
880 }
881#if DBRT_DEBUG
882 else {
883 fprintf(stderr, "read-tree: warning #16 detected\n");
884 show_stage_entry(stderr, "head ", stages[head_match]);
885 show_stage_entry(stderr, "remote ", stages[remote_match]);
886 }
887#endif
888 if (head) { count += keep_entry(head, o); }
889 if (remote) { count += keep_entry(remote, o); }
890 return count;
891}
892
893/*
894 * Two-way merge.
895 *
896 * The rule is to "carry forward" what is in the index without losing
897 * information across a "fast forward", favoring a successful merge
898 * over a merge failure when it makes sense. For details of the
899 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
900 *
901 */
902int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
903{
904 struct cache_entry *current = src[0];
905 struct cache_entry *oldtree = src[1];
906 struct cache_entry *newtree = src[2];
907
908 if (o->merge_size != 2)
909 return error("Cannot do a twoway merge of %d trees",
910 o->merge_size);
911
912 if (oldtree == o->df_conflict_entry)
913 oldtree = NULL;
914 if (newtree == o->df_conflict_entry)
915 newtree = NULL;
916
917 if (current) {
918 if ((!oldtree && !newtree) || /* 4 and 5 */
919 (!oldtree && newtree &&
920 same(current, newtree)) || /* 6 and 7 */
921 (oldtree && newtree &&
922 same(oldtree, newtree)) || /* 14 and 15 */
923 (oldtree && newtree &&
924 !same(oldtree, newtree) && /* 18 and 19 */
925 same(current, newtree))) {
926 return keep_entry(current, o);
927 }
928 else if (oldtree && !newtree && same(current, oldtree)) {
929 /* 10 or 11 */
930 return deleted_entry(oldtree, current, o);
931 }
932 else if (oldtree && newtree &&
933 same(current, oldtree) && !same(current, newtree)) {
934 /* 20 or 21 */
935 return merged_entry(newtree, current, o);
936 }
937 else {
938 /* all other failures */
939 if (oldtree)
940 return o->gently ? -1 : reject_merge(oldtree, o);
941 if (current)
942 return o->gently ? -1 : reject_merge(current, o);
943 if (newtree)
944 return o->gently ? -1 : reject_merge(newtree, o);
945 return -1;
946 }
947 }
948 else if (newtree) {
949 if (oldtree && !o->initial_checkout) {
950 /*
951 * deletion of the path was staged;
952 */
953 if (same(oldtree, newtree))
954 return 1;
955 return reject_merge(oldtree, o);
956 }
957 return merged_entry(newtree, current, o);
958 }
959 return deleted_entry(oldtree, current, o);
960}
961
962/*
963 * Bind merge.
964 *
965 * Keep the index entries at stage0, collapse stage1 but make sure
966 * stage0 does not have anything there.
967 */
968int bind_merge(struct cache_entry **src,
969 struct unpack_trees_options *o)
970{
971 struct cache_entry *old = src[0];
972 struct cache_entry *a = src[1];
973
974 if (o->merge_size != 1)
975 return error("Cannot do a bind merge of %d trees\n",
976 o->merge_size);
977 if (a && old)
978 return o->gently ? -1 :
979 error(ERRORMSG(o, bind_overlap), a->name, old->name);
980 if (!a)
981 return keep_entry(old, o);
982 else
983 return merged_entry(a, NULL, o);
984}
985
986/*
987 * One-way merge.
988 *
989 * The rule is:
990 * - take the stat information from stage0, take the data from stage1
991 */
992int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
993{
994 struct cache_entry *old = src[0];
995 struct cache_entry *a = src[1];
996
997 if (o->merge_size != 1)
998 return error("Cannot do a oneway merge of %d trees",
999 o->merge_size);
1000
1001 if (!a)
1002 return deleted_entry(old, old, o);
1003
1004 if (old && same(old, a)) {
1005 int update = 0;
1006 if (o->reset) {
1007 struct stat st;
1008 if (lstat(old->name, &st) ||
1009 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
1010 update |= CE_UPDATE;
1011 }
1012 add_entry(o, old, update, 0);
1013 return 0;
1014 }
1015 return merged_entry(a, old, o);
1016}