Merge branch 'lt/case-insensitive'
[git/dscho.git] / unpack-trees.c
blobfeae846226237fce9b4ff4a4810a9961637a4ca5
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"
11 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
12 unsigned int set, unsigned int clear)
14 unsigned int size = ce_size(ce);
15 struct cache_entry *new = xmalloc(size);
17 clear |= CE_HASHED | CE_UNHASHED;
19 memcpy(new, ce, size);
20 new->next = NULL;
21 new->ce_flags = (new->ce_flags & ~clear) | set;
22 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
25 /* Unlink the last component and attempt to remove leading
26 * directories, in case this unlink is the removal of the
27 * last entry in the directory -- empty directories are removed.
29 static void unlink_entry(char *name, char *last_symlink)
31 char *cp, *prev;
33 if (has_symlink_leading_path(name, last_symlink))
34 return;
35 if (unlink(name))
36 return;
37 prev = NULL;
38 while (1) {
39 int status;
40 cp = strrchr(name, '/');
41 if (prev)
42 *prev = '/';
43 if (!cp)
44 break;
46 *cp = 0;
47 status = rmdir(name);
48 if (status) {
49 *cp = '/';
50 break;
52 prev = cp;
56 static struct checkout state;
57 static int check_updates(struct unpack_trees_options *o)
59 unsigned cnt = 0, total = 0;
60 struct progress *progress = NULL;
61 char last_symlink[PATH_MAX];
62 struct index_state *index = &o->result;
63 int i;
64 int errs = 0;
66 if (o->update && o->verbose_update) {
67 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
68 struct cache_entry *ce = index->cache[cnt];
69 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
70 total++;
73 progress = start_progress_delay("Checking out files",
74 total, 50, 1);
75 cnt = 0;
78 *last_symlink = '\0';
79 for (i = 0; i < index->cache_nr; i++) {
80 struct cache_entry *ce = index->cache[i];
82 if (ce->ce_flags & CE_REMOVE) {
83 display_progress(progress, ++cnt);
84 if (o->update)
85 unlink_entry(ce->name, last_symlink);
86 remove_index_entry_at(&o->result, i);
87 i--;
88 continue;
92 for (i = 0; i < index->cache_nr; i++) {
93 struct cache_entry *ce = index->cache[i];
95 if (ce->ce_flags & CE_UPDATE) {
96 display_progress(progress, ++cnt);
97 ce->ce_flags &= ~CE_UPDATE;
98 if (o->update) {
99 errs |= checkout_entry(ce, &state, NULL);
100 *last_symlink = '\0';
104 stop_progress(&progress);
105 return errs != 0;
108 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
110 int ret = o->fn(src, o);
111 if (ret > 0)
112 ret = 0;
113 return ret;
116 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
118 struct cache_entry *src[5] = { ce, };
120 o->pos++;
121 if (ce_stage(ce)) {
122 if (o->skip_unmerged) {
123 add_entry(o, ce, 0, 0);
124 return 0;
127 return call_unpack_fn(src, o);
130 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
132 int i;
133 struct tree_desc t[MAX_UNPACK_TREES];
134 struct traverse_info newinfo;
135 struct name_entry *p;
137 p = names;
138 while (!p->mode)
139 p++;
141 newinfo = *info;
142 newinfo.prev = info;
143 newinfo.name = *p;
144 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
145 newinfo.conflicts |= df_conflicts;
147 for (i = 0; i < n; i++, dirmask >>= 1) {
148 const unsigned char *sha1 = NULL;
149 if (dirmask & 1)
150 sha1 = names[i].sha1;
151 fill_tree_descriptor(t+i, sha1);
153 return traverse_trees(n, t, &newinfo);
157 * Compare the traverse-path to the cache entry without actually
158 * having to generate the textual representation of the traverse
159 * path.
161 * NOTE! This *only* compares up to the size of the traverse path
162 * itself - the caller needs to do the final check for the cache
163 * entry having more data at the end!
165 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
167 int len, pathlen, ce_len;
168 const char *ce_name;
170 if (info->prev) {
171 int cmp = do_compare_entry(ce, info->prev, &info->name);
172 if (cmp)
173 return cmp;
175 pathlen = info->pathlen;
176 ce_len = ce_namelen(ce);
178 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
179 if (ce_len < pathlen)
180 return -1;
182 ce_len -= pathlen;
183 ce_name = ce->name + pathlen;
185 len = tree_entry_len(n->path, n->sha1);
186 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
189 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
191 int cmp = do_compare_entry(ce, info, n);
192 if (cmp)
193 return cmp;
196 * Even if the beginning compared identically, the ce should
197 * compare as bigger than a directory leading up to it!
199 return ce_namelen(ce) > traverse_path_len(info, n);
202 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
204 int len = traverse_path_len(info, n);
205 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
207 ce->ce_mode = create_ce_mode(n->mode);
208 ce->ce_flags = create_ce_flags(len, stage);
209 hashcpy(ce->sha1, n->sha1);
210 make_traverse_path(ce->name, info, n);
212 return ce;
215 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
216 const struct name_entry *names, const struct traverse_info *info)
218 int i;
219 struct unpack_trees_options *o = info->data;
220 unsigned long conflicts;
222 /* Do we have *only* directories? Nothing to do */
223 if (mask == dirmask && !src[0])
224 return 0;
226 conflicts = info->conflicts;
227 if (o->merge)
228 conflicts >>= 1;
229 conflicts |= dirmask;
232 * Ok, we've filled in up to any potential index entry in src[0],
233 * now do the rest.
235 for (i = 0; i < n; i++) {
236 int stage;
237 unsigned int bit = 1ul << i;
238 if (conflicts & bit) {
239 src[i + o->merge] = o->df_conflict_entry;
240 continue;
242 if (!(mask & bit))
243 continue;
244 if (!o->merge)
245 stage = 0;
246 else if (i + 1 < o->head_idx)
247 stage = 1;
248 else if (i + 1 > o->head_idx)
249 stage = 3;
250 else
251 stage = 2;
252 src[i + o->merge] = create_ce_entry(info, names + i, stage);
255 if (o->merge)
256 return call_unpack_fn(src, o);
258 n += o->merge;
259 for (i = 0; i < n; i++)
260 add_entry(o, src[i], 0, 0);
261 return 0;
264 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
266 struct cache_entry *src[5] = { NULL, };
267 struct unpack_trees_options *o = info->data;
268 const struct name_entry *p = names;
270 /* Find first entry with a real name (we could use "mask" too) */
271 while (!p->mode)
272 p++;
274 /* Are we supposed to look at the index too? */
275 if (o->merge) {
276 while (o->pos < o->src_index->cache_nr) {
277 struct cache_entry *ce = o->src_index->cache[o->pos];
278 int cmp = compare_entry(ce, info, p);
279 if (cmp < 0) {
280 if (unpack_index_entry(ce, o) < 0)
281 return -1;
282 continue;
284 if (!cmp) {
285 o->pos++;
286 if (ce_stage(ce)) {
288 * If we skip unmerged index entries, we'll skip this
289 * entry *and* the tree entries associated with it!
291 if (o->skip_unmerged) {
292 add_entry(o, ce, 0, 0);
293 return mask;
296 src[0] = ce;
298 break;
302 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
303 return -1;
305 /* Now handle any directories.. */
306 if (dirmask) {
307 unsigned long conflicts = mask & ~dirmask;
308 if (o->merge) {
309 conflicts <<= 1;
310 if (src[0])
311 conflicts |= 1;
313 if (traverse_trees_recursive(n, dirmask, conflicts,
314 names, info) < 0)
315 return -1;
316 return mask;
319 return mask;
322 static int unpack_failed(struct unpack_trees_options *o, const char *message)
324 discard_index(&o->result);
325 if (!o->gently) {
326 if (message)
327 return error(message);
328 return -1;
330 return -1;
333 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
335 static struct cache_entry *dfc;
337 if (len > MAX_UNPACK_TREES)
338 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
339 memset(&state, 0, sizeof(state));
340 state.base_dir = "";
341 state.force = 1;
342 state.quiet = 1;
343 state.refresh_cache = 1;
345 memset(&o->result, 0, sizeof(o->result));
346 if (o->src_index)
347 o->result.timestamp = o->src_index->timestamp;
348 o->merge_size = len;
350 if (!dfc)
351 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
352 o->df_conflict_entry = dfc;
354 if (len) {
355 const char *prefix = o->prefix ? o->prefix : "";
356 struct traverse_info info;
358 setup_traverse_info(&info, prefix);
359 info.fn = unpack_callback;
360 info.data = o;
362 if (traverse_trees(len, t, &info) < 0)
363 return unpack_failed(o, NULL);
366 /* Any left-over entries in the index? */
367 if (o->merge) {
368 while (o->pos < o->src_index->cache_nr) {
369 struct cache_entry *ce = o->src_index->cache[o->pos];
370 if (unpack_index_entry(ce, o) < 0)
371 return unpack_failed(o, NULL);
375 if (o->trivial_merges_only && o->nontrivial_merge)
376 return unpack_failed(o, "Merge requires file-level merging");
378 o->src_index = NULL;
379 if (check_updates(o))
380 return -1;
381 if (o->dst_index)
382 *o->dst_index = o->result;
383 return 0;
386 /* Here come the merge functions */
388 static int reject_merge(struct cache_entry *ce)
390 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
391 ce->name);
394 static int same(struct cache_entry *a, struct cache_entry *b)
396 if (!!a != !!b)
397 return 0;
398 if (!a && !b)
399 return 1;
400 return a->ce_mode == b->ce_mode &&
401 !hashcmp(a->sha1, b->sha1);
406 * When a CE gets turned into an unmerged entry, we
407 * want it to be up-to-date
409 static int verify_uptodate(struct cache_entry *ce,
410 struct unpack_trees_options *o)
412 struct stat st;
414 if (o->index_only || o->reset)
415 return 0;
417 if (!lstat(ce->name, &st)) {
418 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
419 if (!changed)
420 return 0;
422 * NEEDSWORK: the current default policy is to allow
423 * submodule to be out of sync wrt the supermodule
424 * index. This needs to be tightened later for
425 * submodules that are marked to be automatically
426 * checked out.
428 if (S_ISGITLINK(ce->ce_mode))
429 return 0;
430 errno = 0;
432 if (errno == ENOENT)
433 return 0;
434 return o->gently ? -1 :
435 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
438 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
440 if (ce)
441 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
445 * Check that checking out ce->sha1 in subdir ce->name is not
446 * going to overwrite any working files.
448 * Currently, git does not checkout subprojects during a superproject
449 * checkout, so it is not going to overwrite anything.
451 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
452 struct unpack_trees_options *o)
454 return 0;
457 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
458 struct unpack_trees_options *o)
461 * we are about to extract "ce->name"; we would not want to lose
462 * anything in the existing directory there.
464 int namelen;
465 int pos, i;
466 struct dir_struct d;
467 char *pathbuf;
468 int cnt = 0;
469 unsigned char sha1[20];
471 if (S_ISGITLINK(ce->ce_mode) &&
472 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
473 /* If we are not going to update the submodule, then
474 * we don't care.
476 if (!hashcmp(sha1, ce->sha1))
477 return 0;
478 return verify_clean_submodule(ce, action, o);
482 * First let's make sure we do not have a local modification
483 * in that directory.
485 namelen = strlen(ce->name);
486 pos = index_name_pos(o->src_index, ce->name, namelen);
487 if (0 <= pos)
488 return cnt; /* we have it as nondirectory */
489 pos = -pos - 1;
490 for (i = pos; i < o->src_index->cache_nr; i++) {
491 struct cache_entry *ce = o->src_index->cache[i];
492 int len = ce_namelen(ce);
493 if (len < namelen ||
494 strncmp(ce->name, ce->name, namelen) ||
495 ce->name[namelen] != '/')
496 break;
498 * ce->name is an entry in the subdirectory.
500 if (!ce_stage(ce)) {
501 if (verify_uptodate(ce, o))
502 return -1;
503 add_entry(o, ce, CE_REMOVE, 0);
505 cnt++;
509 * Then we need to make sure that we do not lose a locally
510 * present file that is not ignored.
512 pathbuf = xmalloc(namelen + 2);
513 memcpy(pathbuf, ce->name, namelen);
514 strcpy(pathbuf+namelen, "/");
516 memset(&d, 0, sizeof(d));
517 if (o->dir)
518 d.exclude_per_dir = o->dir->exclude_per_dir;
519 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
520 if (i)
521 return o->gently ? -1 :
522 error("Updating '%s' would lose untracked files in it",
523 ce->name);
524 free(pathbuf);
525 return cnt;
529 * This gets called when there was no index entry for the tree entry 'dst',
530 * but we found a file in the working tree that 'lstat()' said was fine,
531 * and we're on a case-insensitive filesystem.
533 * See if we can find a case-insensitive match in the index that also
534 * matches the stat information, and assume it's that other file!
536 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
538 struct cache_entry *src;
540 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
541 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
545 * We do not want to remove or overwrite a working tree file that
546 * is not tracked, unless it is ignored.
548 static int verify_absent(struct cache_entry *ce, const char *action,
549 struct unpack_trees_options *o)
551 struct stat st;
553 if (o->index_only || o->reset || !o->update)
554 return 0;
556 if (has_symlink_leading_path(ce->name, NULL))
557 return 0;
559 if (!lstat(ce->name, &st)) {
560 int cnt;
561 int dtype = ce_to_dtype(ce);
562 struct cache_entry *result;
565 * It may be that the 'lstat()' succeeded even though
566 * target 'ce' was absent, because there is an old
567 * entry that is different only in case..
569 * Ignore that lstat() if it matches.
571 if (ignore_case && icase_exists(o, ce, &st))
572 return 0;
574 if (o->dir && excluded(o->dir, ce->name, &dtype))
576 * ce->name is explicitly excluded, so it is Ok to
577 * overwrite it.
579 return 0;
580 if (S_ISDIR(st.st_mode)) {
582 * We are checking out path "foo" and
583 * found "foo/." in the working tree.
584 * This is tricky -- if we have modified
585 * files that are in "foo/" we would lose
586 * it.
588 cnt = verify_clean_subdirectory(ce, action, o);
591 * If this removed entries from the index,
592 * what that means is:
594 * (1) the caller unpack_trees_rec() saw path/foo
595 * in the index, and it has not removed it because
596 * it thinks it is handling 'path' as blob with
597 * D/F conflict;
598 * (2) we will return "ok, we placed a merged entry
599 * in the index" which would cause o->pos to be
600 * incremented by one;
601 * (3) however, original o->pos now has 'path/foo'
602 * marked with "to be removed".
604 * We need to increment it by the number of
605 * deleted entries here.
607 o->pos += cnt;
608 return 0;
612 * The previous round may already have decided to
613 * delete this path, which is in a subdirectory that
614 * is being replaced with a blob.
616 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
617 if (result) {
618 if (result->ce_flags & CE_REMOVE)
619 return 0;
622 return o->gently ? -1 :
623 error("Untracked working tree file '%s' "
624 "would be %s by merge.", ce->name, action);
626 return 0;
629 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
630 struct unpack_trees_options *o)
632 int update = CE_UPDATE;
634 if (old) {
636 * See if we can re-use the old CE directly?
637 * That way we get the uptodate stat info.
639 * This also removes the UPDATE flag on a match; otherwise
640 * we will end up overwriting local changes in the work tree.
642 if (same(old, merge)) {
643 copy_cache_entry(merge, old);
644 update = 0;
645 } else {
646 if (verify_uptodate(old, o))
647 return -1;
648 invalidate_ce_path(old, o);
651 else {
652 if (verify_absent(merge, "overwritten", o))
653 return -1;
654 invalidate_ce_path(merge, o);
657 add_entry(o, merge, update, CE_STAGEMASK);
658 return 1;
661 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
662 struct unpack_trees_options *o)
664 /* Did it exist in the index? */
665 if (!old) {
666 if (verify_absent(ce, "removed", o))
667 return -1;
668 return 0;
670 if (verify_uptodate(old, o))
671 return -1;
672 add_entry(o, ce, CE_REMOVE, 0);
673 invalidate_ce_path(ce, o);
674 return 1;
677 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
679 add_entry(o, ce, 0, 0);
680 return 1;
683 #if DBRT_DEBUG
684 static void show_stage_entry(FILE *o,
685 const char *label, const struct cache_entry *ce)
687 if (!ce)
688 fprintf(o, "%s (missing)\n", label);
689 else
690 fprintf(o, "%s%06o %s %d\t%s\n",
691 label,
692 ce->ce_mode,
693 sha1_to_hex(ce->sha1),
694 ce_stage(ce),
695 ce->name);
697 #endif
699 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
701 struct cache_entry *index;
702 struct cache_entry *head;
703 struct cache_entry *remote = stages[o->head_idx + 1];
704 int count;
705 int head_match = 0;
706 int remote_match = 0;
708 int df_conflict_head = 0;
709 int df_conflict_remote = 0;
711 int any_anc_missing = 0;
712 int no_anc_exists = 1;
713 int i;
715 for (i = 1; i < o->head_idx; i++) {
716 if (!stages[i] || stages[i] == o->df_conflict_entry)
717 any_anc_missing = 1;
718 else
719 no_anc_exists = 0;
722 index = stages[0];
723 head = stages[o->head_idx];
725 if (head == o->df_conflict_entry) {
726 df_conflict_head = 1;
727 head = NULL;
730 if (remote == o->df_conflict_entry) {
731 df_conflict_remote = 1;
732 remote = NULL;
735 /* First, if there's a #16 situation, note that to prevent #13
736 * and #14.
738 if (!same(remote, head)) {
739 for (i = 1; i < o->head_idx; i++) {
740 if (same(stages[i], head)) {
741 head_match = i;
743 if (same(stages[i], remote)) {
744 remote_match = i;
749 /* We start with cases where the index is allowed to match
750 * something other than the head: #14(ALT) and #2ALT, where it
751 * is permitted to match the result instead.
753 /* #14, #14ALT, #2ALT */
754 if (remote && !df_conflict_head && head_match && !remote_match) {
755 if (index && !same(index, remote) && !same(index, head))
756 return o->gently ? -1 : reject_merge(index);
757 return merged_entry(remote, index, o);
760 * If we have an entry in the index cache, then we want to
761 * make sure that it matches head.
763 if (index && !same(index, head))
764 return o->gently ? -1 : reject_merge(index);
766 if (head) {
767 /* #5ALT, #15 */
768 if (same(head, remote))
769 return merged_entry(head, index, o);
770 /* #13, #3ALT */
771 if (!df_conflict_remote && remote_match && !head_match)
772 return merged_entry(head, index, o);
775 /* #1 */
776 if (!head && !remote && any_anc_missing)
777 return 0;
779 /* Under the new "aggressive" rule, we resolve mostly trivial
780 * cases that we historically had git-merge-one-file resolve.
782 if (o->aggressive) {
783 int head_deleted = !head && !df_conflict_head;
784 int remote_deleted = !remote && !df_conflict_remote;
785 struct cache_entry *ce = NULL;
787 if (index)
788 ce = index;
789 else if (head)
790 ce = head;
791 else if (remote)
792 ce = remote;
793 else {
794 for (i = 1; i < o->head_idx; i++) {
795 if (stages[i] && stages[i] != o->df_conflict_entry) {
796 ce = stages[i];
797 break;
803 * Deleted in both.
804 * Deleted in one and unchanged in the other.
806 if ((head_deleted && remote_deleted) ||
807 (head_deleted && remote && remote_match) ||
808 (remote_deleted && head && head_match)) {
809 if (index)
810 return deleted_entry(index, index, o);
811 if (ce && !head_deleted) {
812 if (verify_absent(ce, "removed", o))
813 return -1;
815 return 0;
818 * Added in both, identically.
820 if (no_anc_exists && head && remote && same(head, remote))
821 return merged_entry(head, index, o);
825 /* Below are "no merge" cases, which require that the index be
826 * up-to-date to avoid the files getting overwritten with
827 * conflict resolution files.
829 if (index) {
830 if (verify_uptodate(index, o))
831 return -1;
834 o->nontrivial_merge = 1;
836 /* #2, #3, #4, #6, #7, #9, #10, #11. */
837 count = 0;
838 if (!head_match || !remote_match) {
839 for (i = 1; i < o->head_idx; i++) {
840 if (stages[i] && stages[i] != o->df_conflict_entry) {
841 keep_entry(stages[i], o);
842 count++;
843 break;
847 #if DBRT_DEBUG
848 else {
849 fprintf(stderr, "read-tree: warning #16 detected\n");
850 show_stage_entry(stderr, "head ", stages[head_match]);
851 show_stage_entry(stderr, "remote ", stages[remote_match]);
853 #endif
854 if (head) { count += keep_entry(head, o); }
855 if (remote) { count += keep_entry(remote, o); }
856 return count;
860 * Two-way merge.
862 * The rule is to "carry forward" what is in the index without losing
863 * information across a "fast forward", favoring a successful merge
864 * over a merge failure when it makes sense. For details of the
865 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
868 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
870 struct cache_entry *current = src[0];
871 struct cache_entry *oldtree = src[1];
872 struct cache_entry *newtree = src[2];
874 if (o->merge_size != 2)
875 return error("Cannot do a twoway merge of %d trees",
876 o->merge_size);
878 if (oldtree == o->df_conflict_entry)
879 oldtree = NULL;
880 if (newtree == o->df_conflict_entry)
881 newtree = NULL;
883 if (current) {
884 if ((!oldtree && !newtree) || /* 4 and 5 */
885 (!oldtree && newtree &&
886 same(current, newtree)) || /* 6 and 7 */
887 (oldtree && newtree &&
888 same(oldtree, newtree)) || /* 14 and 15 */
889 (oldtree && newtree &&
890 !same(oldtree, newtree) && /* 18 and 19 */
891 same(current, newtree))) {
892 return keep_entry(current, o);
894 else if (oldtree && !newtree && same(current, oldtree)) {
895 /* 10 or 11 */
896 return deleted_entry(oldtree, current, o);
898 else if (oldtree && newtree &&
899 same(current, oldtree) && !same(current, newtree)) {
900 /* 20 or 21 */
901 return merged_entry(newtree, current, o);
903 else {
904 /* all other failures */
905 if (oldtree)
906 return o->gently ? -1 : reject_merge(oldtree);
907 if (current)
908 return o->gently ? -1 : reject_merge(current);
909 if (newtree)
910 return o->gently ? -1 : reject_merge(newtree);
911 return -1;
914 else if (newtree)
915 return merged_entry(newtree, current, o);
916 return deleted_entry(oldtree, current, o);
920 * Bind merge.
922 * Keep the index entries at stage0, collapse stage1 but make sure
923 * stage0 does not have anything there.
925 int bind_merge(struct cache_entry **src,
926 struct unpack_trees_options *o)
928 struct cache_entry *old = src[0];
929 struct cache_entry *a = src[1];
931 if (o->merge_size != 1)
932 return error("Cannot do a bind merge of %d trees\n",
933 o->merge_size);
934 if (a && old)
935 return o->gently ? -1 :
936 error("Entry '%s' overlaps with '%s'. Cannot bind.", a->name, old->name);
937 if (!a)
938 return keep_entry(old, o);
939 else
940 return merged_entry(a, NULL, o);
944 * One-way merge.
946 * The rule is:
947 * - take the stat information from stage0, take the data from stage1
949 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
951 struct cache_entry *old = src[0];
952 struct cache_entry *a = src[1];
954 if (o->merge_size != 1)
955 return error("Cannot do a oneway merge of %d trees",
956 o->merge_size);
958 if (!a)
959 return deleted_entry(old, old, o);
961 if (old && same(old, a)) {
962 int update = 0;
963 if (o->reset) {
964 struct stat st;
965 if (lstat(old->name, &st) ||
966 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
967 update |= CE_UPDATE;
969 add_entry(o, old, update, 0);
970 return 0;
972 return merged_entry(a, old, o);