2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 #include "cache-tree.h"
19 static int update = 0;
20 static int index_only = 0;
21 static int nontrivial_merge = 0;
22 static int trivial_merges_only = 0;
23 static int aggressive = 0;
24 static int verbose_update = 0;
25 static volatile int progress_update = 0;
27 static int head_idx = -1;
28 static int merge_size = 0;
30 static struct object_list *trees = NULL;
32 static struct cache_entry df_conflict_entry = {
35 static struct tree_entry_list df_conflict_list = {
37 .next = &df_conflict_list
40 typedef int (*merge_fn_t)(struct cache_entry **src);
42 static int entcmp(char *name1, int dir1, char *name2, int dir2)
44 int len1 = strlen(name1);
45 int len2 = strlen(name2);
46 int len = len1 < len2 ? len1 : len2;
47 int ret = memcmp(name1, name2, len);
57 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
63 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
64 const char *base, merge_fn_t fn, int *indpos)
66 int baselen = strlen(base);
67 int src_size = len + 1;
74 struct tree_entry_list **subposns;
75 struct cache_entry **src;
81 /* Find the first name in the input. */
87 if (merge && *indpos < active_nr) {
88 /* This is a bit tricky: */
89 /* If the index has a subdirectory (with
90 * contents) as the first name, it'll get a
91 * filename like "foo/bar". But that's after
92 * "foo", so the entry in trees will get
93 * handled first, at which point we'll go into
94 * "foo", and deal with "bar" from the index,
95 * because the base will be "foo/". The only
96 * way we can actually have "foo/bar" first of
97 * all the things is if the trees don't
98 * contain "foo" at all, in which case we'll
99 * handle "foo/bar" without going into the
100 * directory, but that's fine (and will return
101 * an error anyway, with the added unknown
105 cache_name = active_cache[*indpos]->name;
106 if (strlen(cache_name) > baselen &&
107 !memcmp(cache_name, base, baselen)) {
108 cache_name += baselen;
117 printf("index %s\n", first);
119 for (i = 0; i < len; i++) {
120 if (!posns[i] || posns[i] == &df_conflict_list)
123 printf("%d %s\n", i + 1, posns[i]->name);
125 if (!first || entcmp(first, firstdir,
127 posns[i]->directory) > 0) {
128 first = posns[i]->name;
129 firstdir = posns[i]->directory;
132 /* No name means we're done */
136 pathlen = strlen(first);
137 ce_size = cache_entry_size(baselen + pathlen);
139 src = xcalloc(src_size, sizeof(struct cache_entry *));
141 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
143 if (cache_name && !strcmp(cache_name, first)) {
145 src[0] = active_cache[*indpos];
146 remove_cache_entry_at(*indpos);
149 for (i = 0; i < len; i++) {
150 struct cache_entry *ce;
153 (posns[i] != &df_conflict_list &&
154 strcmp(first, posns[i]->name))) {
158 if (posns[i] == &df_conflict_list) {
159 src[i + merge] = &df_conflict_entry;
163 if (posns[i]->directory) {
165 parse_tree(posns[i]->item.tree);
166 subposns[i] = posns[i]->item.tree->entries;
167 posns[i] = posns[i]->next;
168 src[i + merge] = &df_conflict_entry;
174 else if (i + 1 < head_idx)
176 else if (i + 1 > head_idx)
181 ce = xcalloc(1, ce_size);
182 ce->ce_mode = create_ce_mode(posns[i]->mode);
183 ce->ce_flags = create_ce_flags(baselen + pathlen,
185 memcpy(ce->name, base, baselen);
186 memcpy(ce->name + baselen, first, pathlen + 1);
190 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
192 subposns[i] = &df_conflict_list;
193 posns[i] = posns[i]->next;
200 printf("%s:\n", first);
201 for (i = 0; i < src_size; i++) {
204 printf("%s\n", sha1_to_hex(src[i]->sha1));
212 printf("Added %d entries\n", ret);
216 for (i = 0; i < src_size; i++) {
218 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
224 char *newbase = xmalloc(baselen + 2 + pathlen);
225 memcpy(newbase, base, baselen);
226 memcpy(newbase + baselen, first, pathlen);
227 newbase[baselen + pathlen] = '/';
228 newbase[baselen + pathlen + 1] = '\0';
229 if (unpack_trees_rec(subposns, len, newbase, fn,
239 static void reject_merge(struct cache_entry *ce)
241 die("Entry '%s' would be overwritten by merge. Cannot merge.",
245 /* Unlink the last component and attempt to remove leading
246 * directories, in case this unlink is the removal of the
247 * last entry in the directory -- empty directories are removed.
249 static void unlink_entry(char *name)
258 cp = strrchr(name, '/');
265 status = rmdir(name);
274 static void progress_interval(int signum)
279 static void setup_progress_signal(void)
284 memset(&sa, 0, sizeof(sa));
285 sa.sa_handler = progress_interval;
286 sigemptyset(&sa.sa_mask);
287 sa.sa_flags = SA_RESTART;
288 sigaction(SIGALRM, &sa, NULL);
290 v.it_interval.tv_sec = 1;
291 v.it_interval.tv_usec = 0;
292 v.it_value = v.it_interval;
293 setitimer(ITIMER_REAL, &v, NULL);
296 static void check_updates(struct cache_entry **src, int nr)
298 static struct checkout state = {
304 unsigned short mask = htons(CE_UPDATE);
305 unsigned last_percent = 200, cnt = 0, total = 0;
307 if (update && verbose_update) {
308 for (total = cnt = 0; cnt < nr; cnt++) {
309 struct cache_entry *ce = src[cnt];
310 if (!ce->ce_mode || ce->ce_flags & mask)
314 /* Don't bother doing this for very small updates */
319 fprintf(stderr, "Checking files out...\n");
320 setup_progress_signal();
327 struct cache_entry *ce = *src++;
330 if (!ce->ce_mode || ce->ce_flags & mask) {
333 percent = (cnt * 100) / total;
334 if (percent != last_percent ||
336 fprintf(stderr, "%4u%% (%u/%u) done\r",
337 percent, cnt, total);
338 last_percent = percent;
344 unlink_entry(ce->name);
347 if (ce->ce_flags & mask) {
348 ce->ce_flags &= ~mask;
350 checkout_entry(ce, &state, NULL);
354 signal(SIGALRM, SIG_IGN);
359 static int unpack_trees(merge_fn_t fn)
362 unsigned len = object_list_length(trees);
363 struct tree_entry_list **posns;
365 struct object_list *posn = trees;
369 posns = xmalloc(len * sizeof(struct tree_entry_list *));
370 for (i = 0; i < len; i++) {
371 posns[i] = ((struct tree *) posn->item)->entries;
374 if (unpack_trees_rec(posns, len, "", fn, &indpos))
378 if (trivial_merges_only && nontrivial_merge)
379 die("Merge requires file-level merging");
381 check_updates(active_cache, active_nr);
385 static int list_tree(unsigned char *sha1)
387 struct tree *tree = parse_tree_indirect(sha1);
390 object_list_append(&tree->object, &trees);
394 static int same(struct cache_entry *a, struct cache_entry *b)
400 return a->ce_mode == b->ce_mode &&
401 !memcmp(a->sha1, b->sha1, 20);
406 * When a CE gets turned into an unmerged entry, we
407 * want it to be up-to-date
409 static void verify_uptodate(struct cache_entry *ce)
413 if (index_only || reset)
416 if (!lstat(ce->name, &st)) {
417 unsigned changed = ce_match_stat(ce, &st, 1);
423 ce->ce_flags |= htons(CE_UPDATE);
428 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
431 static void invalidate_ce_path(struct cache_entry *ce)
434 cache_tree_invalidate_path(active_cache_tree, ce->name);
438 * We do not want to remove or overwrite a working tree file that
441 static void verify_absent(const char *path, const char *action)
445 if (index_only || reset || !update)
447 if (!lstat(path, &st))
448 die("Untracked working tree file '%s' "
449 "would be %s by merge.", path, action);
452 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
454 merge->ce_flags |= htons(CE_UPDATE);
457 * See if we can re-use the old CE directly?
458 * That way we get the uptodate stat info.
460 * This also removes the UPDATE flag on
463 if (same(old, merge)) {
466 verify_uptodate(old);
467 invalidate_ce_path(old);
471 verify_absent(merge->name, "overwritten");
472 invalidate_ce_path(merge);
475 merge->ce_flags &= ~htons(CE_STAGEMASK);
476 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
480 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
483 verify_uptodate(old);
485 verify_absent(ce->name, "removed");
487 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
488 invalidate_ce_path(ce);
492 static int keep_entry(struct cache_entry *ce)
494 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
499 static void show_stage_entry(FILE *o,
500 const char *label, const struct cache_entry *ce)
503 fprintf(o, "%s (missing)\n", label);
505 fprintf(o, "%s%06o %s %d\t%s\n",
508 sha1_to_hex(ce->sha1),
514 static int threeway_merge(struct cache_entry **stages)
516 struct cache_entry *index;
517 struct cache_entry *head;
518 struct cache_entry *remote = stages[head_idx + 1];
521 int remote_match = 0;
522 const char *path = NULL;
524 int df_conflict_head = 0;
525 int df_conflict_remote = 0;
527 int any_anc_missing = 0;
528 int no_anc_exists = 1;
531 for (i = 1; i < head_idx; i++) {
536 path = stages[i]->name;
542 head = stages[head_idx];
544 if (head == &df_conflict_entry) {
545 df_conflict_head = 1;
549 if (remote == &df_conflict_entry) {
550 df_conflict_remote = 1;
561 /* First, if there's a #16 situation, note that to prevent #13
564 if (!same(remote, head)) {
565 for (i = 1; i < head_idx; i++) {
566 if (same(stages[i], head)) {
569 if (same(stages[i], remote)) {
575 /* We start with cases where the index is allowed to match
576 * something other than the head: #14(ALT) and #2ALT, where it
577 * is permitted to match the result instead.
579 /* #14, #14ALT, #2ALT */
580 if (remote && !df_conflict_head && head_match && !remote_match) {
581 if (index && !same(index, remote) && !same(index, head))
583 return merged_entry(remote, index);
586 * If we have an entry in the index cache, then we want to
587 * make sure that it matches head.
589 if (index && !same(index, head)) {
595 if (same(head, remote))
596 return merged_entry(head, index);
598 if (!df_conflict_remote && remote_match && !head_match)
599 return merged_entry(head, index);
603 if (!head && !remote && any_anc_missing)
606 /* Under the new "aggressive" rule, we resolve mostly trivial
607 * cases that we historically had git-merge-one-file resolve.
610 int head_deleted = !head && !df_conflict_head;
611 int remote_deleted = !remote && !df_conflict_remote;
614 * Deleted in one and unchanged in the other.
616 if ((head_deleted && remote_deleted) ||
617 (head_deleted && remote && remote_match) ||
618 (remote_deleted && head && head_match)) {
620 return deleted_entry(index, index);
622 verify_absent(path, "removed");
626 * Added in both, identically.
628 if (no_anc_exists && head && remote && same(head, remote))
629 return merged_entry(head, index);
633 /* Below are "no merge" cases, which require that the index be
634 * up-to-date to avoid the files getting overwritten with
635 * conflict resolution files.
638 verify_uptodate(index);
641 verify_absent(path, "overwritten");
643 nontrivial_merge = 1;
645 /* #2, #3, #4, #6, #7, #9, #11. */
647 if (!head_match || !remote_match) {
648 for (i = 1; i < head_idx; i++) {
650 keep_entry(stages[i]);
658 fprintf(stderr, "read-tree: warning #16 detected\n");
659 show_stage_entry(stderr, "head ", stages[head_match]);
660 show_stage_entry(stderr, "remote ", stages[remote_match]);
663 if (head) { count += keep_entry(head); }
664 if (remote) { count += keep_entry(remote); }
671 * The rule is to "carry forward" what is in the index without losing
672 * information across a "fast forward", favoring a successful merge
673 * over a merge failure when it makes sense. For details of the
674 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
677 static int twoway_merge(struct cache_entry **src)
679 struct cache_entry *current = src[0];
680 struct cache_entry *oldtree = src[1], *newtree = src[2];
683 return error("Cannot do a twoway merge of %d trees",
687 if ((!oldtree && !newtree) || /* 4 and 5 */
688 (!oldtree && newtree &&
689 same(current, newtree)) || /* 6 and 7 */
690 (oldtree && newtree &&
691 same(oldtree, newtree)) || /* 14 and 15 */
692 (oldtree && newtree &&
693 !same(oldtree, newtree) && /* 18 and 19*/
694 same(current, newtree))) {
695 return keep_entry(current);
697 else if (oldtree && !newtree && same(current, oldtree)) {
699 return deleted_entry(oldtree, current);
701 else if (oldtree && newtree &&
702 same(current, oldtree) && !same(current, newtree)) {
704 return merged_entry(newtree, current);
707 /* all other failures */
709 reject_merge(oldtree);
711 reject_merge(current);
713 reject_merge(newtree);
718 return merged_entry(newtree, current);
720 return deleted_entry(oldtree, current);
727 * - take the stat information from stage0, take the data from stage1
729 static int oneway_merge(struct cache_entry **src)
731 struct cache_entry *old = src[0];
732 struct cache_entry *a = src[1];
735 return error("Cannot do a oneway merge of %d trees",
739 return deleted_entry(old, old);
740 if (old && same(old, a)) {
743 if (lstat(old->name, &st) ||
744 ce_match_stat(old, &st, 1))
745 old->ce_flags |= htons(CE_UPDATE);
747 return keep_entry(old);
749 return merged_entry(a, old);
752 static int read_cache_unmerged(void)
755 struct cache_entry **dst;
756 struct cache_entry *last = NULL;
760 for (i = 0; i < active_nr; i++) {
761 struct cache_entry *ce = active_cache[i];
763 if (last && !strcmp(ce->name, last->name))
765 invalidate_ce_path(ce);
768 ce->ce_flags &= ~htons(CE_STAGEMASK);
772 active_nr = dst - active_cache;
776 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
778 struct tree_entry_list *ent;
781 memcpy(it->sha1, tree->object.sha1, 20);
782 for (cnt = 0, ent = tree->entries; ent; ent = ent->next) {
786 struct cache_tree_sub *sub;
787 struct tree *subtree = (struct tree *)ent->item.tree;
788 if (!subtree->object.parsed)
790 sub = cache_tree_sub(it, ent->name);
791 sub->cache_tree = cache_tree();
792 prime_cache_tree_rec(sub->cache_tree, subtree);
793 cnt += sub->cache_tree->entry_count;
796 it->entry_count = cnt;
799 static void prime_cache_tree(void)
801 struct tree *tree = (struct tree *)trees->item;
804 active_cache_tree = cache_tree();
805 prime_cache_tree_rec(active_cache_tree, tree);
809 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
811 static struct cache_file cache_file;
813 int cmd_read_tree(int argc, const char **argv, char **envp)
815 int i, newfd, stage = 0;
816 unsigned char sha1[20];
817 merge_fn_t fn = NULL;
819 setup_git_directory();
820 git_config(git_default_config);
822 newfd = hold_index_file_for_update(&cache_file, get_index_file());
824 die("unable to create new cachefile");
826 git_config(git_default_config);
830 for (i = 1; i < argc; i++) {
831 const char *arg = argv[i];
833 /* "-u" means "update", meaning that a merge will update
836 if (!strcmp(arg, "-u")) {
841 if (!strcmp(arg, "-v")) {
846 /* "-i" means "index only", meaning that a merge will
847 * not even look at the working tree.
849 if (!strcmp(arg, "-i")) {
854 /* This differs from "-m" in that we'll silently ignore
855 * unmerged entries and overwrite working tree files that
856 * correspond to them.
858 if (!strcmp(arg, "--reset")) {
860 usage(read_tree_usage);
864 read_cache_unmerged();
868 if (!strcmp(arg, "--trivial")) {
869 trivial_merges_only = 1;
873 if (!strcmp(arg, "--aggressive")) {
878 /* "-m" stands for "merge", meaning we start in stage 1 */
879 if (!strcmp(arg, "-m")) {
881 usage(read_tree_usage);
882 if (read_cache_unmerged())
883 die("you need to resolve your current index first");
889 /* using -u and -i at the same time makes no sense */
890 if (1 < index_only + update)
891 usage(read_tree_usage);
893 if (get_sha1(arg, sha1))
894 die("Not a valid object name %s", arg);
895 if (list_tree(sha1) < 0)
896 die("failed to unpack tree object %s", arg);
899 if ((update||index_only) && !merge)
900 usage(read_tree_usage);
904 die("just how do you expect me to merge %d trees?", stage-1);
915 cache_tree_free(&active_cache_tree);
920 head_idx = stage - 2;
928 * When reading only one tree (either the most basic form,
929 * "-m ent" or "--reset ent" form), we can obtain a fully
930 * valid cache-tree because the index must match exactly
931 * what came from the tree.
933 if (trees && trees->item && (!merge || (stage == 2))) {
934 cache_tree_free(&active_cache_tree);
938 if (write_cache(newfd, active_cache, active_nr) ||
939 commit_index_file(&cache_file))
940 die("unable to write new index file");