6 #include "cache-tree.h"
7 #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 desc.buf = tree->buffer;
32 desc.size = 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,
76 struct tree_entry_list *df_conflict_list)
78 int baselen = strlen(base);
79 int src_size = len + 1;
86 struct tree_entry_list **subposns;
87 struct cache_entry **src;
93 /* Find the first name in the input. */
99 if (o->merge && *indpos < active_nr) {
100 /* This is a bit tricky: */
101 /* If the index has a subdirectory (with
102 * contents) as the first name, it'll get a
103 * filename like "foo/bar". But that's after
104 * "foo", so the entry in trees will get
105 * handled first, at which point we'll go into
106 * "foo", and deal with "bar" from the index,
107 * because the base will be "foo/". The only
108 * way we can actually have "foo/bar" first of
109 * all the things is if the trees don't
110 * contain "foo" at all, in which case we'll
111 * handle "foo/bar" without going into the
112 * directory, but that's fine (and will return
113 * an error anyway, with the added unknown
117 cache_name = active_cache[*indpos]->name;
118 if (strlen(cache_name) > baselen &&
119 !memcmp(cache_name, base, baselen)) {
120 cache_name += baselen;
129 printf("index %s\n", first);
131 for (i = 0; i < len; i++) {
132 if (!posns[i] || posns[i] == df_conflict_list)
135 printf("%d %s\n", i + 1, posns[i]->name);
137 if (!first || entcmp(first, firstdir,
139 posns[i]->directory) > 0) {
140 first = posns[i]->name;
141 firstdir = posns[i]->directory;
144 /* No name means we're done */
148 pathlen = strlen(first);
149 ce_size = cache_entry_size(baselen + pathlen);
151 src = xcalloc(src_size, sizeof(struct cache_entry *));
153 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
155 if (cache_name && !strcmp(cache_name, first)) {
157 src[0] = active_cache[*indpos];
158 remove_cache_entry_at(*indpos);
161 for (i = 0; i < len; i++) {
162 struct cache_entry *ce;
165 (posns[i] != df_conflict_list &&
166 strcmp(first, posns[i]->name))) {
170 if (posns[i] == df_conflict_list) {
171 src[i + o->merge] = o->df_conflict_entry;
175 if (posns[i]->directory) {
176 struct tree *tree = lookup_tree(posns[i]->sha1);
179 subposns[i] = create_tree_entry_list(tree);
180 posns[i] = posns[i]->next;
181 src[i + o->merge] = o->df_conflict_entry;
187 else if (i + 1 < o->head_idx)
189 else if (i + 1 > o->head_idx)
194 ce = xcalloc(1, ce_size);
195 ce->ce_mode = create_ce_mode(posns[i]->mode);
196 ce->ce_flags = create_ce_flags(baselen + pathlen,
198 memcpy(ce->name, base, baselen);
199 memcpy(ce->name + baselen, first, pathlen + 1);
203 hashcpy(ce->sha1, posns[i]->sha1);
204 src[i + o->merge] = ce;
205 subposns[i] = df_conflict_list;
206 posns[i] = posns[i]->next;
213 printf("%s:\n", first);
214 for (i = 0; i < src_size; i++) {
217 printf("%s\n", sha1_to_hex(src[i]->sha1));
225 printf("Added %d entries\n", ret);
229 for (i = 0; i < src_size; i++) {
231 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
237 char *newbase = xmalloc(baselen + 2 + pathlen);
238 memcpy(newbase, base, baselen);
239 memcpy(newbase + baselen, first, pathlen);
240 newbase[baselen + pathlen] = '/';
241 newbase[baselen + pathlen + 1] = '\0';
242 if (unpack_trees_rec(subposns, len, newbase, o,
243 indpos, df_conflict_list))
252 /* Unlink the last component and attempt to remove leading
253 * directories, in case this unlink is the removal of the
254 * last entry in the directory -- empty directories are removed.
256 static void unlink_entry(char *name)
265 cp = strrchr(name, '/');
272 status = rmdir(name);
281 static volatile sig_atomic_t progress_update;
283 static void progress_interval(int signum)
288 static void setup_progress_signal(void)
293 memset(&sa, 0, sizeof(sa));
294 sa.sa_handler = progress_interval;
295 sigemptyset(&sa.sa_mask);
296 sa.sa_flags = SA_RESTART;
297 sigaction(SIGALRM, &sa, NULL);
299 v.it_interval.tv_sec = 1;
300 v.it_interval.tv_usec = 0;
301 v.it_value = v.it_interval;
302 setitimer(ITIMER_REAL, &v, NULL);
305 static struct checkout state;
306 static void check_updates(struct cache_entry **src, int nr,
307 struct unpack_trees_options *o)
309 unsigned short mask = htons(CE_UPDATE);
310 unsigned last_percent = 200, cnt = 0, total = 0;
312 if (o->update && o->verbose_update) {
313 for (total = cnt = 0; cnt < nr; cnt++) {
314 struct cache_entry *ce = src[cnt];
315 if (!ce->ce_mode || ce->ce_flags & mask)
319 /* Don't bother doing this for very small updates */
324 fprintf(stderr, "Checking files out...\n");
325 setup_progress_signal();
332 struct cache_entry *ce = *src++;
335 if (!ce->ce_mode || ce->ce_flags & mask) {
338 percent = (cnt * 100) / total;
339 if (percent != last_percent ||
341 fprintf(stderr, "%4u%% (%u/%u) done\r",
342 percent, cnt, total);
343 last_percent = percent;
350 unlink_entry(ce->name);
353 if (ce->ce_flags & mask) {
354 ce->ce_flags &= ~mask;
356 checkout_entry(ce, &state, NULL);
360 signal(SIGALRM, SIG_IGN);
365 int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
368 unsigned len = object_list_length(trees);
369 struct tree_entry_list **posns;
371 struct object_list *posn = trees;
372 struct tree_entry_list df_conflict_list;
373 static struct cache_entry *dfc;
375 memset(&df_conflict_list, 0, sizeof(df_conflict_list));
376 df_conflict_list.next = &df_conflict_list;
377 memset(&state, 0, sizeof(state));
381 state.refresh_cache = 1;
386 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
387 o->df_conflict_entry = dfc;
390 posns = xmalloc(len * sizeof(struct tree_entry_list *));
391 for (i = 0; i < len; i++) {
392 posns[i] = create_tree_entry_list((struct tree *) posn->item);
395 if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
396 o, &indpos, &df_conflict_list))
400 if (o->trivial_merges_only && o->nontrivial_merge)
401 die("Merge requires file-level merging");
403 check_updates(active_cache, active_nr, o);
407 /* Here come the merge functions */
409 static void reject_merge(struct cache_entry *ce)
411 die("Entry '%s' would be overwritten by merge. Cannot merge.",
415 static int same(struct cache_entry *a, struct cache_entry *b)
421 return a->ce_mode == b->ce_mode &&
422 !hashcmp(a->sha1, b->sha1);
427 * When a CE gets turned into an unmerged entry, we
428 * want it to be up-to-date
430 static void verify_uptodate(struct cache_entry *ce,
431 struct unpack_trees_options *o)
435 if (o->index_only || o->reset)
438 if (!lstat(ce->name, &st)) {
439 unsigned changed = ce_match_stat(ce, &st, 1);
445 ce->ce_flags |= htons(CE_UPDATE);
450 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
453 static void invalidate_ce_path(struct cache_entry *ce)
456 cache_tree_invalidate_path(active_cache_tree, ce->name);
460 * We do not want to remove or overwrite a working tree file that
463 static void verify_absent(const char *path, const char *action,
464 struct unpack_trees_options *o)
468 if (o->index_only || o->reset || !o->update)
470 if (!lstat(path, &st))
471 die("Untracked working tree file '%s' "
472 "would be %s by merge.", path, action);
475 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
476 struct unpack_trees_options *o)
478 merge->ce_flags |= htons(CE_UPDATE);
481 * See if we can re-use the old CE directly?
482 * That way we get the uptodate stat info.
484 * This also removes the UPDATE flag on
487 if (same(old, merge)) {
490 verify_uptodate(old, o);
491 invalidate_ce_path(old);
495 verify_absent(merge->name, "overwritten", o);
496 invalidate_ce_path(merge);
499 merge->ce_flags &= ~htons(CE_STAGEMASK);
500 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
504 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
505 struct unpack_trees_options *o)
508 verify_uptodate(old, o);
510 verify_absent(ce->name, "removed", o);
512 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
513 invalidate_ce_path(ce);
517 static int keep_entry(struct cache_entry *ce)
519 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
524 static void show_stage_entry(FILE *o,
525 const char *label, const struct cache_entry *ce)
528 fprintf(o, "%s (missing)\n", label);
530 fprintf(o, "%s%06o %s %d\t%s\n",
533 sha1_to_hex(ce->sha1),
539 int threeway_merge(struct cache_entry **stages,
540 struct unpack_trees_options *o)
542 struct cache_entry *index;
543 struct cache_entry *head;
544 struct cache_entry *remote = stages[o->head_idx + 1];
547 int remote_match = 0;
548 const char *path = NULL;
550 int df_conflict_head = 0;
551 int df_conflict_remote = 0;
553 int any_anc_missing = 0;
554 int no_anc_exists = 1;
557 for (i = 1; i < o->head_idx; i++) {
562 path = stages[i]->name;
568 head = stages[o->head_idx];
570 if (head == o->df_conflict_entry) {
571 df_conflict_head = 1;
575 if (remote == o->df_conflict_entry) {
576 df_conflict_remote = 1;
587 /* First, if there's a #16 situation, note that to prevent #13
590 if (!same(remote, head)) {
591 for (i = 1; i < o->head_idx; i++) {
592 if (same(stages[i], head)) {
595 if (same(stages[i], remote)) {
601 /* We start with cases where the index is allowed to match
602 * something other than the head: #14(ALT) and #2ALT, where it
603 * is permitted to match the result instead.
605 /* #14, #14ALT, #2ALT */
606 if (remote && !df_conflict_head && head_match && !remote_match) {
607 if (index && !same(index, remote) && !same(index, head))
609 return merged_entry(remote, index, o);
612 * If we have an entry in the index cache, then we want to
613 * make sure that it matches head.
615 if (index && !same(index, head)) {
621 if (same(head, remote))
622 return merged_entry(head, index, o);
624 if (!df_conflict_remote && remote_match && !head_match)
625 return merged_entry(head, index, o);
629 if (!head && !remote && any_anc_missing)
632 /* Under the new "aggressive" rule, we resolve mostly trivial
633 * cases that we historically had git-merge-one-file resolve.
636 int head_deleted = !head && !df_conflict_head;
637 int remote_deleted = !remote && !df_conflict_remote;
640 * Deleted in one and unchanged in the other.
642 if ((head_deleted && remote_deleted) ||
643 (head_deleted && remote && remote_match) ||
644 (remote_deleted && head && head_match)) {
646 return deleted_entry(index, index, o);
647 else if (path && !head_deleted)
648 verify_absent(path, "removed", o);
652 * Added in both, identically.
654 if (no_anc_exists && head && remote && same(head, remote))
655 return merged_entry(head, index, o);
659 /* Below are "no merge" cases, which require that the index be
660 * up-to-date to avoid the files getting overwritten with
661 * conflict resolution files.
664 verify_uptodate(index, o);
667 o->nontrivial_merge = 1;
669 /* #2, #3, #4, #6, #7, #9, #11. */
671 if (!head_match || !remote_match) {
672 for (i = 1; i < o->head_idx; i++) {
674 keep_entry(stages[i]);
682 fprintf(stderr, "read-tree: warning #16 detected\n");
683 show_stage_entry(stderr, "head ", stages[head_match]);
684 show_stage_entry(stderr, "remote ", stages[remote_match]);
687 if (head) { count += keep_entry(head); }
688 if (remote) { count += keep_entry(remote); }
695 * The rule is to "carry forward" what is in the index without losing
696 * information across a "fast forward", favoring a successful merge
697 * over a merge failure when it makes sense. For details of the
698 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
701 int twoway_merge(struct cache_entry **src,
702 struct unpack_trees_options *o)
704 struct cache_entry *current = src[0];
705 struct cache_entry *oldtree = src[1], *newtree = src[2];
707 if (o->merge_size != 2)
708 return error("Cannot do a twoway merge of %d trees",
712 if ((!oldtree && !newtree) || /* 4 and 5 */
713 (!oldtree && newtree &&
714 same(current, newtree)) || /* 6 and 7 */
715 (oldtree && newtree &&
716 same(oldtree, newtree)) || /* 14 and 15 */
717 (oldtree && newtree &&
718 !same(oldtree, newtree) && /* 18 and 19*/
719 same(current, newtree))) {
720 return keep_entry(current);
722 else if (oldtree && !newtree && same(current, oldtree)) {
724 return deleted_entry(oldtree, current, o);
726 else if (oldtree && newtree &&
727 same(current, oldtree) && !same(current, newtree)) {
729 return merged_entry(newtree, current, o);
732 /* all other failures */
734 reject_merge(oldtree);
736 reject_merge(current);
738 reject_merge(newtree);
743 return merged_entry(newtree, current, o);
745 return deleted_entry(oldtree, current, o);
751 * Keep the index entries at stage0, collapse stage1 but make sure
752 * stage0 does not have anything there.
754 int bind_merge(struct cache_entry **src,
755 struct unpack_trees_options *o)
757 struct cache_entry *old = src[0];
758 struct cache_entry *a = src[1];
760 if (o->merge_size != 1)
761 return error("Cannot do a bind merge of %d trees\n",
764 die("Entry '%s' overlaps. Cannot bind.", a->name);
766 return keep_entry(old);
768 return merged_entry(a, NULL, o);
775 * - take the stat information from stage0, take the data from stage1
777 int oneway_merge(struct cache_entry **src,
778 struct unpack_trees_options *o)
780 struct cache_entry *old = src[0];
781 struct cache_entry *a = src[1];
783 if (o->merge_size != 1)
784 return error("Cannot do a oneway merge of %d trees",
788 return deleted_entry(old, old, o);
789 if (old && same(old, a)) {
792 if (lstat(old->name, &st) ||
793 ce_match_stat(old, &st, 1))
794 old->ce_flags |= htons(CE_UPDATE);
796 return keep_entry(old);
798 return merged_entry(a, old, o);