1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 #include "cache-tree.h"
7 #include "unpack-trees.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 = {
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.",
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
37 #define ERRORMSG(o,fld) \
38 ( ((o) && (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);
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)))
64 if (unlink_or_warn(ce->name))
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;
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))
85 progress = start_progress_delay("Checking out files",
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);
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;
111 errs |= checkout_entry(ce, &state, NULL);
115 stop_progress(&progress);
117 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
121 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
123 int ret = o->fn(src, o);
129 static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
131 ce->ce_flags |= CE_UNPACKED;
133 if (o->cache_bottom < o->src_index->cache_nr &&
134 o->src_index->cache[o->cache_bottom] == ce) {
135 int bottom = o->cache_bottom;
136 while (bottom < o->src_index->cache_nr &&
137 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
139 o->cache_bottom = bottom;
143 static void mark_all_ce_unused(struct index_state *index)
146 for (i = 0; i < index->cache_nr; i++)
147 index->cache[i]->ce_flags &= ~CE_UNPACKED;
150 static int locate_in_src_index(struct cache_entry *ce,
151 struct unpack_trees_options *o)
153 struct index_state *index = o->src_index;
154 int len = ce_namelen(ce);
155 int pos = index_name_pos(index, ce->name, len);
162 * We call unpack_index_entry() with an unmerged cache entry
163 * only in diff-index, and it wants a single callback. Skip
164 * the other unmerged entry with the same name.
166 static void mark_ce_used_same_name(struct cache_entry *ce,
167 struct unpack_trees_options *o)
169 struct index_state *index = o->src_index;
170 int len = ce_namelen(ce);
173 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
174 struct cache_entry *next = index->cache[pos];
175 if (len != ce_namelen(next) ||
176 memcmp(ce->name, next->name, len))
178 mark_ce_used(next, o);
182 static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
184 const struct index_state *index = o->src_index;
185 int pos = o->cache_bottom;
187 while (pos < index->cache_nr) {
188 struct cache_entry *ce = index->cache[pos];
189 if (!(ce->ce_flags & CE_UNPACKED))
196 static void add_same_unmerged(struct cache_entry *ce,
197 struct unpack_trees_options *o)
199 struct index_state *index = o->src_index;
200 int len = ce_namelen(ce);
201 int pos = index_name_pos(index, ce->name, len);
204 die("programming error in a caller of mark_ce_used_same_name");
205 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
206 struct cache_entry *next = index->cache[pos];
207 if (len != ce_namelen(next) ||
208 memcmp(ce->name, next->name, len))
210 add_entry(o, next, 0, 0);
211 mark_ce_used(next, o);
215 static int unpack_index_entry(struct cache_entry *ce,
216 struct unpack_trees_options *o)
218 struct cache_entry *src[5] = { ce, NULL, };
223 if (o->skip_unmerged) {
224 add_entry(o, ce, 0, 0);
228 ret = call_unpack_fn(src, o);
230 mark_ce_used_same_name(ce, o);
234 static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
237 struct tree_desc t[MAX_UNPACK_TREES];
238 struct traverse_info newinfo;
239 struct name_entry *p;
248 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
249 newinfo.conflicts |= df_conflicts;
251 for (i = 0; i < n; i++, dirmask >>= 1) {
252 const unsigned char *sha1 = NULL;
254 sha1 = names[i].sha1;
255 fill_tree_descriptor(t+i, sha1);
257 return traverse_trees(n, t, &newinfo);
261 * Compare the traverse-path to the cache entry without actually
262 * having to generate the textual representation of the traverse
265 * NOTE! This *only* compares up to the size of the traverse path
266 * itself - the caller needs to do the final check for the cache
267 * entry having more data at the end!
269 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
271 int len, pathlen, ce_len;
275 int cmp = do_compare_entry(ce, info->prev, &info->name);
279 pathlen = info->pathlen;
280 ce_len = ce_namelen(ce);
282 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
283 if (ce_len < pathlen)
287 ce_name = ce->name + pathlen;
289 len = tree_entry_len(n->path, n->sha1);
290 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
293 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
295 int cmp = do_compare_entry(ce, info, n);
300 * Even if the beginning compared identically, the ce should
301 * compare as bigger than a directory leading up to it!
303 return ce_namelen(ce) > traverse_path_len(info, n);
306 static int ce_in_traverse_path(const struct cache_entry *ce,
307 const struct traverse_info *info)
311 if (do_compare_entry(ce, info->prev, &info->name))
314 * If ce (blob) is the same name as the path (which is a tree
315 * we will be descending into), it won't be inside it.
317 return (info->pathlen < ce_namelen(ce));
320 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
322 int len = traverse_path_len(info, n);
323 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
325 ce->ce_mode = create_ce_mode(n->mode);
326 ce->ce_flags = create_ce_flags(len, stage);
327 hashcpy(ce->sha1, n->sha1);
328 make_traverse_path(ce->name, info, n);
333 static int unpack_nondirectories(int n, unsigned long mask,
334 unsigned long dirmask,
335 struct cache_entry **src,
336 const struct name_entry *names,
337 const struct traverse_info *info)
340 struct unpack_trees_options *o = info->data;
341 unsigned long conflicts;
343 /* Do we have *only* directories? Nothing to do */
344 if (mask == dirmask && !src[0])
347 conflicts = info->conflicts;
350 conflicts |= dirmask;
353 * Ok, we've filled in up to any potential index entry in src[0],
356 for (i = 0; i < n; i++) {
358 unsigned int bit = 1ul << i;
359 if (conflicts & bit) {
360 src[i + o->merge] = o->df_conflict_entry;
367 else if (i + 1 < o->head_idx)
369 else if (i + 1 > o->head_idx)
373 src[i + o->merge] = create_ce_entry(info, names + i, stage);
377 return call_unpack_fn(src, o);
379 for (i = 0; i < n; i++)
380 if (src[i] && src[i] != o->df_conflict_entry)
381 add_entry(o, src[i], 0, 0);
385 static int unpack_failed(struct unpack_trees_options *o, const char *message)
387 discard_index(&o->result);
390 return error("%s", message);
396 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
398 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
399 struct unpack_trees_options *o = info->data;
400 const struct name_entry *p = names;
402 /* Find first entry with a real name (we could use "mask" too) */
406 /* Are we supposed to look at the index too? */
409 struct cache_entry *ce = next_cache_entry(o);
413 cmp = compare_entry(ce, info, p);
415 if (unpack_index_entry(ce, o) < 0)
416 return unpack_failed(o, NULL);
422 * If we skip unmerged index
423 * entries, we'll skip this
424 * entry *and* the tree
425 * entries associated with it!
427 if (o->skip_unmerged) {
428 add_same_unmerged(ce, o);
438 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
442 if (ce_stage(src[0]))
443 mark_ce_used_same_name(src[0], o);
445 mark_ce_used(src[0], o);
448 /* Now handle any directories.. */
450 unsigned long conflicts = mask & ~dirmask;
457 /* special case: "diff-index --cached" looking at a tree */
458 if (o->diff_index_cached &&
459 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
461 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
464 * Everything under the name matches; skip the
465 * entire hierarchy. diff_index_cached codepath
466 * special cases D/F conflicts in such a way that
467 * it does not do any look-ahead, so this is safe.
470 o->cache_bottom += matches;
475 if (traverse_trees_recursive(n, dirmask, conflicts,
485 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
486 * resulting index, -2 on failure to reflect the changes to the work tree.
488 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
491 static struct cache_entry *dfc;
493 if (len > MAX_UNPACK_TREES)
494 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
495 memset(&state, 0, sizeof(state));
499 state.refresh_cache = 1;
501 memset(&o->result, 0, sizeof(o->result));
502 o->result.initialized = 1;
503 o->result.timestamp.sec = o->src_index->timestamp.sec;
504 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
506 mark_all_ce_unused(o->src_index);
509 dfc = xcalloc(1, cache_entry_size(0));
510 o->df_conflict_entry = dfc;
513 const char *prefix = o->prefix ? o->prefix : "";
514 struct traverse_info info;
516 setup_traverse_info(&info, prefix);
517 info.fn = unpack_callback;
522 * Unpack existing index entries that sort before the
523 * prefix the tree is spliced into. Note that o->merge
524 * is always true in this case.
527 struct cache_entry *ce = next_cache_entry(o);
530 if (ce_in_traverse_path(ce, &info))
532 if (unpack_index_entry(ce, o) < 0)
537 if (traverse_trees(len, t, &info) < 0)
541 /* Any left-over entries in the index? */
544 struct cache_entry *ce = next_cache_entry(o);
547 if (unpack_index_entry(ce, o) < 0)
551 mark_all_ce_unused(o->src_index);
553 if (o->trivial_merges_only && o->nontrivial_merge)
554 return unpack_failed(o, "Merge requires file-level merging");
557 ret = check_updates(o) ? (-2) : 0;
559 *o->dst_index = o->result;
563 mark_all_ce_unused(o->src_index);
564 return unpack_failed(o, NULL);
567 /* Here come the merge functions */
569 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
571 return error(ERRORMSG(o, would_overwrite), ce->name);
574 static int same(struct cache_entry *a, struct cache_entry *b)
580 return a->ce_mode == b->ce_mode &&
581 !hashcmp(a->sha1, b->sha1);
586 * When a CE gets turned into an unmerged entry, we
587 * want it to be up-to-date
589 static int verify_uptodate(struct cache_entry *ce,
590 struct unpack_trees_options *o)
594 if (o->index_only || o->reset || ce_uptodate(ce))
597 if (!lstat(ce->name, &st)) {
598 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
602 * NEEDSWORK: the current default policy is to allow
603 * submodule to be out of sync wrt the supermodule
604 * index. This needs to be tightened later for
605 * submodules that are marked to be automatically
608 if (S_ISGITLINK(ce->ce_mode))
614 return o->gently ? -1 :
615 error(ERRORMSG(o, not_uptodate_file), ce->name);
618 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
621 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
625 * Check that checking out ce->sha1 in subdir ce->name is not
626 * going to overwrite any working files.
628 * Currently, git does not checkout subprojects during a superproject
629 * checkout, so it is not going to overwrite anything.
631 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
632 struct unpack_trees_options *o)
637 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
638 struct unpack_trees_options *o)
641 * we are about to extract "ce->name"; we would not want to lose
642 * anything in the existing directory there.
649 unsigned char sha1[20];
651 if (S_ISGITLINK(ce->ce_mode) &&
652 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
653 /* If we are not going to update the submodule, then
656 if (!hashcmp(sha1, ce->sha1))
658 return verify_clean_submodule(ce, action, o);
662 * First let's make sure we do not have a local modification
665 namelen = strlen(ce->name);
666 for (i = locate_in_src_index(ce, o);
667 i < o->src_index->cache_nr;
669 struct cache_entry *ce2 = o->src_index->cache[i];
670 int len = ce_namelen(ce2);
672 strncmp(ce->name, ce2->name, namelen) ||
673 ce2->name[namelen] != '/')
676 * ce2->name is an entry in the subdirectory to be
679 if (!ce_stage(ce2)) {
680 if (verify_uptodate(ce2, o))
682 add_entry(o, ce2, CE_REMOVE, 0);
683 mark_ce_used(ce2, o);
689 * Then we need to make sure that we do not lose a locally
690 * present file that is not ignored.
692 pathbuf = xmalloc(namelen + 2);
693 memcpy(pathbuf, ce->name, namelen);
694 strcpy(pathbuf+namelen, "/");
696 memset(&d, 0, sizeof(d));
698 d.exclude_per_dir = o->dir->exclude_per_dir;
699 i = read_directory(&d, pathbuf, namelen+1, NULL);
701 return o->gently ? -1 :
702 error(ERRORMSG(o, not_uptodate_dir), ce->name);
708 * This gets called when there was no index entry for the tree entry 'dst',
709 * but we found a file in the working tree that 'lstat()' said was fine,
710 * and we're on a case-insensitive filesystem.
712 * See if we can find a case-insensitive match in the index that also
713 * matches the stat information, and assume it's that other file!
715 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
717 struct cache_entry *src;
719 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
720 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
724 * We do not want to remove or overwrite a working tree file that
725 * is not tracked, unless it is ignored.
727 static int verify_absent(struct cache_entry *ce, const char *action,
728 struct unpack_trees_options *o)
732 if (o->index_only || o->reset || !o->update)
735 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
738 if (!lstat(ce->name, &st)) {
739 int dtype = ce_to_dtype(ce);
740 struct cache_entry *result;
743 * It may be that the 'lstat()' succeeded even though
744 * target 'ce' was absent, because there is an old
745 * entry that is different only in case..
747 * Ignore that lstat() if it matches.
749 if (ignore_case && icase_exists(o, ce, &st))
752 if (o->dir && excluded(o->dir, ce->name, &dtype))
754 * ce->name is explicitly excluded, so it is Ok to
758 if (S_ISDIR(st.st_mode)) {
760 * We are checking out path "foo" and
761 * found "foo/." in the working tree.
762 * This is tricky -- if we have modified
763 * files that are in "foo/" we would lose
766 if (verify_clean_subdirectory(ce, action, o) < 0)
772 * The previous round may already have decided to
773 * delete this path, which is in a subdirectory that
774 * is being replaced with a blob.
776 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
778 if (result->ce_flags & CE_REMOVE)
782 return o->gently ? -1 :
783 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
788 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
789 struct unpack_trees_options *o)
791 int update = CE_UPDATE;
795 * See if we can re-use the old CE directly?
796 * That way we get the uptodate stat info.
798 * This also removes the UPDATE flag on a match; otherwise
799 * we will end up overwriting local changes in the work tree.
801 if (same(old, merge)) {
802 copy_cache_entry(merge, old);
805 if (verify_uptodate(old, o))
807 invalidate_ce_path(old, o);
811 if (verify_absent(merge, "overwritten", o))
813 invalidate_ce_path(merge, o);
816 add_entry(o, merge, update, CE_STAGEMASK);
820 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
821 struct unpack_trees_options *o)
823 /* Did it exist in the index? */
825 if (verify_absent(ce, "removed", o))
829 if (verify_uptodate(old, o))
831 add_entry(o, ce, CE_REMOVE, 0);
832 invalidate_ce_path(ce, o);
836 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
838 add_entry(o, ce, 0, 0);
843 static void show_stage_entry(FILE *o,
844 const char *label, const struct cache_entry *ce)
847 fprintf(o, "%s (missing)\n", label);
849 fprintf(o, "%s%06o %s %d\t%s\n",
852 sha1_to_hex(ce->sha1),
858 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
860 struct cache_entry *index;
861 struct cache_entry *head;
862 struct cache_entry *remote = stages[o->head_idx + 1];
865 int remote_match = 0;
867 int df_conflict_head = 0;
868 int df_conflict_remote = 0;
870 int any_anc_missing = 0;
871 int no_anc_exists = 1;
874 for (i = 1; i < o->head_idx; i++) {
875 if (!stages[i] || stages[i] == o->df_conflict_entry)
882 head = stages[o->head_idx];
884 if (head == o->df_conflict_entry) {
885 df_conflict_head = 1;
889 if (remote == o->df_conflict_entry) {
890 df_conflict_remote = 1;
895 * First, if there's a #16 situation, note that to prevent #13
898 if (!same(remote, head)) {
899 for (i = 1; i < o->head_idx; i++) {
900 if (same(stages[i], head)) {
903 if (same(stages[i], remote)) {
910 * We start with cases where the index is allowed to match
911 * something other than the head: #14(ALT) and #2ALT, where it
912 * is permitted to match the result instead.
914 /* #14, #14ALT, #2ALT */
915 if (remote && !df_conflict_head && head_match && !remote_match) {
916 if (index && !same(index, remote) && !same(index, head))
917 return o->gently ? -1 : reject_merge(index, o);
918 return merged_entry(remote, index, o);
921 * If we have an entry in the index cache, then we want to
922 * make sure that it matches head.
924 if (index && !same(index, head))
925 return o->gently ? -1 : reject_merge(index, o);
929 if (same(head, remote))
930 return merged_entry(head, index, o);
932 if (!df_conflict_remote && remote_match && !head_match)
933 return merged_entry(head, index, o);
937 if (!head && !remote && any_anc_missing)
941 * Under the "aggressive" rule, we resolve mostly trivial
942 * cases that we historically had git-merge-one-file resolve.
945 int head_deleted = !head;
946 int remote_deleted = !remote;
947 struct cache_entry *ce = NULL;
956 for (i = 1; i < o->head_idx; i++) {
957 if (stages[i] && stages[i] != o->df_conflict_entry) {
966 * Deleted in one and unchanged in the other.
968 if ((head_deleted && remote_deleted) ||
969 (head_deleted && remote && remote_match) ||
970 (remote_deleted && head && head_match)) {
972 return deleted_entry(index, index, o);
973 if (ce && !head_deleted) {
974 if (verify_absent(ce, "removed", o))
980 * Added in both, identically.
982 if (no_anc_exists && head && remote && same(head, remote))
983 return merged_entry(head, index, o);
987 /* Below are "no merge" cases, which require that the index be
988 * up-to-date to avoid the files getting overwritten with
989 * conflict resolution files.
992 if (verify_uptodate(index, o))
996 o->nontrivial_merge = 1;
998 /* #2, #3, #4, #6, #7, #9, #10, #11. */
1000 if (!head_match || !remote_match) {
1001 for (i = 1; i < o->head_idx; i++) {
1002 if (stages[i] && stages[i] != o->df_conflict_entry) {
1003 keep_entry(stages[i], o);
1011 fprintf(stderr, "read-tree: warning #16 detected\n");
1012 show_stage_entry(stderr, "head ", stages[head_match]);
1013 show_stage_entry(stderr, "remote ", stages[remote_match]);
1016 if (head) { count += keep_entry(head, o); }
1017 if (remote) { count += keep_entry(remote, o); }
1024 * The rule is to "carry forward" what is in the index without losing
1025 * information across a "fast-forward", favoring a successful merge
1026 * over a merge failure when it makes sense. For details of the
1027 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1030 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1032 struct cache_entry *current = src[0];
1033 struct cache_entry *oldtree = src[1];
1034 struct cache_entry *newtree = src[2];
1036 if (o->merge_size != 2)
1037 return error("Cannot do a twoway merge of %d trees",
1040 if (oldtree == o->df_conflict_entry)
1042 if (newtree == o->df_conflict_entry)
1046 if ((!oldtree && !newtree) || /* 4 and 5 */
1047 (!oldtree && newtree &&
1048 same(current, newtree)) || /* 6 and 7 */
1049 (oldtree && newtree &&
1050 same(oldtree, newtree)) || /* 14 and 15 */
1051 (oldtree && newtree &&
1052 !same(oldtree, newtree) && /* 18 and 19 */
1053 same(current, newtree))) {
1054 return keep_entry(current, o);
1056 else if (oldtree && !newtree && same(current, oldtree)) {
1058 return deleted_entry(oldtree, current, o);
1060 else if (oldtree && newtree &&
1061 same(current, oldtree) && !same(current, newtree)) {
1063 return merged_entry(newtree, current, o);
1066 /* all other failures */
1068 return o->gently ? -1 : reject_merge(oldtree, o);
1070 return o->gently ? -1 : reject_merge(current, o);
1072 return o->gently ? -1 : reject_merge(newtree, o);
1077 if (oldtree && !o->initial_checkout) {
1079 * deletion of the path was staged;
1081 if (same(oldtree, newtree))
1083 return reject_merge(oldtree, o);
1085 return merged_entry(newtree, current, o);
1087 return deleted_entry(oldtree, current, o);
1093 * Keep the index entries at stage0, collapse stage1 but make sure
1094 * stage0 does not have anything there.
1096 int bind_merge(struct cache_entry **src,
1097 struct unpack_trees_options *o)
1099 struct cache_entry *old = src[0];
1100 struct cache_entry *a = src[1];
1102 if (o->merge_size != 1)
1103 return error("Cannot do a bind merge of %d trees\n",
1106 return o->gently ? -1 :
1107 error(ERRORMSG(o, bind_overlap), a->name, old->name);
1109 return keep_entry(old, o);
1111 return merged_entry(a, NULL, o);
1118 * - take the stat information from stage0, take the data from stage1
1120 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1122 struct cache_entry *old = src[0];
1123 struct cache_entry *a = src[1];
1125 if (o->merge_size != 1)
1126 return error("Cannot do a oneway merge of %d trees",
1129 if (!a || a == o->df_conflict_entry)
1130 return deleted_entry(old, old, o);
1132 if (old && same(old, a)) {
1134 if (o->reset && !ce_uptodate(old)) {
1136 if (lstat(old->name, &st) ||
1137 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
1138 update |= CE_UPDATE;
1140 add_entry(o, old, update, 0);
1143 return merged_entry(a, old, o);