doc/git-daemon: add missing arguments to options
[git/jrn.git] / unpack-trees.c
blob4229eec1235842cf3432e4d5707b0e75845708b1
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,
244 unsigned long dirmask,
245 struct cache_entry **src,
246 const struct name_entry *names,
247 const struct traverse_info *info)
249 int i;
250 struct unpack_trees_options *o = info->data;
251 unsigned long conflicts;
253 /* Do we have *only* directories? Nothing to do */
254 if (mask == dirmask && !src[0])
255 return 0;
257 conflicts = info->conflicts;
258 if (o->merge)
259 conflicts >>= 1;
260 conflicts |= dirmask;
263 * Ok, we've filled in up to any potential index entry in src[0],
264 * now do the rest.
266 for (i = 0; i < n; i++) {
267 int stage;
268 unsigned int bit = 1ul << i;
269 if (conflicts & bit) {
270 src[i + o->merge] = o->df_conflict_entry;
271 continue;
273 if (!(mask & bit))
274 continue;
275 if (!o->merge)
276 stage = 0;
277 else if (i + 1 < o->head_idx)
278 stage = 1;
279 else if (i + 1 > o->head_idx)
280 stage = 3;
281 else
282 stage = 2;
283 src[i + o->merge] = create_ce_entry(info, names + i, stage);
286 if (o->merge)
287 return call_unpack_fn(src, o);
289 n += o->merge;
290 for (i = 0; i < n; i++)
291 add_entry(o, src[i], 0, 0);
292 return 0;
295 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
297 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
298 struct unpack_trees_options *o = info->data;
299 const struct name_entry *p = names;
301 /* Find first entry with a real name (we could use "mask" too) */
302 while (!p->mode)
303 p++;
305 /* Are we supposed to look at the index too? */
306 if (o->merge) {
307 while (o->pos < o->src_index->cache_nr) {
308 struct cache_entry *ce = o->src_index->cache[o->pos];
309 int cmp = compare_entry(ce, info, p);
310 if (cmp < 0) {
311 if (unpack_index_entry(ce, o) < 0)
312 return -1;
313 continue;
315 if (!cmp) {
316 o->pos++;
317 if (ce_stage(ce)) {
319 * If we skip unmerged index entries, we'll skip this
320 * entry *and* the tree entries associated with it!
322 if (o->skip_unmerged) {
323 add_entry(o, ce, 0, 0);
324 return mask;
327 src[0] = ce;
329 break;
333 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
334 return -1;
336 /* Now handle any directories.. */
337 if (dirmask) {
338 unsigned long conflicts = mask & ~dirmask;
339 if (o->merge) {
340 conflicts <<= 1;
341 if (src[0])
342 conflicts |= 1;
344 if (traverse_trees_recursive(n, dirmask, conflicts,
345 names, info) < 0)
346 return -1;
347 return mask;
350 return mask;
353 static int unpack_failed(struct unpack_trees_options *o, const char *message)
355 discard_index(&o->result);
356 if (!o->gently) {
357 if (message)
358 return error("%s", message);
359 return -1;
361 return -1;
365 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
366 * resulting index, -2 on failure to reflect the changes to the work tree.
368 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
370 int ret;
371 static struct cache_entry *dfc;
373 if (len > MAX_UNPACK_TREES)
374 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
375 memset(&state, 0, sizeof(state));
376 state.base_dir = "";
377 state.force = 1;
378 state.quiet = 1;
379 state.refresh_cache = 1;
381 memset(&o->result, 0, sizeof(o->result));
382 o->result.initialized = 1;
383 if (o->src_index)
384 o->result.timestamp = o->src_index->timestamp;
385 o->merge_size = len;
387 if (!dfc)
388 dfc = xcalloc(1, cache_entry_size(0));
389 o->df_conflict_entry = dfc;
391 if (len) {
392 const char *prefix = o->prefix ? o->prefix : "";
393 struct traverse_info info;
395 setup_traverse_info(&info, prefix);
396 info.fn = unpack_callback;
397 info.data = o;
399 if (traverse_trees(len, t, &info) < 0)
400 return unpack_failed(o, NULL);
403 /* Any left-over entries in the index? */
404 if (o->merge) {
405 while (o->pos < o->src_index->cache_nr) {
406 struct cache_entry *ce = o->src_index->cache[o->pos];
407 if (unpack_index_entry(ce, o) < 0)
408 return unpack_failed(o, NULL);
412 if (o->trivial_merges_only && o->nontrivial_merge)
413 return unpack_failed(o, "Merge requires file-level merging");
415 o->src_index = NULL;
416 ret = check_updates(o) ? (-2) : 0;
417 if (o->dst_index)
418 *o->dst_index = o->result;
419 return ret;
422 /* Here come the merge functions */
424 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
426 return error(ERRORMSG(o, would_overwrite), ce->name);
429 static int same(struct cache_entry *a, struct cache_entry *b)
431 if (!!a != !!b)
432 return 0;
433 if (!a && !b)
434 return 1;
435 return a->ce_mode == b->ce_mode &&
436 !hashcmp(a->sha1, b->sha1);
441 * When a CE gets turned into an unmerged entry, we
442 * want it to be up-to-date
444 static int verify_uptodate(struct cache_entry *ce,
445 struct unpack_trees_options *o)
447 struct stat st;
449 if (o->index_only || o->reset)
450 return 0;
452 if (!lstat(ce->name, &st)) {
453 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
454 if (!changed)
455 return 0;
457 * NEEDSWORK: the current default policy is to allow
458 * submodule to be out of sync wrt the supermodule
459 * index. This needs to be tightened later for
460 * submodules that are marked to be automatically
461 * checked out.
463 if (S_ISGITLINK(ce->ce_mode))
464 return 0;
465 errno = 0;
467 if (errno == ENOENT)
468 return 0;
469 return o->gently ? -1 :
470 error(ERRORMSG(o, not_uptodate_file), ce->name);
473 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
475 if (ce)
476 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
480 * Check that checking out ce->sha1 in subdir ce->name is not
481 * going to overwrite any working files.
483 * Currently, git does not checkout subprojects during a superproject
484 * checkout, so it is not going to overwrite anything.
486 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
487 struct unpack_trees_options *o)
489 return 0;
492 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
493 struct unpack_trees_options *o)
496 * we are about to extract "ce->name"; we would not want to lose
497 * anything in the existing directory there.
499 int namelen;
500 int pos, i;
501 struct dir_struct d;
502 char *pathbuf;
503 int cnt = 0;
504 unsigned char sha1[20];
506 if (S_ISGITLINK(ce->ce_mode) &&
507 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
508 /* If we are not going to update the submodule, then
509 * we don't care.
511 if (!hashcmp(sha1, ce->sha1))
512 return 0;
513 return verify_clean_submodule(ce, action, o);
517 * First let's make sure we do not have a local modification
518 * in that directory.
520 namelen = strlen(ce->name);
521 pos = index_name_pos(o->src_index, ce->name, namelen);
522 if (0 <= pos)
523 return cnt; /* we have it as nondirectory */
524 pos = -pos - 1;
525 for (i = pos; i < o->src_index->cache_nr; i++) {
526 struct cache_entry *ce = o->src_index->cache[i];
527 int len = ce_namelen(ce);
528 if (len < namelen ||
529 strncmp(ce->name, ce->name, namelen) ||
530 ce->name[namelen] != '/')
531 break;
533 * ce->name is an entry in the subdirectory.
535 if (!ce_stage(ce)) {
536 if (verify_uptodate(ce, o))
537 return -1;
538 add_entry(o, ce, CE_REMOVE, 0);
540 cnt++;
544 * Then we need to make sure that we do not lose a locally
545 * present file that is not ignored.
547 pathbuf = xmalloc(namelen + 2);
548 memcpy(pathbuf, ce->name, namelen);
549 strcpy(pathbuf+namelen, "/");
551 memset(&d, 0, sizeof(d));
552 if (o->dir)
553 d.exclude_per_dir = o->dir->exclude_per_dir;
554 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
555 if (i)
556 return o->gently ? -1 :
557 error(ERRORMSG(o, not_uptodate_dir), ce->name);
558 free(pathbuf);
559 return cnt;
563 * This gets called when there was no index entry for the tree entry 'dst',
564 * but we found a file in the working tree that 'lstat()' said was fine,
565 * and we're on a case-insensitive filesystem.
567 * See if we can find a case-insensitive match in the index that also
568 * matches the stat information, and assume it's that other file!
570 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
572 struct cache_entry *src;
574 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
575 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
579 * We do not want to remove or overwrite a working tree file that
580 * is not tracked, unless it is ignored.
582 static int verify_absent(struct cache_entry *ce, const char *action,
583 struct unpack_trees_options *o)
585 struct stat st;
587 if (o->index_only || o->reset || !o->update)
588 return 0;
590 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
591 return 0;
593 if (!lstat(ce->name, &st)) {
594 int cnt;
595 int dtype = ce_to_dtype(ce);
596 struct cache_entry *result;
599 * It may be that the 'lstat()' succeeded even though
600 * target 'ce' was absent, because there is an old
601 * entry that is different only in case..
603 * Ignore that lstat() if it matches.
605 if (ignore_case && icase_exists(o, ce, &st))
606 return 0;
608 if (o->dir && excluded(o->dir, ce->name, &dtype))
610 * ce->name is explicitly excluded, so it is Ok to
611 * overwrite it.
613 return 0;
614 if (S_ISDIR(st.st_mode)) {
616 * We are checking out path "foo" and
617 * found "foo/." in the working tree.
618 * This is tricky -- if we have modified
619 * files that are in "foo/" we would lose
620 * it.
622 cnt = verify_clean_subdirectory(ce, action, o);
625 * If this removed entries from the index,
626 * what that means is:
628 * (1) the caller unpack_trees_rec() saw path/foo
629 * in the index, and it has not removed it because
630 * it thinks it is handling 'path' as blob with
631 * D/F conflict;
632 * (2) we will return "ok, we placed a merged entry
633 * in the index" which would cause o->pos to be
634 * incremented by one;
635 * (3) however, original o->pos now has 'path/foo'
636 * marked with "to be removed".
638 * We need to increment it by the number of
639 * deleted entries here.
641 o->pos += cnt;
642 return 0;
646 * The previous round may already have decided to
647 * delete this path, which is in a subdirectory that
648 * is being replaced with a blob.
650 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
651 if (result) {
652 if (result->ce_flags & CE_REMOVE)
653 return 0;
656 return o->gently ? -1 :
657 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
659 return 0;
662 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
663 struct unpack_trees_options *o)
665 int update = CE_UPDATE;
667 if (old) {
669 * See if we can re-use the old CE directly?
670 * That way we get the uptodate stat info.
672 * This also removes the UPDATE flag on a match; otherwise
673 * we will end up overwriting local changes in the work tree.
675 if (same(old, merge)) {
676 copy_cache_entry(merge, old);
677 update = 0;
678 } else {
679 if (verify_uptodate(old, o))
680 return -1;
681 invalidate_ce_path(old, o);
684 else {
685 if (verify_absent(merge, "overwritten", o))
686 return -1;
687 invalidate_ce_path(merge, o);
690 add_entry(o, merge, update, CE_STAGEMASK);
691 return 1;
694 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
695 struct unpack_trees_options *o)
697 /* Did it exist in the index? */
698 if (!old) {
699 if (verify_absent(ce, "removed", o))
700 return -1;
701 return 0;
703 if (verify_uptodate(old, o))
704 return -1;
705 add_entry(o, ce, CE_REMOVE, 0);
706 invalidate_ce_path(ce, o);
707 return 1;
710 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
712 add_entry(o, ce, 0, 0);
713 return 1;
716 #if DBRT_DEBUG
717 static void show_stage_entry(FILE *o,
718 const char *label, const struct cache_entry *ce)
720 if (!ce)
721 fprintf(o, "%s (missing)\n", label);
722 else
723 fprintf(o, "%s%06o %s %d\t%s\n",
724 label,
725 ce->ce_mode,
726 sha1_to_hex(ce->sha1),
727 ce_stage(ce),
728 ce->name);
730 #endif
732 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
734 struct cache_entry *index;
735 struct cache_entry *head;
736 struct cache_entry *remote = stages[o->head_idx + 1];
737 int count;
738 int head_match = 0;
739 int remote_match = 0;
741 int df_conflict_head = 0;
742 int df_conflict_remote = 0;
744 int any_anc_missing = 0;
745 int no_anc_exists = 1;
746 int i;
748 for (i = 1; i < o->head_idx; i++) {
749 if (!stages[i] || stages[i] == o->df_conflict_entry)
750 any_anc_missing = 1;
751 else
752 no_anc_exists = 0;
755 index = stages[0];
756 head = stages[o->head_idx];
758 if (head == o->df_conflict_entry) {
759 df_conflict_head = 1;
760 head = NULL;
763 if (remote == o->df_conflict_entry) {
764 df_conflict_remote = 1;
765 remote = NULL;
768 /* First, if there's a #16 situation, note that to prevent #13
769 * and #14.
771 if (!same(remote, head)) {
772 for (i = 1; i < o->head_idx; i++) {
773 if (same(stages[i], head)) {
774 head_match = i;
776 if (same(stages[i], remote)) {
777 remote_match = i;
782 /* We start with cases where the index is allowed to match
783 * something other than the head: #14(ALT) and #2ALT, where it
784 * is permitted to match the result instead.
786 /* #14, #14ALT, #2ALT */
787 if (remote && !df_conflict_head && head_match && !remote_match) {
788 if (index && !same(index, remote) && !same(index, head))
789 return o->gently ? -1 : reject_merge(index, o);
790 return merged_entry(remote, index, o);
793 * If we have an entry in the index cache, then we want to
794 * make sure that it matches head.
796 if (index && !same(index, head))
797 return o->gently ? -1 : reject_merge(index, o);
799 if (head) {
800 /* #5ALT, #15 */
801 if (same(head, remote))
802 return merged_entry(head, index, o);
803 /* #13, #3ALT */
804 if (!df_conflict_remote && remote_match && !head_match)
805 return merged_entry(head, index, o);
808 /* #1 */
809 if (!head && !remote && any_anc_missing)
810 return 0;
812 /* Under the new "aggressive" rule, we resolve mostly trivial
813 * cases that we historically had git-merge-one-file resolve.
815 if (o->aggressive) {
816 int head_deleted = !head && !df_conflict_head;
817 int remote_deleted = !remote && !df_conflict_remote;
818 struct cache_entry *ce = NULL;
820 if (index)
821 ce = index;
822 else if (head)
823 ce = head;
824 else if (remote)
825 ce = remote;
826 else {
827 for (i = 1; i < o->head_idx; i++) {
828 if (stages[i] && stages[i] != o->df_conflict_entry) {
829 ce = stages[i];
830 break;
836 * Deleted in both.
837 * Deleted in one and unchanged in the other.
839 if ((head_deleted && remote_deleted) ||
840 (head_deleted && remote && remote_match) ||
841 (remote_deleted && head && head_match)) {
842 if (index)
843 return deleted_entry(index, index, o);
844 if (ce && !head_deleted) {
845 if (verify_absent(ce, "removed", o))
846 return -1;
848 return 0;
851 * Added in both, identically.
853 if (no_anc_exists && head && remote && same(head, remote))
854 return merged_entry(head, index, o);
858 /* Below are "no merge" cases, which require that the index be
859 * up-to-date to avoid the files getting overwritten with
860 * conflict resolution files.
862 if (index) {
863 if (verify_uptodate(index, o))
864 return -1;
867 o->nontrivial_merge = 1;
869 /* #2, #3, #4, #6, #7, #9, #10, #11. */
870 count = 0;
871 if (!head_match || !remote_match) {
872 for (i = 1; i < o->head_idx; i++) {
873 if (stages[i] && stages[i] != o->df_conflict_entry) {
874 keep_entry(stages[i], o);
875 count++;
876 break;
880 #if DBRT_DEBUG
881 else {
882 fprintf(stderr, "read-tree: warning #16 detected\n");
883 show_stage_entry(stderr, "head ", stages[head_match]);
884 show_stage_entry(stderr, "remote ", stages[remote_match]);
886 #endif
887 if (head) { count += keep_entry(head, o); }
888 if (remote) { count += keep_entry(remote, o); }
889 return count;
893 * Two-way merge.
895 * The rule is to "carry forward" what is in the index without losing
896 * information across a "fast forward", favoring a successful merge
897 * over a merge failure when it makes sense. For details of the
898 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
901 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
903 struct cache_entry *current = src[0];
904 struct cache_entry *oldtree = src[1];
905 struct cache_entry *newtree = src[2];
907 if (o->merge_size != 2)
908 return error("Cannot do a twoway merge of %d trees",
909 o->merge_size);
911 if (oldtree == o->df_conflict_entry)
912 oldtree = NULL;
913 if (newtree == o->df_conflict_entry)
914 newtree = NULL;
916 if (current) {
917 if ((!oldtree && !newtree) || /* 4 and 5 */
918 (!oldtree && newtree &&
919 same(current, newtree)) || /* 6 and 7 */
920 (oldtree && newtree &&
921 same(oldtree, newtree)) || /* 14 and 15 */
922 (oldtree && newtree &&
923 !same(oldtree, newtree) && /* 18 and 19 */
924 same(current, newtree))) {
925 return keep_entry(current, o);
927 else if (oldtree && !newtree && same(current, oldtree)) {
928 /* 10 or 11 */
929 return deleted_entry(oldtree, current, o);
931 else if (oldtree && newtree &&
932 same(current, oldtree) && !same(current, newtree)) {
933 /* 20 or 21 */
934 return merged_entry(newtree, current, o);
936 else {
937 /* all other failures */
938 if (oldtree)
939 return o->gently ? -1 : reject_merge(oldtree, o);
940 if (current)
941 return o->gently ? -1 : reject_merge(current, o);
942 if (newtree)
943 return o->gently ? -1 : reject_merge(newtree, o);
944 return -1;
947 else if (newtree) {
948 if (oldtree && !o->initial_checkout) {
950 * deletion of the path was staged;
952 if (same(oldtree, newtree))
953 return 1;
954 return reject_merge(oldtree, o);
956 return merged_entry(newtree, current, o);
958 return deleted_entry(oldtree, current, o);
962 * Bind merge.
964 * Keep the index entries at stage0, collapse stage1 but make sure
965 * stage0 does not have anything there.
967 int bind_merge(struct cache_entry **src,
968 struct unpack_trees_options *o)
970 struct cache_entry *old = src[0];
971 struct cache_entry *a = src[1];
973 if (o->merge_size != 1)
974 return error("Cannot do a bind merge of %d trees\n",
975 o->merge_size);
976 if (a && old)
977 return o->gently ? -1 :
978 error(ERRORMSG(o, bind_overlap), a->name, old->name);
979 if (!a)
980 return keep_entry(old, o);
981 else
982 return merged_entry(a, NULL, o);
986 * One-way merge.
988 * The rule is:
989 * - take the stat information from stage0, take the data from stage1
991 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
993 struct cache_entry *old = src[0];
994 struct cache_entry *a = src[1];
996 if (o->merge_size != 1)
997 return error("Cannot do a oneway merge of %d trees",
998 o->merge_size);
1000 if (!a)
1001 return deleted_entry(old, old, o);
1003 if (old && same(old, a)) {
1004 int update = 0;
1005 if (o->reset) {
1006 struct stat st;
1007 if (lstat(old->name, &st) ||
1008 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
1009 update |= CE_UPDATE;
1011 add_entry(o, old, update, 0);
1012 return 0;
1014 return merged_entry(a, old, o);