5 #include "cache-tree.h"
6 #include "unpack-trees.h"
10 static inline void remove_entry(int remove)
13 remove_cache_entry_at(remove);
16 /* Unlink the last component and attempt to remove leading
17 * directories, in case this unlink is the removal of the
18 * last entry in the directory -- empty directories are removed.
20 static void unlink_entry(char *name, char *last_symlink)
24 if (has_symlink_leading_path(name, last_symlink))
31 cp = strrchr(name, '/');
47 static struct checkout state;
48 static void check_updates(struct unpack_trees_options *o)
50 unsigned cnt = 0, total = 0;
51 struct progress *progress = NULL;
52 char last_symlink[PATH_MAX];
55 if (o->update && o->verbose_update) {
56 for (total = cnt = 0; cnt < active_nr; cnt++) {
57 struct cache_entry *ce = active_cache[cnt];
58 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
62 progress = start_progress_delay("Checking out files",
68 for (i = 0; i < active_nr; i++) {
69 struct cache_entry *ce = active_cache[i];
71 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
72 display_progress(progress, ++cnt);
73 if (ce->ce_flags & CE_REMOVE) {
75 unlink_entry(ce->name, last_symlink);
76 remove_cache_entry_at(i);
80 if (ce->ce_flags & CE_UPDATE) {
81 ce->ce_flags &= ~CE_UPDATE;
83 checkout_entry(ce, &state, NULL);
88 stop_progress(&progress);
91 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o, int remove)
93 int ret = o->fn(src, o, remove);
101 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
103 struct cache_entry *src[5] = { ce, };
105 if (o->skip_unmerged) {
108 remove_entry(o->pos);
112 return call_unpack_fn(src, o, o->pos);
115 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
118 struct tree_desc t[3];
119 struct traverse_info newinfo;
120 struct name_entry *p;
129 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
130 newinfo.conflicts |= df_conflicts;
132 for (i = 0; i < n; i++, dirmask >>= 1) {
133 const unsigned char *sha1 = NULL;
135 sha1 = names[i].sha1;
136 fill_tree_descriptor(t+i, sha1);
138 traverse_trees(n, t, &newinfo);
143 * Compare the traverse-path to the cache entry without actually
144 * having to generate the textual representation of the traverse
147 * NOTE! This *only* compares up to the size of the traverse path
148 * itself - the caller needs to do the final check for the cache
149 * entry having more data at the end!
151 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
153 int len, pathlen, ce_len;
157 int cmp = do_compare_entry(ce, info->prev, &info->name);
161 pathlen = info->pathlen;
162 ce_len = ce_namelen(ce);
164 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
165 if (ce_len < pathlen)
169 ce_name = ce->name + pathlen;
171 len = tree_entry_len(n->path, n->sha1);
172 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
175 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
177 int cmp = do_compare_entry(ce, info, n);
182 * Even if the beginning compared identically, the ce should
183 * compare as bigger than a directory leading up to it!
185 return ce_namelen(ce) > traverse_path_len(info, n);
188 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
190 int len = traverse_path_len(info, n);
191 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
193 ce->ce_mode = create_ce_mode(n->mode);
194 ce->ce_flags = create_ce_flags(len, stage);
195 hashcpy(ce->sha1, n->sha1);
196 make_traverse_path(ce->name, info, n);
201 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
202 const struct name_entry *names, const struct traverse_info *info, int remove)
205 struct unpack_trees_options *o = info->data;
206 unsigned long conflicts;
208 /* Do we have *only* directories? Nothing to do */
209 if (mask == dirmask && !src[0])
212 conflicts = info->conflicts;
215 conflicts |= dirmask;
218 * Ok, we've filled in up to any potential index entry in src[0],
221 for (i = 0; i < n; i++) {
223 unsigned int bit = 1ul << i;
224 if (conflicts & bit) {
225 src[i + o->merge] = o->df_conflict_entry;
232 else if (i + 1 < o->head_idx)
234 else if (i + 1 > o->head_idx)
238 src[i + o->merge] = create_ce_entry(info, names + i, stage);
242 return call_unpack_fn(src, o, remove);
245 remove_entry(remove);
246 for (i = 0; i < n; i++)
247 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
251 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
253 struct cache_entry *src[5] = { NULL, };
254 struct unpack_trees_options *o = info->data;
256 const struct name_entry *p = names;
258 /* Find first entry with a real name (we could use "mask" too) */
262 /* Are we supposed to look at the index too? */
264 while (o->pos < active_nr) {
265 struct cache_entry *ce = active_cache[o->pos];
266 int cmp = compare_entry(ce, info, p);
268 if (unpack_index_entry(ce, o) < 0)
275 * If we skip unmerged index entries, we'll skip this
276 * entry *and* the tree entries associated with it!
278 if (o->skip_unmerged)
280 remove_entry(o->pos);
290 if (unpack_nondirectories(n, mask, dirmask, src, names, info, remove) < 0)
293 /* Now handle any directories.. */
295 unsigned long conflicts = mask & ~dirmask;
301 traverse_trees_recursive(n, dirmask, conflicts, names, info);
308 static int unpack_failed(struct unpack_trees_options *o, const char *message)
312 return error(message);
320 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
322 static struct cache_entry *dfc;
325 die("unpack_trees takes at most four trees");
326 memset(&state, 0, sizeof(state));
330 state.refresh_cache = 1;
335 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
336 o->df_conflict_entry = dfc;
339 const char *prefix = o->prefix ? o->prefix : "";
340 struct traverse_info info;
342 setup_traverse_info(&info, prefix);
343 info.fn = unpack_callback;
346 if (traverse_trees(len, t, &info) < 0)
347 return unpack_failed(o, NULL);
350 /* Any left-over entries in the index? */
352 while (o->pos < active_nr) {
353 struct cache_entry *ce = active_cache[o->pos];
354 if (unpack_index_entry(ce, o) < 0)
355 return unpack_failed(o, NULL);
359 if (o->trivial_merges_only && o->nontrivial_merge)
360 return unpack_failed(o, "Merge requires file-level merging");
366 /* Here come the merge functions */
368 static int reject_merge(struct cache_entry *ce)
370 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
374 static int same(struct cache_entry *a, struct cache_entry *b)
380 return a->ce_mode == b->ce_mode &&
381 !hashcmp(a->sha1, b->sha1);
386 * When a CE gets turned into an unmerged entry, we
387 * want it to be up-to-date
389 static int verify_uptodate(struct cache_entry *ce,
390 struct unpack_trees_options *o)
394 if (o->index_only || o->reset)
397 if (!lstat(ce->name, &st)) {
398 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
402 * NEEDSWORK: the current default policy is to allow
403 * submodule to be out of sync wrt the supermodule
404 * index. This needs to be tightened later for
405 * submodules that are marked to be automatically
408 if (S_ISGITLINK(ce->ce_mode))
414 return o->gently ? -1 :
415 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
418 static void invalidate_ce_path(struct cache_entry *ce)
421 cache_tree_invalidate_path(active_cache_tree, ce->name);
425 * Check that checking out ce->sha1 in subdir ce->name is not
426 * going to overwrite any working files.
428 * Currently, git does not checkout subprojects during a superproject
429 * checkout, so it is not going to overwrite anything.
431 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
432 struct unpack_trees_options *o)
437 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
438 struct unpack_trees_options *o)
441 * we are about to extract "ce->name"; we would not want to lose
442 * anything in the existing directory there.
449 unsigned char sha1[20];
451 if (S_ISGITLINK(ce->ce_mode) &&
452 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
453 /* If we are not going to update the submodule, then
456 if (!hashcmp(sha1, ce->sha1))
458 return verify_clean_submodule(ce, action, o);
462 * First let's make sure we do not have a local modification
465 namelen = strlen(ce->name);
466 pos = cache_name_pos(ce->name, namelen);
468 return cnt; /* we have it as nondirectory */
470 for (i = pos; i < active_nr; i++) {
471 struct cache_entry *ce = active_cache[i];
472 int len = ce_namelen(ce);
474 strncmp(ce->name, ce->name, namelen) ||
475 ce->name[namelen] != '/')
478 * ce->name is an entry in the subdirectory.
481 if (verify_uptodate(ce, o))
483 ce->ce_flags |= CE_REMOVE;
489 * Then we need to make sure that we do not lose a locally
490 * present file that is not ignored.
492 pathbuf = xmalloc(namelen + 2);
493 memcpy(pathbuf, ce->name, namelen);
494 strcpy(pathbuf+namelen, "/");
496 memset(&d, 0, sizeof(d));
498 d.exclude_per_dir = o->dir->exclude_per_dir;
499 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
501 return o->gently ? -1 :
502 error("Updating '%s' would lose untracked files in it",
509 * We do not want to remove or overwrite a working tree file that
510 * is not tracked, unless it is ignored.
512 static int verify_absent(struct cache_entry *ce, const char *action,
513 struct unpack_trees_options *o)
517 if (o->index_only || o->reset || !o->update)
520 if (has_symlink_leading_path(ce->name, NULL))
523 if (!lstat(ce->name, &st)) {
525 int dtype = ce_to_dtype(ce);
527 if (o->dir && excluded(o->dir, ce->name, &dtype))
529 * ce->name is explicitly excluded, so it is Ok to
533 if (S_ISDIR(st.st_mode)) {
535 * We are checking out path "foo" and
536 * found "foo/." in the working tree.
537 * This is tricky -- if we have modified
538 * files that are in "foo/" we would lose
541 cnt = verify_clean_subdirectory(ce, action, o);
544 * If this removed entries from the index,
545 * what that means is:
547 * (1) the caller unpack_trees_rec() saw path/foo
548 * in the index, and it has not removed it because
549 * it thinks it is handling 'path' as blob with
551 * (2) we will return "ok, we placed a merged entry
552 * in the index" which would cause o->pos to be
553 * incremented by one;
554 * (3) however, original o->pos now has 'path/foo'
555 * marked with "to be removed".
557 * We need to increment it by the number of
558 * deleted entries here.
565 * The previous round may already have decided to
566 * delete this path, which is in a subdirectory that
567 * is being replaced with a blob.
569 cnt = cache_name_pos(ce->name, strlen(ce->name));
571 struct cache_entry *ce = active_cache[cnt];
572 if (ce->ce_flags & CE_REMOVE)
576 return o->gently ? -1 :
577 error("Untracked working tree file '%s' "
578 "would be %s by merge.", ce->name, action);
583 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
584 struct unpack_trees_options *o)
586 merge->ce_flags |= CE_UPDATE;
589 * See if we can re-use the old CE directly?
590 * That way we get the uptodate stat info.
592 * This also removes the UPDATE flag on
595 if (same(old, merge)) {
596 copy_cache_entry(merge, old);
598 if (verify_uptodate(old, o))
600 invalidate_ce_path(old);
604 if (verify_absent(merge, "overwritten", o))
606 invalidate_ce_path(merge);
609 merge->ce_flags &= ~CE_STAGEMASK;
610 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
614 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
615 struct unpack_trees_options *o)
618 if (verify_uptodate(old, o))
621 if (verify_absent(ce, "removed", o))
623 ce->ce_flags |= CE_REMOVE;
624 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
625 invalidate_ce_path(ce);
629 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
631 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
636 static void show_stage_entry(FILE *o,
637 const char *label, const struct cache_entry *ce)
640 fprintf(o, "%s (missing)\n", label);
642 fprintf(o, "%s%06o %s %d\t%s\n",
645 sha1_to_hex(ce->sha1),
651 int threeway_merge(struct cache_entry **stages,
652 struct unpack_trees_options *o,
655 struct cache_entry *index;
656 struct cache_entry *head;
657 struct cache_entry *remote = stages[o->head_idx + 1];
660 int remote_match = 0;
662 int df_conflict_head = 0;
663 int df_conflict_remote = 0;
665 int any_anc_missing = 0;
666 int no_anc_exists = 1;
669 for (i = 1; i < o->head_idx; i++) {
670 if (!stages[i] || stages[i] == o->df_conflict_entry)
677 head = stages[o->head_idx];
679 if (head == o->df_conflict_entry) {
680 df_conflict_head = 1;
684 if (remote == o->df_conflict_entry) {
685 df_conflict_remote = 1;
689 /* First, if there's a #16 situation, note that to prevent #13
692 if (!same(remote, head)) {
693 for (i = 1; i < o->head_idx; i++) {
694 if (same(stages[i], head)) {
697 if (same(stages[i], remote)) {
703 /* We start with cases where the index is allowed to match
704 * something other than the head: #14(ALT) and #2ALT, where it
705 * is permitted to match the result instead.
707 /* #14, #14ALT, #2ALT */
708 if (remote && !df_conflict_head && head_match && !remote_match) {
709 if (index && !same(index, remote) && !same(index, head))
710 return o->gently ? -1 : reject_merge(index);
711 return merged_entry(remote, index, o);
714 * If we have an entry in the index cache, then we want to
715 * make sure that it matches head.
717 if (index && !same(index, head))
718 return o->gently ? -1 : reject_merge(index);
722 if (same(head, remote))
723 return merged_entry(head, index, o);
725 if (!df_conflict_remote && remote_match && !head_match)
726 return merged_entry(head, index, o);
730 if (!head && !remote && any_anc_missing) {
731 remove_entry(remove);
735 /* Under the new "aggressive" rule, we resolve mostly trivial
736 * cases that we historically had git-merge-one-file resolve.
739 int head_deleted = !head && !df_conflict_head;
740 int remote_deleted = !remote && !df_conflict_remote;
741 struct cache_entry *ce = NULL;
750 for (i = 1; i < o->head_idx; i++) {
751 if (stages[i] && stages[i] != o->df_conflict_entry) {
760 * Deleted in one and unchanged in the other.
762 if ((head_deleted && remote_deleted) ||
763 (head_deleted && remote && remote_match) ||
764 (remote_deleted && head && head_match)) {
765 remove_entry(remove);
767 return deleted_entry(index, index, o);
768 else if (ce && !head_deleted) {
769 if (verify_absent(ce, "removed", o))
775 * Added in both, identically.
777 if (no_anc_exists && head && remote && same(head, remote))
778 return merged_entry(head, index, o);
782 /* Below are "no merge" cases, which require that the index be
783 * up-to-date to avoid the files getting overwritten with
784 * conflict resolution files.
787 if (verify_uptodate(index, o))
791 remove_entry(remove);
792 o->nontrivial_merge = 1;
794 /* #2, #3, #4, #6, #7, #9, #10, #11. */
796 if (!head_match || !remote_match) {
797 for (i = 1; i < o->head_idx; i++) {
798 if (stages[i] && stages[i] != o->df_conflict_entry) {
799 keep_entry(stages[i], o);
807 fprintf(stderr, "read-tree: warning #16 detected\n");
808 show_stage_entry(stderr, "head ", stages[head_match]);
809 show_stage_entry(stderr, "remote ", stages[remote_match]);
812 if (head) { count += keep_entry(head, o); }
813 if (remote) { count += keep_entry(remote, o); }
820 * The rule is to "carry forward" what is in the index without losing
821 * information across a "fast forward", favoring a successful merge
822 * over a merge failure when it makes sense. For details of the
823 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
826 int twoway_merge(struct cache_entry **src,
827 struct unpack_trees_options *o,
830 struct cache_entry *current = src[0];
831 struct cache_entry *oldtree = src[1];
832 struct cache_entry *newtree = src[2];
834 if (o->merge_size != 2)
835 return error("Cannot do a twoway merge of %d trees",
838 if (oldtree == o->df_conflict_entry)
840 if (newtree == o->df_conflict_entry)
844 if ((!oldtree && !newtree) || /* 4 and 5 */
845 (!oldtree && newtree &&
846 same(current, newtree)) || /* 6 and 7 */
847 (oldtree && newtree &&
848 same(oldtree, newtree)) || /* 14 and 15 */
849 (oldtree && newtree &&
850 !same(oldtree, newtree) && /* 18 and 19 */
851 same(current, newtree))) {
852 return keep_entry(current, o);
854 else if (oldtree && !newtree && same(current, oldtree)) {
856 remove_entry(remove);
857 return deleted_entry(oldtree, current, o);
859 else if (oldtree && newtree &&
860 same(current, oldtree) && !same(current, newtree)) {
862 return merged_entry(newtree, current, o);
865 /* all other failures */
866 remove_entry(remove);
868 return o->gently ? -1 : reject_merge(oldtree);
870 return o->gently ? -1 : reject_merge(current);
872 return o->gently ? -1 : reject_merge(newtree);
877 return merged_entry(newtree, current, o);
878 remove_entry(remove);
879 return deleted_entry(oldtree, current, o);
885 * Keep the index entries at stage0, collapse stage1 but make sure
886 * stage0 does not have anything there.
888 int bind_merge(struct cache_entry **src,
889 struct unpack_trees_options *o,
892 struct cache_entry *old = src[0];
893 struct cache_entry *a = src[1];
895 if (o->merge_size != 1)
896 return error("Cannot do a bind merge of %d trees\n",
899 return o->gently ? -1 :
900 error("Entry '%s' overlaps. Cannot bind.", a->name);
902 return keep_entry(old, o);
904 return merged_entry(a, NULL, o);
911 * - take the stat information from stage0, take the data from stage1
913 int oneway_merge(struct cache_entry **src,
914 struct unpack_trees_options *o,
917 struct cache_entry *old = src[0];
918 struct cache_entry *a = src[1];
920 if (o->merge_size != 1)
921 return error("Cannot do a oneway merge of %d trees",
925 remove_entry(remove);
926 return deleted_entry(old, old, o);
928 if (old && same(old, a)) {
931 if (lstat(old->name, &st) ||
932 ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
933 old->ce_flags |= CE_UPDATE;
935 return keep_entry(old, o);
937 return merged_entry(a, old, o);