1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
11 static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
12 unsigned int set, unsigned int clear)
14 unsigned int size = ce_size(ce);
15 struct cache_entry *new = xmalloc(size);
17 clear |= CE_HASHED | CE_UNHASHED;
19 memcpy(new, ce, size);
21 new->ce_flags = (new->ce_flags & ~clear) | set;
22 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
25 /* Unlink the last component and attempt to remove leading
26 * directories, in case this unlink is the removal of the
27 * last entry in the directory -- empty directories are removed.
29 static void unlink_entry(struct cache_entry *ce)
32 char *name = ce->name;
34 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
41 cp = strrchr(name, '/');
57 static struct checkout state;
58 static int check_updates(struct unpack_trees_options *o)
60 unsigned cnt = 0, total = 0;
61 struct progress *progress = NULL;
62 struct index_state *index = &o->result;
66 if (o->update && o->verbose_update) {
67 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
68 struct cache_entry *ce = index->cache[cnt];
69 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
73 progress = start_progress_delay("Checking out files",
78 for (i = 0; i < index->cache_nr; i++) {
79 struct cache_entry *ce = index->cache[i];
81 if (ce->ce_flags & CE_REMOVE) {
82 display_progress(progress, ++cnt);
85 remove_index_entry_at(&o->result, i);
91 for (i = 0; i < index->cache_nr; i++) {
92 struct cache_entry *ce = index->cache[i];
94 if (ce->ce_flags & CE_UPDATE) {
95 display_progress(progress, ++cnt);
96 ce->ce_flags &= ~CE_UPDATE;
98 errs |= checkout_entry(ce, &state, NULL);
102 stop_progress(&progress);
106 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
108 int ret = o->fn(src, o);
114 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
116 struct cache_entry *src[5] = { ce, };
120 if (o->skip_unmerged) {
121 add_entry(o, ce, 0, 0);
125 return call_unpack_fn(src, o);
128 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
131 struct tree_desc t[MAX_UNPACK_TREES];
132 struct traverse_info newinfo;
133 struct name_entry *p;
142 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
143 newinfo.conflicts |= df_conflicts;
145 for (i = 0; i < n; i++, dirmask >>= 1) {
146 const unsigned char *sha1 = NULL;
148 sha1 = names[i].sha1;
149 fill_tree_descriptor(t+i, sha1);
151 return traverse_trees(n, t, &newinfo);
155 * Compare the traverse-path to the cache entry without actually
156 * having to generate the textual representation of the traverse
159 * NOTE! This *only* compares up to the size of the traverse path
160 * itself - the caller needs to do the final check for the cache
161 * entry having more data at the end!
163 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
165 int len, pathlen, ce_len;
169 int cmp = do_compare_entry(ce, info->prev, &info->name);
173 pathlen = info->pathlen;
174 ce_len = ce_namelen(ce);
176 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
177 if (ce_len < pathlen)
181 ce_name = ce->name + pathlen;
183 len = tree_entry_len(n->path, n->sha1);
184 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
187 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
189 int cmp = do_compare_entry(ce, info, n);
194 * Even if the beginning compared identically, the ce should
195 * compare as bigger than a directory leading up to it!
197 return ce_namelen(ce) > traverse_path_len(info, n);
200 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
202 int len = traverse_path_len(info, n);
203 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
205 ce->ce_mode = create_ce_mode(n->mode);
206 ce->ce_flags = create_ce_flags(len, stage);
207 hashcpy(ce->sha1, n->sha1);
208 make_traverse_path(ce->name, info, n);
213 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
214 const struct name_entry *names, const struct traverse_info *info)
217 struct unpack_trees_options *o = info->data;
218 unsigned long conflicts;
220 /* Do we have *only* directories? Nothing to do */
221 if (mask == dirmask && !src[0])
224 conflicts = info->conflicts;
227 conflicts |= dirmask;
230 * Ok, we've filled in up to any potential index entry in src[0],
233 for (i = 0; i < n; i++) {
235 unsigned int bit = 1ul << i;
236 if (conflicts & bit) {
237 src[i + o->merge] = o->df_conflict_entry;
244 else if (i + 1 < o->head_idx)
246 else if (i + 1 > o->head_idx)
250 src[i + o->merge] = create_ce_entry(info, names + i, stage);
254 return call_unpack_fn(src, o);
257 for (i = 0; i < n; i++)
258 add_entry(o, src[i], 0, 0);
262 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
264 struct cache_entry *src[5] = { NULL, };
265 struct unpack_trees_options *o = info->data;
266 const struct name_entry *p = names;
268 /* Find first entry with a real name (we could use "mask" too) */
272 /* Are we supposed to look at the index too? */
274 while (o->pos < o->src_index->cache_nr) {
275 struct cache_entry *ce = o->src_index->cache[o->pos];
276 int cmp = compare_entry(ce, info, p);
278 if (unpack_index_entry(ce, o) < 0)
286 * If we skip unmerged index entries, we'll skip this
287 * entry *and* the tree entries associated with it!
289 if (o->skip_unmerged) {
290 add_entry(o, ce, 0, 0);
300 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
303 /* Now handle any directories.. */
305 unsigned long conflicts = mask & ~dirmask;
311 if (traverse_trees_recursive(n, dirmask, conflicts,
320 static int unpack_failed(struct unpack_trees_options *o, const char *message)
322 discard_index(&o->result);
325 return error(message);
331 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
333 static struct cache_entry *dfc;
335 if (len > MAX_UNPACK_TREES)
336 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
337 memset(&state, 0, sizeof(state));
341 state.refresh_cache = 1;
343 memset(&o->result, 0, sizeof(o->result));
345 o->result.timestamp = o->src_index->timestamp;
349 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
350 o->df_conflict_entry = dfc;
353 const char *prefix = o->prefix ? o->prefix : "";
354 struct traverse_info info;
356 setup_traverse_info(&info, prefix);
357 info.fn = unpack_callback;
360 if (traverse_trees(len, t, &info) < 0)
361 return unpack_failed(o, NULL);
364 /* Any left-over entries in the index? */
366 while (o->pos < o->src_index->cache_nr) {
367 struct cache_entry *ce = o->src_index->cache[o->pos];
368 if (unpack_index_entry(ce, o) < 0)
369 return unpack_failed(o, NULL);
373 if (o->trivial_merges_only && o->nontrivial_merge)
374 return unpack_failed(o, "Merge requires file-level merging");
377 if (check_updates(o))
380 *o->dst_index = o->result;
384 /* Here come the merge functions */
386 static int reject_merge(struct cache_entry *ce)
388 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
392 static int same(struct cache_entry *a, struct cache_entry *b)
398 return a->ce_mode == b->ce_mode &&
399 !hashcmp(a->sha1, b->sha1);
404 * When a CE gets turned into an unmerged entry, we
405 * want it to be up-to-date
407 static int verify_uptodate(struct cache_entry *ce,
408 struct unpack_trees_options *o)
412 if (o->index_only || o->reset)
415 if (!lstat(ce->name, &st)) {
416 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
420 * NEEDSWORK: the current default policy is to allow
421 * submodule to be out of sync wrt the supermodule
422 * index. This needs to be tightened later for
423 * submodules that are marked to be automatically
426 if (S_ISGITLINK(ce->ce_mode))
432 return o->gently ? -1 :
433 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
436 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
439 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
443 * Check that checking out ce->sha1 in subdir ce->name is not
444 * going to overwrite any working files.
446 * Currently, git does not checkout subprojects during a superproject
447 * checkout, so it is not going to overwrite anything.
449 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
450 struct unpack_trees_options *o)
455 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
456 struct unpack_trees_options *o)
459 * we are about to extract "ce->name"; we would not want to lose
460 * anything in the existing directory there.
467 unsigned char sha1[20];
469 if (S_ISGITLINK(ce->ce_mode) &&
470 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
471 /* If we are not going to update the submodule, then
474 if (!hashcmp(sha1, ce->sha1))
476 return verify_clean_submodule(ce, action, o);
480 * First let's make sure we do not have a local modification
483 namelen = strlen(ce->name);
484 pos = index_name_pos(o->src_index, ce->name, namelen);
486 return cnt; /* we have it as nondirectory */
488 for (i = pos; i < o->src_index->cache_nr; i++) {
489 struct cache_entry *ce = o->src_index->cache[i];
490 int len = ce_namelen(ce);
492 strncmp(ce->name, ce->name, namelen) ||
493 ce->name[namelen] != '/')
496 * ce->name is an entry in the subdirectory.
499 if (verify_uptodate(ce, o))
501 add_entry(o, ce, CE_REMOVE, 0);
507 * Then we need to make sure that we do not lose a locally
508 * present file that is not ignored.
510 pathbuf = xmalloc(namelen + 2);
511 memcpy(pathbuf, ce->name, namelen);
512 strcpy(pathbuf+namelen, "/");
514 memset(&d, 0, sizeof(d));
516 d.exclude_per_dir = o->dir->exclude_per_dir;
517 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
519 return o->gently ? -1 :
520 error("Updating '%s' would lose untracked files in it",
527 * This gets called when there was no index entry for the tree entry 'dst',
528 * but we found a file in the working tree that 'lstat()' said was fine,
529 * and we're on a case-insensitive filesystem.
531 * See if we can find a case-insensitive match in the index that also
532 * matches the stat information, and assume it's that other file!
534 static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
536 struct cache_entry *src;
538 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
539 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
543 * We do not want to remove or overwrite a working tree file that
544 * is not tracked, unless it is ignored.
546 static int verify_absent(struct cache_entry *ce, const char *action,
547 struct unpack_trees_options *o)
551 if (o->index_only || o->reset || !o->update)
554 if (has_symlink_leading_path(ce_namelen(ce), ce->name))
557 if (!lstat(ce->name, &st)) {
559 int dtype = ce_to_dtype(ce);
560 struct cache_entry *result;
563 * It may be that the 'lstat()' succeeded even though
564 * target 'ce' was absent, because there is an old
565 * entry that is different only in case..
567 * Ignore that lstat() if it matches.
569 if (ignore_case && icase_exists(o, ce, &st))
572 if (o->dir && excluded(o->dir, ce->name, &dtype))
574 * ce->name is explicitly excluded, so it is Ok to
578 if (S_ISDIR(st.st_mode)) {
580 * We are checking out path "foo" and
581 * found "foo/." in the working tree.
582 * This is tricky -- if we have modified
583 * files that are in "foo/" we would lose
586 cnt = verify_clean_subdirectory(ce, action, o);
589 * If this removed entries from the index,
590 * what that means is:
592 * (1) the caller unpack_trees_rec() saw path/foo
593 * in the index, and it has not removed it because
594 * it thinks it is handling 'path' as blob with
596 * (2) we will return "ok, we placed a merged entry
597 * in the index" which would cause o->pos to be
598 * incremented by one;
599 * (3) however, original o->pos now has 'path/foo'
600 * marked with "to be removed".
602 * We need to increment it by the number of
603 * deleted entries here.
610 * The previous round may already have decided to
611 * delete this path, which is in a subdirectory that
612 * is being replaced with a blob.
614 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
616 if (result->ce_flags & CE_REMOVE)
620 return o->gently ? -1 :
621 error("Untracked working tree file '%s' "
622 "would be %s by merge.", ce->name, action);
627 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
628 struct unpack_trees_options *o)
630 int update = CE_UPDATE;
634 * See if we can re-use the old CE directly?
635 * That way we get the uptodate stat info.
637 * This also removes the UPDATE flag on a match; otherwise
638 * we will end up overwriting local changes in the work tree.
640 if (same(old, merge)) {
641 copy_cache_entry(merge, old);
644 if (verify_uptodate(old, o))
646 invalidate_ce_path(old, o);
650 if (verify_absent(merge, "overwritten", o))
652 invalidate_ce_path(merge, o);
655 add_entry(o, merge, update, CE_STAGEMASK);
659 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
660 struct unpack_trees_options *o)
662 /* Did it exist in the index? */
664 if (verify_absent(ce, "removed", o))
668 if (verify_uptodate(old, o))
670 add_entry(o, ce, CE_REMOVE, 0);
671 invalidate_ce_path(ce, o);
675 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
677 add_entry(o, ce, 0, 0);
682 static void show_stage_entry(FILE *o,
683 const char *label, const struct cache_entry *ce)
686 fprintf(o, "%s (missing)\n", label);
688 fprintf(o, "%s%06o %s %d\t%s\n",
691 sha1_to_hex(ce->sha1),
697 int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
699 struct cache_entry *index;
700 struct cache_entry *head;
701 struct cache_entry *remote = stages[o->head_idx + 1];
704 int remote_match = 0;
706 int df_conflict_head = 0;
707 int df_conflict_remote = 0;
709 int any_anc_missing = 0;
710 int no_anc_exists = 1;
713 for (i = 1; i < o->head_idx; i++) {
714 if (!stages[i] || stages[i] == o->df_conflict_entry)
721 head = stages[o->head_idx];
723 if (head == o->df_conflict_entry) {
724 df_conflict_head = 1;
728 if (remote == o->df_conflict_entry) {
729 df_conflict_remote = 1;
733 /* First, if there's a #16 situation, note that to prevent #13
736 if (!same(remote, head)) {
737 for (i = 1; i < o->head_idx; i++) {
738 if (same(stages[i], head)) {
741 if (same(stages[i], remote)) {
747 /* We start with cases where the index is allowed to match
748 * something other than the head: #14(ALT) and #2ALT, where it
749 * is permitted to match the result instead.
751 /* #14, #14ALT, #2ALT */
752 if (remote && !df_conflict_head && head_match && !remote_match) {
753 if (index && !same(index, remote) && !same(index, head))
754 return o->gently ? -1 : reject_merge(index);
755 return merged_entry(remote, index, o);
758 * If we have an entry in the index cache, then we want to
759 * make sure that it matches head.
761 if (index && !same(index, head))
762 return o->gently ? -1 : reject_merge(index);
766 if (same(head, remote))
767 return merged_entry(head, index, o);
769 if (!df_conflict_remote && remote_match && !head_match)
770 return merged_entry(head, index, o);
774 if (!head && !remote && any_anc_missing)
777 /* Under the new "aggressive" rule, we resolve mostly trivial
778 * cases that we historically had git-merge-one-file resolve.
781 int head_deleted = !head && !df_conflict_head;
782 int remote_deleted = !remote && !df_conflict_remote;
783 struct cache_entry *ce = NULL;
792 for (i = 1; i < o->head_idx; i++) {
793 if (stages[i] && stages[i] != o->df_conflict_entry) {
802 * Deleted in one and unchanged in the other.
804 if ((head_deleted && remote_deleted) ||
805 (head_deleted && remote && remote_match) ||
806 (remote_deleted && head && head_match)) {
808 return deleted_entry(index, index, o);
809 if (ce && !head_deleted) {
810 if (verify_absent(ce, "removed", o))
816 * Added in both, identically.
818 if (no_anc_exists && head && remote && same(head, remote))
819 return merged_entry(head, index, o);
823 /* Below are "no merge" cases, which require that the index be
824 * up-to-date to avoid the files getting overwritten with
825 * conflict resolution files.
828 if (verify_uptodate(index, o))
832 o->nontrivial_merge = 1;
834 /* #2, #3, #4, #6, #7, #9, #10, #11. */
836 if (!head_match || !remote_match) {
837 for (i = 1; i < o->head_idx; i++) {
838 if (stages[i] && stages[i] != o->df_conflict_entry) {
839 keep_entry(stages[i], o);
847 fprintf(stderr, "read-tree: warning #16 detected\n");
848 show_stage_entry(stderr, "head ", stages[head_match]);
849 show_stage_entry(stderr, "remote ", stages[remote_match]);
852 if (head) { count += keep_entry(head, o); }
853 if (remote) { count += keep_entry(remote, o); }
860 * The rule is to "carry forward" what is in the index without losing
861 * information across a "fast forward", favoring a successful merge
862 * over a merge failure when it makes sense. For details of the
863 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
866 int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
868 struct cache_entry *current = src[0];
869 struct cache_entry *oldtree = src[1];
870 struct cache_entry *newtree = src[2];
872 if (o->merge_size != 2)
873 return error("Cannot do a twoway merge of %d trees",
876 if (oldtree == o->df_conflict_entry)
878 if (newtree == o->df_conflict_entry)
882 if ((!oldtree && !newtree) || /* 4 and 5 */
883 (!oldtree && newtree &&
884 same(current, newtree)) || /* 6 and 7 */
885 (oldtree && newtree &&
886 same(oldtree, newtree)) || /* 14 and 15 */
887 (oldtree && newtree &&
888 !same(oldtree, newtree) && /* 18 and 19 */
889 same(current, newtree))) {
890 return keep_entry(current, o);
892 else if (oldtree && !newtree && same(current, oldtree)) {
894 return deleted_entry(oldtree, current, o);
896 else if (oldtree && newtree &&
897 same(current, oldtree) && !same(current, newtree)) {
899 return merged_entry(newtree, current, o);
902 /* all other failures */
904 return o->gently ? -1 : reject_merge(oldtree);
906 return o->gently ? -1 : reject_merge(current);
908 return o->gently ? -1 : reject_merge(newtree);
913 return merged_entry(newtree, current, o);
914 return deleted_entry(oldtree, current, o);
920 * Keep the index entries at stage0, collapse stage1 but make sure
921 * stage0 does not have anything there.
923 int bind_merge(struct cache_entry **src,
924 struct unpack_trees_options *o)
926 struct cache_entry *old = src[0];
927 struct cache_entry *a = src[1];
929 if (o->merge_size != 1)
930 return error("Cannot do a bind merge of %d trees\n",
933 return o->gently ? -1 :
934 error("Entry '%s' overlaps with '%s'. Cannot bind.", a->name, old->name);
936 return keep_entry(old, o);
938 return merged_entry(a, NULL, o);
945 * - take the stat information from stage0, take the data from stage1
947 int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
949 struct cache_entry *old = src[0];
950 struct cache_entry *a = src[1];
952 if (o->merge_size != 1)
953 return error("Cannot do a oneway merge of %d trees",
957 return deleted_entry(old, old, o);
959 if (old && same(old, a)) {
963 if (lstat(old->name, &st) ||
964 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
967 add_entry(o, old, update, 0);
970 return merged_entry(a, old, o);