1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 #include "cache-tree.h"
7 #include "unpack-trees.h"
11 static inline void remove_entry(int remove, struct unpack_trees_options *o)
14 remove_index_entry_at(o->index, remove);
17 /* Unlink the last component and attempt to remove leading
18 * directories, in case this unlink is the removal of the
19 * last entry in the directory -- empty directories are removed.
21 static void unlink_entry(char *name, char *last_symlink)
25 if (has_symlink_leading_path(name, last_symlink))
32 cp = strrchr(name, '/');
48 static struct checkout state;
49 static void check_updates(struct unpack_trees_options *o)
51 unsigned cnt = 0, total = 0;
52 struct progress *progress = NULL;
53 char last_symlink[PATH_MAX];
56 if (o->update && o->verbose_update) {
57 for (total = cnt = 0; cnt < o->index->cache_nr; cnt++) {
58 struct cache_entry *ce = o->index->cache[cnt];
59 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
63 progress = start_progress_delay("Checking out files",
69 for (i = 0; i < o->index->cache_nr; i++) {
70 struct cache_entry *ce = o->index->cache[i];
72 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
73 display_progress(progress, ++cnt);
74 if (ce->ce_flags & CE_REMOVE) {
76 unlink_entry(ce->name, last_symlink);
77 remove_index_entry_at(o->index, i);
81 if (ce->ce_flags & CE_UPDATE) {
82 ce->ce_flags &= ~CE_UPDATE;
84 checkout_entry(ce, &state, NULL);
89 stop_progress(&progress);
92 static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o, int remove)
94 int ret = o->fn(src, o, remove);
102 static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
104 struct cache_entry *src[5] = { ce, };
106 if (o->skip_unmerged) {
109 remove_entry(o->pos, o);
113 return call_unpack_fn(src, o, o->pos);
116 int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
119 struct tree_desc t[3];
120 struct traverse_info newinfo;
121 struct name_entry *p;
130 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
131 newinfo.conflicts |= df_conflicts;
133 for (i = 0; i < n; i++, dirmask >>= 1) {
134 const unsigned char *sha1 = NULL;
136 sha1 = names[i].sha1;
137 fill_tree_descriptor(t+i, sha1);
139 traverse_trees(n, t, &newinfo);
144 * Compare the traverse-path to the cache entry without actually
145 * having to generate the textual representation of the traverse
148 * NOTE! This *only* compares up to the size of the traverse path
149 * itself - the caller needs to do the final check for the cache
150 * entry having more data at the end!
152 static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
154 int len, pathlen, ce_len;
158 int cmp = do_compare_entry(ce, info->prev, &info->name);
162 pathlen = info->pathlen;
163 ce_len = ce_namelen(ce);
165 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
166 if (ce_len < pathlen)
170 ce_name = ce->name + pathlen;
172 len = tree_entry_len(n->path, n->sha1);
173 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
176 static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
178 int cmp = do_compare_entry(ce, info, n);
183 * Even if the beginning compared identically, the ce should
184 * compare as bigger than a directory leading up to it!
186 return ce_namelen(ce) > traverse_path_len(info, n);
189 static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
191 int len = traverse_path_len(info, n);
192 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
194 ce->ce_mode = create_ce_mode(n->mode);
195 ce->ce_flags = create_ce_flags(len, stage);
196 hashcpy(ce->sha1, n->sha1);
197 make_traverse_path(ce->name, info, n);
202 static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
203 const struct name_entry *names, const struct traverse_info *info, int remove)
206 struct unpack_trees_options *o = info->data;
207 unsigned long conflicts;
209 /* Do we have *only* directories? Nothing to do */
210 if (mask == dirmask && !src[0])
213 conflicts = info->conflicts;
216 conflicts |= dirmask;
219 * Ok, we've filled in up to any potential index entry in src[0],
222 for (i = 0; i < n; i++) {
224 unsigned int bit = 1ul << i;
225 if (conflicts & bit) {
226 src[i + o->merge] = o->df_conflict_entry;
233 else if (i + 1 < o->head_idx)
235 else if (i + 1 > o->head_idx)
239 src[i + o->merge] = create_ce_entry(info, names + i, stage);
243 return call_unpack_fn(src, o, remove);
246 remove_entry(remove, o);
247 for (i = 0; i < n; i++)
248 add_index_entry(o->index, src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
252 static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
254 struct cache_entry *src[5] = { NULL, };
255 struct unpack_trees_options *o = info->data;
257 const struct name_entry *p = names;
259 /* Find first entry with a real name (we could use "mask" too) */
263 /* Are we supposed to look at the index too? */
265 while (o->pos < o->index->cache_nr) {
266 struct cache_entry *ce = o->index->cache[o->pos];
267 int cmp = compare_entry(ce, info, p);
269 if (unpack_index_entry(ce, o) < 0)
276 * If we skip unmerged index entries, we'll skip this
277 * entry *and* the tree entries associated with it!
279 if (o->skip_unmerged)
281 remove_entry(o->pos, o);
291 if (unpack_nondirectories(n, mask, dirmask, src, names, info, remove) < 0)
294 /* Now handle any directories.. */
296 unsigned long conflicts = mask & ~dirmask;
302 traverse_trees_recursive(n, dirmask, conflicts, names, info);
309 static int unpack_failed(struct unpack_trees_options *o, const char *message)
313 return error(message);
316 discard_index(o->index);
317 read_index(o->index);
321 int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
323 static struct cache_entry *dfc;
326 die("unpack_trees takes at most four trees");
327 memset(&state, 0, sizeof(state));
331 state.refresh_cache = 1;
336 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
337 o->df_conflict_entry = dfc;
340 const char *prefix = o->prefix ? o->prefix : "";
341 struct traverse_info info;
343 setup_traverse_info(&info, prefix);
344 info.fn = unpack_callback;
347 if (traverse_trees(len, t, &info) < 0)
348 return unpack_failed(o, NULL);
351 /* Any left-over entries in the index? */
353 while (o->pos < o->index->cache_nr) {
354 struct cache_entry *ce = o->index->cache[o->pos];
355 if (unpack_index_entry(ce, o) < 0)
356 return unpack_failed(o, NULL);
360 if (o->trivial_merges_only && o->nontrivial_merge)
361 return unpack_failed(o, "Merge requires file-level merging");
367 /* Here come the merge functions */
369 static int reject_merge(struct cache_entry *ce)
371 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
375 static int same(struct cache_entry *a, struct cache_entry *b)
381 return a->ce_mode == b->ce_mode &&
382 !hashcmp(a->sha1, b->sha1);
387 * When a CE gets turned into an unmerged entry, we
388 * want it to be up-to-date
390 static int verify_uptodate(struct cache_entry *ce,
391 struct unpack_trees_options *o)
395 if (o->index_only || o->reset)
398 if (!lstat(ce->name, &st)) {
399 unsigned changed = ie_match_stat(o->index, ce, &st, CE_MATCH_IGNORE_VALID);
403 * NEEDSWORK: the current default policy is to allow
404 * submodule to be out of sync wrt the supermodule
405 * index. This needs to be tightened later for
406 * submodules that are marked to be automatically
409 if (S_ISGITLINK(ce->ce_mode))
415 return o->gently ? -1 :
416 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
419 static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
422 cache_tree_invalidate_path(o->index->cache_tree, ce->name);
426 * Check that checking out ce->sha1 in subdir ce->name is not
427 * going to overwrite any working files.
429 * Currently, git does not checkout subprojects during a superproject
430 * checkout, so it is not going to overwrite anything.
432 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
433 struct unpack_trees_options *o)
438 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
439 struct unpack_trees_options *o)
442 * we are about to extract "ce->name"; we would not want to lose
443 * anything in the existing directory there.
450 unsigned char sha1[20];
452 if (S_ISGITLINK(ce->ce_mode) &&
453 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
454 /* If we are not going to update the submodule, then
457 if (!hashcmp(sha1, ce->sha1))
459 return verify_clean_submodule(ce, action, o);
463 * First let's make sure we do not have a local modification
466 namelen = strlen(ce->name);
467 pos = index_name_pos(o->index, ce->name, namelen);
469 return cnt; /* we have it as nondirectory */
471 for (i = pos; i < o->index->cache_nr; i++) {
472 struct cache_entry *ce = o->index->cache[i];
473 int len = ce_namelen(ce);
475 strncmp(ce->name, ce->name, namelen) ||
476 ce->name[namelen] != '/')
479 * ce->name is an entry in the subdirectory.
482 if (verify_uptodate(ce, o))
484 ce->ce_flags |= CE_REMOVE;
490 * Then we need to make sure that we do not lose a locally
491 * present file that is not ignored.
493 pathbuf = xmalloc(namelen + 2);
494 memcpy(pathbuf, ce->name, namelen);
495 strcpy(pathbuf+namelen, "/");
497 memset(&d, 0, sizeof(d));
499 d.exclude_per_dir = o->dir->exclude_per_dir;
500 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
502 return o->gently ? -1 :
503 error("Updating '%s' would lose untracked files in it",
510 * We do not want to remove or overwrite a working tree file that
511 * is not tracked, unless it is ignored.
513 static int verify_absent(struct cache_entry *ce, const char *action,
514 struct unpack_trees_options *o)
518 if (o->index_only || o->reset || !o->update)
521 if (has_symlink_leading_path(ce->name, NULL))
524 if (!lstat(ce->name, &st)) {
526 int dtype = ce_to_dtype(ce);
528 if (o->dir && excluded(o->dir, ce->name, &dtype))
530 * ce->name is explicitly excluded, so it is Ok to
534 if (S_ISDIR(st.st_mode)) {
536 * We are checking out path "foo" and
537 * found "foo/." in the working tree.
538 * This is tricky -- if we have modified
539 * files that are in "foo/" we would lose
542 cnt = verify_clean_subdirectory(ce, action, o);
545 * If this removed entries from the index,
546 * what that means is:
548 * (1) the caller unpack_trees_rec() saw path/foo
549 * in the index, and it has not removed it because
550 * it thinks it is handling 'path' as blob with
552 * (2) we will return "ok, we placed a merged entry
553 * in the index" which would cause o->pos to be
554 * incremented by one;
555 * (3) however, original o->pos now has 'path/foo'
556 * marked with "to be removed".
558 * We need to increment it by the number of
559 * deleted entries here.
566 * The previous round may already have decided to
567 * delete this path, which is in a subdirectory that
568 * is being replaced with a blob.
570 cnt = index_name_pos(o->index, ce->name, strlen(ce->name));
572 struct cache_entry *ce = o->index->cache[cnt];
573 if (ce->ce_flags & CE_REMOVE)
577 return o->gently ? -1 :
578 error("Untracked working tree file '%s' "
579 "would be %s by merge.", ce->name, action);
584 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
585 struct unpack_trees_options *o)
587 merge->ce_flags |= CE_UPDATE;
590 * See if we can re-use the old CE directly?
591 * That way we get the uptodate stat info.
593 * This also removes the UPDATE flag on
596 if (same(old, merge)) {
597 copy_cache_entry(merge, old);
599 if (verify_uptodate(old, o))
601 invalidate_ce_path(old, o);
605 if (verify_absent(merge, "overwritten", o))
607 invalidate_ce_path(merge, o);
610 merge->ce_flags &= ~CE_STAGEMASK;
611 add_index_entry(o->index, merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
615 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
616 struct unpack_trees_options *o)
619 if (verify_uptodate(old, o))
622 if (verify_absent(ce, "removed", o))
624 ce->ce_flags |= CE_REMOVE;
625 add_index_entry(o->index, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
626 invalidate_ce_path(ce, o);
630 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
632 add_index_entry(o->index, ce, ADD_CACHE_OK_TO_ADD);
637 static void show_stage_entry(FILE *o,
638 const char *label, const struct cache_entry *ce)
641 fprintf(o, "%s (missing)\n", label);
643 fprintf(o, "%s%06o %s %d\t%s\n",
646 sha1_to_hex(ce->sha1),
652 int threeway_merge(struct cache_entry **stages,
653 struct unpack_trees_options *o,
656 struct cache_entry *index;
657 struct cache_entry *head;
658 struct cache_entry *remote = stages[o->head_idx + 1];
661 int remote_match = 0;
663 int df_conflict_head = 0;
664 int df_conflict_remote = 0;
666 int any_anc_missing = 0;
667 int no_anc_exists = 1;
670 for (i = 1; i < o->head_idx; i++) {
671 if (!stages[i] || stages[i] == o->df_conflict_entry)
678 head = stages[o->head_idx];
680 if (head == o->df_conflict_entry) {
681 df_conflict_head = 1;
685 if (remote == o->df_conflict_entry) {
686 df_conflict_remote = 1;
690 /* First, if there's a #16 situation, note that to prevent #13
693 if (!same(remote, head)) {
694 for (i = 1; i < o->head_idx; i++) {
695 if (same(stages[i], head)) {
698 if (same(stages[i], remote)) {
704 /* We start with cases where the index is allowed to match
705 * something other than the head: #14(ALT) and #2ALT, where it
706 * is permitted to match the result instead.
708 /* #14, #14ALT, #2ALT */
709 if (remote && !df_conflict_head && head_match && !remote_match) {
710 if (index && !same(index, remote) && !same(index, head))
711 return o->gently ? -1 : reject_merge(index);
712 return merged_entry(remote, index, o);
715 * If we have an entry in the index cache, then we want to
716 * make sure that it matches head.
718 if (index && !same(index, head))
719 return o->gently ? -1 : reject_merge(index);
723 if (same(head, remote))
724 return merged_entry(head, index, o);
726 if (!df_conflict_remote && remote_match && !head_match)
727 return merged_entry(head, index, o);
731 if (!head && !remote && any_anc_missing) {
732 remove_entry(remove, o);
736 /* Under the new "aggressive" rule, we resolve mostly trivial
737 * cases that we historically had git-merge-one-file resolve.
740 int head_deleted = !head && !df_conflict_head;
741 int remote_deleted = !remote && !df_conflict_remote;
742 struct cache_entry *ce = NULL;
751 for (i = 1; i < o->head_idx; i++) {
752 if (stages[i] && stages[i] != o->df_conflict_entry) {
761 * Deleted in one and unchanged in the other.
763 if ((head_deleted && remote_deleted) ||
764 (head_deleted && remote && remote_match) ||
765 (remote_deleted && head && head_match)) {
766 remove_entry(remove, o);
768 return deleted_entry(index, index, o);
769 else if (ce && !head_deleted) {
770 if (verify_absent(ce, "removed", o))
776 * Added in both, identically.
778 if (no_anc_exists && head && remote && same(head, remote))
779 return merged_entry(head, index, o);
783 /* Below are "no merge" cases, which require that the index be
784 * up-to-date to avoid the files getting overwritten with
785 * conflict resolution files.
788 if (verify_uptodate(index, o))
792 remove_entry(remove, o);
793 o->nontrivial_merge = 1;
795 /* #2, #3, #4, #6, #7, #9, #10, #11. */
797 if (!head_match || !remote_match) {
798 for (i = 1; i < o->head_idx; i++) {
799 if (stages[i] && stages[i] != o->df_conflict_entry) {
800 keep_entry(stages[i], o);
808 fprintf(stderr, "read-tree: warning #16 detected\n");
809 show_stage_entry(stderr, "head ", stages[head_match]);
810 show_stage_entry(stderr, "remote ", stages[remote_match]);
813 if (head) { count += keep_entry(head, o); }
814 if (remote) { count += keep_entry(remote, o); }
821 * The rule is to "carry forward" what is in the index without losing
822 * information across a "fast forward", favoring a successful merge
823 * over a merge failure when it makes sense. For details of the
824 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
827 int twoway_merge(struct cache_entry **src,
828 struct unpack_trees_options *o,
831 struct cache_entry *current = src[0];
832 struct cache_entry *oldtree = src[1];
833 struct cache_entry *newtree = src[2];
835 if (o->merge_size != 2)
836 return error("Cannot do a twoway merge of %d trees",
839 if (oldtree == o->df_conflict_entry)
841 if (newtree == o->df_conflict_entry)
845 if ((!oldtree && !newtree) || /* 4 and 5 */
846 (!oldtree && newtree &&
847 same(current, newtree)) || /* 6 and 7 */
848 (oldtree && newtree &&
849 same(oldtree, newtree)) || /* 14 and 15 */
850 (oldtree && newtree &&
851 !same(oldtree, newtree) && /* 18 and 19 */
852 same(current, newtree))) {
853 return keep_entry(current, o);
855 else if (oldtree && !newtree && same(current, oldtree)) {
857 remove_entry(remove, o);
858 return deleted_entry(oldtree, current, o);
860 else if (oldtree && newtree &&
861 same(current, oldtree) && !same(current, newtree)) {
863 return merged_entry(newtree, current, o);
866 /* all other failures */
867 remove_entry(remove, o);
869 return o->gently ? -1 : reject_merge(oldtree);
871 return o->gently ? -1 : reject_merge(current);
873 return o->gently ? -1 : reject_merge(newtree);
878 return merged_entry(newtree, current, o);
879 remove_entry(remove, o);
880 return deleted_entry(oldtree, current, o);
886 * Keep the index entries at stage0, collapse stage1 but make sure
887 * stage0 does not have anything there.
889 int bind_merge(struct cache_entry **src,
890 struct unpack_trees_options *o,
893 struct cache_entry *old = src[0];
894 struct cache_entry *a = src[1];
896 if (o->merge_size != 1)
897 return error("Cannot do a bind merge of %d trees\n",
900 return o->gently ? -1 :
901 error("Entry '%s' overlaps. Cannot bind.", a->name);
903 return keep_entry(old, o);
905 return merged_entry(a, NULL, o);
912 * - take the stat information from stage0, take the data from stage1
914 int oneway_merge(struct cache_entry **src,
915 struct unpack_trees_options *o,
918 struct cache_entry *old = src[0];
919 struct cache_entry *a = src[1];
921 if (o->merge_size != 1)
922 return error("Cannot do a oneway merge of %d trees",
926 remove_entry(remove, o);
927 return deleted_entry(old, old, o);
929 if (old && same(old, a)) {
932 if (lstat(old->name, &st) ||
933 ie_match_stat(o->index, old, &st, CE_MATCH_IGNORE_VALID))
934 old->ce_flags |= CE_UPDATE;
936 return keep_entry(old, o);
938 return merged_entry(a, old, o);