Don't load missing ACL files in paranoid update hook
[git/jrn.git] / unpack-trees.c
blob0de5a31c0b68f87491e540fdc1978809fee00240
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"
12 * Error messages expected by scripts out of plumbing commands such as
13 * read-tree. Non-scripted Porcelain is not required to use these messages
14 * and in fact are encouraged to reword them to better suit their particular
15 * situation better. See how "git checkout" replaces not_uptodate_file to
16 * explain why it does not allow switching between branches when you have
17 * local changes, for example.
19 static struct unpack_trees_error_msgs unpack_plumbing_errors = {
20 /* would_overwrite */
21 "Entry '%s' would be overwritten by merge. Cannot merge.",
23 /* not_uptodate_file */
24 "Entry '%s' not uptodate. Cannot merge.",
26 /* not_uptodate_dir */
27 "Updating '%s' would lose untracked files in it",
29 /* would_lose_untracked */
30 "Untracked working tree file '%s' would be %s by merge.",
32 /* bind_overlap */
33 "Entry '%s' overlaps with '%s'. Cannot bind.",
36 #define ERRORMSG(o,fld) \
37 ( ((o) && (o)->msgs.fld) \
38 ? ((o)->msgs.fld) \
39 : (unpack_plumbing_errors.fld) )
41 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
42 unsigned int set, unsigned int clear)
44 unsigned int size = ce_size(ce);
45 struct cache_entry *new = xmalloc(size);
47 clear |= CE_HASHED | CE_UNHASHED;
49 memcpy(new, ce, size);
50 new->next = NULL;
51 new->ce_flags = (new->ce_flags & ~clear) | set;
52 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
55 /* Unlink the last component and attempt to remove leading
56 * directories, in case this unlink is the removal of the
57 * last entry in the directory -- empty directories are removed.
59 static void unlink_entry(struct cache_entry *ce)
61 char *cp, *prev;
62 char *name = ce->name;
64 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
65 return;
66 if (unlink(name))
67 return;
68 prev = NULL;
69 while (1) {
70 int status;
71 cp = strrchr(name, '/');
72 if (prev)
73 *prev = '/';
74 if (!cp)
75 break;
77 *cp = 0;
78 status = rmdir(name);
79 if (status) {
80 *cp = '/';
81 break;
83 prev = cp;
87 static struct checkout state;
88 static int check_updates(struct unpack_trees_options *o)
90 unsigned cnt = 0, total = 0;
91 struct progress *progress = NULL;
92 struct index_state *index = &o->result;
93 int i;
94 int errs = 0;
96 if (o->update && o->verbose_update) {
97 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
98 struct cache_entry *ce = index->cache[cnt];
99 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
100 total++;
103 progress = start_progress_delay("Checking out files",
104 total, 50, 1);
105 cnt = 0;
108 for (i = 0; i < index->cache_nr; i++) {
109 struct cache_entry *ce = index->cache[i];
111 if (ce->ce_flags & CE_REMOVE) {
112 display_progress(progress, ++cnt);
113 if (o->update)
114 unlink_entry(ce);
115 remove_index_entry_at(&o->result, i);
116 i--;
117 continue;
121 for (i = 0; i < index->cache_nr; i++) {
122 struct cache_entry *ce = index->cache[i];
124 if (ce->ce_flags & CE_UPDATE) {
125 display_progress(progress, ++cnt);
126 ce->ce_flags &= ~CE_UPDATE;
127 if (o->update) {
128 errs |= checkout_entry(ce, &state, NULL);
132 stop_progress(&progress);
133 return errs != 0;
136 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
138 int ret = o->fn(src, o);
139 if (ret > 0)
140 ret = 0;
141 return ret;
144 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
146 struct cache_entry *src[5] = { ce, };
148 o->pos++;
149 if (ce_stage(ce)) {
150 if (o->skip_unmerged) {
151 add_entry(o, ce, 0, 0);
152 return 0;
155 return call_unpack_fn(src, o);
158 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
160 int i;
161 struct tree_desc t[MAX_UNPACK_TREES];
162 struct traverse_info newinfo;
163 struct name_entry *p;
165 p = names;
166 while (!p->mode)
167 p++;
169 newinfo = *info;
170 newinfo.prev = info;
171 newinfo.name = *p;
172 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
173 newinfo.conflicts |= df_conflicts;
175 for (i = 0; i < n; i++, dirmask >>= 1) {
176 const unsigned char *sha1 = NULL;
177 if (dirmask & 1)
178 sha1 = names[i].sha1;
179 fill_tree_descriptor(t+i, sha1);
181 return traverse_trees(n, t, &newinfo);
185 * Compare the traverse-path to the cache entry without actually
186 * having to generate the textual representation of the traverse
187 * path.
189 * NOTE! This *only* compares up to the size of the traverse path
190 * itself - the caller needs to do the final check for the cache
191 * entry having more data at the end!
193 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
195 int len, pathlen, ce_len;
196 const char *ce_name;
198 if (info->prev) {
199 int cmp = do_compare_entry(ce, info->prev, &info->name);
200 if (cmp)
201 return cmp;
203 pathlen = info->pathlen;
204 ce_len = ce_namelen(ce);
206 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
207 if (ce_len < pathlen)
208 return -1;
210 ce_len -= pathlen;
211 ce_name = ce->name + pathlen;
213 len = tree_entry_len(n->path, n->sha1);
214 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
217 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
219 int cmp = do_compare_entry(ce, info, n);
220 if (cmp)
221 return cmp;
224 * Even if the beginning compared identically, the ce should
225 * compare as bigger than a directory leading up to it!
227 return ce_namelen(ce) > traverse_path_len(info, n);
230 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
232 int len = traverse_path_len(info, n);
233 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
235 ce->ce_mode = create_ce_mode(n->mode);
236 ce->ce_flags = create_ce_flags(len, stage);
237 hashcpy(ce->sha1, n->sha1);
238 make_traverse_path(ce->name, info, n);
240 return ce;
243 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
244 const struct name_entry *names, const struct traverse_info *info)
246 int i;
247 struct unpack_trees_options *o = info->data;
248 unsigned long conflicts;
250 /* Do we have *only* directories? Nothing to do */
251 if (mask == dirmask && !src[0])
252 return 0;
254 conflicts = info->conflicts;
255 if (o->merge)
256 conflicts >>= 1;
257 conflicts |= dirmask;
260 * Ok, we've filled in up to any potential index entry in src[0],
261 * now do the rest.
263 for (i = 0; i < n; i++) {
264 int stage;
265 unsigned int bit = 1ul << i;
266 if (conflicts & bit) {
267 src[i + o->merge] = o->df_conflict_entry;
268 continue;
270 if (!(mask & bit))
271 continue;
272 if (!o->merge)
273 stage = 0;
274 else if (i + 1 < o->head_idx)
275 stage = 1;
276 else if (i + 1 > o->head_idx)
277 stage = 3;
278 else
279 stage = 2;
280 src[i + o->merge] = create_ce_entry(info, names + i, stage);
283 if (o->merge)
284 return call_unpack_fn(src, o);
286 n += o->merge;
287 for (i = 0; i < n; i++)
288 add_entry(o, src[i], 0, 0);
289 return 0;
292 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
294 struct cache_entry *src[5] = { NULL, };
295 struct unpack_trees_options *o = info->data;
296 const struct name_entry *p = names;
298 /* Find first entry with a real name (we could use "mask" too) */
299 while (!p->mode)
300 p++;
302 /* Are we supposed to look at the index too? */
303 if (o->merge) {
304 while (o->pos < o->src_index->cache_nr) {
305 struct cache_entry *ce = o->src_index->cache[o->pos];
306 int cmp = compare_entry(ce, info, p);
307 if (cmp < 0) {
308 if (unpack_index_entry(ce, o) < 0)
309 return -1;
310 continue;
312 if (!cmp) {
313 o->pos++;
314 if (ce_stage(ce)) {
316 * If we skip unmerged index entries, we'll skip this
317 * entry *and* the tree entries associated with it!
319 if (o->skip_unmerged) {
320 add_entry(o, ce, 0, 0);
321 return mask;
324 src[0] = ce;
326 break;
330 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
331 return -1;
333 /* Now handle any directories.. */
334 if (dirmask) {
335 unsigned long conflicts = mask & ~dirmask;
336 if (o->merge) {
337 conflicts <<= 1;
338 if (src[0])
339 conflicts |= 1;
341 if (traverse_trees_recursive(n, dirmask, conflicts,
342 names, info) < 0)
343 return -1;
344 return mask;
347 return mask;
350 static int unpack_failed(struct unpack_trees_options *o, const char *message)
352 discard_index(&o->result);
353 if (!o->gently) {
354 if (message)
355 return error(message);
356 return -1;
358 return -1;
361 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
363 static struct cache_entry *dfc;
365 if (len > MAX_UNPACK_TREES)
366 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
367 memset(&state, 0, sizeof(state));
368 state.base_dir = "";
369 state.force = 1;
370 state.quiet = 1;
371 state.refresh_cache = 1;
373 memset(&o->result, 0, sizeof(o->result));
374 if (o->src_index)
375 o->result.timestamp = o->src_index->timestamp;
376 o->merge_size = len;
378 if (!dfc)
379 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
380 o->df_conflict_entry = dfc;
382 if (len) {
383 const char *prefix = o->prefix ? o->prefix : "";
384 struct traverse_info info;
386 setup_traverse_info(&info, prefix);
387 info.fn = unpack_callback;
388 info.data = o;
390 if (traverse_trees(len, t, &info) < 0)
391 return unpack_failed(o, NULL);
394 /* Any left-over entries in the index? */
395 if (o->merge) {
396 while (o->pos < o->src_index->cache_nr) {
397 struct cache_entry *ce = o->src_index->cache[o->pos];
398 if (unpack_index_entry(ce, o) < 0)
399 return unpack_failed(o, NULL);
403 if (o->trivial_merges_only && o->nontrivial_merge)
404 return unpack_failed(o, "Merge requires file-level merging");
406 o->src_index = NULL;
407 if (check_updates(o))
408 return -1;
409 if (o->dst_index)
410 *o->dst_index = o->result;
411 return 0;
414 /* Here come the merge functions */
416 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
418 return error(ERRORMSG(o, would_overwrite), ce->name);
421 static int same(struct cache_entry *a, struct cache_entry *b)
423 if (!!a != !!b)
424 return 0;
425 if (!a && !b)
426 return 1;
427 return a->ce_mode == b->ce_mode &&
428 !hashcmp(a->sha1, b->sha1);
433 * When a CE gets turned into an unmerged entry, we
434 * want it to be up-to-date
436 static int verify_uptodate(struct cache_entry *ce,
437 struct unpack_trees_options *o)
439 struct stat st;
441 if (o->index_only || o->reset)
442 return 0;
444 if (!lstat(ce->name, &st)) {
445 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
446 if (!changed)
447 return 0;
449 * NEEDSWORK: the current default policy is to allow
450 * submodule to be out of sync wrt the supermodule
451 * index. This needs to be tightened later for
452 * submodules that are marked to be automatically
453 * checked out.
455 if (S_ISGITLINK(ce->ce_mode))
456 return 0;
457 errno = 0;
459 if (errno == ENOENT)
460 return 0;
461 return o->gently ? -1 :
462 error(ERRORMSG(o, not_uptodate_file), ce->name);
465 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
467 if (ce)
468 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
472 * Check that checking out ce->sha1 in subdir ce->name is not
473 * going to overwrite any working files.
475 * Currently, git does not checkout subprojects during a superproject
476 * checkout, so it is not going to overwrite anything.
478 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
479 struct unpack_trees_options *o)
481 return 0;
484 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
485 struct unpack_trees_options *o)
488 * we are about to extract "ce->name"; we would not want to lose
489 * anything in the existing directory there.
491 int namelen;
492 int pos, i;
493 struct dir_struct d;
494 char *pathbuf;
495 int cnt = 0;
496 unsigned char sha1[20];
498 if (S_ISGITLINK(ce->ce_mode) &&
499 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
500 /* If we are not going to update the submodule, then
501 * we don't care.
503 if (!hashcmp(sha1, ce->sha1))
504 return 0;
505 return verify_clean_submodule(ce, action, o);
509 * First let's make sure we do not have a local modification
510 * in that directory.
512 namelen = strlen(ce->name);
513 pos = index_name_pos(o->src_index, ce->name, namelen);
514 if (0 <= pos)
515 return cnt; /* we have it as nondirectory */
516 pos = -pos - 1;
517 for (i = pos; i < o->src_index->cache_nr; i++) {
518 struct cache_entry *ce = o->src_index->cache[i];
519 int len = ce_namelen(ce);
520 if (len < namelen ||
521 strncmp(ce->name, ce->name, namelen) ||
522 ce->name[namelen] != '/')
523 break;
525 * ce->name is an entry in the subdirectory.
527 if (!ce_stage(ce)) {
528 if (verify_uptodate(ce, o))
529 return -1;
530 add_entry(o, ce, CE_REMOVE, 0);
532 cnt++;
536 * Then we need to make sure that we do not lose a locally
537 * present file that is not ignored.
539 pathbuf = xmalloc(namelen + 2);
540 memcpy(pathbuf, ce->name, namelen);
541 strcpy(pathbuf+namelen, "/");
543 memset(&d, 0, sizeof(d));
544 if (o->dir)
545 d.exclude_per_dir = o->dir->exclude_per_dir;
546 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
547 if (i)
548 return o->gently ? -1 :
549 error(ERRORMSG(o, not_uptodate_dir), ce->name);
550 free(pathbuf);
551 return cnt;
555 * This gets called when there was no index entry for the tree entry 'dst',
556 * but we found a file in the working tree that 'lstat()' said was fine,
557 * and we're on a case-insensitive filesystem.
559 * See if we can find a case-insensitive match in the index that also
560 * matches the stat information, and assume it's that other file!
562 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
564 struct cache_entry *src;
566 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
567 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
571 * We do not want to remove or overwrite a working tree file that
572 * is not tracked, unless it is ignored.
574 static int verify_absent(struct cache_entry *ce, const char *action,
575 struct unpack_trees_options *o)
577 struct stat st;
579 if (o->index_only || o->reset || !o->update)
580 return 0;
582 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
583 return 0;
585 if (!lstat(ce->name, &st)) {
586 int cnt;
587 int dtype = ce_to_dtype(ce);
588 struct cache_entry *result;
591 * It may be that the 'lstat()' succeeded even though
592 * target 'ce' was absent, because there is an old
593 * entry that is different only in case..
595 * Ignore that lstat() if it matches.
597 if (ignore_case && icase_exists(o, ce, &st))
598 return 0;
600 if (o->dir && excluded(o->dir, ce->name, &dtype))
602 * ce->name is explicitly excluded, so it is Ok to
603 * overwrite it.
605 return 0;
606 if (S_ISDIR(st.st_mode)) {
608 * We are checking out path "foo" and
609 * found "foo/." in the working tree.
610 * This is tricky -- if we have modified
611 * files that are in "foo/" we would lose
612 * it.
614 cnt = verify_clean_subdirectory(ce, action, o);
617 * If this removed entries from the index,
618 * what that means is:
620 * (1) the caller unpack_trees_rec() saw path/foo
621 * in the index, and it has not removed it because
622 * it thinks it is handling 'path' as blob with
623 * D/F conflict;
624 * (2) we will return "ok, we placed a merged entry
625 * in the index" which would cause o->pos to be
626 * incremented by one;
627 * (3) however, original o->pos now has 'path/foo'
628 * marked with "to be removed".
630 * We need to increment it by the number of
631 * deleted entries here.
633 o->pos += cnt;
634 return 0;
638 * The previous round may already have decided to
639 * delete this path, which is in a subdirectory that
640 * is being replaced with a blob.
642 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
643 if (result) {
644 if (result->ce_flags & CE_REMOVE)
645 return 0;
648 return o->gently ? -1 :
649 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
651 return 0;
654 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
655 struct unpack_trees_options *o)
657 int update = CE_UPDATE;
659 if (old) {
661 * See if we can re-use the old CE directly?
662 * That way we get the uptodate stat info.
664 * This also removes the UPDATE flag on a match; otherwise
665 * we will end up overwriting local changes in the work tree.
667 if (same(old, merge)) {
668 copy_cache_entry(merge, old);
669 update = 0;
670 } else {
671 if (verify_uptodate(old, o))
672 return -1;
673 invalidate_ce_path(old, o);
676 else {
677 if (verify_absent(merge, "overwritten", o))
678 return -1;
679 invalidate_ce_path(merge, o);
682 add_entry(o, merge, update, CE_STAGEMASK);
683 return 1;
686 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
687 struct unpack_trees_options *o)
689 /* Did it exist in the index? */
690 if (!old) {
691 if (verify_absent(ce, "removed", o))
692 return -1;
693 return 0;
695 if (verify_uptodate(old, o))
696 return -1;
697 add_entry(o, ce, CE_REMOVE, 0);
698 invalidate_ce_path(ce, o);
699 return 1;
702 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
704 add_entry(o, ce, 0, 0);
705 return 1;
708 #if DBRT_DEBUG
709 static void show_stage_entry(FILE *o,
710 const char *label, const struct cache_entry *ce)
712 if (!ce)
713 fprintf(o, "%s (missing)\n", label);
714 else
715 fprintf(o, "%s%06o %s %d\t%s\n",
716 label,
717 ce->ce_mode,
718 sha1_to_hex(ce->sha1),
719 ce_stage(ce),
720 ce->name);
722 #endif
724 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
726 struct cache_entry *index;
727 struct cache_entry *head;
728 struct cache_entry *remote = stages[o->head_idx + 1];
729 int count;
730 int head_match = 0;
731 int remote_match = 0;
733 int df_conflict_head = 0;
734 int df_conflict_remote = 0;
736 int any_anc_missing = 0;
737 int no_anc_exists = 1;
738 int i;
740 for (i = 1; i < o->head_idx; i++) {
741 if (!stages[i] || stages[i] == o->df_conflict_entry)
742 any_anc_missing = 1;
743 else
744 no_anc_exists = 0;
747 index = stages[0];
748 head = stages[o->head_idx];
750 if (head == o->df_conflict_entry) {
751 df_conflict_head = 1;
752 head = NULL;
755 if (remote == o->df_conflict_entry) {
756 df_conflict_remote = 1;
757 remote = NULL;
760 /* First, if there's a #16 situation, note that to prevent #13
761 * and #14.
763 if (!same(remote, head)) {
764 for (i = 1; i < o->head_idx; i++) {
765 if (same(stages[i], head)) {
766 head_match = i;
768 if (same(stages[i], remote)) {
769 remote_match = i;
774 /* We start with cases where the index is allowed to match
775 * something other than the head: #14(ALT) and #2ALT, where it
776 * is permitted to match the result instead.
778 /* #14, #14ALT, #2ALT */
779 if (remote && !df_conflict_head && head_match && !remote_match) {
780 if (index && !same(index, remote) && !same(index, head))
781 return o->gently ? -1 : reject_merge(index, o);
782 return merged_entry(remote, index, o);
785 * If we have an entry in the index cache, then we want to
786 * make sure that it matches head.
788 if (index && !same(index, head))
789 return o->gently ? -1 : reject_merge(index, o);
791 if (head) {
792 /* #5ALT, #15 */
793 if (same(head, remote))
794 return merged_entry(head, index, o);
795 /* #13, #3ALT */
796 if (!df_conflict_remote && remote_match && !head_match)
797 return merged_entry(head, index, o);
800 /* #1 */
801 if (!head && !remote && any_anc_missing)
802 return 0;
804 /* Under the new "aggressive" rule, we resolve mostly trivial
805 * cases that we historically had git-merge-one-file resolve.
807 if (o->aggressive) {
808 int head_deleted = !head && !df_conflict_head;
809 int remote_deleted = !remote && !df_conflict_remote;
810 struct cache_entry *ce = NULL;
812 if (index)
813 ce = index;
814 else if (head)
815 ce = head;
816 else if (remote)
817 ce = remote;
818 else {
819 for (i = 1; i < o->head_idx; i++) {
820 if (stages[i] && stages[i] != o->df_conflict_entry) {
821 ce = stages[i];
822 break;
828 * Deleted in both.
829 * Deleted in one and unchanged in the other.
831 if ((head_deleted && remote_deleted) ||
832 (head_deleted && remote && remote_match) ||
833 (remote_deleted && head && head_match)) {
834 if (index)
835 return deleted_entry(index, index, o);
836 if (ce && !head_deleted) {
837 if (verify_absent(ce, "removed", o))
838 return -1;
840 return 0;
843 * Added in both, identically.
845 if (no_anc_exists && head && remote && same(head, remote))
846 return merged_entry(head, index, o);
850 /* Below are "no merge" cases, which require that the index be
851 * up-to-date to avoid the files getting overwritten with
852 * conflict resolution files.
854 if (index) {
855 if (verify_uptodate(index, o))
856 return -1;
859 o->nontrivial_merge = 1;
861 /* #2, #3, #4, #6, #7, #9, #10, #11. */
862 count = 0;
863 if (!head_match || !remote_match) {
864 for (i = 1; i < o->head_idx; i++) {
865 if (stages[i] && stages[i] != o->df_conflict_entry) {
866 keep_entry(stages[i], o);
867 count++;
868 break;
872 #if DBRT_DEBUG
873 else {
874 fprintf(stderr, "read-tree: warning #16 detected\n");
875 show_stage_entry(stderr, "head ", stages[head_match]);
876 show_stage_entry(stderr, "remote ", stages[remote_match]);
878 #endif
879 if (head) { count += keep_entry(head, o); }
880 if (remote) { count += keep_entry(remote, o); }
881 return count;
885 * Two-way merge.
887 * The rule is to "carry forward" what is in the index without losing
888 * information across a "fast forward", favoring a successful merge
889 * over a merge failure when it makes sense. For details of the
890 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
893 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
895 struct cache_entry *current = src[0];
896 struct cache_entry *oldtree = src[1];
897 struct cache_entry *newtree = src[2];
899 if (o->merge_size != 2)
900 return error("Cannot do a twoway merge of %d trees",
901 o->merge_size);
903 if (oldtree == o->df_conflict_entry)
904 oldtree = NULL;
905 if (newtree == o->df_conflict_entry)
906 newtree = NULL;
908 if (current) {
909 if ((!oldtree && !newtree) || /* 4 and 5 */
910 (!oldtree && newtree &&
911 same(current, newtree)) || /* 6 and 7 */
912 (oldtree && newtree &&
913 same(oldtree, newtree)) || /* 14 and 15 */
914 (oldtree && newtree &&
915 !same(oldtree, newtree) && /* 18 and 19 */
916 same(current, newtree))) {
917 return keep_entry(current, o);
919 else if (oldtree && !newtree && same(current, oldtree)) {
920 /* 10 or 11 */
921 return deleted_entry(oldtree, current, o);
923 else if (oldtree && newtree &&
924 same(current, oldtree) && !same(current, newtree)) {
925 /* 20 or 21 */
926 return merged_entry(newtree, current, o);
928 else {
929 /* all other failures */
930 if (oldtree)
931 return o->gently ? -1 : reject_merge(oldtree, o);
932 if (current)
933 return o->gently ? -1 : reject_merge(current, o);
934 if (newtree)
935 return o->gently ? -1 : reject_merge(newtree, o);
936 return -1;
939 else if (newtree)
940 return merged_entry(newtree, current, o);
941 return deleted_entry(oldtree, current, o);
945 * Bind merge.
947 * Keep the index entries at stage0, collapse stage1 but make sure
948 * stage0 does not have anything there.
950 int bind_merge(struct cache_entry **src,
951 struct unpack_trees_options *o)
953 struct cache_entry *old = src[0];
954 struct cache_entry *a = src[1];
956 if (o->merge_size != 1)
957 return error("Cannot do a bind merge of %d trees\n",
958 o->merge_size);
959 if (a && old)
960 return o->gently ? -1 :
961 error(ERRORMSG(o, bind_overlap), a->name, old->name);
962 if (!a)
963 return keep_entry(old, o);
964 else
965 return merged_entry(a, NULL, o);
969 * One-way merge.
971 * The rule is:
972 * - take the stat information from stage0, take the data from stage1
974 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
976 struct cache_entry *old = src[0];
977 struct cache_entry *a = src[1];
979 if (o->merge_size != 1)
980 return error("Cannot do a oneway merge of %d trees",
981 o->merge_size);
983 if (!a)
984 return deleted_entry(old, old, o);
986 if (old && same(old, a)) {
987 int update = 0;
988 if (o->reset) {
989 struct stat st;
990 if (lstat(old->name, &st) ||
991 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
992 update |= CE_UPDATE;
994 add_entry(o, old, update, 0);
995 return 0;
997 return merged_entry(a, old, o);