5 #include "cache-tree.h"
6 #include "unpack-trees.h"
12 struct tree_entry_list {
13 struct tree_entry_list *next;
14 unsigned directory : 1;
15 unsigned executable : 1;
19 const unsigned char *sha1;
22 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
24 struct tree_desc desc;
25 struct name_entry one;
26 struct tree_entry_list *ret = NULL;
27 struct tree_entry_list **list_p = &ret;
29 if (!tree->object.parsed)
32 init_tree_desc(&desc, tree->buffer, tree->size);
34 while (tree_entry(&desc, &one)) {
35 struct tree_entry_list *entry;
37 entry = xmalloc(sizeof(struct tree_entry_list));
38 entry->name = one.path;
39 entry->sha1 = one.sha1;
40 entry->mode = one.mode;
41 entry->directory = S_ISDIR(one.mode) != 0;
42 entry->executable = (one.mode & S_IXUSR) != 0;
43 entry->symlink = S_ISLNK(one.mode) != 0;
47 list_p = &entry->next;
52 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
54 int len1 = strlen(name1);
55 int len2 = strlen(name2);
56 int len = len1 < len2 ? len1 : len2;
57 int ret = memcmp(name1, name2, len);
67 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
73 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
74 const char *base, struct unpack_trees_options *o,
75 struct tree_entry_list *df_conflict_list)
77 int baselen = strlen(base);
78 int src_size = len + 1;
83 i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
91 struct tree_entry_list **subposns;
92 struct cache_entry **src;
98 /* Find the first name in the input. */
103 /* Check the cache */
104 if (o->merge && o->pos < active_nr) {
105 /* This is a bit tricky: */
106 /* If the index has a subdirectory (with
107 * contents) as the first name, it'll get a
108 * filename like "foo/bar". But that's after
109 * "foo", so the entry in trees will get
110 * handled first, at which point we'll go into
111 * "foo", and deal with "bar" from the index,
112 * because the base will be "foo/". The only
113 * way we can actually have "foo/bar" first of
114 * all the things is if the trees don't
115 * contain "foo" at all, in which case we'll
116 * handle "foo/bar" without going into the
117 * directory, but that's fine (and will return
118 * an error anyway, with the added unknown
122 cache_name = active_cache[o->pos]->name;
123 if (strlen(cache_name) > baselen &&
124 !memcmp(cache_name, base, baselen)) {
125 cache_name += baselen;
134 printf("index %s\n", first);
136 for (i = 0; i < len; i++) {
137 if (!posns[i] || posns[i] == df_conflict_list)
140 printf("%d %s\n", i + 1, posns[i]->name);
142 if (!first || entcmp(first, firstdir,
144 posns[i]->directory) > 0) {
145 first = posns[i]->name;
146 firstdir = posns[i]->directory;
149 /* No name means we're done */
151 goto leave_directory;
153 pathlen = strlen(first);
154 ce_size = cache_entry_size(baselen + pathlen);
156 src = xcalloc(src_size, sizeof(struct cache_entry *));
158 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
160 if (cache_name && !strcmp(cache_name, first)) {
162 src[0] = active_cache[o->pos];
163 remove_cache_entry_at(o->pos);
166 for (i = 0; i < len; i++) {
167 struct cache_entry *ce;
170 (posns[i] != df_conflict_list &&
171 strcmp(first, posns[i]->name))) {
175 if (posns[i] == df_conflict_list) {
176 src[i + o->merge] = o->df_conflict_entry;
180 if (posns[i]->directory) {
181 struct tree *tree = lookup_tree(posns[i]->sha1);
184 subposns[i] = create_tree_entry_list(tree);
185 posns[i] = posns[i]->next;
186 src[i + o->merge] = o->df_conflict_entry;
192 else if (i + 1 < o->head_idx)
194 else if (i + 1 > o->head_idx)
199 ce = xcalloc(1, ce_size);
200 ce->ce_mode = create_ce_mode(posns[i]->mode);
201 ce->ce_flags = create_ce_flags(baselen + pathlen,
203 memcpy(ce->name, base, baselen);
204 memcpy(ce->name + baselen, first, pathlen + 1);
208 hashcpy(ce->sha1, posns[i]->sha1);
209 src[i + o->merge] = ce;
210 subposns[i] = df_conflict_list;
211 posns[i] = posns[i]->next;
218 printf("%s:\n", first);
219 for (i = 0; i < src_size; i++) {
222 printf("%s\n", sha1_to_hex(src[i]->sha1));
230 printf("Added %d entries\n", ret);
234 for (i = 0; i < src_size; i++) {
236 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
242 char *newbase = xmalloc(baselen + 2 + pathlen);
243 memcpy(newbase, base, baselen);
244 memcpy(newbase + baselen, first, pathlen);
245 newbase[baselen + pathlen] = '/';
246 newbase[baselen + pathlen + 1] = '\0';
247 if (unpack_trees_rec(subposns, len, newbase, o,
250 goto leave_directory;
260 pop_exclude_per_directory(o->dir, i_stk);
264 /* Unlink the last component and attempt to remove leading
265 * directories, in case this unlink is the removal of the
266 * last entry in the directory -- empty directories are removed.
268 static void unlink_entry(char *name, char *last_symlink)
272 if (has_symlink_leading_path(name, last_symlink))
279 cp = strrchr(name, '/');
286 status = rmdir(name);
295 static struct checkout state;
296 static void check_updates(struct cache_entry **src, int nr,
297 struct unpack_trees_options *o)
299 unsigned short mask = htons(CE_UPDATE);
300 unsigned cnt = 0, total = 0;
301 struct progress progress;
302 char last_symlink[PATH_MAX];
304 if (o->update && o->verbose_update) {
305 for (total = cnt = 0; cnt < nr; cnt++) {
306 struct cache_entry *ce = src[cnt];
307 if (!ce->ce_mode || ce->ce_flags & mask)
311 start_progress_delay(&progress, "Checking %u files out...",
316 *last_symlink = '\0';
318 struct cache_entry *ce = *src++;
321 if (!ce->ce_mode || ce->ce_flags & mask)
322 display_progress(&progress, ++cnt);
325 unlink_entry(ce->name, last_symlink);
328 if (ce->ce_flags & mask) {
329 ce->ce_flags &= ~mask;
331 checkout_entry(ce, &state, NULL);
332 *last_symlink = '\0';
337 stop_progress(&progress);;
340 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
342 unsigned len = object_list_length(trees);
343 struct tree_entry_list **posns;
345 struct object_list *posn = trees;
346 struct tree_entry_list df_conflict_list;
347 static struct cache_entry *dfc;
349 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
350 df_conflict_list.next = &df_conflict_list;
351 memset(&state, 0, sizeof(state));
355 state.refresh_cache = 1;
360 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
361 o->df_conflict_entry = dfc;
364 posns = xmalloc(len * sizeof(struct tree_entry_list *));
365 for (i = 0; i < len; i++) {
366 posns[i] = create_tree_entry_list((struct tree *) posn->item);
369 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
370 o, &df_conflict_list))
374 if (o->trivial_merges_only && o->nontrivial_merge)
375 die("Merge requires file-level merging");
377 check_updates(active_cache, active_nr, o);
381 /* Here come the merge functions */
383 static void reject_merge(struct cache_entry *ce)
385 die("Entry '%s' would be overwritten by merge. Cannot merge.",
389 static int same(struct cache_entry *a, struct cache_entry *b)
395 return a->ce_mode == b->ce_mode &&
396 !hashcmp(a->sha1, b->sha1);
401 * When a CE gets turned into an unmerged entry, we
402 * want it to be up-to-date
404 static void verify_uptodate(struct cache_entry *ce,
405 struct unpack_trees_options *o)
409 if (o->index_only || o->reset)
412 if (!lstat(ce->name, &st)) {
413 unsigned changed = ce_match_stat(ce, &st, 1);
420 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
423 static void invalidate_ce_path(struct cache_entry *ce)
426 cache_tree_invalidate_path(active_cache_tree, ce->name);
430 * Check that checking out ce->sha1 in subdir ce->name is not
431 * going to overwrite any working files.
433 * Currently, git does not checkout subprojects during a superproject
434 * checkout, so it is not going to overwrite anything.
436 static int verify_clean_submodule(struct cache_entry *ce, const char *action,
437 struct unpack_trees_options *o)
442 static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
443 struct unpack_trees_options *o)
446 * we are about to extract "ce->name"; we would not want to lose
447 * anything in the existing directory there.
454 unsigned char sha1[20];
456 if (S_ISGITLINK(ntohl(ce->ce_mode)) &&
457 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
458 /* If we are not going to update the submodule, then
461 if (!hashcmp(sha1, ce->sha1))
463 return verify_clean_submodule(ce, action, o);
467 * First let's make sure we do not have a local modification
470 namelen = strlen(ce->name);
471 pos = cache_name_pos(ce->name, namelen);
473 return cnt; /* we have it as nondirectory */
475 for (i = pos; i < active_nr; i++) {
476 struct cache_entry *ce = active_cache[i];
477 int len = ce_namelen(ce);
479 strncmp(ce->name, ce->name, namelen) ||
480 ce->name[namelen] != '/')
483 * ce->name is an entry in the subdirectory.
486 verify_uptodate(ce, o);
493 * Then we need to make sure that we do not lose a locally
494 * present file that is not ignored.
496 pathbuf = xmalloc(namelen + 2);
497 memcpy(pathbuf, ce->name, namelen);
498 strcpy(pathbuf+namelen, "/");
500 memset(&d, 0, sizeof(d));
502 d.exclude_per_dir = o->dir->exclude_per_dir;
503 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
505 die("Updating '%s' would lose untracked files in it",
512 * We do not want to remove or overwrite a working tree file that
513 * is not tracked, unless it is ignored.
515 static void verify_absent(struct cache_entry *ce, const char *action,
516 struct unpack_trees_options *o)
520 if (o->index_only || o->reset || !o->update)
523 if (has_symlink_leading_path(ce->name, NULL))
526 if (!lstat(ce->name, &st)) {
529 if (o->dir && excluded(o->dir, ce->name))
531 * ce->name is explicitly excluded, so it is Ok to
535 if (S_ISDIR(st.st_mode)) {
537 * We are checking out path "foo" and
538 * found "foo/." in the working tree.
539 * This is tricky -- if we have modified
540 * files that are in "foo/" we would lose
543 cnt = verify_clean_subdirectory(ce, action, o);
546 * If this removed entries from the index,
547 * what that means is:
549 * (1) the caller unpack_trees_rec() saw path/foo
550 * in the index, and it has not removed it because
551 * it thinks it is handling 'path' as blob with
553 * (2) we will return "ok, we placed a merged entry
554 * in the index" which would cause o->pos to be
555 * incremented by one;
556 * (3) however, original o->pos now has 'path/foo'
557 * marked with "to be removed".
559 * We need to increment it by the number of
560 * deleted entries here.
567 * The previous round may already have decided to
568 * delete this path, which is in a subdirectory that
569 * is being replaced with a blob.
571 cnt = cache_name_pos(ce->name, strlen(ce->name));
573 struct cache_entry *ce = active_cache[cnt];
574 if (!ce_stage(ce) && !ce->ce_mode)
578 die("Untracked working tree file '%s' "
579 "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 |= htons(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)) {
598 verify_uptodate(old, o);
599 invalidate_ce_path(old);
603 verify_absent(merge, "overwritten", o);
604 invalidate_ce_path(merge);
607 merge->ce_flags &= ~htons(CE_STAGEMASK);
608 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
612 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
613 struct unpack_trees_options *o)
616 verify_uptodate(old, o);
618 verify_absent(ce, "removed", o);
620 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
621 invalidate_ce_path(ce);
625 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
627 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
632 static void show_stage_entry(FILE *o,
633 const char *label, const struct cache_entry *ce)
636 fprintf(o, "%s (missing)\n", label);
638 fprintf(o, "%s%06o %s %d\t%s\n",
641 sha1_to_hex(ce->sha1),
647 int threeway_merge(struct cache_entry **stages,
648 struct unpack_trees_options *o)
650 struct cache_entry *index;
651 struct cache_entry *head;
652 struct cache_entry *remote = stages[o->head_idx + 1];
655 int remote_match = 0;
657 int df_conflict_head = 0;
658 int df_conflict_remote = 0;
660 int any_anc_missing = 0;
661 int no_anc_exists = 1;
664 for (i = 1; i < o->head_idx; i++) {
665 if (!stages[i] || stages[i] == o->df_conflict_entry)
672 head = stages[o->head_idx];
674 if (head == o->df_conflict_entry) {
675 df_conflict_head = 1;
679 if (remote == o->df_conflict_entry) {
680 df_conflict_remote = 1;
684 /* First, if there's a #16 situation, note that to prevent #13
687 if (!same(remote, head)) {
688 for (i = 1; i < o->head_idx; i++) {
689 if (same(stages[i], head)) {
692 if (same(stages[i], remote)) {
698 /* We start with cases where the index is allowed to match
699 * something other than the head: #14(ALT) and #2ALT, where it
700 * is permitted to match the result instead.
702 /* #14, #14ALT, #2ALT */
703 if (remote && !df_conflict_head && head_match && !remote_match) {
704 if (index && !same(index, remote) && !same(index, head))
706 return merged_entry(remote, index, o);
709 * If we have an entry in the index cache, then we want to
710 * make sure that it matches head.
712 if (index && !same(index, head)) {
718 if (same(head, remote))
719 return merged_entry(head, index, o);
721 if (!df_conflict_remote && remote_match && !head_match)
722 return merged_entry(head, index, o);
726 if (!head && !remote && any_anc_missing)
729 /* Under the new "aggressive" rule, we resolve mostly trivial
730 * cases that we historically had git-merge-one-file resolve.
733 int head_deleted = !head && !df_conflict_head;
734 int remote_deleted = !remote && !df_conflict_remote;
735 struct cache_entry *ce = NULL;
744 for (i = 1; i < o->head_idx; i++) {
745 if (stages[i] && stages[i] != o->df_conflict_entry) {
754 * Deleted in one and unchanged in the other.
756 if ((head_deleted && remote_deleted) ||
757 (head_deleted && remote && remote_match) ||
758 (remote_deleted && head && head_match)) {
760 return deleted_entry(index, index, o);
761 else if (ce && !head_deleted)
762 verify_absent(ce, "removed", o);
766 * Added in both, identically.
768 if (no_anc_exists && head && remote && same(head, remote))
769 return merged_entry(head, index, o);
773 /* Below are "no merge" cases, which require that the index be
774 * up-to-date to avoid the files getting overwritten with
775 * conflict resolution files.
778 verify_uptodate(index, o);
781 o->nontrivial_merge = 1;
783 /* #2, #3, #4, #6, #7, #9, #10, #11. */
785 if (!head_match || !remote_match) {
786 for (i = 1; i < o->head_idx; i++) {
787 if (stages[i] && stages[i] != o->df_conflict_entry) {
788 keep_entry(stages[i], o);
796 fprintf(stderr, "read-tree: warning #16 detected\n");
797 show_stage_entry(stderr, "head ", stages[head_match]);
798 show_stage_entry(stderr, "remote ", stages[remote_match]);
801 if (head) { count += keep_entry(head, o); }
802 if (remote) { count += keep_entry(remote, o); }
809 * The rule is to "carry forward" what is in the index without losing
810 * information across a "fast forward", favoring a successful merge
811 * over a merge failure when it makes sense. For details of the
812 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
815 int twoway_merge(struct cache_entry **src,
816 struct unpack_trees_options *o)
818 struct cache_entry *current = src[0];
819 struct cache_entry *oldtree = src[1];
820 struct cache_entry *newtree = src[2];
822 if (o->merge_size != 2)
823 return error("Cannot do a twoway merge of %d trees",
826 if (oldtree == o->df_conflict_entry)
828 if (newtree == o->df_conflict_entry)
832 if ((!oldtree && !newtree) || /* 4 and 5 */
833 (!oldtree && newtree &&
834 same(current, newtree)) || /* 6 and 7 */
835 (oldtree && newtree &&
836 same(oldtree, newtree)) || /* 14 and 15 */
837 (oldtree && newtree &&
838 !same(oldtree, newtree) && /* 18 and 19 */
839 same(current, newtree))) {
840 return keep_entry(current, o);
842 else if (oldtree && !newtree && same(current, oldtree)) {
844 return deleted_entry(oldtree, current, o);
846 else if (oldtree && newtree &&
847 same(current, oldtree) && !same(current, newtree)) {
849 return merged_entry(newtree, current, o);
852 /* all other failures */
854 reject_merge(oldtree);
856 reject_merge(current);
858 reject_merge(newtree);
863 return merged_entry(newtree, current, o);
865 return deleted_entry(oldtree, current, o);
871 * Keep the index entries at stage0, collapse stage1 but make sure
872 * stage0 does not have anything there.
874 int bind_merge(struct cache_entry **src,
875 struct unpack_trees_options *o)
877 struct cache_entry *old = src[0];
878 struct cache_entry *a = src[1];
880 if (o->merge_size != 1)
881 return error("Cannot do a bind merge of %d trees\n",
884 die("Entry '%s' overlaps. Cannot bind.", a->name);
886 return keep_entry(old, o);
888 return merged_entry(a, NULL, o);
895 * - take the stat information from stage0, take the data from stage1
897 int oneway_merge(struct cache_entry **src,
898 struct unpack_trees_options *o)
900 struct cache_entry *old = src[0];
901 struct cache_entry *a = src[1];
903 if (o->merge_size != 1)
904 return error("Cannot do a oneway merge of %d trees",
908 return deleted_entry(old, old, o);
909 if (old && same(old, a)) {
912 if (lstat(old->name, &st) ||
913 ce_match_stat(old, &st, 1))
914 old->ce_flags |= htons(CE_UPDATE);
916 return keep_entry(old, o);
918 return merged_entry(a, old, o);