unpack-trees(): "enable" sparse checkout and load $GIT_DIR/info/sparse-checkout
[git/gitweb.git] / unpack-trees.c
blob44f8fdf808249268490f5791f9137838a6a39a3d
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 | CE_WT_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_WT_REMOVE) {
96 display_progress(progress, ++cnt);
97 if (o->update)
98 unlink_entry(ce);
99 continue;
102 if (ce->ce_flags & CE_REMOVE) {
103 display_progress(progress, ++cnt);
104 if (o->update)
105 unlink_entry(ce);
108 remove_marked_cache_entries(&o->result);
109 remove_scheduled_dirs();
111 for (i = 0; i < index->cache_nr; i++) {
112 struct cache_entry *ce = index->cache[i];
114 if (ce->ce_flags & CE_UPDATE) {
115 display_progress(progress, ++cnt);
116 ce->ce_flags &= ~CE_UPDATE;
117 if (o->update) {
118 errs |= checkout_entry(ce, &state, NULL);
122 stop_progress(&progress);
123 if (o->update)
124 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
125 return errs != 0;
128 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
130 int ret = o->fn(src, o);
131 if (ret > 0)
132 ret = 0;
133 return ret;
136 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
138 struct cache_entry *src[5] = { ce, NULL, };
140 o->pos++;
141 if (ce_stage(ce)) {
142 if (o->skip_unmerged) {
143 add_entry(o, ce, 0, 0);
144 return 0;
147 return call_unpack_fn(src, o);
150 static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
152 int i;
153 struct tree_desc t[MAX_UNPACK_TREES];
154 struct traverse_info newinfo;
155 struct name_entry *p;
157 p = names;
158 while (!p->mode)
159 p++;
161 newinfo = *info;
162 newinfo.prev = info;
163 newinfo.name = *p;
164 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
165 newinfo.conflicts |= df_conflicts;
167 for (i = 0; i < n; i++, dirmask >>= 1) {
168 const unsigned char *sha1 = NULL;
169 if (dirmask & 1)
170 sha1 = names[i].sha1;
171 fill_tree_descriptor(t+i, sha1);
173 return traverse_trees(n, t, &newinfo);
177 * Compare the traverse-path to the cache entry without actually
178 * having to generate the textual representation of the traverse
179 * path.
181 * NOTE! This *only* compares up to the size of the traverse path
182 * itself - the caller needs to do the final check for the cache
183 * entry having more data at the end!
185 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
187 int len, pathlen, ce_len;
188 const char *ce_name;
190 if (info->prev) {
191 int cmp = do_compare_entry(ce, info->prev, &info->name);
192 if (cmp)
193 return cmp;
195 pathlen = info->pathlen;
196 ce_len = ce_namelen(ce);
198 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
199 if (ce_len < pathlen)
200 return -1;
202 ce_len -= pathlen;
203 ce_name = ce->name + pathlen;
205 len = tree_entry_len(n->path, n->sha1);
206 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
209 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
211 int cmp = do_compare_entry(ce, info, n);
212 if (cmp)
213 return cmp;
216 * Even if the beginning compared identically, the ce should
217 * compare as bigger than a directory leading up to it!
219 return ce_namelen(ce) > traverse_path_len(info, n);
222 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
224 int len = traverse_path_len(info, n);
225 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
227 ce->ce_mode = create_ce_mode(n->mode);
228 ce->ce_flags = create_ce_flags(len, stage);
229 hashcpy(ce->sha1, n->sha1);
230 make_traverse_path(ce->name, info, n);
232 return ce;
235 static int unpack_nondirectories(int n, unsigned long mask,
236 unsigned long dirmask,
237 struct cache_entry **src,
238 const struct name_entry *names,
239 const struct traverse_info *info)
241 int i;
242 struct unpack_trees_options *o = info->data;
243 unsigned long conflicts;
245 /* Do we have *only* directories? Nothing to do */
246 if (mask == dirmask && !src[0])
247 return 0;
249 conflicts = info->conflicts;
250 if (o->merge)
251 conflicts >>= 1;
252 conflicts |= dirmask;
255 * Ok, we've filled in up to any potential index entry in src[0],
256 * now do the rest.
258 for (i = 0; i < n; i++) {
259 int stage;
260 unsigned int bit = 1ul << i;
261 if (conflicts & bit) {
262 src[i + o->merge] = o->df_conflict_entry;
263 continue;
265 if (!(mask & bit))
266 continue;
267 if (!o->merge)
268 stage = 0;
269 else if (i + 1 < o->head_idx)
270 stage = 1;
271 else if (i + 1 > o->head_idx)
272 stage = 3;
273 else
274 stage = 2;
275 src[i + o->merge] = create_ce_entry(info, names + i, stage);
278 if (o->merge)
279 return call_unpack_fn(src, o);
281 for (i = 0; i < n; i++)
282 if (src[i] && src[i] != o->df_conflict_entry)
283 add_entry(o, src[i], 0, 0);
284 return 0;
287 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
289 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
290 struct unpack_trees_options *o = info->data;
291 const struct name_entry *p = names;
293 /* Find first entry with a real name (we could use "mask" too) */
294 while (!p->mode)
295 p++;
297 /* Are we supposed to look at the index too? */
298 if (o->merge) {
299 while (o->pos < o->src_index->cache_nr) {
300 struct cache_entry *ce = o->src_index->cache[o->pos];
301 int cmp = compare_entry(ce, info, p);
302 if (cmp < 0) {
303 if (unpack_index_entry(ce, o) < 0)
304 return -1;
305 continue;
307 if (!cmp) {
308 o->pos++;
309 if (ce_stage(ce)) {
311 * If we skip unmerged index entries, we'll skip this
312 * entry *and* the tree entries associated with it!
314 if (o->skip_unmerged) {
315 add_entry(o, ce, 0, 0);
316 return mask;
319 src[0] = ce;
321 break;
325 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
326 return -1;
328 /* Now handle any directories.. */
329 if (dirmask) {
330 unsigned long conflicts = mask & ~dirmask;
331 if (o->merge) {
332 conflicts <<= 1;
333 if (src[0])
334 conflicts |= 1;
337 /* special case: "diff-index --cached" looking at a tree */
338 if (o->diff_index_cached &&
339 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
340 int matches;
341 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
342 names, info);
344 * Everything under the name matches. Adjust o->pos to
345 * skip the entire hierarchy.
347 if (matches) {
348 o->pos += matches;
349 return mask;
353 if (traverse_trees_recursive(n, dirmask, conflicts,
354 names, info) < 0)
355 return -1;
356 return mask;
359 return mask;
362 static int unpack_failed(struct unpack_trees_options *o, const char *message)
364 discard_index(&o->result);
365 if (!o->gently) {
366 if (message)
367 return error("%s", message);
368 return -1;
370 return -1;
374 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
375 * resulting index, -2 on failure to reflect the changes to the work tree.
377 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
379 int ret;
380 static struct cache_entry *dfc;
381 struct exclude_list el;
383 if (len > MAX_UNPACK_TREES)
384 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
385 memset(&state, 0, sizeof(state));
386 state.base_dir = "";
387 state.force = 1;
388 state.quiet = 1;
389 state.refresh_cache = 1;
391 memset(&el, 0, sizeof(el));
392 if (!core_apply_sparse_checkout || !o->update)
393 o->skip_sparse_checkout = 1;
394 if (!o->skip_sparse_checkout) {
395 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0)
396 o->skip_sparse_checkout = 1;
397 else
398 o->el = &el;
401 memset(&o->result, 0, sizeof(o->result));
402 o->result.initialized = 1;
403 if (o->src_index) {
404 o->result.timestamp.sec = o->src_index->timestamp.sec;
405 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
407 o->merge_size = len;
409 if (!dfc)
410 dfc = xcalloc(1, cache_entry_size(0));
411 o->df_conflict_entry = dfc;
413 if (len) {
414 const char *prefix = o->prefix ? o->prefix : "";
415 struct traverse_info info;
417 setup_traverse_info(&info, prefix);
418 info.fn = unpack_callback;
419 info.data = o;
421 if (traverse_trees(len, t, &info) < 0) {
422 ret = unpack_failed(o, NULL);
423 goto done;
427 /* Any left-over entries in the index? */
428 if (o->merge) {
429 while (o->pos < o->src_index->cache_nr) {
430 struct cache_entry *ce = o->src_index->cache[o->pos];
431 if (unpack_index_entry(ce, o) < 0) {
432 ret = unpack_failed(o, NULL);
433 goto done;
438 if (o->trivial_merges_only && o->nontrivial_merge) {
439 ret = unpack_failed(o, "Merge requires file-level merging");
440 goto done;
443 o->src_index = NULL;
444 ret = check_updates(o) ? (-2) : 0;
445 if (o->dst_index)
446 *o->dst_index = o->result;
448 done:
449 for (i = 0;i < el.nr;i++)
450 free(el.excludes[i]);
451 if (el.excludes)
452 free(el.excludes);
454 return ret;
457 /* Here come the merge functions */
459 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
461 return error(ERRORMSG(o, would_overwrite), ce->name);
464 static int same(struct cache_entry *a, struct cache_entry *b)
466 if (!!a != !!b)
467 return 0;
468 if (!a && !b)
469 return 1;
470 return a->ce_mode == b->ce_mode &&
471 !hashcmp(a->sha1, b->sha1);
476 * When a CE gets turned into an unmerged entry, we
477 * want it to be up-to-date
479 static int verify_uptodate_1(struct cache_entry *ce,
480 struct unpack_trees_options *o,
481 const char *error_msg)
483 struct stat st;
485 if (o->index_only || (!ce_skip_worktree(ce) && (o->reset || ce_uptodate(ce))))
486 return 0;
488 if (!lstat(ce->name, &st)) {
489 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
490 if (!changed)
491 return 0;
493 * NEEDSWORK: the current default policy is to allow
494 * submodule to be out of sync wrt the supermodule
495 * index. This needs to be tightened later for
496 * submodules that are marked to be automatically
497 * checked out.
499 if (S_ISGITLINK(ce->ce_mode))
500 return 0;
501 errno = 0;
503 if (errno == ENOENT)
504 return 0;
505 return o->gently ? -1 :
506 error(error_msg, ce->name);
509 static int verify_uptodate(struct cache_entry *ce,
510 struct unpack_trees_options *o)
512 return verify_uptodate_1(ce, o, ERRORMSG(o, not_uptodate_file));
515 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
517 if (ce)
518 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
522 * Check that checking out ce->sha1 in subdir ce->name is not
523 * going to overwrite any working files.
525 * Currently, git does not checkout subprojects during a superproject
526 * checkout, so it is not going to overwrite anything.
528 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
529 struct unpack_trees_options *o)
531 return 0;
534 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
535 struct unpack_trees_options *o)
538 * we are about to extract "ce->name"; we would not want to lose
539 * anything in the existing directory there.
541 int namelen;
542 int i;
543 struct dir_struct d;
544 char *pathbuf;
545 int cnt = 0;
546 unsigned char sha1[20];
548 if (S_ISGITLINK(ce->ce_mode) &&
549 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
550 /* If we are not going to update the submodule, then
551 * we don't care.
553 if (!hashcmp(sha1, ce->sha1))
554 return 0;
555 return verify_clean_submodule(ce, action, o);
559 * First let's make sure we do not have a local modification
560 * in that directory.
562 namelen = strlen(ce->name);
563 for (i = o->pos; i < o->src_index->cache_nr; i++) {
564 struct cache_entry *ce2 = o->src_index->cache[i];
565 int len = ce_namelen(ce2);
566 if (len < namelen ||
567 strncmp(ce->name, ce2->name, namelen) ||
568 ce2->name[namelen] != '/')
569 break;
571 * ce2->name is an entry in the subdirectory.
573 if (!ce_stage(ce2)) {
574 if (verify_uptodate(ce2, o))
575 return -1;
576 add_entry(o, ce2, CE_REMOVE, 0);
578 cnt++;
582 * Then we need to make sure that we do not lose a locally
583 * present file that is not ignored.
585 pathbuf = xmalloc(namelen + 2);
586 memcpy(pathbuf, ce->name, namelen);
587 strcpy(pathbuf+namelen, "/");
589 memset(&d, 0, sizeof(d));
590 if (o->dir)
591 d.exclude_per_dir = o->dir->exclude_per_dir;
592 i = read_directory(&d, pathbuf, namelen+1, NULL);
593 if (i)
594 return o->gently ? -1 :
595 error(ERRORMSG(o, not_uptodate_dir), ce->name);
596 free(pathbuf);
597 return cnt;
601 * This gets called when there was no index entry for the tree entry 'dst',
602 * but we found a file in the working tree that 'lstat()' said was fine,
603 * and we're on a case-insensitive filesystem.
605 * See if we can find a case-insensitive match in the index that also
606 * matches the stat information, and assume it's that other file!
608 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
610 struct cache_entry *src;
612 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
613 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
617 * We do not want to remove or overwrite a working tree file that
618 * is not tracked, unless it is ignored.
620 static int verify_absent_1(struct cache_entry *ce, const char *action,
621 struct unpack_trees_options *o,
622 const char *error_msg)
624 struct stat st;
626 if (o->index_only || o->reset || !o->update)
627 return 0;
629 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
630 return 0;
632 if (!lstat(ce->name, &st)) {
633 int ret;
634 int dtype = ce_to_dtype(ce);
635 struct cache_entry *result;
638 * It may be that the 'lstat()' succeeded even though
639 * target 'ce' was absent, because there is an old
640 * entry that is different only in case..
642 * Ignore that lstat() if it matches.
644 if (ignore_case && icase_exists(o, ce, &st))
645 return 0;
647 if (o->dir && excluded(o->dir, ce->name, &dtype))
649 * ce->name is explicitly excluded, so it is Ok to
650 * overwrite it.
652 return 0;
653 if (S_ISDIR(st.st_mode)) {
655 * We are checking out path "foo" and
656 * found "foo/." in the working tree.
657 * This is tricky -- if we have modified
658 * files that are in "foo/" we would lose
659 * it.
661 ret = verify_clean_subdirectory(ce, action, o);
662 if (ret < 0)
663 return ret;
666 * If this removed entries from the index,
667 * what that means is:
669 * (1) the caller unpack_callback() saw path/foo
670 * in the index, and it has not removed it because
671 * it thinks it is handling 'path' as blob with
672 * D/F conflict;
673 * (2) we will return "ok, we placed a merged entry
674 * in the index" which would cause o->pos to be
675 * incremented by one;
676 * (3) however, original o->pos now has 'path/foo'
677 * marked with "to be removed".
679 * We need to increment it by the number of
680 * deleted entries here.
682 o->pos += ret;
683 return 0;
687 * The previous round may already have decided to
688 * delete this path, which is in a subdirectory that
689 * is being replaced with a blob.
691 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
692 if (result) {
693 if (result->ce_flags & CE_REMOVE)
694 return 0;
697 return o->gently ? -1 :
698 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
700 return 0;
702 static int verify_absent(struct cache_entry *ce, const char *action,
703 struct unpack_trees_options *o)
705 return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_untracked));
708 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
709 struct unpack_trees_options *o)
711 int update = CE_UPDATE;
713 if (old) {
715 * See if we can re-use the old CE directly?
716 * That way we get the uptodate stat info.
718 * This also removes the UPDATE flag on a match; otherwise
719 * we will end up overwriting local changes in the work tree.
721 if (same(old, merge)) {
722 copy_cache_entry(merge, old);
723 update = 0;
724 } else {
725 if (verify_uptodate(old, o))
726 return -1;
727 if (ce_skip_worktree(old))
728 update |= CE_SKIP_WORKTREE;
729 invalidate_ce_path(old, o);
732 else {
733 if (verify_absent(merge, "overwritten", o))
734 return -1;
735 invalidate_ce_path(merge, o);
738 add_entry(o, merge, update, CE_STAGEMASK);
739 return 1;
742 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
743 struct unpack_trees_options *o)
745 /* Did it exist in the index? */
746 if (!old) {
747 if (verify_absent(ce, "removed", o))
748 return -1;
749 return 0;
751 if (verify_uptodate(old, o))
752 return -1;
753 add_entry(o, ce, CE_REMOVE, 0);
754 invalidate_ce_path(ce, o);
755 return 1;
758 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
760 add_entry(o, ce, 0, 0);
761 return 1;
764 #if DBRT_DEBUG
765 static void show_stage_entry(FILE *o,
766 const char *label, const struct cache_entry *ce)
768 if (!ce)
769 fprintf(o, "%s (missing)\n", label);
770 else
771 fprintf(o, "%s%06o %s %d\t%s\n",
772 label,
773 ce->ce_mode,
774 sha1_to_hex(ce->sha1),
775 ce_stage(ce),
776 ce->name);
778 #endif
780 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
782 struct cache_entry *index;
783 struct cache_entry *head;
784 struct cache_entry *remote = stages[o->head_idx + 1];
785 int count;
786 int head_match = 0;
787 int remote_match = 0;
789 int df_conflict_head = 0;
790 int df_conflict_remote = 0;
792 int any_anc_missing = 0;
793 int no_anc_exists = 1;
794 int i;
796 for (i = 1; i < o->head_idx; i++) {
797 if (!stages[i] || stages[i] == o->df_conflict_entry)
798 any_anc_missing = 1;
799 else
800 no_anc_exists = 0;
803 index = stages[0];
804 head = stages[o->head_idx];
806 if (head == o->df_conflict_entry) {
807 df_conflict_head = 1;
808 head = NULL;
811 if (remote == o->df_conflict_entry) {
812 df_conflict_remote = 1;
813 remote = NULL;
816 /* First, if there's a #16 situation, note that to prevent #13
817 * and #14.
819 if (!same(remote, head)) {
820 for (i = 1; i < o->head_idx; i++) {
821 if (same(stages[i], head)) {
822 head_match = i;
824 if (same(stages[i], remote)) {
825 remote_match = i;
830 /* We start with cases where the index is allowed to match
831 * something other than the head: #14(ALT) and #2ALT, where it
832 * is permitted to match the result instead.
834 /* #14, #14ALT, #2ALT */
835 if (remote && !df_conflict_head && head_match && !remote_match) {
836 if (index && !same(index, remote) && !same(index, head))
837 return o->gently ? -1 : reject_merge(index, o);
838 return merged_entry(remote, index, o);
841 * If we have an entry in the index cache, then we want to
842 * make sure that it matches head.
844 if (index && !same(index, head))
845 return o->gently ? -1 : reject_merge(index, o);
847 if (head) {
848 /* #5ALT, #15 */
849 if (same(head, remote))
850 return merged_entry(head, index, o);
851 /* #13, #3ALT */
852 if (!df_conflict_remote && remote_match && !head_match)
853 return merged_entry(head, index, o);
856 /* #1 */
857 if (!head && !remote && any_anc_missing)
858 return 0;
860 /* Under the new "aggressive" rule, we resolve mostly trivial
861 * cases that we historically had git-merge-one-file resolve.
863 if (o->aggressive) {
864 int head_deleted = !head && !df_conflict_head;
865 int remote_deleted = !remote && !df_conflict_remote;
866 struct cache_entry *ce = NULL;
868 if (index)
869 ce = index;
870 else if (head)
871 ce = head;
872 else if (remote)
873 ce = remote;
874 else {
875 for (i = 1; i < o->head_idx; i++) {
876 if (stages[i] && stages[i] != o->df_conflict_entry) {
877 ce = stages[i];
878 break;
884 * Deleted in both.
885 * Deleted in one and unchanged in the other.
887 if ((head_deleted && remote_deleted) ||
888 (head_deleted && remote && remote_match) ||
889 (remote_deleted && head && head_match)) {
890 if (index)
891 return deleted_entry(index, index, o);
892 if (ce && !head_deleted) {
893 if (verify_absent(ce, "removed", o))
894 return -1;
896 return 0;
899 * Added in both, identically.
901 if (no_anc_exists && head && remote && same(head, remote))
902 return merged_entry(head, index, o);
906 /* Below are "no merge" cases, which require that the index be
907 * up-to-date to avoid the files getting overwritten with
908 * conflict resolution files.
910 if (index) {
911 if (verify_uptodate(index, o))
912 return -1;
915 o->nontrivial_merge = 1;
917 /* #2, #3, #4, #6, #7, #9, #10, #11. */
918 count = 0;
919 if (!head_match || !remote_match) {
920 for (i = 1; i < o->head_idx; i++) {
921 if (stages[i] && stages[i] != o->df_conflict_entry) {
922 keep_entry(stages[i], o);
923 count++;
924 break;
928 #if DBRT_DEBUG
929 else {
930 fprintf(stderr, "read-tree: warning #16 detected\n");
931 show_stage_entry(stderr, "head ", stages[head_match]);
932 show_stage_entry(stderr, "remote ", stages[remote_match]);
934 #endif
935 if (head) { count += keep_entry(head, o); }
936 if (remote) { count += keep_entry(remote, o); }
937 return count;
941 * Two-way merge.
943 * The rule is to "carry forward" what is in the index without losing
944 * information across a "fast forward", favoring a successful merge
945 * over a merge failure when it makes sense. For details of the
946 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
949 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
951 struct cache_entry *current = src[0];
952 struct cache_entry *oldtree = src[1];
953 struct cache_entry *newtree = src[2];
955 if (o->merge_size != 2)
956 return error("Cannot do a twoway merge of %d trees",
957 o->merge_size);
959 if (oldtree == o->df_conflict_entry)
960 oldtree = NULL;
961 if (newtree == o->df_conflict_entry)
962 newtree = NULL;
964 if (current) {
965 if ((!oldtree && !newtree) || /* 4 and 5 */
966 (!oldtree && newtree &&
967 same(current, newtree)) || /* 6 and 7 */
968 (oldtree && newtree &&
969 same(oldtree, newtree)) || /* 14 and 15 */
970 (oldtree && newtree &&
971 !same(oldtree, newtree) && /* 18 and 19 */
972 same(current, newtree))) {
973 return keep_entry(current, o);
975 else if (oldtree && !newtree && same(current, oldtree)) {
976 /* 10 or 11 */
977 return deleted_entry(oldtree, current, o);
979 else if (oldtree && newtree &&
980 same(current, oldtree) && !same(current, newtree)) {
981 /* 20 or 21 */
982 return merged_entry(newtree, current, o);
984 else {
985 /* all other failures */
986 if (oldtree)
987 return o->gently ? -1 : reject_merge(oldtree, o);
988 if (current)
989 return o->gently ? -1 : reject_merge(current, o);
990 if (newtree)
991 return o->gently ? -1 : reject_merge(newtree, o);
992 return -1;
995 else if (newtree) {
996 if (oldtree && !o->initial_checkout) {
998 * deletion of the path was staged;
1000 if (same(oldtree, newtree))
1001 return 1;
1002 return reject_merge(oldtree, o);
1004 return merged_entry(newtree, current, o);
1006 return deleted_entry(oldtree, current, o);
1010 * Bind merge.
1012 * Keep the index entries at stage0, collapse stage1 but make sure
1013 * stage0 does not have anything there.
1015 int bind_merge(struct cache_entry **src,
1016 struct unpack_trees_options *o)
1018 struct cache_entry *old = src[0];
1019 struct cache_entry *a = src[1];
1021 if (o->merge_size != 1)
1022 return error("Cannot do a bind merge of %d trees\n",
1023 o->merge_size);
1024 if (a && old)
1025 return o->gently ? -1 :
1026 error(ERRORMSG(o, bind_overlap), a->name, old->name);
1027 if (!a)
1028 return keep_entry(old, o);
1029 else
1030 return merged_entry(a, NULL, o);
1034 * One-way merge.
1036 * The rule is:
1037 * - take the stat information from stage0, take the data from stage1
1039 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1041 struct cache_entry *old = src[0];
1042 struct cache_entry *a = src[1];
1044 if (o->merge_size != 1)
1045 return error("Cannot do a oneway merge of %d trees",
1046 o->merge_size);
1048 if (!a || a == o->df_conflict_entry)
1049 return deleted_entry(old, old, o);
1051 if (old && same(old, a)) {
1052 int update = 0;
1053 if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) {
1054 struct stat st;
1055 if (lstat(old->name, &st) ||
1056 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
1057 update |= CE_UPDATE;
1059 add_entry(o, old, update, 0);
1060 return 0;
1062 return merged_entry(a, old, o);