2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 #include "tree-walk.h"
13 #include "cache-tree.h"
20 static int update = 0;
21 static int index_only = 0;
22 static int nontrivial_merge = 0;
23 static int trivial_merges_only = 0;
24 static int aggressive = 0;
25 static int verbose_update = 0;
26 static volatile int progress_update = 0;
27 static const char *prefix = NULL;
29 static int head_idx = -1;
30 static int merge_size = 0;
32 static struct object_list *trees = NULL;
34 static struct cache_entry df_conflict_entry;
36 struct tree_entry_list {
37 struct tree_entry_list *next;
38 unsigned directory : 1;
39 unsigned executable : 1;
43 const unsigned char *sha1;
46 static struct tree_entry_list df_conflict_list;
48 typedef int (*merge_fn_t)(struct cache_entry **src);
50 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
52 struct tree_desc desc;
53 struct name_entry one;
54 struct tree_entry_list *ret = NULL;
55 struct tree_entry_list **list_p = &ret;
57 desc.buf = tree->buffer;
58 desc.size = tree->size;
60 while (tree_entry(&desc, &one)) {
61 struct tree_entry_list *entry;
63 entry = xmalloc(sizeof(struct tree_entry_list));
64 entry->name = one.path;
65 entry->sha1 = one.sha1;
66 entry->mode = one.mode;
67 entry->directory = S_ISDIR(one.mode) != 0;
68 entry->executable = (one.mode & S_IXUSR) != 0;
69 entry->symlink = S_ISLNK(one.mode) != 0;
73 list_p = &entry->next;
78 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
80 int len1 = strlen(name1);
81 int len2 = strlen(name2);
82 int len = len1 < len2 ? len1 : len2;
83 int ret = memcmp(name1, name2, len);
93 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
99 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
100 const char *base, merge_fn_t fn, int *indpos)
102 int baselen = strlen(base);
103 int src_size = len + 1;
110 struct tree_entry_list **subposns;
111 struct cache_entry **src;
117 /* Find the first name in the input. */
122 /* Check the cache */
123 if (merge && *indpos < active_nr) {
124 /* This is a bit tricky: */
125 /* If the index has a subdirectory (with
126 * contents) as the first name, it'll get a
127 * filename like "foo/bar". But that's after
128 * "foo", so the entry in trees will get
129 * handled first, at which point we'll go into
130 * "foo", and deal with "bar" from the index,
131 * because the base will be "foo/". The only
132 * way we can actually have "foo/bar" first of
133 * all the things is if the trees don't
134 * contain "foo" at all, in which case we'll
135 * handle "foo/bar" without going into the
136 * directory, but that's fine (and will return
137 * an error anyway, with the added unknown
141 cache_name = active_cache[*indpos]->name;
142 if (strlen(cache_name) > baselen &&
143 !memcmp(cache_name, base, baselen)) {
144 cache_name += baselen;
153 printf("index %s\n", first);
155 for (i = 0; i < len; i++) {
156 if (!posns[i] || posns[i] == &df_conflict_list)
159 printf("%d %s\n", i + 1, posns[i]->name);
161 if (!first || entcmp(first, firstdir,
163 posns[i]->directory) > 0) {
164 first = posns[i]->name;
165 firstdir = posns[i]->directory;
168 /* No name means we're done */
172 pathlen = strlen(first);
173 ce_size = cache_entry_size(baselen + pathlen);
175 src = xcalloc(src_size, sizeof(struct cache_entry *));
177 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
179 if (cache_name && !strcmp(cache_name, first)) {
181 src[0] = active_cache[*indpos];
182 remove_cache_entry_at(*indpos);
185 for (i = 0; i < len; i++) {
186 struct cache_entry *ce;
189 (posns[i] != &df_conflict_list &&
190 strcmp(first, posns[i]->name))) {
194 if (posns[i] == &df_conflict_list) {
195 src[i + merge] = &df_conflict_entry;
199 if (posns[i]->directory) {
200 struct tree *tree = lookup_tree(posns[i]->sha1);
203 subposns[i] = create_tree_entry_list(tree);
204 posns[i] = posns[i]->next;
205 src[i + merge] = &df_conflict_entry;
211 else if (i + 1 < head_idx)
213 else if (i + 1 > head_idx)
218 ce = xcalloc(1, ce_size);
219 ce->ce_mode = create_ce_mode(posns[i]->mode);
220 ce->ce_flags = create_ce_flags(baselen + pathlen,
222 memcpy(ce->name, base, baselen);
223 memcpy(ce->name + baselen, first, pathlen + 1);
227 memcpy(ce->sha1, posns[i]->sha1, 20);
229 subposns[i] = &df_conflict_list;
230 posns[i] = posns[i]->next;
237 printf("%s:\n", first);
238 for (i = 0; i < src_size; i++) {
241 printf("%s\n", sha1_to_hex(src[i]->sha1));
249 printf("Added %d entries\n", ret);
253 for (i = 0; i < src_size; i++) {
255 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
261 char *newbase = xmalloc(baselen + 2 + pathlen);
262 memcpy(newbase, base, baselen);
263 memcpy(newbase + baselen, first, pathlen);
264 newbase[baselen + pathlen] = '/';
265 newbase[baselen + pathlen + 1] = '\0';
266 if (unpack_trees_rec(subposns, len, newbase, fn,
276 static void reject_merge(struct cache_entry *ce)
278 die("Entry '%s' would be overwritten by merge. Cannot merge.",
282 /* Unlink the last component and attempt to remove leading
283 * directories, in case this unlink is the removal of the
284 * last entry in the directory -- empty directories are removed.
286 static void unlink_entry(char *name)
295 cp = strrchr(name, '/');
302 status = rmdir(name);
311 static void progress_interval(int signum)
316 static void setup_progress_signal(void)
321 memset(&sa, 0, sizeof(sa));
322 sa.sa_handler = progress_interval;
323 sigemptyset(&sa.sa_mask);
324 sa.sa_flags = SA_RESTART;
325 sigaction(SIGALRM, &sa, NULL);
327 v.it_interval.tv_sec = 1;
328 v.it_interval.tv_usec = 0;
329 v.it_value = v.it_interval;
330 setitimer(ITIMER_REAL, &v, NULL);
333 static struct checkout state;
334 static void check_updates(struct cache_entry **src, int nr)
336 unsigned short mask = htons(CE_UPDATE);
337 unsigned last_percent = 200, cnt = 0, total = 0;
339 if (update && verbose_update) {
340 for (total = cnt = 0; cnt < nr; cnt++) {
341 struct cache_entry *ce = src[cnt];
342 if (!ce->ce_mode || ce->ce_flags & mask)
346 /* Don't bother doing this for very small updates */
351 fprintf(stderr, "Checking files out...\n");
352 setup_progress_signal();
359 struct cache_entry *ce = *src++;
362 if (!ce->ce_mode || ce->ce_flags & mask) {
365 percent = (cnt * 100) / total;
366 if (percent != last_percent ||
368 fprintf(stderr, "%4u%% (%u/%u) done\r",
369 percent, cnt, total);
370 last_percent = percent;
377 unlink_entry(ce->name);
380 if (ce->ce_flags & mask) {
381 ce->ce_flags &= ~mask;
383 checkout_entry(ce, &state, NULL);
387 signal(SIGALRM, SIG_IGN);
392 static int unpack_trees(merge_fn_t fn)
395 unsigned len = object_list_length(trees);
396 struct tree_entry_list **posns;
398 struct object_list *posn = trees;
402 posns = xmalloc(len * sizeof(struct tree_entry_list *));
403 for (i = 0; i < len; i++) {
404 posns[i] = create_tree_entry_list((struct tree *) posn->item);
407 if (unpack_trees_rec(posns, len, prefix ? prefix : "",
412 if (trivial_merges_only && nontrivial_merge)
413 die("Merge requires file-level merging");
415 check_updates(active_cache, active_nr);
419 static int list_tree(unsigned char *sha1)
421 struct tree *tree = parse_tree_indirect(sha1);
424 object_list_append(&tree->object, &trees);
428 static int same(struct cache_entry *a, struct cache_entry *b)
434 return a->ce_mode == b->ce_mode &&
435 !memcmp(a->sha1, b->sha1, 20);
440 * When a CE gets turned into an unmerged entry, we
441 * want it to be up-to-date
443 static void verify_uptodate(struct cache_entry *ce)
447 if (index_only || reset)
450 if (!lstat(ce->name, &st)) {
451 unsigned changed = ce_match_stat(ce, &st, 1);
457 ce->ce_flags |= htons(CE_UPDATE);
462 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
465 static void invalidate_ce_path(struct cache_entry *ce)
468 cache_tree_invalidate_path(active_cache_tree, ce->name);
472 * We do not want to remove or overwrite a working tree file that
475 static void verify_absent(const char *path, const char *action)
479 if (index_only || reset || !update)
481 if (!lstat(path, &st))
482 die("Untracked working tree file '%s' "
483 "would be %s by merge.", path, action);
486 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
488 merge->ce_flags |= htons(CE_UPDATE);
491 * See if we can re-use the old CE directly?
492 * That way we get the uptodate stat info.
494 * This also removes the UPDATE flag on
497 if (same(old, merge)) {
500 verify_uptodate(old);
501 invalidate_ce_path(old);
505 verify_absent(merge->name, "overwritten");
506 invalidate_ce_path(merge);
509 merge->ce_flags &= ~htons(CE_STAGEMASK);
510 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
514 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
517 verify_uptodate(old);
519 verify_absent(ce->name, "removed");
521 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
522 invalidate_ce_path(ce);
526 static int keep_entry(struct cache_entry *ce)
528 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
533 static void show_stage_entry(FILE *o,
534 const char *label, const struct cache_entry *ce)
537 fprintf(o, "%s (missing)\n", label);
539 fprintf(o, "%s%06o %s %d\t%s\n",
542 sha1_to_hex(ce->sha1),
548 static int threeway_merge(struct cache_entry **stages)
550 struct cache_entry *index;
551 struct cache_entry *head;
552 struct cache_entry *remote = stages[head_idx + 1];
555 int remote_match = 0;
556 const char *path = NULL;
558 int df_conflict_head = 0;
559 int df_conflict_remote = 0;
561 int any_anc_missing = 0;
562 int no_anc_exists = 1;
565 for (i = 1; i < head_idx; i++) {
570 path = stages[i]->name;
576 head = stages[head_idx];
578 if (head == &df_conflict_entry) {
579 df_conflict_head = 1;
583 if (remote == &df_conflict_entry) {
584 df_conflict_remote = 1;
595 /* First, if there's a #16 situation, note that to prevent #13
598 if (!same(remote, head)) {
599 for (i = 1; i < head_idx; i++) {
600 if (same(stages[i], head)) {
603 if (same(stages[i], remote)) {
609 /* We start with cases where the index is allowed to match
610 * something other than the head: #14(ALT) and #2ALT, where it
611 * is permitted to match the result instead.
613 /* #14, #14ALT, #2ALT */
614 if (remote && !df_conflict_head && head_match && !remote_match) {
615 if (index && !same(index, remote) && !same(index, head))
617 return merged_entry(remote, index);
620 * If we have an entry in the index cache, then we want to
621 * make sure that it matches head.
623 if (index && !same(index, head)) {
629 if (same(head, remote))
630 return merged_entry(head, index);
632 if (!df_conflict_remote && remote_match && !head_match)
633 return merged_entry(head, index);
637 if (!head && !remote && any_anc_missing)
640 /* Under the new "aggressive" rule, we resolve mostly trivial
641 * cases that we historically had git-merge-one-file resolve.
644 int head_deleted = !head && !df_conflict_head;
645 int remote_deleted = !remote && !df_conflict_remote;
648 * Deleted in one and unchanged in the other.
650 if ((head_deleted && remote_deleted) ||
651 (head_deleted && remote && remote_match) ||
652 (remote_deleted && head && head_match)) {
654 return deleted_entry(index, index);
656 verify_absent(path, "removed");
660 * Added in both, identically.
662 if (no_anc_exists && head && remote && same(head, remote))
663 return merged_entry(head, index);
667 /* Below are "no merge" cases, which require that the index be
668 * up-to-date to avoid the files getting overwritten with
669 * conflict resolution files.
672 verify_uptodate(index);
675 verify_absent(path, "overwritten");
677 nontrivial_merge = 1;
679 /* #2, #3, #4, #6, #7, #9, #11. */
681 if (!head_match || !remote_match) {
682 for (i = 1; i < head_idx; i++) {
684 keep_entry(stages[i]);
692 fprintf(stderr, "read-tree: warning #16 detected\n");
693 show_stage_entry(stderr, "head ", stages[head_match]);
694 show_stage_entry(stderr, "remote ", stages[remote_match]);
697 if (head) { count += keep_entry(head); }
698 if (remote) { count += keep_entry(remote); }
705 * The rule is to "carry forward" what is in the index without losing
706 * information across a "fast forward", favoring a successful merge
707 * over a merge failure when it makes sense. For details of the
708 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
711 static int twoway_merge(struct cache_entry **src)
713 struct cache_entry *current = src[0];
714 struct cache_entry *oldtree = src[1], *newtree = src[2];
717 return error("Cannot do a twoway merge of %d trees",
721 if ((!oldtree && !newtree) || /* 4 and 5 */
722 (!oldtree && newtree &&
723 same(current, newtree)) || /* 6 and 7 */
724 (oldtree && newtree &&
725 same(oldtree, newtree)) || /* 14 and 15 */
726 (oldtree && newtree &&
727 !same(oldtree, newtree) && /* 18 and 19*/
728 same(current, newtree))) {
729 return keep_entry(current);
731 else if (oldtree && !newtree && same(current, oldtree)) {
733 return deleted_entry(oldtree, current);
735 else if (oldtree && newtree &&
736 same(current, oldtree) && !same(current, newtree)) {
738 return merged_entry(newtree, current);
741 /* all other failures */
743 reject_merge(oldtree);
745 reject_merge(current);
747 reject_merge(newtree);
752 return merged_entry(newtree, current);
754 return deleted_entry(oldtree, current);
760 * Keep the index entries at stage0, collapse stage1 but make sure
761 * stage0 does not have anything there.
763 static int bind_merge(struct cache_entry **src)
765 struct cache_entry *old = src[0];
766 struct cache_entry *a = src[1];
769 return error("Cannot do a bind merge of %d trees\n",
772 die("Entry '%s' overlaps. Cannot bind.", a->name);
774 return keep_entry(old);
776 return merged_entry(a, NULL);
783 * - take the stat information from stage0, take the data from stage1
785 static int oneway_merge(struct cache_entry **src)
787 struct cache_entry *old = src[0];
788 struct cache_entry *a = src[1];
791 return error("Cannot do a oneway merge of %d trees",
795 return deleted_entry(old, old);
796 if (old && same(old, a)) {
799 if (lstat(old->name, &st) ||
800 ce_match_stat(old, &st, 1))
801 old->ce_flags |= htons(CE_UPDATE);
803 return keep_entry(old);
805 return merged_entry(a, old);
808 static int read_cache_unmerged(void)
811 struct cache_entry **dst;
812 struct cache_entry *last = NULL;
816 for (i = 0; i < active_nr; i++) {
817 struct cache_entry *ce = active_cache[i];
819 if (last && !strcmp(ce->name, last->name))
821 invalidate_ce_path(ce);
824 ce->ce_flags &= ~htons(CE_STAGEMASK);
828 active_nr = dst - active_cache;
832 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
834 struct tree_desc desc;
835 struct name_entry entry;
838 memcpy(it->sha1, tree->object.sha1, 20);
839 desc.buf = tree->buffer;
840 desc.size = tree->size;
842 while (tree_entry(&desc, &entry)) {
843 if (!S_ISDIR(entry.mode))
846 struct cache_tree_sub *sub;
847 struct tree *subtree = lookup_tree(entry.sha1);
848 if (!subtree->object.parsed)
850 sub = cache_tree_sub(it, entry.path);
851 sub->cache_tree = cache_tree();
852 prime_cache_tree_rec(sub->cache_tree, subtree);
853 cnt += sub->cache_tree->entry_count;
856 it->entry_count = cnt;
859 static void prime_cache_tree(void)
861 struct tree *tree = (struct tree *)trees->item;
864 active_cache_tree = cache_tree();
865 prime_cache_tree_rec(active_cache_tree, tree);
869 static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
871 static struct lock_file lock_file;
873 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
875 int i, newfd, stage = 0;
876 unsigned char sha1[20];
877 merge_fn_t fn = NULL;
879 df_conflict_list.next = &df_conflict_list;
883 state.refresh_cache = 1;
885 git_config(git_default_config);
887 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
889 git_config(git_default_config);
893 for (i = 1; i < argc; i++) {
894 const char *arg = argv[i];
896 /* "-u" means "update", meaning that a merge will update
899 if (!strcmp(arg, "-u")) {
904 if (!strcmp(arg, "-v")) {
909 /* "-i" means "index only", meaning that a merge will
910 * not even look at the working tree.
912 if (!strcmp(arg, "-i")) {
917 /* "--prefix=<subdirectory>/" means keep the current index
918 * entries and put the entries from the tree under the
919 * given subdirectory.
921 if (!strncmp(arg, "--prefix=", 9)) {
922 if (stage || merge || prefix)
923 usage(read_tree_usage);
927 if (read_cache_unmerged())
928 die("you need to resolve your current index first");
932 /* This differs from "-m" in that we'll silently ignore
933 * unmerged entries and overwrite working tree files that
934 * correspond to them.
936 if (!strcmp(arg, "--reset")) {
937 if (stage || merge || prefix)
938 usage(read_tree_usage);
942 read_cache_unmerged();
946 if (!strcmp(arg, "--trivial")) {
947 trivial_merges_only = 1;
951 if (!strcmp(arg, "--aggressive")) {
956 /* "-m" stands for "merge", meaning we start in stage 1 */
957 if (!strcmp(arg, "-m")) {
958 if (stage || merge || prefix)
959 usage(read_tree_usage);
960 if (read_cache_unmerged())
961 die("you need to resolve your current index first");
967 /* using -u and -i at the same time makes no sense */
968 if (1 < index_only + update)
969 usage(read_tree_usage);
971 if (get_sha1(arg, sha1))
972 die("Not a valid object name %s", arg);
973 if (list_tree(sha1) < 0)
974 die("failed to unpack tree object %s", arg);
977 if ((update||index_only) && !merge)
978 usage(read_tree_usage);
981 int pfxlen = strlen(prefix);
983 if (prefix[pfxlen-1] != '/')
984 die("prefix must end with /");
986 die("binding merge takes only one tree");
987 pos = cache_name_pos(prefix, pfxlen);
989 die("corrupt index file");
991 if (pos < active_nr &&
992 !strncmp(active_cache[pos]->name, prefix, pfxlen))
993 die("subdirectory '%s' already exists.", prefix);
994 pos = cache_name_pos(prefix, pfxlen-1);
996 die("file '%.*s' already exists.", pfxlen-1, prefix);
1001 die("just how do you expect me to merge %d trees?", stage-1);
1002 switch (stage - 1) {
1004 fn = prefix ? bind_merge : oneway_merge;
1011 fn = threeway_merge;
1012 cache_tree_free(&active_cache_tree);
1017 head_idx = stage - 2;
1025 * When reading only one tree (either the most basic form,
1026 * "-m ent" or "--reset ent" form), we can obtain a fully
1027 * valid cache-tree because the index must match exactly
1028 * what came from the tree.
1030 if (trees && trees->item && !prefix && (!merge || (stage == 2))) {
1031 cache_tree_free(&active_cache_tree);
1035 if (write_cache(newfd, active_cache, active_nr) ||
1036 close(newfd) || commit_lock_file(&lock_file))
1037 die("unable to write new index file");