5 #include "cache-tree.h"
6 #include "unpack-trees.h"
11 struct tree_entry_list {
12 struct tree_entry_list *next;
13 unsigned directory : 1;
14 unsigned executable : 1;
18 const unsigned char *sha1;
21 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
23 struct tree_desc desc;
24 struct name_entry one;
25 struct tree_entry_list *ret = NULL;
26 struct tree_entry_list **list_p = &ret;
28 if (!tree->object.parsed)
31 init_tree_desc(&desc, tree->buffer, tree->size);
33 while (tree_entry(&desc, &one)) {
34 struct tree_entry_list *entry;
36 entry = xmalloc(sizeof(struct tree_entry_list));
37 entry->name = one.path;
38 entry->sha1 = one.sha1;
39 entry->mode = one.mode;
40 entry->directory = S_ISDIR(one.mode) != 0;
41 entry->executable = (one.mode & S_IXUSR) != 0;
42 entry->symlink = S_ISLNK(one.mode) != 0;
46 list_p = &entry->next;
51 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
53 int len1 = strlen(name1);
54 int len2 = strlen(name2);
55 int len = len1 < len2 ? len1 : len2;
56 int ret = memcmp(name1, name2, len);
66 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
72 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
73 const char *base, struct unpack_trees_options *o,
74 struct tree_entry_list *df_conflict_list)
76 int baselen = strlen(base);
77 int src_size = len + 1;
82 i_stk = push_exclude_per_directory(o->dir, base, strlen(base));
90 struct tree_entry_list **subposns;
91 struct cache_entry **src;
97 /* Find the first name in the input. */
102 /* Check the cache */
103 if (o->merge && o->pos < active_nr) {
104 /* This is a bit tricky: */
105 /* If the index has a subdirectory (with
106 * contents) as the first name, it'll get a
107 * filename like "foo/bar". But that's after
108 * "foo", so the entry in trees will get
109 * handled first, at which point we'll go into
110 * "foo", and deal with "bar" from the index,
111 * because the base will be "foo/". The only
112 * way we can actually have "foo/bar" first of
113 * all the things is if the trees don't
114 * contain "foo" at all, in which case we'll
115 * handle "foo/bar" without going into the
116 * directory, but that's fine (and will return
117 * an error anyway, with the added unknown
121 cache_name = active_cache[o->pos]->name;
122 if (strlen(cache_name) > baselen &&
123 !memcmp(cache_name, base, baselen)) {
124 cache_name += baselen;
133 printf("index %s\n", first);
135 for (i = 0; i < len; i++) {
136 if (!posns[i] || posns[i] == df_conflict_list)
139 printf("%d %s\n", i + 1, posns[i]->name);
141 if (!first || entcmp(first, firstdir,
143 posns[i]->directory) > 0) {
144 first = posns[i]->name;
145 firstdir = posns[i]->directory;
148 /* No name means we're done */
150 goto leave_directory;
152 pathlen = strlen(first);
153 ce_size = cache_entry_size(baselen + pathlen);
155 src = xcalloc(src_size, sizeof(struct cache_entry *));
157 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
159 if (cache_name && !strcmp(cache_name, first)) {
161 src[0] = active_cache[o->pos];
162 remove_cache_entry_at(o->pos);
165 for (i = 0; i < len; i++) {
166 struct cache_entry *ce;
169 (posns[i] != df_conflict_list &&
170 strcmp(first, posns[i]->name))) {
174 if (posns[i] == df_conflict_list) {
175 src[i + o->merge] = o->df_conflict_entry;
179 if (posns[i]->directory) {
180 struct tree *tree = lookup_tree(posns[i]->sha1);
183 subposns[i] = create_tree_entry_list(tree);
184 posns[i] = posns[i]->next;
185 src[i + o->merge] = o->df_conflict_entry;
191 else if (i + 1 < o->head_idx)
193 else if (i + 1 > o->head_idx)
198 ce = xcalloc(1, ce_size);
199 ce->ce_mode = create_ce_mode(posns[i]->mode);
200 ce->ce_flags = create_ce_flags(baselen + pathlen,
202 memcpy(ce->name, base, baselen);
203 memcpy(ce->name + baselen, first, pathlen + 1);
207 hashcpy(ce->sha1, posns[i]->sha1);
208 src[i + o->merge] = ce;
209 subposns[i] = df_conflict_list;
210 posns[i] = posns[i]->next;
217 printf("%s:\n", first);
218 for (i = 0; i < src_size; i++) {
221 printf("%s\n", sha1_to_hex(src[i]->sha1));
229 printf("Added %d entries\n", ret);
233 for (i = 0; i < src_size; i++) {
235 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
241 char *newbase = xmalloc(baselen + 2 + pathlen);
242 memcpy(newbase, base, baselen);
243 memcpy(newbase + baselen, first, pathlen);
244 newbase[baselen + pathlen] = '/';
245 newbase[baselen + pathlen + 1] = '\0';
246 if (unpack_trees_rec(subposns, len, newbase, o,
249 goto leave_directory;
259 pop_exclude_per_directory(o->dir, i_stk);
263 /* Unlink the last component and attempt to remove leading
264 * directories, in case this unlink is the removal of the
265 * last entry in the directory -- empty directories are removed.
267 static void unlink_entry(char *name, char *last_symlink)
271 if (has_symlink_leading_path(name, last_symlink))
278 cp = strrchr(name, '/');
285 status = rmdir(name);
294 static struct checkout state;
295 static void check_updates(struct cache_entry **src, int nr,
296 struct unpack_trees_options *o)
298 unsigned short mask = htons(CE_UPDATE);
299 unsigned cnt = 0, total = 0;
300 struct progress progress;
301 char last_symlink[PATH_MAX];
303 if (o->update && o->verbose_update) {
304 for (total = cnt = 0; cnt < nr; cnt++) {
305 struct cache_entry *ce = src[cnt];
306 if (!ce->ce_mode || ce->ce_flags & mask)
310 start_progress_delay(&progress, "Checking %u files out...",
315 *last_symlink = '\0';
317 struct cache_entry *ce = *src++;
320 if (!ce->ce_mode || ce->ce_flags & mask)
321 display_progress(&progress, ++cnt);
324 unlink_entry(ce->name, last_symlink);
327 if (ce->ce_flags & mask) {
328 ce->ce_flags &= ~mask;
330 checkout_entry(ce, &state, NULL);
331 *last_symlink = '\0';
336 stop_progress(&progress);;
339 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
341 unsigned len = object_list_length(trees);
342 struct tree_entry_list **posns;
344 struct object_list *posn = trees;
345 struct tree_entry_list df_conflict_list;
346 static struct cache_entry *dfc;
348 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
349 df_conflict_list.next = &df_conflict_list;
350 memset(&state, 0, sizeof(state));
354 state.refresh_cache = 1;
359 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
360 o->df_conflict_entry = dfc;
363 posns = xmalloc(len * sizeof(struct tree_entry_list *));
364 for (i = 0; i < len; i++) {
365 posns[i] = create_tree_entry_list((struct tree *) posn->item);
368 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
369 o, &df_conflict_list))
373 if (o->trivial_merges_only && o->nontrivial_merge)
374 die("Merge requires file-level merging");
376 check_updates(active_cache, active_nr, o);
380 /* Here come the merge functions */
382 static void reject_merge(struct cache_entry *ce)
384 die("Entry '%s' would be overwritten by merge. Cannot merge.",
388 static int same(struct cache_entry *a, struct cache_entry *b)
394 return a->ce_mode == b->ce_mode &&
395 !hashcmp(a->sha1, b->sha1);
400 * When a CE gets turned into an unmerged entry, we
401 * want it to be up-to-date
403 static void verify_uptodate(struct cache_entry *ce,
404 struct unpack_trees_options *o)
408 if (o->index_only || o->reset)
411 if (!lstat(ce->name, &st)) {
412 unsigned changed = ce_match_stat(ce, &st, 1);
419 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
422 static void invalidate_ce_path(struct cache_entry *ce)
425 cache_tree_invalidate_path(active_cache_tree, ce->name);
428 static int verify_clean_subdirectory(const char *path, const char *action,
429 struct unpack_trees_options *o)
432 * we are about to extract "path"; we would not want to lose
433 * anything in the existing directory there.
442 * First let's make sure we do not have a local modification
445 namelen = strlen(path);
446 pos = cache_name_pos(path, namelen);
448 return cnt; /* we have it as nondirectory */
450 for (i = pos; i < active_nr; i++) {
451 struct cache_entry *ce = active_cache[i];
452 int len = ce_namelen(ce);
454 strncmp(path, ce->name, namelen) ||
455 ce->name[namelen] != '/')
458 * ce->name is an entry in the subdirectory.
461 verify_uptodate(ce, o);
468 * Then we need to make sure that we do not lose a locally
469 * present file that is not ignored.
471 pathbuf = xmalloc(namelen + 2);
472 memcpy(pathbuf, path, namelen);
473 strcpy(pathbuf+namelen, "/");
475 memset(&d, 0, sizeof(d));
477 d.exclude_per_dir = o->dir->exclude_per_dir;
478 i = read_directory(&d, path, pathbuf, namelen+1, NULL);
480 die("Updating '%s' would lose untracked files in it",
487 * We do not want to remove or overwrite a working tree file that
488 * is not tracked, unless it is ignored.
490 static void verify_absent(const char *path, const char *action,
491 struct unpack_trees_options *o)
495 if (o->index_only || o->reset || !o->update)
498 if (!lstat(path, &st)) {
501 if (o->dir && excluded(o->dir, path))
503 * path is explicitly excluded, so it is Ok to
507 if (S_ISDIR(st.st_mode)) {
509 * We are checking out path "foo" and
510 * found "foo/." in the working tree.
511 * This is tricky -- if we have modified
512 * files that are in "foo/" we would lose
515 cnt = verify_clean_subdirectory(path, action, o);
518 * If this removed entries from the index,
519 * what that means is:
521 * (1) the caller unpack_trees_rec() saw path/foo
522 * in the index, and it has not removed it because
523 * it thinks it is handling 'path' as blob with
525 * (2) we will return "ok, we placed a merged entry
526 * in the index" which would cause o->pos to be
527 * incremented by one;
528 * (3) however, original o->pos now has 'path/foo'
529 * marked with "to be removed".
531 * We need to increment it by the number of
532 * deleted entries here.
539 * The previous round may already have decided to
540 * delete this path, which is in a subdirectory that
541 * is being replaced with a blob.
543 cnt = cache_name_pos(path, strlen(path));
545 struct cache_entry *ce = active_cache[cnt];
546 if (!ce_stage(ce) && !ce->ce_mode)
550 die("Untracked working tree file '%s' "
551 "would be %s by merge.", path, action);
555 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
556 struct unpack_trees_options *o)
558 merge->ce_flags |= htons(CE_UPDATE);
561 * See if we can re-use the old CE directly?
562 * That way we get the uptodate stat info.
564 * This also removes the UPDATE flag on
567 if (same(old, merge)) {
570 verify_uptodate(old, o);
571 invalidate_ce_path(old);
575 verify_absent(merge->name, "overwritten", o);
576 invalidate_ce_path(merge);
579 merge->ce_flags &= ~htons(CE_STAGEMASK);
580 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
584 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
585 struct unpack_trees_options *o)
588 verify_uptodate(old, o);
590 verify_absent(ce->name, "removed", o);
592 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
593 invalidate_ce_path(ce);
597 static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
599 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
604 static void show_stage_entry(FILE *o,
605 const char *label, const struct cache_entry *ce)
608 fprintf(o, "%s (missing)\n", label);
610 fprintf(o, "%s%06o %s %d\t%s\n",
613 sha1_to_hex(ce->sha1),
619 int threeway_merge(struct cache_entry **stages,
620 struct unpack_trees_options *o)
622 struct cache_entry *index;
623 struct cache_entry *head;
624 struct cache_entry *remote = stages[o->head_idx + 1];
627 int remote_match = 0;
629 int df_conflict_head = 0;
630 int df_conflict_remote = 0;
632 int any_anc_missing = 0;
633 int no_anc_exists = 1;
636 for (i = 1; i < o->head_idx; i++) {
637 if (!stages[i] || stages[i] == o->df_conflict_entry)
644 head = stages[o->head_idx];
646 if (head == o->df_conflict_entry) {
647 df_conflict_head = 1;
651 if (remote == o->df_conflict_entry) {
652 df_conflict_remote = 1;
656 /* First, if there's a #16 situation, note that to prevent #13
659 if (!same(remote, head)) {
660 for (i = 1; i < o->head_idx; i++) {
661 if (same(stages[i], head)) {
664 if (same(stages[i], remote)) {
670 /* We start with cases where the index is allowed to match
671 * something other than the head: #14(ALT) and #2ALT, where it
672 * is permitted to match the result instead.
674 /* #14, #14ALT, #2ALT */
675 if (remote && !df_conflict_head && head_match && !remote_match) {
676 if (index && !same(index, remote) && !same(index, head))
678 return merged_entry(remote, index, o);
681 * If we have an entry in the index cache, then we want to
682 * make sure that it matches head.
684 if (index && !same(index, head)) {
690 if (same(head, remote))
691 return merged_entry(head, index, o);
693 if (!df_conflict_remote && remote_match && !head_match)
694 return merged_entry(head, index, o);
698 if (!head && !remote && any_anc_missing)
701 /* Under the new "aggressive" rule, we resolve mostly trivial
702 * cases that we historically had git-merge-one-file resolve.
705 int head_deleted = !head && !df_conflict_head;
706 int remote_deleted = !remote && !df_conflict_remote;
707 const char *path = NULL;
716 for (i = 1; i < o->head_idx; i++) {
717 if (stages[i] && stages[i] != o->df_conflict_entry) {
718 path = stages[i]->name;
726 * Deleted in one and unchanged in the other.
728 if ((head_deleted && remote_deleted) ||
729 (head_deleted && remote && remote_match) ||
730 (remote_deleted && head && head_match)) {
732 return deleted_entry(index, index, o);
733 else if (path && !head_deleted)
734 verify_absent(path, "removed", o);
738 * Added in both, identically.
740 if (no_anc_exists && head && remote && same(head, remote))
741 return merged_entry(head, index, o);
745 /* Below are "no merge" cases, which require that the index be
746 * up-to-date to avoid the files getting overwritten with
747 * conflict resolution files.
750 verify_uptodate(index, o);
753 o->nontrivial_merge = 1;
755 /* #2, #3, #4, #6, #7, #9, #10, #11. */
757 if (!head_match || !remote_match) {
758 for (i = 1; i < o->head_idx; i++) {
759 if (stages[i] && stages[i] != o->df_conflict_entry) {
760 keep_entry(stages[i], o);
768 fprintf(stderr, "read-tree: warning #16 detected\n");
769 show_stage_entry(stderr, "head ", stages[head_match]);
770 show_stage_entry(stderr, "remote ", stages[remote_match]);
773 if (head) { count += keep_entry(head, o); }
774 if (remote) { count += keep_entry(remote, o); }
781 * The rule is to "carry forward" what is in the index without losing
782 * information across a "fast forward", favoring a successful merge
783 * over a merge failure when it makes sense. For details of the
784 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
787 int twoway_merge(struct cache_entry **src,
788 struct unpack_trees_options *o)
790 struct cache_entry *current = src[0];
791 struct cache_entry *oldtree = src[1];
792 struct cache_entry *newtree = src[2];
794 if (o->merge_size != 2)
795 return error("Cannot do a twoway merge of %d trees",
798 if (oldtree == o->df_conflict_entry)
800 if (newtree == o->df_conflict_entry)
804 if ((!oldtree && !newtree) || /* 4 and 5 */
805 (!oldtree && newtree &&
806 same(current, newtree)) || /* 6 and 7 */
807 (oldtree && newtree &&
808 same(oldtree, newtree)) || /* 14 and 15 */
809 (oldtree && newtree &&
810 !same(oldtree, newtree) && /* 18 and 19 */
811 same(current, newtree))) {
812 return keep_entry(current, o);
814 else if (oldtree && !newtree && same(current, oldtree)) {
816 return deleted_entry(oldtree, current, o);
818 else if (oldtree && newtree &&
819 same(current, oldtree) && !same(current, newtree)) {
821 return merged_entry(newtree, current, o);
824 /* all other failures */
826 reject_merge(oldtree);
828 reject_merge(current);
830 reject_merge(newtree);
835 return merged_entry(newtree, current, o);
837 return deleted_entry(oldtree, current, o);
843 * Keep the index entries at stage0, collapse stage1 but make sure
844 * stage0 does not have anything there.
846 int bind_merge(struct cache_entry **src,
847 struct unpack_trees_options *o)
849 struct cache_entry *old = src[0];
850 struct cache_entry *a = src[1];
852 if (o->merge_size != 1)
853 return error("Cannot do a bind merge of %d trees\n",
856 die("Entry '%s' overlaps. Cannot bind.", a->name);
858 return keep_entry(old, o);
860 return merged_entry(a, NULL, o);
867 * - take the stat information from stage0, take the data from stage1
869 int oneway_merge(struct cache_entry **src,
870 struct unpack_trees_options *o)
872 struct cache_entry *old = src[0];
873 struct cache_entry *a = src[1];
875 if (o->merge_size != 1)
876 return error("Cannot do a oneway merge of %d trees",
880 return deleted_entry(old, old, o);
881 if (old && same(old, a)) {
884 if (lstat(old->name, &st) ||
885 ce_match_stat(old, &st, 1))
886 old->ce_flags |= htons(CE_UPDATE);
888 return keep_entry(old, o);
890 return merged_entry(a, old, o);