commit --amend: copy notes to the new commit
[git.git] / unpack-trees.c
blob0ddbef3e6355da40a1293a6a7c49b4bc5d75b842
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"
10 #include "attr.h"
13 * Error messages expected by scripts out of plumbing commands such as
14 * read-tree. Non-scripted Porcelain is not required to use these messages
15 * and in fact are encouraged to reword them to better suit their particular
16 * situation better. See how "git checkout" replaces not_uptodate_file to
17 * explain why it does not allow switching between branches when you have
18 * local changes, for example.
20 static struct unpack_trees_error_msgs unpack_plumbing_errors = {
21 /* would_overwrite */
22 "Entry '%s' would be overwritten by merge. Cannot merge.",
24 /* not_uptodate_file */
25 "Entry '%s' not uptodate. Cannot merge.",
27 /* not_uptodate_dir */
28 "Updating '%s' would lose untracked files in it",
30 /* would_lose_untracked */
31 "Untracked working tree file '%s' would be %s by merge.",
33 /* bind_overlap */
34 "Entry '%s' overlaps with '%s'. Cannot bind.",
36 /* sparse_not_uptodate_file */
37 "Entry '%s' not uptodate. Cannot update sparse checkout.",
39 /* would_lose_orphaned */
40 "Working tree file '%s' would be %s by sparse checkout update.",
43 #define ERRORMSG(o,fld) \
44 ( ((o) && (o)->msgs.fld) \
45 ? ((o)->msgs.fld) \
46 : (unpack_plumbing_errors.fld) )
48 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
49 unsigned int set, unsigned int clear)
51 unsigned int size = ce_size(ce);
52 struct cache_entry *new = xmalloc(size);
54 clear |= CE_HASHED | CE_UNHASHED;
56 memcpy(new, ce, size);
57 new->next = NULL;
58 new->ce_flags = (new->ce_flags & ~clear) | set;
59 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
63 * Unlink the last component and schedule the leading directories for
64 * removal, such that empty directories get removed.
66 static void unlink_entry(struct cache_entry *ce)
68 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
69 return;
70 if (S_ISGITLINK(ce->ce_mode)) {
71 if (rmdir(ce->name)) {
72 warning("unable to rmdir %s: %s",
73 ce->name, strerror(errno));
74 return;
77 else
78 if (unlink_or_warn(ce->name))
79 return;
80 schedule_dir_for_removal(ce->name, ce_namelen(ce));
83 static struct checkout state;
84 static int check_updates(struct unpack_trees_options *o)
86 unsigned cnt = 0, total = 0;
87 struct progress *progress = NULL;
88 struct index_state *index = &o->result;
89 int i;
90 int errs = 0;
92 if (o->update && o->verbose_update) {
93 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
94 struct cache_entry *ce = index->cache[cnt];
95 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE | CE_WT_REMOVE))
96 total++;
99 progress = start_progress_delay("Checking out files",
100 total, 50, 1);
101 cnt = 0;
104 if (o->update)
105 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
106 for (i = 0; i < index->cache_nr; i++) {
107 struct cache_entry *ce = index->cache[i];
109 if (ce->ce_flags & CE_WT_REMOVE) {
110 display_progress(progress, ++cnt);
111 if (o->update)
112 unlink_entry(ce);
113 continue;
116 if (ce->ce_flags & CE_REMOVE) {
117 display_progress(progress, ++cnt);
118 if (o->update)
119 unlink_entry(ce);
122 remove_marked_cache_entries(&o->result);
123 remove_scheduled_dirs();
125 for (i = 0; i < index->cache_nr; i++) {
126 struct cache_entry *ce = index->cache[i];
128 if (ce->ce_flags & CE_UPDATE) {
129 display_progress(progress, ++cnt);
130 ce->ce_flags &= ~CE_UPDATE;
131 if (o->update) {
132 errs |= checkout_entry(ce, &state, NULL);
136 stop_progress(&progress);
137 if (o->update)
138 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
139 return errs != 0;
142 static int verify_uptodate_sparse(struct cache_entry *ce, struct unpack_trees_options *o);
143 static int verify_absent_sparse(struct cache_entry *ce, const char *action, struct unpack_trees_options *o);
145 static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_trees_options *o)
147 const char *basename;
149 if (ce_stage(ce))
150 return 0;
152 basename = strrchr(ce->name, '/');
153 basename = basename ? basename+1 : ce->name;
154 return excluded_from_list(ce->name, ce_namelen(ce), basename, NULL, o->el) <= 0;
157 static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_options *o)
159 int was_skip_worktree = ce_skip_worktree(ce);
161 if (will_have_skip_worktree(ce, o))
162 ce->ce_flags |= CE_SKIP_WORKTREE;
163 else
164 ce->ce_flags &= ~CE_SKIP_WORKTREE;
167 * We only care about files getting into the checkout area
168 * If merge strategies want to remove some, go ahead, this
169 * flag will be removed eventually in unpack_trees() if it's
170 * outside checkout area.
172 if (ce->ce_flags & CE_REMOVE)
173 return 0;
175 if (!was_skip_worktree && ce_skip_worktree(ce)) {
177 * If CE_UPDATE is set, verify_uptodate() must be called already
178 * also stat info may have lost after merged_entry() so calling
179 * verify_uptodate() again may fail
181 if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
182 return -1;
183 ce->ce_flags |= CE_WT_REMOVE;
185 if (was_skip_worktree && !ce_skip_worktree(ce)) {
186 if (verify_absent_sparse(ce, "overwritten", o))
187 return -1;
188 ce->ce_flags |= CE_UPDATE;
190 return 0;
193 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
195 int ret = o->fn(src, o);
196 if (ret > 0)
197 ret = 0;
198 return ret;
201 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
203 struct cache_entry *src[5] = { ce, NULL, };
205 o->pos++;
206 if (ce_stage(ce)) {
207 if (o->skip_unmerged) {
208 add_entry(o, ce, 0, 0);
209 return 0;
212 return call_unpack_fn(src, o);
215 static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
217 int i;
218 struct tree_desc t[MAX_UNPACK_TREES];
219 struct traverse_info newinfo;
220 struct name_entry *p;
222 p = names;
223 while (!p->mode)
224 p++;
226 newinfo = *info;
227 newinfo.prev = info;
228 newinfo.name = *p;
229 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
230 newinfo.conflicts |= df_conflicts;
232 for (i = 0; i < n; i++, dirmask >>= 1) {
233 const unsigned char *sha1 = NULL;
234 if (dirmask & 1)
235 sha1 = names[i].sha1;
236 fill_tree_descriptor(t+i, sha1);
238 return traverse_trees(n, t, &newinfo);
242 * Compare the traverse-path to the cache entry without actually
243 * having to generate the textual representation of the traverse
244 * path.
246 * NOTE! This *only* compares up to the size of the traverse path
247 * itself - the caller needs to do the final check for the cache
248 * entry having more data at the end!
250 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
252 int len, pathlen, ce_len;
253 const char *ce_name;
255 if (info->prev) {
256 int cmp = do_compare_entry(ce, info->prev, &info->name);
257 if (cmp)
258 return cmp;
260 pathlen = info->pathlen;
261 ce_len = ce_namelen(ce);
263 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
264 if (ce_len < pathlen)
265 return -1;
267 ce_len -= pathlen;
268 ce_name = ce->name + pathlen;
270 len = tree_entry_len(n->path, n->sha1);
271 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
274 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
276 int cmp = do_compare_entry(ce, info, n);
277 if (cmp)
278 return cmp;
281 * Even if the beginning compared identically, the ce should
282 * compare as bigger than a directory leading up to it!
284 return ce_namelen(ce) > traverse_path_len(info, n);
287 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
289 int len = traverse_path_len(info, n);
290 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
292 ce->ce_mode = create_ce_mode(n->mode);
293 ce->ce_flags = create_ce_flags(len, stage);
294 hashcpy(ce->sha1, n->sha1);
295 make_traverse_path(ce->name, info, n);
297 return ce;
300 static int unpack_nondirectories(int n, unsigned long mask,
301 unsigned long dirmask,
302 struct cache_entry **src,
303 const struct name_entry *names,
304 const struct traverse_info *info)
306 int i;
307 struct unpack_trees_options *o = info->data;
308 unsigned long conflicts;
310 /* Do we have *only* directories? Nothing to do */
311 if (mask == dirmask && !src[0])
312 return 0;
314 conflicts = info->conflicts;
315 if (o->merge)
316 conflicts >>= 1;
317 conflicts |= dirmask;
320 * Ok, we've filled in up to any potential index entry in src[0],
321 * now do the rest.
323 for (i = 0; i < n; i++) {
324 int stage;
325 unsigned int bit = 1ul << i;
326 if (conflicts & bit) {
327 src[i + o->merge] = o->df_conflict_entry;
328 continue;
330 if (!(mask & bit))
331 continue;
332 if (!o->merge)
333 stage = 0;
334 else if (i + 1 < o->head_idx)
335 stage = 1;
336 else if (i + 1 > o->head_idx)
337 stage = 3;
338 else
339 stage = 2;
340 src[i + o->merge] = create_ce_entry(info, names + i, stage);
343 if (o->merge)
344 return call_unpack_fn(src, o);
346 for (i = 0; i < n; i++)
347 if (src[i] && src[i] != o->df_conflict_entry)
348 add_entry(o, src[i], 0, 0);
349 return 0;
352 static int unpack_failed(struct unpack_trees_options *o, const char *message)
354 discard_index(&o->result);
355 if (!o->gently) {
356 if (message)
357 return error("%s", message);
358 return -1;
360 return -1;
363 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
365 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
366 struct unpack_trees_options *o = info->data;
367 const struct name_entry *p = names;
369 /* Find first entry with a real name (we could use "mask" too) */
370 while (!p->mode)
371 p++;
373 /* Are we supposed to look at the index too? */
374 if (o->merge) {
375 while (o->pos < o->src_index->cache_nr) {
376 struct cache_entry *ce = o->src_index->cache[o->pos];
377 int cmp = compare_entry(ce, info, p);
378 if (cmp < 0) {
379 if (unpack_index_entry(ce, o) < 0)
380 return unpack_failed(o, NULL);
381 continue;
383 if (!cmp) {
384 o->pos++;
385 if (ce_stage(ce)) {
387 * If we skip unmerged index entries, we'll skip this
388 * entry *and* the tree entries associated with it!
390 if (o->skip_unmerged) {
391 add_entry(o, ce, 0, 0);
392 return mask;
395 src[0] = ce;
397 break;
401 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
402 return -1;
404 /* Now handle any directories.. */
405 if (dirmask) {
406 unsigned long conflicts = mask & ~dirmask;
407 if (o->merge) {
408 conflicts <<= 1;
409 if (src[0])
410 conflicts |= 1;
413 /* special case: "diff-index --cached" looking at a tree */
414 if (o->diff_index_cached &&
415 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
416 int matches;
417 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
418 names, info);
420 * Everything under the name matches. Adjust o->pos to
421 * skip the entire hierarchy.
423 if (matches) {
424 o->pos += matches;
425 return mask;
429 if (traverse_trees_recursive(n, dirmask, conflicts,
430 names, info) < 0)
431 return -1;
432 return mask;
435 return mask;
439 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
440 * resulting index, -2 on failure to reflect the changes to the work tree.
442 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
444 int i, ret;
445 static struct cache_entry *dfc;
446 struct exclude_list el;
448 if (len > MAX_UNPACK_TREES)
449 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
450 memset(&state, 0, sizeof(state));
451 state.base_dir = "";
452 state.force = 1;
453 state.quiet = 1;
454 state.refresh_cache = 1;
456 memset(&el, 0, sizeof(el));
457 if (!core_apply_sparse_checkout || !o->update)
458 o->skip_sparse_checkout = 1;
459 if (!o->skip_sparse_checkout) {
460 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0)
461 o->skip_sparse_checkout = 1;
462 else
463 o->el = &el;
466 memset(&o->result, 0, sizeof(o->result));
467 o->result.initialized = 1;
468 if (o->src_index) {
469 o->result.timestamp.sec = o->src_index->timestamp.sec;
470 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
472 o->merge_size = len;
474 if (!dfc)
475 dfc = xcalloc(1, cache_entry_size(0));
476 o->df_conflict_entry = dfc;
478 if (len) {
479 const char *prefix = o->prefix ? o->prefix : "";
480 struct traverse_info info;
482 setup_traverse_info(&info, prefix);
483 info.fn = unpack_callback;
484 info.data = o;
486 if (traverse_trees(len, t, &info) < 0) {
487 ret = unpack_failed(o, NULL);
488 goto done;
492 /* Any left-over entries in the index? */
493 if (o->merge) {
494 while (o->pos < o->src_index->cache_nr) {
495 struct cache_entry *ce = o->src_index->cache[o->pos];
496 if (unpack_index_entry(ce, o) < 0) {
497 ret = unpack_failed(o, NULL);
498 goto done;
503 if (o->trivial_merges_only && o->nontrivial_merge) {
504 ret = unpack_failed(o, "Merge requires file-level merging");
505 goto done;
508 if (!o->skip_sparse_checkout) {
509 int empty_worktree = 1;
510 for (i = 0;i < o->result.cache_nr;i++) {
511 struct cache_entry *ce = o->result.cache[i];
513 if (apply_sparse_checkout(ce, o)) {
514 ret = -1;
515 goto done;
518 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
519 * area as a result of ce_skip_worktree() shortcuts in
520 * verify_absent() and verify_uptodate(). Clear them.
522 if (ce_skip_worktree(ce))
523 ce->ce_flags &= ~(CE_UPDATE | CE_REMOVE);
524 else
525 empty_worktree = 0;
528 if (o->result.cache_nr && empty_worktree) {
529 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
530 goto done;
534 o->src_index = NULL;
535 ret = check_updates(o) ? (-2) : 0;
536 if (o->dst_index)
537 *o->dst_index = o->result;
539 done:
540 for (i = 0;i < el.nr;i++)
541 free(el.excludes[i]);
542 if (el.excludes)
543 free(el.excludes);
545 return ret;
548 /* Here come the merge functions */
550 static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
552 return error(ERRORMSG(o, would_overwrite), ce->name);
555 static int same(struct cache_entry *a, struct cache_entry *b)
557 if (!!a != !!b)
558 return 0;
559 if (!a && !b)
560 return 1;
561 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
562 return 0;
563 return a->ce_mode == b->ce_mode &&
564 !hashcmp(a->sha1, b->sha1);
569 * When a CE gets turned into an unmerged entry, we
570 * want it to be up-to-date
572 static int verify_uptodate_1(struct cache_entry *ce,
573 struct unpack_trees_options *o,
574 const char *error_msg)
576 struct stat st;
578 if (o->index_only || (!ce_skip_worktree(ce) && (o->reset || ce_uptodate(ce))))
579 return 0;
581 if (!lstat(ce->name, &st)) {
582 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
583 if (!changed)
584 return 0;
586 * NEEDSWORK: the current default policy is to allow
587 * submodule to be out of sync wrt the supermodule
588 * index. This needs to be tightened later for
589 * submodules that are marked to be automatically
590 * checked out.
592 if (S_ISGITLINK(ce->ce_mode))
593 return 0;
594 errno = 0;
596 if (errno == ENOENT)
597 return 0;
598 return o->gently ? -1 :
599 error(error_msg, ce->name);
602 static int verify_uptodate(struct cache_entry *ce,
603 struct unpack_trees_options *o)
605 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
606 return 0;
607 return verify_uptodate_1(ce, o, ERRORMSG(o, not_uptodate_file));
610 static int verify_uptodate_sparse(struct cache_entry *ce,
611 struct unpack_trees_options *o)
613 return verify_uptodate_1(ce, o, ERRORMSG(o, sparse_not_uptodate_file));
616 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
618 if (ce)
619 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
623 * Check that checking out ce->sha1 in subdir ce->name is not
624 * going to overwrite any working files.
626 * Currently, git does not checkout subprojects during a superproject
627 * checkout, so it is not going to overwrite anything.
629 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
630 struct unpack_trees_options *o)
632 return 0;
635 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
636 struct unpack_trees_options *o)
639 * we are about to extract "ce->name"; we would not want to lose
640 * anything in the existing directory there.
642 int namelen;
643 int i;
644 struct dir_struct d;
645 char *pathbuf;
646 int cnt = 0;
647 unsigned char sha1[20];
649 if (S_ISGITLINK(ce->ce_mode) &&
650 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
651 /* If we are not going to update the submodule, then
652 * we don't care.
654 if (!hashcmp(sha1, ce->sha1))
655 return 0;
656 return verify_clean_submodule(ce, action, o);
660 * First let's make sure we do not have a local modification
661 * in that directory.
663 namelen = strlen(ce->name);
664 for (i = o->pos; i < o->src_index->cache_nr; i++) {
665 struct cache_entry *ce2 = o->src_index->cache[i];
666 int len = ce_namelen(ce2);
667 if (len < namelen ||
668 strncmp(ce->name, ce2->name, namelen) ||
669 ce2->name[namelen] != '/')
670 break;
672 * ce2->name is an entry in the subdirectory.
674 if (!ce_stage(ce2)) {
675 if (verify_uptodate(ce2, o))
676 return -1;
677 add_entry(o, ce2, CE_REMOVE, 0);
679 cnt++;
683 * Then we need to make sure that we do not lose a locally
684 * present file that is not ignored.
686 pathbuf = xmalloc(namelen + 2);
687 memcpy(pathbuf, ce->name, namelen);
688 strcpy(pathbuf+namelen, "/");
690 memset(&d, 0, sizeof(d));
691 if (o->dir)
692 d.exclude_per_dir = o->dir->exclude_per_dir;
693 i = read_directory(&d, pathbuf, namelen+1, NULL);
694 if (i)
695 return o->gently ? -1 :
696 error(ERRORMSG(o, not_uptodate_dir), ce->name);
697 free(pathbuf);
698 return cnt;
702 * This gets called when there was no index entry for the tree entry 'dst',
703 * but we found a file in the working tree that 'lstat()' said was fine,
704 * and we're on a case-insensitive filesystem.
706 * See if we can find a case-insensitive match in the index that also
707 * matches the stat information, and assume it's that other file!
709 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
711 struct cache_entry *src;
713 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
714 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
718 * We do not want to remove or overwrite a working tree file that
719 * is not tracked, unless it is ignored.
721 static int verify_absent_1(struct cache_entry *ce, const char *action,
722 struct unpack_trees_options *o,
723 const char *error_msg)
725 struct stat st;
727 if (o->index_only || o->reset || !o->update)
728 return 0;
730 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
731 return 0;
733 if (!lstat(ce->name, &st)) {
734 int ret;
735 int dtype = ce_to_dtype(ce);
736 struct cache_entry *result;
739 * It may be that the 'lstat()' succeeded even though
740 * target 'ce' was absent, because there is an old
741 * entry that is different only in case..
743 * Ignore that lstat() if it matches.
745 if (ignore_case && icase_exists(o, ce, &st))
746 return 0;
748 if (o->dir && excluded(o->dir, ce->name, &dtype))
750 * ce->name is explicitly excluded, so it is Ok to
751 * overwrite it.
753 return 0;
754 if (S_ISDIR(st.st_mode)) {
756 * We are checking out path "foo" and
757 * found "foo/." in the working tree.
758 * This is tricky -- if we have modified
759 * files that are in "foo/" we would lose
760 * them.
762 ret = verify_clean_subdirectory(ce, action, o);
763 if (ret < 0)
764 return ret;
767 * If this removed entries from the index,
768 * what that means is:
770 * (1) the caller unpack_callback() saw path/foo
771 * in the index, and it has not removed it because
772 * it thinks it is handling 'path' as blob with
773 * D/F conflict;
774 * (2) we will return "ok, we placed a merged entry
775 * in the index" which would cause o->pos to be
776 * incremented by one;
777 * (3) however, original o->pos now has 'path/foo'
778 * marked with "to be removed".
780 * We need to increment it by the number of
781 * deleted entries here.
783 o->pos += ret;
784 return 0;
788 * The previous round may already have decided to
789 * delete this path, which is in a subdirectory that
790 * is being replaced with a blob.
792 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
793 if (result) {
794 if (result->ce_flags & CE_REMOVE)
795 return 0;
798 return o->gently ? -1 :
799 error(ERRORMSG(o, would_lose_untracked), ce->name, action);
801 return 0;
803 static int verify_absent(struct cache_entry *ce, const char *action,
804 struct unpack_trees_options *o)
806 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
807 return 0;
808 return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_untracked));
811 static int verify_absent_sparse(struct cache_entry *ce, const char *action,
812 struct unpack_trees_options *o)
814 return verify_absent_1(ce, action, o, ERRORMSG(o, would_lose_orphaned));
817 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
818 struct unpack_trees_options *o)
820 int update = CE_UPDATE;
822 if (!old) {
823 if (verify_absent(merge, "overwritten", o))
824 return -1;
825 invalidate_ce_path(merge, o);
826 } else if (!(old->ce_flags & CE_CONFLICTED)) {
828 * See if we can re-use the old CE directly?
829 * That way we get the uptodate stat info.
831 * This also removes the UPDATE flag on a match; otherwise
832 * we will end up overwriting local changes in the work tree.
834 if (same(old, merge)) {
835 copy_cache_entry(merge, old);
836 update = 0;
837 } else {
838 if (verify_uptodate(old, o))
839 return -1;
840 if (ce_skip_worktree(old))
841 update |= CE_SKIP_WORKTREE;
842 invalidate_ce_path(old, o);
844 } else {
846 * Previously unmerged entry left as an existence
847 * marker by read_index_unmerged();
849 invalidate_ce_path(old, o);
852 add_entry(o, merge, update, CE_STAGEMASK);
853 return 1;
856 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
857 struct unpack_trees_options *o)
859 /* Did it exist in the index? */
860 if (!old) {
861 if (verify_absent(ce, "removed", o))
862 return -1;
863 return 0;
865 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
866 return -1;
867 add_entry(o, ce, CE_REMOVE, 0);
868 invalidate_ce_path(ce, o);
869 return 1;
872 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
874 add_entry(o, ce, 0, 0);
875 return 1;
878 #if DBRT_DEBUG
879 static void show_stage_entry(FILE *o,
880 const char *label, const struct cache_entry *ce)
882 if (!ce)
883 fprintf(o, "%s (missing)\n", label);
884 else
885 fprintf(o, "%s%06o %s %d\t%s\n",
886 label,
887 ce->ce_mode,
888 sha1_to_hex(ce->sha1),
889 ce_stage(ce),
890 ce->name);
892 #endif
894 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
896 struct cache_entry *index;
897 struct cache_entry *head;
898 struct cache_entry *remote = stages[o->head_idx + 1];
899 int count;
900 int head_match = 0;
901 int remote_match = 0;
903 int df_conflict_head = 0;
904 int df_conflict_remote = 0;
906 int any_anc_missing = 0;
907 int no_anc_exists = 1;
908 int i;
910 for (i = 1; i < o->head_idx; i++) {
911 if (!stages[i] || stages[i] == o->df_conflict_entry)
912 any_anc_missing = 1;
913 else
914 no_anc_exists = 0;
917 index = stages[0];
918 head = stages[o->head_idx];
920 if (head == o->df_conflict_entry) {
921 df_conflict_head = 1;
922 head = NULL;
925 if (remote == o->df_conflict_entry) {
926 df_conflict_remote = 1;
927 remote = NULL;
930 /* First, if there's a #16 situation, note that to prevent #13
931 * and #14.
933 if (!same(remote, head)) {
934 for (i = 1; i < o->head_idx; i++) {
935 if (same(stages[i], head)) {
936 head_match = i;
938 if (same(stages[i], remote)) {
939 remote_match = i;
944 /* We start with cases where the index is allowed to match
945 * something other than the head: #14(ALT) and #2ALT, where it
946 * is permitted to match the result instead.
948 /* #14, #14ALT, #2ALT */
949 if (remote && !df_conflict_head && head_match && !remote_match) {
950 if (index && !same(index, remote) && !same(index, head))
951 return o->gently ? -1 : reject_merge(index, o);
952 return merged_entry(remote, index, o);
955 * If we have an entry in the index cache, then we want to
956 * make sure that it matches head.
958 if (index && !same(index, head))
959 return o->gently ? -1 : reject_merge(index, o);
961 if (head) {
962 /* #5ALT, #15 */
963 if (same(head, remote))
964 return merged_entry(head, index, o);
965 /* #13, #3ALT */
966 if (!df_conflict_remote && remote_match && !head_match)
967 return merged_entry(head, index, o);
970 /* #1 */
971 if (!head && !remote && any_anc_missing)
972 return 0;
974 /* Under the new "aggressive" rule, we resolve mostly trivial
975 * cases that we historically had git-merge-one-file resolve.
977 if (o->aggressive) {
978 int head_deleted = !head && !df_conflict_head;
979 int remote_deleted = !remote && !df_conflict_remote;
980 struct cache_entry *ce = NULL;
982 if (index)
983 ce = index;
984 else if (head)
985 ce = head;
986 else if (remote)
987 ce = remote;
988 else {
989 for (i = 1; i < o->head_idx; i++) {
990 if (stages[i] && stages[i] != o->df_conflict_entry) {
991 ce = stages[i];
992 break;
998 * Deleted in both.
999 * Deleted in one and unchanged in the other.
1001 if ((head_deleted && remote_deleted) ||
1002 (head_deleted && remote && remote_match) ||
1003 (remote_deleted && head && head_match)) {
1004 if (index)
1005 return deleted_entry(index, index, o);
1006 if (ce && !head_deleted) {
1007 if (verify_absent(ce, "removed", o))
1008 return -1;
1010 return 0;
1013 * Added in both, identically.
1015 if (no_anc_exists && head && remote && same(head, remote))
1016 return merged_entry(head, index, o);
1020 /* Below are "no merge" cases, which require that the index be
1021 * up-to-date to avoid the files getting overwritten with
1022 * conflict resolution files.
1024 if (index) {
1025 if (verify_uptodate(index, o))
1026 return -1;
1029 o->nontrivial_merge = 1;
1031 /* #2, #3, #4, #6, #7, #9, #10, #11. */
1032 count = 0;
1033 if (!head_match || !remote_match) {
1034 for (i = 1; i < o->head_idx; i++) {
1035 if (stages[i] && stages[i] != o->df_conflict_entry) {
1036 keep_entry(stages[i], o);
1037 count++;
1038 break;
1042 #if DBRT_DEBUG
1043 else {
1044 fprintf(stderr, "read-tree: warning #16 detected\n");
1045 show_stage_entry(stderr, "head ", stages[head_match]);
1046 show_stage_entry(stderr, "remote ", stages[remote_match]);
1048 #endif
1049 if (head) { count += keep_entry(head, o); }
1050 if (remote) { count += keep_entry(remote, o); }
1051 return count;
1055 * Two-way merge.
1057 * The rule is to "carry forward" what is in the index without losing
1058 * information across a "fast-forward", favoring a successful merge
1059 * over a merge failure when it makes sense. For details of the
1060 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1063 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1065 struct cache_entry *current = src[0];
1066 struct cache_entry *oldtree = src[1];
1067 struct cache_entry *newtree = src[2];
1069 if (o->merge_size != 2)
1070 return error("Cannot do a twoway merge of %d trees",
1071 o->merge_size);
1073 if (oldtree == o->df_conflict_entry)
1074 oldtree = NULL;
1075 if (newtree == o->df_conflict_entry)
1076 newtree = NULL;
1078 if (current) {
1079 if ((!oldtree && !newtree) || /* 4 and 5 */
1080 (!oldtree && newtree &&
1081 same(current, newtree)) || /* 6 and 7 */
1082 (oldtree && newtree &&
1083 same(oldtree, newtree)) || /* 14 and 15 */
1084 (oldtree && newtree &&
1085 !same(oldtree, newtree) && /* 18 and 19 */
1086 same(current, newtree))) {
1087 return keep_entry(current, o);
1089 else if (oldtree && !newtree && same(current, oldtree)) {
1090 /* 10 or 11 */
1091 return deleted_entry(oldtree, current, o);
1093 else if (oldtree && newtree &&
1094 same(current, oldtree) && !same(current, newtree)) {
1095 /* 20 or 21 */
1096 return merged_entry(newtree, current, o);
1098 else {
1099 /* all other failures */
1100 if (oldtree)
1101 return o->gently ? -1 : reject_merge(oldtree, o);
1102 if (current)
1103 return o->gently ? -1 : reject_merge(current, o);
1104 if (newtree)
1105 return o->gently ? -1 : reject_merge(newtree, o);
1106 return -1;
1109 else if (newtree) {
1110 if (oldtree && !o->initial_checkout) {
1112 * deletion of the path was staged;
1114 if (same(oldtree, newtree))
1115 return 1;
1116 return reject_merge(oldtree, o);
1118 return merged_entry(newtree, current, o);
1120 return deleted_entry(oldtree, current, o);
1124 * Bind merge.
1126 * Keep the index entries at stage0, collapse stage1 but make sure
1127 * stage0 does not have anything there.
1129 int bind_merge(struct cache_entry **src,
1130 struct unpack_trees_options *o)
1132 struct cache_entry *old = src[0];
1133 struct cache_entry *a = src[1];
1135 if (o->merge_size != 1)
1136 return error("Cannot do a bind merge of %d trees\n",
1137 o->merge_size);
1138 if (a && old)
1139 return o->gently ? -1 :
1140 error(ERRORMSG(o, bind_overlap), a->name, old->name);
1141 if (!a)
1142 return keep_entry(old, o);
1143 else
1144 return merged_entry(a, NULL, o);
1148 * One-way merge.
1150 * The rule is:
1151 * - take the stat information from stage0, take the data from stage1
1153 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1155 struct cache_entry *old = src[0];
1156 struct cache_entry *a = src[1];
1158 if (o->merge_size != 1)
1159 return error("Cannot do a oneway merge of %d trees",
1160 o->merge_size);
1162 if (!a || a == o->df_conflict_entry)
1163 return deleted_entry(old, old, o);
1165 if (old && same(old, a)) {
1166 int update = 0;
1167 if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) {
1168 struct stat st;
1169 if (lstat(old->name, &st) ||
1170 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
1171 update |= CE_UPDATE;
1173 add_entry(o, old, update, 0);
1174 return 0;
1176 return merged_entry(a, old, o);