Fix bit assignment for CE_CONFLICTED
[git.git] / unpack-trees.c
blob3df0de60054a2cd82b1284f5c223ff8c44a0768b
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"
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.
20 static struct unpack_trees_error_msgs unpack_plumbing_errors = {
21 /* would_overwrite */
22 "Entry '%s' would be overwritten by merge. Cannot merge.",
24 /* not_uptodate_file */
25 "Entry '%s' not uptodate. Cannot merge.",
27 /* not_uptodate_dir */
28 "Updating '%s' would lose untracked files in it",
30 /* would_lose_untracked */
31 "Untracked working tree file '%s' would be %s by merge.",
33 /* bind_overlap */
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
37 #define ERRORMSG(o,fld) \
38 ( ((o) && (o)->msgs.fld) \
39 ? ((o)->msgs.fld) \
40 : (unpack_plumbing_errors.fld) )
42 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
43 unsigned int set, unsigned int clear)
45 unsigned int size = ce_size(ce);
46 struct cache_entry *new = xmalloc(size);
48 clear |= CE_HASHED | CE_UNHASHED;
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);
57 * Unlink the last component and schedule the leading directories for
58 * removal, such that empty directories get removed.
60 static void unlink_entry(struct cache_entry *ce)
62 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
63 return;
64 if (unlink_or_warn(ce->name))
65 return;
66 schedule_dir_for_removal(ce->name, ce_namelen(ce));
69 static struct checkout state;
70 static int check_updates(struct unpack_trees_options *o)
72 unsigned cnt = 0, total = 0;
73 struct progress *progress = NULL;
74 struct index_state *index = &o->result;
75 int i;
76 int errs = 0;
78 if (o->update && o->verbose_update) {
79 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
80 struct cache_entry *ce = index->cache[cnt];
81 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
82 total++;
85 progress = start_progress_delay("Checking out files",
86 total, 50, 1);
87 cnt = 0;
90 if (o->update)
91 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
92 for (i = 0; i < index->cache_nr; i++) {
93 struct cache_entry *ce = index->cache[i];
95 if (ce->ce_flags & CE_REMOVE) {
96 display_progress(progress, ++cnt);
97 if (o->update)
98 unlink_entry(ce);
101 remove_marked_cache_entries(&o->result);
102 remove_scheduled_dirs();
104 for (i = 0; i < index->cache_nr; i++) {
105 struct cache_entry *ce = index->cache[i];
107 if (ce->ce_flags & CE_UPDATE) {
108 display_progress(progress, ++cnt);
109 ce->ce_flags &= ~CE_UPDATE;
110 if (o->update) {
111 errs |= checkout_entry(ce, &state, NULL);
115 stop_progress(&progress);
116 if (o->update)
117 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
118 return errs != 0;
121 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
123 int ret = o->fn(src, o);
124 if (ret > 0)
125 ret = 0;
126 return ret;
129 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
131 struct cache_entry *src[5] = { ce, NULL, };
133 o->pos++;
134 if (ce_stage(ce)) {
135 if (o->skip_unmerged) {
136 add_entry(o, ce, 0, 0);
137 return 0;
140 return call_unpack_fn(src, o);
143 static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
145 int i;
146 struct tree_desc t[MAX_UNPACK_TREES];
147 struct traverse_info newinfo;
148 struct name_entry *p;
150 p = names;
151 while (!p->mode)
152 p++;
154 newinfo = *info;
155 newinfo.prev = info;
156 newinfo.name = *p;
157 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
158 newinfo.conflicts |= df_conflicts;
160 for (i = 0; i < n; i++, dirmask >>= 1) {
161 const unsigned char *sha1 = NULL;
162 if (dirmask & 1)
163 sha1 = names[i].sha1;
164 fill_tree_descriptor(t+i, sha1);
166 return traverse_trees(n, t, &newinfo);
170 * Compare the traverse-path to the cache entry without actually
171 * having to generate the textual representation of the traverse
172 * path.
174 * NOTE! This *only* compares up to the size of the traverse path
175 * itself - the caller needs to do the final check for the cache
176 * entry having more data at the end!
178 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
180 int len, pathlen, ce_len;
181 const char *ce_name;
183 if (info->prev) {
184 int cmp = do_compare_entry(ce, info->prev, &info->name);
185 if (cmp)
186 return cmp;
188 pathlen = info->pathlen;
189 ce_len = ce_namelen(ce);
191 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
192 if (ce_len < pathlen)
193 return -1;
195 ce_len -= pathlen;
196 ce_name = ce->name + pathlen;
198 len = tree_entry_len(n->path, n->sha1);
199 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
202 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
204 int cmp = do_compare_entry(ce, info, n);
205 if (cmp)
206 return cmp;
209 * Even if the beginning compared identically, the ce should
210 * compare as bigger than a directory leading up to it!
212 return ce_namelen(ce) > traverse_path_len(info, n);
215 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
217 int len = traverse_path_len(info, n);
218 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
220 ce->ce_mode = create_ce_mode(n->mode);
221 ce->ce_flags = create_ce_flags(len, stage);
222 hashcpy(ce->sha1, n->sha1);
223 make_traverse_path(ce->name, info, n);
225 return ce;
228 static int unpack_nondirectories(int n, unsigned long mask,
229 unsigned long dirmask,
230 struct cache_entry **src,
231 const struct name_entry *names,
232 const struct traverse_info *info)
234 int i;
235 struct unpack_trees_options *o = info->data;
236 unsigned long conflicts;
238 /* Do we have *only* directories? Nothing to do */
239 if (mask == dirmask && !src[0])
240 return 0;
242 conflicts = info->conflicts;
243 if (o->merge)
244 conflicts >>= 1;
245 conflicts |= dirmask;
248 * Ok, we've filled in up to any potential index entry in src[0],
249 * now do the rest.
251 for (i = 0; i < n; i++) {
252 int stage;
253 unsigned int bit = 1ul << i;
254 if (conflicts & bit) {
255 src[i + o->merge] = o->df_conflict_entry;
256 continue;
258 if (!(mask & bit))
259 continue;
260 if (!o->merge)
261 stage = 0;
262 else if (i + 1 < o->head_idx)
263 stage = 1;
264 else if (i + 1 > o->head_idx)
265 stage = 3;
266 else
267 stage = 2;
268 src[i + o->merge] = create_ce_entry(info, names + i, stage);
271 if (o->merge)
272 return call_unpack_fn(src, o);
274 for (i = 0; i < n; i++)
275 if (src[i] && src[i] != o->df_conflict_entry)
276 add_entry(o, src[i], 0, 0);
277 return 0;
280 static int unpack_failed(struct unpack_trees_options *o, const char *message)
282 discard_index(&o->result);
283 if (!o->gently) {
284 if (message)
285 return error("%s", message);
286 return -1;
288 return -1;
291 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
293 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
294 struct unpack_trees_options *o = info->data;
295 const struct name_entry *p = names;
297 /* Find first entry with a real name (we could use "mask" too) */
298 while (!p->mode)
299 p++;
301 /* Are we supposed to look at the index too? */
302 if (o->merge) {
303 while (o->pos < o->src_index->cache_nr) {
304 struct cache_entry *ce = o->src_index->cache[o->pos];
305 int cmp = compare_entry(ce, info, p);
306 if (cmp < 0) {
307 if (unpack_index_entry(ce, o) < 0)
308 return unpack_failed(o, NULL);
309 continue;
311 if (!cmp) {
312 o->pos++;
313 if (ce_stage(ce)) {
315 * If we skip unmerged index entries, we'll skip this
316 * entry *and* the tree entries associated with it!
318 if (o->skip_unmerged) {
319 add_entry(o, ce, 0, 0);
320 return mask;
323 src[0] = ce;
325 break;
329 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
330 return -1;
332 /* Now handle any directories.. */
333 if (dirmask) {
334 unsigned long conflicts = mask & ~dirmask;
335 if (o->merge) {
336 conflicts <<= 1;
337 if (src[0])
338 conflicts |= 1;
341 /* special case: "diff-index --cached" looking at a tree */
342 if (o->diff_index_cached &&
343 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
344 int matches;
345 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
346 names, info);
348 * Everything under the name matches. Adjust o->pos to
349 * skip the entire hierarchy.
351 if (matches) {
352 o->pos += matches;
353 return mask;
357 if (traverse_trees_recursive(n, dirmask, conflicts,
358 names, info) < 0)
359 return -1;
360 return mask;
363 return mask;
367 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
368 * resulting index, -2 on failure to reflect the changes to the work tree.
370 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
372 int ret;
373 static struct cache_entry *dfc;
375 if (len > MAX_UNPACK_TREES)
376 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
377 memset(&state, 0, sizeof(state));
378 state.base_dir = "";
379 state.force = 1;
380 state.quiet = 1;
381 state.refresh_cache = 1;
383 memset(&o->result, 0, sizeof(o->result));
384 o->result.initialized = 1;
385 if (o->src_index) {
386 o->result.timestamp.sec = o->src_index->timestamp.sec;
387 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
389 o->merge_size = len;
391 if (!dfc)
392 dfc = xcalloc(1, cache_entry_size(0));
393 o->df_conflict_entry = dfc;
395 if (len) {
396 const char *prefix = o->prefix ? o->prefix : "";
397 struct traverse_info info;
399 setup_traverse_info(&info, prefix);
400 info.fn = unpack_callback;
401 info.data = o;
403 if (traverse_trees(len, t, &info) < 0)
404 return unpack_failed(o, NULL);
407 /* Any left-over entries in the index? */
408 if (o->merge) {
409 while (o->pos < o->src_index->cache_nr) {
410 struct cache_entry *ce = o->src_index->cache[o->pos];
411 if (unpack_index_entry(ce, o) < 0)
412 return unpack_failed(o, NULL);
416 if (o->trivial_merges_only && o->nontrivial_merge)
417 return unpack_failed(o, "Merge requires file-level merging");
419 o->src_index = NULL;
420 ret = check_updates(o) ? (-2) : 0;
421 if (o->dst_index)
422 *o->dst_index = o->result;
423 return ret;
426 /* Here come the merge functions */
428 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
430 return error(ERRORMSG(o, would_overwrite), ce->name);
433 static int same(struct cache_entry *a, struct cache_entry *b)
435 if (!!a != !!b)
436 return 0;
437 if (!a && !b)
438 return 1;
439 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
440 return 0;
441 return a->ce_mode == b->ce_mode &&
442 !hashcmp(a->sha1, b->sha1);
447 * When a CE gets turned into an unmerged entry, we
448 * want it to be up-to-date
450 static int verify_uptodate(struct cache_entry *ce,
451 struct unpack_trees_options *o)
453 struct stat st;
455 if (o->index_only || o->reset || ce_uptodate(ce))
456 return 0;
458 if (!lstat(ce->name, &st)) {
459 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
460 if (!changed)
461 return 0;
463 * NEEDSWORK: the current default policy is to allow
464 * submodule to be out of sync wrt the supermodule
465 * index. This needs to be tightened later for
466 * submodules that are marked to be automatically
467 * checked out.
469 if (S_ISGITLINK(ce->ce_mode))
470 return 0;
471 errno = 0;
473 if (errno == ENOENT)
474 return 0;
475 return o->gently ? -1 :
476 error(ERRORMSG(o, not_uptodate_file), ce->name);
479 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
481 if (ce)
482 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
486 * Check that checking out ce->sha1 in subdir ce->name is not
487 * going to overwrite any working files.
489 * Currently, git does not checkout subprojects during a superproject
490 * checkout, so it is not going to overwrite anything.
492 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
493 struct unpack_trees_options *o)
495 return 0;
498 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
499 struct unpack_trees_options *o)
502 * we are about to extract "ce->name"; we would not want to lose
503 * anything in the existing directory there.
505 int namelen;
506 int i;
507 struct dir_struct d;
508 char *pathbuf;
509 int cnt = 0;
510 unsigned char sha1[20];
512 if (S_ISGITLINK(ce->ce_mode) &&
513 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
514 /* If we are not going to update the submodule, then
515 * we don't care.
517 if (!hashcmp(sha1, ce->sha1))
518 return 0;
519 return verify_clean_submodule(ce, action, o);
523 * First let's make sure we do not have a local modification
524 * in that directory.
526 namelen = strlen(ce->name);
527 for (i = o->pos; i < o->src_index->cache_nr; i++) {
528 struct cache_entry *ce2 = o->src_index->cache[i];
529 int len = ce_namelen(ce2);
530 if (len < namelen ||
531 strncmp(ce->name, ce2->name, namelen) ||
532 ce2->name[namelen] != '/')
533 break;
535 * ce2->name is an entry in the subdirectory.
537 if (!ce_stage(ce2)) {
538 if (verify_uptodate(ce2, o))
539 return -1;
540 add_entry(o, ce2, CE_REMOVE, 0);
542 cnt++;
546 * Then we need to make sure that we do not lose a locally
547 * present file that is not ignored.
549 pathbuf = xmalloc(namelen + 2);
550 memcpy(pathbuf, ce->name, namelen);
551 strcpy(pathbuf+namelen, "/");
553 memset(&d, 0, sizeof(d));
554 if (o->dir)
555 d.exclude_per_dir = o->dir->exclude_per_dir;
556 i = read_directory(&d, pathbuf, namelen+1, NULL);
557 if (i)
558 return o->gently ? -1 :
559 error(ERRORMSG(o, not_uptodate_dir), ce->name);
560 free(pathbuf);
561 return cnt;
565 * This gets called when there was no index entry for the tree entry 'dst',
566 * but we found a file in the working tree that 'lstat()' said was fine,
567 * and we're on a case-insensitive filesystem.
569 * See if we can find a case-insensitive match in the index that also
570 * matches the stat information, and assume it's that other file!
572 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
574 struct cache_entry *src;
576 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
577 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
581 * We do not want to remove or overwrite a working tree file that
582 * is not tracked, unless it is ignored.
584 static int verify_absent(struct cache_entry *ce, const char *action,
585 struct unpack_trees_options *o)
587 struct stat st;
589 if (o->index_only || o->reset || !o->update)
590 return 0;
592 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
593 return 0;
595 if (!lstat(ce->name, &st)) {
596 int ret;
597 int dtype = ce_to_dtype(ce);
598 struct cache_entry *result;
601 * It may be that the 'lstat()' succeeded even though
602 * target 'ce' was absent, because there is an old
603 * entry that is different only in case..
605 * Ignore that lstat() if it matches.
607 if (ignore_case && icase_exists(o, ce, &st))
608 return 0;
610 if (o->dir && excluded(o->dir, ce->name, &dtype))
612 * ce->name is explicitly excluded, so it is Ok to
613 * overwrite it.
615 return 0;
616 if (S_ISDIR(st.st_mode)) {
618 * We are checking out path "foo" and
619 * found "foo/." in the working tree.
620 * This is tricky -- if we have modified
621 * files that are in "foo/" we would lose
622 * them.
624 ret = verify_clean_subdirectory(ce, action, o);
625 if (ret < 0)
626 return ret;
629 * If this removed entries from the index,
630 * what that means is:
632 * (1) the caller unpack_callback() saw path/foo
633 * in the index, and it has not removed it because
634 * it thinks it is handling 'path' as blob with
635 * D/F conflict;
636 * (2) we will return "ok, we placed a merged entry
637 * in the index" which would cause o->pos to be
638 * incremented by one;
639 * (3) however, original o->pos now has 'path/foo'
640 * marked with "to be removed".
642 * We need to increment it by the number of
643 * deleted entries here.
645 o->pos += ret;
646 return 0;
650 * The previous round may already have decided to
651 * delete this path, which is in a subdirectory that
652 * is being replaced with a blob.
654 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
655 if (result) {
656 if (result->ce_flags & CE_REMOVE)
657 return 0;
660 return o->gently ? -1 :
661 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
663 return 0;
666 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
667 struct unpack_trees_options *o)
669 int update = CE_UPDATE;
671 if (!old) {
672 if (verify_absent(merge, "overwritten", o))
673 return -1;
674 invalidate_ce_path(merge, o);
675 } else if (!(old->ce_flags & CE_CONFLICTED)) {
677 * See if we can re-use the old CE directly?
678 * That way we get the uptodate stat info.
680 * This also removes the UPDATE flag on a match; otherwise
681 * we will end up overwriting local changes in the work tree.
683 if (same(old, merge)) {
684 copy_cache_entry(merge, old);
685 update = 0;
686 } else {
687 if (verify_uptodate(old, o))
688 return -1;
689 invalidate_ce_path(old, o);
691 } else {
693 * Previously unmerged entry left as an existence
694 * marker by read_index_unmerged();
696 invalidate_ce_path(old, o);
699 add_entry(o, merge, update, CE_STAGEMASK);
700 return 1;
703 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
704 struct unpack_trees_options *o)
706 /* Did it exist in the index? */
707 if (!old) {
708 if (verify_absent(ce, "removed", o))
709 return -1;
710 return 0;
712 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
713 return -1;
714 add_entry(o, ce, CE_REMOVE, 0);
715 invalidate_ce_path(ce, o);
716 return 1;
719 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
721 add_entry(o, ce, 0, 0);
722 return 1;
725 #if DBRT_DEBUG
726 static void show_stage_entry(FILE *o,
727 const char *label, const struct cache_entry *ce)
729 if (!ce)
730 fprintf(o, "%s (missing)\n", label);
731 else
732 fprintf(o, "%s%06o %s %d\t%s\n",
733 label,
734 ce->ce_mode,
735 sha1_to_hex(ce->sha1),
736 ce_stage(ce),
737 ce->name);
739 #endif
741 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
743 struct cache_entry *index;
744 struct cache_entry *head;
745 struct cache_entry *remote = stages[o->head_idx + 1];
746 int count;
747 int head_match = 0;
748 int remote_match = 0;
750 int df_conflict_head = 0;
751 int df_conflict_remote = 0;
753 int any_anc_missing = 0;
754 int no_anc_exists = 1;
755 int i;
757 for (i = 1; i < o->head_idx; i++) {
758 if (!stages[i] || stages[i] == o->df_conflict_entry)
759 any_anc_missing = 1;
760 else
761 no_anc_exists = 0;
764 index = stages[0];
765 head = stages[o->head_idx];
767 if (head == o->df_conflict_entry) {
768 df_conflict_head = 1;
769 head = NULL;
772 if (remote == o->df_conflict_entry) {
773 df_conflict_remote = 1;
774 remote = NULL;
777 /* First, if there's a #16 situation, note that to prevent #13
778 * and #14.
780 if (!same(remote, head)) {
781 for (i = 1; i < o->head_idx; i++) {
782 if (same(stages[i], head)) {
783 head_match = i;
785 if (same(stages[i], remote)) {
786 remote_match = i;
791 /* We start with cases where the index is allowed to match
792 * something other than the head: #14(ALT) and #2ALT, where it
793 * is permitted to match the result instead.
795 /* #14, #14ALT, #2ALT */
796 if (remote && !df_conflict_head && head_match && !remote_match) {
797 if (index && !same(index, remote) && !same(index, head))
798 return o->gently ? -1 : reject_merge(index, o);
799 return merged_entry(remote, index, o);
802 * If we have an entry in the index cache, then we want to
803 * make sure that it matches head.
805 if (index && !same(index, head))
806 return o->gently ? -1 : reject_merge(index, o);
808 if (head) {
809 /* #5ALT, #15 */
810 if (same(head, remote))
811 return merged_entry(head, index, o);
812 /* #13, #3ALT */
813 if (!df_conflict_remote && remote_match && !head_match)
814 return merged_entry(head, index, o);
817 /* #1 */
818 if (!head && !remote && any_anc_missing)
819 return 0;
821 /* Under the new "aggressive" rule, we resolve mostly trivial
822 * cases that we historically had git-merge-one-file resolve.
824 if (o->aggressive) {
825 int head_deleted = !head && !df_conflict_head;
826 int remote_deleted = !remote && !df_conflict_remote;
827 struct cache_entry *ce = NULL;
829 if (index)
830 ce = index;
831 else if (head)
832 ce = head;
833 else if (remote)
834 ce = remote;
835 else {
836 for (i = 1; i < o->head_idx; i++) {
837 if (stages[i] && stages[i] != o->df_conflict_entry) {
838 ce = stages[i];
839 break;
845 * Deleted in both.
846 * Deleted in one and unchanged in the other.
848 if ((head_deleted && remote_deleted) ||
849 (head_deleted && remote && remote_match) ||
850 (remote_deleted && head && head_match)) {
851 if (index)
852 return deleted_entry(index, index, o);
853 if (ce && !head_deleted) {
854 if (verify_absent(ce, "removed", o))
855 return -1;
857 return 0;
860 * Added in both, identically.
862 if (no_anc_exists && head && remote && same(head, remote))
863 return merged_entry(head, index, o);
867 /* Below are "no merge" cases, which require that the index be
868 * up-to-date to avoid the files getting overwritten with
869 * conflict resolution files.
871 if (index) {
872 if (verify_uptodate(index, o))
873 return -1;
876 o->nontrivial_merge = 1;
878 /* #2, #3, #4, #6, #7, #9, #10, #11. */
879 count = 0;
880 if (!head_match || !remote_match) {
881 for (i = 1; i < o->head_idx; i++) {
882 if (stages[i] && stages[i] != o->df_conflict_entry) {
883 keep_entry(stages[i], o);
884 count++;
885 break;
889 #if DBRT_DEBUG
890 else {
891 fprintf(stderr, "read-tree: warning #16 detected\n");
892 show_stage_entry(stderr, "head ", stages[head_match]);
893 show_stage_entry(stderr, "remote ", stages[remote_match]);
895 #endif
896 if (head) { count += keep_entry(head, o); }
897 if (remote) { count += keep_entry(remote, o); }
898 return count;
902 * Two-way merge.
904 * The rule is to "carry forward" what is in the index without losing
905 * information across a "fast-forward", favoring a successful merge
906 * over a merge failure when it makes sense. For details of the
907 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
910 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
912 struct cache_entry *current = src[0];
913 struct cache_entry *oldtree = src[1];
914 struct cache_entry *newtree = src[2];
916 if (o->merge_size != 2)
917 return error("Cannot do a twoway merge of %d trees",
918 o->merge_size);
920 if (oldtree == o->df_conflict_entry)
921 oldtree = NULL;
922 if (newtree == o->df_conflict_entry)
923 newtree = NULL;
925 if (current) {
926 if ((!oldtree && !newtree) || /* 4 and 5 */
927 (!oldtree && newtree &&
928 same(current, newtree)) || /* 6 and 7 */
929 (oldtree && newtree &&
930 same(oldtree, newtree)) || /* 14 and 15 */
931 (oldtree && newtree &&
932 !same(oldtree, newtree) && /* 18 and 19 */
933 same(current, newtree))) {
934 return keep_entry(current, o);
936 else if (oldtree && !newtree && same(current, oldtree)) {
937 /* 10 or 11 */
938 return deleted_entry(oldtree, current, o);
940 else if (oldtree && newtree &&
941 same(current, oldtree) && !same(current, newtree)) {
942 /* 20 or 21 */
943 return merged_entry(newtree, current, o);
945 else {
946 /* all other failures */
947 if (oldtree)
948 return o->gently ? -1 : reject_merge(oldtree, o);
949 if (current)
950 return o->gently ? -1 : reject_merge(current, o);
951 if (newtree)
952 return o->gently ? -1 : reject_merge(newtree, o);
953 return -1;
956 else if (newtree) {
957 if (oldtree && !o->initial_checkout) {
959 * deletion of the path was staged;
961 if (same(oldtree, newtree))
962 return 1;
963 return reject_merge(oldtree, o);
965 return merged_entry(newtree, current, o);
967 return deleted_entry(oldtree, current, o);
971 * Bind merge.
973 * Keep the index entries at stage0, collapse stage1 but make sure
974 * stage0 does not have anything there.
976 int bind_merge(struct cache_entry **src,
977 struct unpack_trees_options *o)
979 struct cache_entry *old = src[0];
980 struct cache_entry *a = src[1];
982 if (o->merge_size != 1)
983 return error("Cannot do a bind merge of %d trees\n",
984 o->merge_size);
985 if (a && old)
986 return o->gently ? -1 :
987 error(ERRORMSG(o, bind_overlap), a->name, old->name);
988 if (!a)
989 return keep_entry(old, o);
990 else
991 return merged_entry(a, NULL, o);
995 * One-way merge.
997 * The rule is:
998 * - take the stat information from stage0, take the data from stage1
1000 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1002 struct cache_entry *old = src[0];
1003 struct cache_entry *a = src[1];
1005 if (o->merge_size != 1)
1006 return error("Cannot do a oneway merge of %d trees",
1007 o->merge_size);
1009 if (!a || a == o->df_conflict_entry)
1010 return deleted_entry(old, old, o);
1012 if (old && same(old, a)) {
1013 int update = 0;
1014 if (o->reset && !ce_uptodate(old)) {
1015 struct stat st;
1016 if (lstat(old->name, &st) ||
1017 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
1018 update |= CE_UPDATE;
1020 add_entry(o, old, update, 0);
1021 return 0;
1023 return merged_entry(a, old, o);