git-cvsserver: add ability to guess -kb from contents
[git/dscho.git] / unpack-trees.c
blob1ab28fda45be940b300b504ae029f3fba2c28fa9
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(struct cache_entry *ce)
31 char *cp, *prev;
32 char *name = ce->name;
34 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
35 return;
36 if (unlink(name))
37 return;
38 prev = NULL;
39 while (1) {
40 int status;
41 cp = strrchr(name, '/');
42 if (prev)
43 *prev = '/';
44 if (!cp)
45 break;
47 *cp = 0;
48 status = rmdir(name);
49 if (status) {
50 *cp = '/';
51 break;
53 prev = cp;
57 static struct checkout state;
58 static int check_updates(struct unpack_trees_options *o)
60 unsigned cnt = 0, total = 0;
61 struct progress *progress = NULL;
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 for (i = 0; i < index->cache_nr; i++) {
79 struct cache_entry *ce = index->cache[i];
81 if (ce->ce_flags & CE_REMOVE) {
82 display_progress(progress, ++cnt);
83 if (o->update)
84 unlink_entry(ce);
85 remove_index_entry_at(&o->result, i);
86 i--;
87 continue;
91 for (i = 0; i < index->cache_nr; i++) {
92 struct cache_entry *ce = index->cache[i];
94 if (ce->ce_flags & CE_UPDATE) {
95 display_progress(progress, ++cnt);
96 ce->ce_flags &= ~CE_UPDATE;
97 if (o->update) {
98 errs |= checkout_entry(ce, &state, NULL);
102 stop_progress(&progress);
103 return errs != 0;
106 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
108 int ret = o->fn(src, o);
109 if (ret > 0)
110 ret = 0;
111 return ret;
114 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
116 struct cache_entry *src[5] = { ce, };
118 o->pos++;
119 if (ce_stage(ce)) {
120 if (o->skip_unmerged) {
121 add_entry(o, ce, 0, 0);
122 return 0;
125 return call_unpack_fn(src, o);
128 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
130 int i;
131 struct tree_desc t[MAX_UNPACK_TREES];
132 struct traverse_info newinfo;
133 struct name_entry *p;
135 p = names;
136 while (!p->mode)
137 p++;
139 newinfo = *info;
140 newinfo.prev = info;
141 newinfo.name = *p;
142 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
143 newinfo.conflicts |= df_conflicts;
145 for (i = 0; i < n; i++, dirmask >>= 1) {
146 const unsigned char *sha1 = NULL;
147 if (dirmask & 1)
148 sha1 = names[i].sha1;
149 fill_tree_descriptor(t+i, sha1);
151 return traverse_trees(n, t, &newinfo);
155 * Compare the traverse-path to the cache entry without actually
156 * having to generate the textual representation of the traverse
157 * path.
159 * NOTE! This *only* compares up to the size of the traverse path
160 * itself - the caller needs to do the final check for the cache
161 * entry having more data at the end!
163 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
165 int len, pathlen, ce_len;
166 const char *ce_name;
168 if (info->prev) {
169 int cmp = do_compare_entry(ce, info->prev, &info->name);
170 if (cmp)
171 return cmp;
173 pathlen = info->pathlen;
174 ce_len = ce_namelen(ce);
176 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
177 if (ce_len < pathlen)
178 return -1;
180 ce_len -= pathlen;
181 ce_name = ce->name + pathlen;
183 len = tree_entry_len(n->path, n->sha1);
184 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
187 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
189 int cmp = do_compare_entry(ce, info, n);
190 if (cmp)
191 return cmp;
194 * Even if the beginning compared identically, the ce should
195 * compare as bigger than a directory leading up to it!
197 return ce_namelen(ce) > traverse_path_len(info, n);
200 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
202 int len = traverse_path_len(info, n);
203 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
205 ce->ce_mode = create_ce_mode(n->mode);
206 ce->ce_flags = create_ce_flags(len, stage);
207 hashcpy(ce->sha1, n->sha1);
208 make_traverse_path(ce->name, info, n);
210 return ce;
213 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
214 const struct name_entry *names, const struct traverse_info *info)
216 int i;
217 struct unpack_trees_options *o = info->data;
218 unsigned long conflicts;
220 /* Do we have *only* directories? Nothing to do */
221 if (mask == dirmask && !src[0])
222 return 0;
224 conflicts = info->conflicts;
225 if (o->merge)
226 conflicts >>= 1;
227 conflicts |= dirmask;
230 * Ok, we've filled in up to any potential index entry in src[0],
231 * now do the rest.
233 for (i = 0; i < n; i++) {
234 int stage;
235 unsigned int bit = 1ul << i;
236 if (conflicts & bit) {
237 src[i + o->merge] = o->df_conflict_entry;
238 continue;
240 if (!(mask & bit))
241 continue;
242 if (!o->merge)
243 stage = 0;
244 else if (i + 1 < o->head_idx)
245 stage = 1;
246 else if (i + 1 > o->head_idx)
247 stage = 3;
248 else
249 stage = 2;
250 src[i + o->merge] = create_ce_entry(info, names + i, stage);
253 if (o->merge)
254 return call_unpack_fn(src, o);
256 n += o->merge;
257 for (i = 0; i < n; i++)
258 add_entry(o, src[i], 0, 0);
259 return 0;
262 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
264 struct cache_entry *src[5] = { NULL, };
265 struct unpack_trees_options *o = info->data;
266 const struct name_entry *p = names;
268 /* Find first entry with a real name (we could use "mask" too) */
269 while (!p->mode)
270 p++;
272 /* Are we supposed to look at the index too? */
273 if (o->merge) {
274 while (o->pos < o->src_index->cache_nr) {
275 struct cache_entry *ce = o->src_index->cache[o->pos];
276 int cmp = compare_entry(ce, info, p);
277 if (cmp < 0) {
278 if (unpack_index_entry(ce, o) < 0)
279 return -1;
280 continue;
282 if (!cmp) {
283 o->pos++;
284 if (ce_stage(ce)) {
286 * If we skip unmerged index entries, we'll skip this
287 * entry *and* the tree entries associated with it!
289 if (o->skip_unmerged) {
290 add_entry(o, ce, 0, 0);
291 return mask;
294 src[0] = ce;
296 break;
300 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
301 return -1;
303 /* Now handle any directories.. */
304 if (dirmask) {
305 unsigned long conflicts = mask & ~dirmask;
306 if (o->merge) {
307 conflicts <<= 1;
308 if (src[0])
309 conflicts |= 1;
311 if (traverse_trees_recursive(n, dirmask, conflicts,
312 names, info) < 0)
313 return -1;
314 return mask;
317 return mask;
320 static int unpack_failed(struct unpack_trees_options *o, const char *message)
322 discard_index(&o->result);
323 if (!o->gently) {
324 if (message)
325 return error(message);
326 return -1;
328 return -1;
331 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
333 static struct cache_entry *dfc;
335 if (len > MAX_UNPACK_TREES)
336 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
337 memset(&state, 0, sizeof(state));
338 state.base_dir = "";
339 state.force = 1;
340 state.quiet = 1;
341 state.refresh_cache = 1;
343 memset(&o->result, 0, sizeof(o->result));
344 if (o->src_index)
345 o->result.timestamp = o->src_index->timestamp;
346 o->merge_size = len;
348 if (!dfc)
349 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
350 o->df_conflict_entry = dfc;
352 if (len) {
353 const char *prefix = o->prefix ? o->prefix : "";
354 struct traverse_info info;
356 setup_traverse_info(&info, prefix);
357 info.fn = unpack_callback;
358 info.data = o;
360 if (traverse_trees(len, t, &info) < 0)
361 return unpack_failed(o, NULL);
364 /* Any left-over entries in the index? */
365 if (o->merge) {
366 while (o->pos < o->src_index->cache_nr) {
367 struct cache_entry *ce = o->src_index->cache[o->pos];
368 if (unpack_index_entry(ce, o) < 0)
369 return unpack_failed(o, NULL);
373 if (o->trivial_merges_only && o->nontrivial_merge)
374 return unpack_failed(o, "Merge requires file-level merging");
376 o->src_index = NULL;
377 if (check_updates(o))
378 return -1;
379 if (o->dst_index)
380 *o->dst_index = o->result;
381 return 0;
384 /* Here come the merge functions */
386 static int reject_merge(struct cache_entry *ce)
388 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
389 ce->name);
392 static int same(struct cache_entry *a, struct cache_entry *b)
394 if (!!a != !!b)
395 return 0;
396 if (!a && !b)
397 return 1;
398 return a->ce_mode == b->ce_mode &&
399 !hashcmp(a->sha1, b->sha1);
404 * When a CE gets turned into an unmerged entry, we
405 * want it to be up-to-date
407 static int verify_uptodate(struct cache_entry *ce,
408 struct unpack_trees_options *o)
410 struct stat st;
412 if (o->index_only || o->reset)
413 return 0;
415 if (!lstat(ce->name, &st)) {
416 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
417 if (!changed)
418 return 0;
420 * NEEDSWORK: the current default policy is to allow
421 * submodule to be out of sync wrt the supermodule
422 * index. This needs to be tightened later for
423 * submodules that are marked to be automatically
424 * checked out.
426 if (S_ISGITLINK(ce->ce_mode))
427 return 0;
428 errno = 0;
430 if (errno == ENOENT)
431 return 0;
432 return o->gently ? -1 :
433 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
436 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
438 if (ce)
439 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
443 * Check that checking out ce->sha1 in subdir ce->name is not
444 * going to overwrite any working files.
446 * Currently, git does not checkout subprojects during a superproject
447 * checkout, so it is not going to overwrite anything.
449 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
450 struct unpack_trees_options *o)
452 return 0;
455 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
456 struct unpack_trees_options *o)
459 * we are about to extract "ce->name"; we would not want to lose
460 * anything in the existing directory there.
462 int namelen;
463 int pos, i;
464 struct dir_struct d;
465 char *pathbuf;
466 int cnt = 0;
467 unsigned char sha1[20];
469 if (S_ISGITLINK(ce->ce_mode) &&
470 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
471 /* If we are not going to update the submodule, then
472 * we don't care.
474 if (!hashcmp(sha1, ce->sha1))
475 return 0;
476 return verify_clean_submodule(ce, action, o);
480 * First let's make sure we do not have a local modification
481 * in that directory.
483 namelen = strlen(ce->name);
484 pos = index_name_pos(o->src_index, ce->name, namelen);
485 if (0 <= pos)
486 return cnt; /* we have it as nondirectory */
487 pos = -pos - 1;
488 for (i = pos; i < o->src_index->cache_nr; i++) {
489 struct cache_entry *ce = o->src_index->cache[i];
490 int len = ce_namelen(ce);
491 if (len < namelen ||
492 strncmp(ce->name, ce->name, namelen) ||
493 ce->name[namelen] != '/')
494 break;
496 * ce->name is an entry in the subdirectory.
498 if (!ce_stage(ce)) {
499 if (verify_uptodate(ce, o))
500 return -1;
501 add_entry(o, ce, CE_REMOVE, 0);
503 cnt++;
507 * Then we need to make sure that we do not lose a locally
508 * present file that is not ignored.
510 pathbuf = xmalloc(namelen + 2);
511 memcpy(pathbuf, ce->name, namelen);
512 strcpy(pathbuf+namelen, "/");
514 memset(&d, 0, sizeof(d));
515 if (o->dir)
516 d.exclude_per_dir = o->dir->exclude_per_dir;
517 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
518 if (i)
519 return o->gently ? -1 :
520 error("Updating '%s' would lose untracked files in it",
521 ce->name);
522 free(pathbuf);
523 return cnt;
527 * This gets called when there was no index entry for the tree entry 'dst',
528 * but we found a file in the working tree that 'lstat()' said was fine,
529 * and we're on a case-insensitive filesystem.
531 * See if we can find a case-insensitive match in the index that also
532 * matches the stat information, and assume it's that other file!
534 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
536 struct cache_entry *src;
538 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
539 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
543 * We do not want to remove or overwrite a working tree file that
544 * is not tracked, unless it is ignored.
546 static int verify_absent(struct cache_entry *ce, const char *action,
547 struct unpack_trees_options *o)
549 struct stat st;
551 if (o->index_only || o->reset || !o->update)
552 return 0;
554 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
555 return 0;
557 if (!lstat(ce->name, &st)) {
558 int cnt;
559 int dtype = ce_to_dtype(ce);
560 struct cache_entry *result;
563 * It may be that the 'lstat()' succeeded even though
564 * target 'ce' was absent, because there is an old
565 * entry that is different only in case..
567 * Ignore that lstat() if it matches.
569 if (ignore_case && icase_exists(o, ce, &st))
570 return 0;
572 if (o->dir && excluded(o->dir, ce->name, &dtype))
574 * ce->name is explicitly excluded, so it is Ok to
575 * overwrite it.
577 return 0;
578 if (S_ISDIR(st.st_mode)) {
580 * We are checking out path "foo" and
581 * found "foo/." in the working tree.
582 * This is tricky -- if we have modified
583 * files that are in "foo/" we would lose
584 * it.
586 cnt = verify_clean_subdirectory(ce, action, o);
589 * If this removed entries from the index,
590 * what that means is:
592 * (1) the caller unpack_trees_rec() saw path/foo
593 * in the index, and it has not removed it because
594 * it thinks it is handling 'path' as blob with
595 * D/F conflict;
596 * (2) we will return "ok, we placed a merged entry
597 * in the index" which would cause o->pos to be
598 * incremented by one;
599 * (3) however, original o->pos now has 'path/foo'
600 * marked with "to be removed".
602 * We need to increment it by the number of
603 * deleted entries here.
605 o->pos += cnt;
606 return 0;
610 * The previous round may already have decided to
611 * delete this path, which is in a subdirectory that
612 * is being replaced with a blob.
614 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
615 if (result) {
616 if (result->ce_flags & CE_REMOVE)
617 return 0;
620 return o->gently ? -1 :
621 error("Untracked working tree file '%s' "
622 "would be %s by merge.", ce->name, action);
624 return 0;
627 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
628 struct unpack_trees_options *o)
630 int update = CE_UPDATE;
632 if (old) {
634 * See if we can re-use the old CE directly?
635 * That way we get the uptodate stat info.
637 * This also removes the UPDATE flag on a match; otherwise
638 * we will end up overwriting local changes in the work tree.
640 if (same(old, merge)) {
641 copy_cache_entry(merge, old);
642 update = 0;
643 } else {
644 if (verify_uptodate(old, o))
645 return -1;
646 invalidate_ce_path(old, o);
649 else {
650 if (verify_absent(merge, "overwritten", o))
651 return -1;
652 invalidate_ce_path(merge, o);
655 add_entry(o, merge, update, CE_STAGEMASK);
656 return 1;
659 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
660 struct unpack_trees_options *o)
662 /* Did it exist in the index? */
663 if (!old) {
664 if (verify_absent(ce, "removed", o))
665 return -1;
666 return 0;
668 if (verify_uptodate(old, o))
669 return -1;
670 add_entry(o, ce, CE_REMOVE, 0);
671 invalidate_ce_path(ce, o);
672 return 1;
675 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
677 add_entry(o, ce, 0, 0);
678 return 1;
681 #if DBRT_DEBUG
682 static void show_stage_entry(FILE *o,
683 const char *label, const struct cache_entry *ce)
685 if (!ce)
686 fprintf(o, "%s (missing)\n", label);
687 else
688 fprintf(o, "%s%06o %s %d\t%s\n",
689 label,
690 ce->ce_mode,
691 sha1_to_hex(ce->sha1),
692 ce_stage(ce),
693 ce->name);
695 #endif
697 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
699 struct cache_entry *index;
700 struct cache_entry *head;
701 struct cache_entry *remote = stages[o->head_idx + 1];
702 int count;
703 int head_match = 0;
704 int remote_match = 0;
706 int df_conflict_head = 0;
707 int df_conflict_remote = 0;
709 int any_anc_missing = 0;
710 int no_anc_exists = 1;
711 int i;
713 for (i = 1; i < o->head_idx; i++) {
714 if (!stages[i] || stages[i] == o->df_conflict_entry)
715 any_anc_missing = 1;
716 else
717 no_anc_exists = 0;
720 index = stages[0];
721 head = stages[o->head_idx];
723 if (head == o->df_conflict_entry) {
724 df_conflict_head = 1;
725 head = NULL;
728 if (remote == o->df_conflict_entry) {
729 df_conflict_remote = 1;
730 remote = NULL;
733 /* First, if there's a #16 situation, note that to prevent #13
734 * and #14.
736 if (!same(remote, head)) {
737 for (i = 1; i < o->head_idx; i++) {
738 if (same(stages[i], head)) {
739 head_match = i;
741 if (same(stages[i], remote)) {
742 remote_match = i;
747 /* We start with cases where the index is allowed to match
748 * something other than the head: #14(ALT) and #2ALT, where it
749 * is permitted to match the result instead.
751 /* #14, #14ALT, #2ALT */
752 if (remote && !df_conflict_head && head_match && !remote_match) {
753 if (index && !same(index, remote) && !same(index, head))
754 return o->gently ? -1 : reject_merge(index);
755 return merged_entry(remote, index, o);
758 * If we have an entry in the index cache, then we want to
759 * make sure that it matches head.
761 if (index && !same(index, head))
762 return o->gently ? -1 : reject_merge(index);
764 if (head) {
765 /* #5ALT, #15 */
766 if (same(head, remote))
767 return merged_entry(head, index, o);
768 /* #13, #3ALT */
769 if (!df_conflict_remote && remote_match && !head_match)
770 return merged_entry(head, index, o);
773 /* #1 */
774 if (!head && !remote && any_anc_missing)
775 return 0;
777 /* Under the new "aggressive" rule, we resolve mostly trivial
778 * cases that we historically had git-merge-one-file resolve.
780 if (o->aggressive) {
781 int head_deleted = !head && !df_conflict_head;
782 int remote_deleted = !remote && !df_conflict_remote;
783 struct cache_entry *ce = NULL;
785 if (index)
786 ce = index;
787 else if (head)
788 ce = head;
789 else if (remote)
790 ce = remote;
791 else {
792 for (i = 1; i < o->head_idx; i++) {
793 if (stages[i] && stages[i] != o->df_conflict_entry) {
794 ce = stages[i];
795 break;
801 * Deleted in both.
802 * Deleted in one and unchanged in the other.
804 if ((head_deleted && remote_deleted) ||
805 (head_deleted && remote && remote_match) ||
806 (remote_deleted && head && head_match)) {
807 if (index)
808 return deleted_entry(index, index, o);
809 if (ce && !head_deleted) {
810 if (verify_absent(ce, "removed", o))
811 return -1;
813 return 0;
816 * Added in both, identically.
818 if (no_anc_exists && head && remote && same(head, remote))
819 return merged_entry(head, index, o);
823 /* Below are "no merge" cases, which require that the index be
824 * up-to-date to avoid the files getting overwritten with
825 * conflict resolution files.
827 if (index) {
828 if (verify_uptodate(index, o))
829 return -1;
832 o->nontrivial_merge = 1;
834 /* #2, #3, #4, #6, #7, #9, #10, #11. */
835 count = 0;
836 if (!head_match || !remote_match) {
837 for (i = 1; i < o->head_idx; i++) {
838 if (stages[i] && stages[i] != o->df_conflict_entry) {
839 keep_entry(stages[i], o);
840 count++;
841 break;
845 #if DBRT_DEBUG
846 else {
847 fprintf(stderr, "read-tree: warning #16 detected\n");
848 show_stage_entry(stderr, "head ", stages[head_match]);
849 show_stage_entry(stderr, "remote ", stages[remote_match]);
851 #endif
852 if (head) { count += keep_entry(head, o); }
853 if (remote) { count += keep_entry(remote, o); }
854 return count;
858 * Two-way merge.
860 * The rule is to "carry forward" what is in the index without losing
861 * information across a "fast forward", favoring a successful merge
862 * over a merge failure when it makes sense. For details of the
863 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
866 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
868 struct cache_entry *current = src[0];
869 struct cache_entry *oldtree = src[1];
870 struct cache_entry *newtree = src[2];
872 if (o->merge_size != 2)
873 return error("Cannot do a twoway merge of %d trees",
874 o->merge_size);
876 if (oldtree == o->df_conflict_entry)
877 oldtree = NULL;
878 if (newtree == o->df_conflict_entry)
879 newtree = NULL;
881 if (current) {
882 if ((!oldtree && !newtree) || /* 4 and 5 */
883 (!oldtree && newtree &&
884 same(current, newtree)) || /* 6 and 7 */
885 (oldtree && newtree &&
886 same(oldtree, newtree)) || /* 14 and 15 */
887 (oldtree && newtree &&
888 !same(oldtree, newtree) && /* 18 and 19 */
889 same(current, newtree))) {
890 return keep_entry(current, o);
892 else if (oldtree && !newtree && same(current, oldtree)) {
893 /* 10 or 11 */
894 return deleted_entry(oldtree, current, o);
896 else if (oldtree && newtree &&
897 same(current, oldtree) && !same(current, newtree)) {
898 /* 20 or 21 */
899 return merged_entry(newtree, current, o);
901 else {
902 /* all other failures */
903 if (oldtree)
904 return o->gently ? -1 : reject_merge(oldtree);
905 if (current)
906 return o->gently ? -1 : reject_merge(current);
907 if (newtree)
908 return o->gently ? -1 : reject_merge(newtree);
909 return -1;
912 else if (newtree)
913 return merged_entry(newtree, current, o);
914 return deleted_entry(oldtree, current, o);
918 * Bind merge.
920 * Keep the index entries at stage0, collapse stage1 but make sure
921 * stage0 does not have anything there.
923 int bind_merge(struct cache_entry **src,
924 struct unpack_trees_options *o)
926 struct cache_entry *old = src[0];
927 struct cache_entry *a = src[1];
929 if (o->merge_size != 1)
930 return error("Cannot do a bind merge of %d trees\n",
931 o->merge_size);
932 if (a && old)
933 return o->gently ? -1 :
934 error("Entry '%s' overlaps with '%s'. Cannot bind.", a->name, old->name);
935 if (!a)
936 return keep_entry(old, o);
937 else
938 return merged_entry(a, NULL, o);
942 * One-way merge.
944 * The rule is:
945 * - take the stat information from stage0, take the data from stage1
947 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
949 struct cache_entry *old = src[0];
950 struct cache_entry *a = src[1];
952 if (o->merge_size != 1)
953 return error("Cannot do a oneway merge of %d trees",
954 o->merge_size);
956 if (!a)
957 return deleted_entry(old, old, o);
959 if (old && same(old, a)) {
960 int update = 0;
961 if (o->reset) {
962 struct stat st;
963 if (lstat(old->name, &st) ||
964 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
965 update |= CE_UPDATE;
967 add_entry(o, old, update, 0);
968 return 0;
970 return merged_entry(a, old, o);