Read attributes from the index that is being checked out
[git/spearce.git] / unpack-trees.c
blob661218c5aee90ac5c4933f3409b0858c3cd10ef0
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|ADD_CACHE_SKIP_DFCHECK);
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.
60 static void unlink_entry(struct cache_entry *ce)
62 char *cp, *prev;
63 char *name = ce->name;
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;
78 *cp = 0;
79 status = rmdir(name);
80 if (status) {
81 *cp = '/';
82 break;
84 prev = cp;
88 static struct checkout state;
89 static int check_updates(struct unpack_trees_options *o)
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;
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++;
104 progress = start_progress_delay("Checking out files",
105 total, 50, 1);
106 cnt = 0;
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];
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;
123 for (i = 0; i < index->cache_nr; i++) {
124 struct cache_entry *ce = index->cache[i];
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);
134 stop_progress(&progress);
135 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
136 return errs != 0;
139 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
141 int ret = o->fn(src, o);
142 if (ret > 0)
143 ret = 0;
144 return ret;
147 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
149 struct cache_entry *src[5] = { ce, };
151 o->pos++;
152 if (ce_stage(ce)) {
153 if (o->skip_unmerged) {
154 add_entry(o, ce, 0, 0);
155 return 0;
158 return call_unpack_fn(src, o);
161 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
163 int i;
164 struct tree_desc t[MAX_UNPACK_TREES];
165 struct traverse_info newinfo;
166 struct name_entry *p;
168 p = names;
169 while (!p->mode)
170 p++;
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;
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);
184 return traverse_trees(n, t, &newinfo);
188 * Compare the traverse-path to the cache entry without actually
189 * having to generate the textual representation of the traverse
190 * path.
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!
196 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
198 int len, pathlen, ce_len;
199 const char *ce_name;
201 if (info->prev) {
202 int cmp = do_compare_entry(ce, info->prev, &info->name);
203 if (cmp)
204 return cmp;
206 pathlen = info->pathlen;
207 ce_len = ce_namelen(ce);
209 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
210 if (ce_len < pathlen)
211 return -1;
213 ce_len -= pathlen;
214 ce_name = ce->name + pathlen;
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);
220 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
222 int cmp = do_compare_entry(ce, info, n);
223 if (cmp)
224 return cmp;
227 * Even if the beginning compared identically, the ce should
228 * compare as bigger than a directory leading up to it!
230 return ce_namelen(ce) > traverse_path_len(info, n);
233 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
235 int len = traverse_path_len(info, n);
236 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
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);
243 return ce;
246 static 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)
252 int i;
253 struct unpack_trees_options *o = info->data;
254 unsigned long conflicts;
256 /* Do we have *only* directories? Nothing to do */
257 if (mask == dirmask && !src[0])
258 return 0;
260 conflicts = info->conflicts;
261 if (o->merge)
262 conflicts >>= 1;
263 conflicts |= dirmask;
266 * Ok, we've filled in up to any potential index entry in src[0],
267 * now do the rest.
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;
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);
289 if (o->merge)
290 return call_unpack_fn(src, o);
292 n += o->merge;
293 for (i = 0; i < n; i++)
294 add_entry(o, src[i], 0, 0);
295 return 0;
298 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
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;
304 /* Find first entry with a real name (we could use "mask" too) */
305 while (!p->mode)
306 p++;
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;
318 if (!cmp) {
319 o->pos++;
320 if (ce_stage(ce)) {
322 * If we skip unmerged index entries, we'll skip this
323 * entry *and* the tree entries associated with it!
325 if (o->skip_unmerged) {
326 add_entry(o, ce, 0, 0);
327 return mask;
330 src[0] = ce;
332 break;
336 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
337 return -1;
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;
347 if (traverse_trees_recursive(n, dirmask, conflicts,
348 names, info) < 0)
349 return -1;
350 return mask;
353 return mask;
356 static int unpack_failed(struct unpack_trees_options *o, const char *message)
358 discard_index(&o->result);
359 if (!o->gently) {
360 if (message)
361 return error("%s", message);
362 return -1;
364 return -1;
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.
371 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
373 int ret;
374 static struct cache_entry *dfc;
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;
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;
390 if (!dfc)
391 dfc = xcalloc(1, cache_entry_size(0));
392 o->df_conflict_entry = dfc;
394 if (len) {
395 const char *prefix = o->prefix ? o->prefix : "";
396 struct traverse_info info;
398 setup_traverse_info(&info, prefix);
399 info.fn = unpack_callback;
400 info.data = o;
402 if (traverse_trees(len, t, &info) < 0)
403 return unpack_failed(o, NULL);
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);
415 if (o->trivial_merges_only && o->nontrivial_merge)
416 return unpack_failed(o, "Merge requires file-level merging");
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;
425 /* Here come the merge functions */
427 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
429 return error(ERRORMSG(o, would_overwrite), ce->name);
432 static int same(struct cache_entry *a, struct cache_entry *b)
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);
444 * When a CE gets turned into an unmerged entry, we
445 * want it to be up-to-date
447 static int verify_uptodate(struct cache_entry *ce,
448 struct unpack_trees_options *o)
450 struct stat st;
452 if (o->index_only || o->reset)
453 return 0;
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;
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.
466 if (S_ISGITLINK(ce->ce_mode))
467 return 0;
468 errno = 0;
470 if (errno == ENOENT)
471 return 0;
472 return o->gently ? -1 :
473 error(ERRORMSG(o, not_uptodate_file), ce->name);
476 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
478 if (ce)
479 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
483 * Check that checking out ce->sha1 in subdir ce->name is not
484 * going to overwrite any working files.
486 * Currently, git does not checkout subprojects during a superproject
487 * checkout, so it is not going to overwrite anything.
489 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
490 struct unpack_trees_options *o)
492 return 0;
495 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
496 struct unpack_trees_options *o)
499 * we are about to extract "ce->name"; we would not want to lose
500 * anything in the existing directory there.
502 int namelen;
503 int i;
504 struct dir_struct d;
505 char *pathbuf;
506 int cnt = 0;
507 unsigned char sha1[20];
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.
514 if (!hashcmp(sha1, ce->sha1))
515 return 0;
516 return verify_clean_submodule(ce, action, o);
520 * First let's make sure we do not have a local modification
521 * in that directory.
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;
532 * ce2->name is an entry in the subdirectory.
534 if (!ce_stage(ce2)) {
535 if (verify_uptodate(ce2, o))
536 return -1;
537 add_entry(o, ce2, CE_REMOVE, 0);
539 cnt++;
543 * Then we need to make sure that we do not lose a locally
544 * present file that is not ignored.
546 pathbuf = xmalloc(namelen + 2);
547 memcpy(pathbuf, ce->name, namelen);
548 strcpy(pathbuf+namelen, "/");
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;
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.
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!
569 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
571 struct cache_entry *src;
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);
578 * We do not want to remove or overwrite a working tree file that
579 * is not tracked, unless it is ignored.
581 static int verify_absent(struct cache_entry *ce, const char *action,
582 struct unpack_trees_options *o)
584 struct stat st;
586 if (o->index_only || o->reset || !o->update)
587 return 0;
589 if (has_symlink_or_noent_leading_path(ce_namelen(ce), ce->name))
590 return 0;
592 if (!lstat(ce->name, &st)) {
593 int ret;
594 int dtype = ce_to_dtype(ce);
595 struct cache_entry *result;
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..
602 * Ignore that lstat() if it matches.
604 if (ignore_case && icase_exists(o, ce, &st))
605 return 0;
607 if (o->dir && excluded(o->dir, ce->name, &dtype))
609 * ce->name is explicitly excluded, so it is Ok to
610 * overwrite it.
612 return 0;
613 if (S_ISDIR(st.st_mode)) {
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.
621 ret = verify_clean_subdirectory(ce, action, o);
622 if (ret < 0)
623 return ret;
626 * If this removed entries from the index,
627 * what that means is:
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".
639 * We need to increment it by the number of
640 * deleted entries here.
642 o->pos += ret;
643 return 0;
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.
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;
657 return o->gently ? -1 :
658 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
660 return 0;
663 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
664 struct unpack_trees_options *o)
666 int update = CE_UPDATE;
668 if (old) {
670 * See if we can re-use the old CE directly?
671 * That way we get the uptodate stat info.
673 * This also removes the UPDATE flag on a match; otherwise
674 * we will end up overwriting local changes in the work tree.
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);
685 else {
686 if (verify_absent(merge, "overwritten", o))
687 return -1;
688 invalidate_ce_path(merge, o);
691 add_entry(o, merge, update, CE_STAGEMASK);
692 return 1;
695 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
696 struct unpack_trees_options *o)
698 /* Did it exist in the index? */
699 if (!old) {
700 if (verify_absent(ce, "removed", o))
701 return -1;
702 return 0;
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;
711 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
713 add_entry(o, ce, 0, 0);
714 return 1;
717 #if DBRT_DEBUG
718 static void show_stage_entry(FILE *o,
719 const char *label, const struct cache_entry *ce)
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);
731 #endif
733 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
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;
742 int df_conflict_head = 0;
743 int df_conflict_remote = 0;
745 int any_anc_missing = 0;
746 int no_anc_exists = 1;
747 int i;
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;
756 index = stages[0];
757 head = stages[o->head_idx];
759 if (head == o->df_conflict_entry) {
760 df_conflict_head = 1;
761 head = NULL;
764 if (remote == o->df_conflict_entry) {
765 df_conflict_remote = 1;
766 remote = NULL;
769 /* First, if there's a #16 situation, note that to prevent #13
770 * and #14.
772 if (!same(remote, head)) {
773 for (i = 1; i < o->head_idx; i++) {
774 if (same(stages[i], head)) {
775 head_match = i;
777 if (same(stages[i], remote)) {
778 remote_match = i;
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.
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);
794 * If we have an entry in the index cache, then we want to
795 * make sure that it matches head.
797 if (index && !same(index, head))
798 return o->gently ? -1 : reject_merge(index, o);
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);
809 /* #1 */
810 if (!head && !remote && any_anc_missing)
811 return 0;
813 /* Under the new "aggressive" rule, we resolve mostly trivial
814 * cases that we historically had git-merge-one-file resolve.
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;
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;
837 * Deleted in both.
838 * Deleted in one and unchanged in the other.
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;
849 return 0;
852 * Added in both, identically.
854 if (no_anc_exists && head && remote && same(head, remote))
855 return merged_entry(head, index, o);
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.
863 if (index) {
864 if (verify_uptodate(index, o))
865 return -1;
868 o->nontrivial_merge = 1;
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;
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]);
887 #endif
888 if (head) { count += keep_entry(head, o); }
889 if (remote) { count += keep_entry(remote, o); }
890 return count;
894 * Two-way merge.
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>.
902 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
904 struct cache_entry *current = src[0];
905 struct cache_entry *oldtree = src[1];
906 struct cache_entry *newtree = src[2];
908 if (o->merge_size != 2)
909 return error("Cannot do a twoway merge of %d trees",
910 o->merge_size);
912 if (oldtree == o->df_conflict_entry)
913 oldtree = NULL;
914 if (newtree == o->df_conflict_entry)
915 newtree = NULL;
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);
928 else if (oldtree && !newtree && same(current, oldtree)) {
929 /* 10 or 11 */
930 return deleted_entry(oldtree, current, o);
932 else if (oldtree && newtree &&
933 same(current, oldtree) && !same(current, newtree)) {
934 /* 20 or 21 */
935 return merged_entry(newtree, current, o);
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;
948 else if (newtree) {
949 if (oldtree && !o->initial_checkout) {
951 * deletion of the path was staged;
953 if (same(oldtree, newtree))
954 return 1;
955 return reject_merge(oldtree, o);
957 return merged_entry(newtree, current, o);
959 return deleted_entry(oldtree, current, o);
963 * Bind merge.
965 * Keep the index entries at stage0, collapse stage1 but make sure
966 * stage0 does not have anything there.
968 int bind_merge(struct cache_entry **src,
969 struct unpack_trees_options *o)
971 struct cache_entry *old = src[0];
972 struct cache_entry *a = src[1];
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);
987 * One-way merge.
989 * The rule is:
990 * - take the stat information from stage0, take the data from stage1
992 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
994 struct cache_entry *old = src[0];
995 struct cache_entry *a = src[1];
997 if (o->merge_size != 1)
998 return error("Cannot do a oneway merge of %d trees",
999 o->merge_size);
1001 if (!a)
1002 return deleted_entry(old, old, o);
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;
1012 add_entry(o, old, update, 0);
1013 return 0;
1015 return merged_entry(a, old, o);