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;
28 static int head_idx = -1;
29 static int merge_size = 0;
31 static struct object_list *trees = NULL;
33 static struct cache_entry df_conflict_entry = {
36 static struct tree_entry_list df_conflict_list = {
38 .next = &df_conflict_list
41 typedef int (*merge_fn_t)(struct cache_entry **src);
43 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
45 int len1 = strlen(name1);
46 int len2 = strlen(name2);
47 int len = len1 < len2 ? len1 : len2;
48 int ret = memcmp(name1, name2, len);
58 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
64 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
65 const char *base, merge_fn_t fn, int *indpos)
67 int baselen = strlen(base);
68 int src_size = len + 1;
75 struct tree_entry_list **subposns;
76 struct cache_entry **src;
82 /* Find the first name in the input. */
88 if (merge && *indpos < active_nr) {
89 /* This is a bit tricky: */
90 /* If the index has a subdirectory (with
91 * contents) as the first name, it'll get a
92 * filename like "foo/bar". But that's after
93 * "foo", so the entry in trees will get
94 * handled first, at which point we'll go into
95 * "foo", and deal with "bar" from the index,
96 * because the base will be "foo/". The only
97 * way we can actually have "foo/bar" first of
98 * all the things is if the trees don't
99 * contain "foo" at all, in which case we'll
100 * handle "foo/bar" without going into the
101 * directory, but that's fine (and will return
102 * an error anyway, with the added unknown
106 cache_name = active_cache[*indpos]->name;
107 if (strlen(cache_name) > baselen &&
108 !memcmp(cache_name, base, baselen)) {
109 cache_name += baselen;
118 printf("index %s\n", first);
120 for (i = 0; i < len; i++) {
121 if (!posns[i] || posns[i] == &df_conflict_list)
124 printf("%d %s\n", i + 1, posns[i]->name);
126 if (!first || entcmp(first, firstdir,
128 posns[i]->directory) > 0) {
129 first = posns[i]->name;
130 firstdir = posns[i]->directory;
133 /* No name means we're done */
137 pathlen = strlen(first);
138 ce_size = cache_entry_size(baselen + pathlen);
140 src = xcalloc(src_size, sizeof(struct cache_entry *));
142 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
144 if (cache_name && !strcmp(cache_name, first)) {
146 src[0] = active_cache[*indpos];
147 remove_cache_entry_at(*indpos);
150 for (i = 0; i < len; i++) {
151 struct cache_entry *ce;
154 (posns[i] != &df_conflict_list &&
155 strcmp(first, posns[i]->name))) {
159 if (posns[i] == &df_conflict_list) {
160 src[i + merge] = &df_conflict_entry;
164 if (posns[i]->directory) {
165 struct tree *tree = lookup_tree(posns[i]->sha1);
168 subposns[i] = create_tree_entry_list(tree);
169 posns[i] = posns[i]->next;
170 src[i + merge] = &df_conflict_entry;
176 else if (i + 1 < head_idx)
178 else if (i + 1 > head_idx)
183 ce = xcalloc(1, ce_size);
184 ce->ce_mode = create_ce_mode(posns[i]->mode);
185 ce->ce_flags = create_ce_flags(baselen + pathlen,
187 memcpy(ce->name, base, baselen);
188 memcpy(ce->name + baselen, first, pathlen + 1);
192 memcpy(ce->sha1, posns[i]->sha1, 20);
194 subposns[i] = &df_conflict_list;
195 posns[i] = posns[i]->next;
202 printf("%s:\n", first);
203 for (i = 0; i < src_size; i++) {
206 printf("%s\n", sha1_to_hex(src[i]->sha1));
214 printf("Added %d entries\n", ret);
218 for (i = 0; i < src_size; i++) {
220 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
226 char *newbase = xmalloc(baselen + 2 + pathlen);
227 memcpy(newbase, base, baselen);
228 memcpy(newbase + baselen, first, pathlen);
229 newbase[baselen + pathlen] = '/';
230 newbase[baselen + pathlen + 1] = '\0';
231 if (unpack_trees_rec(subposns, len, newbase, fn,
241 static void reject_merge(struct cache_entry *ce)
243 die("Entry '%s' would be overwritten by merge. Cannot merge.",
247 /* Unlink the last component and attempt to remove leading
248 * directories, in case this unlink is the removal of the
249 * last entry in the directory -- empty directories are removed.
251 static void unlink_entry(char *name)
260 cp = strrchr(name, '/');
267 status = rmdir(name);
276 static void progress_interval(int signum)
281 static void setup_progress_signal(void)
286 memset(&sa, 0, sizeof(sa));
287 sa.sa_handler = progress_interval;
288 sigemptyset(&sa.sa_mask);
289 sa.sa_flags = SA_RESTART;
290 sigaction(SIGALRM, &sa, NULL);
292 v.it_interval.tv_sec = 1;
293 v.it_interval.tv_usec = 0;
294 v.it_value = v.it_interval;
295 setitimer(ITIMER_REAL, &v, NULL);
298 static void check_updates(struct cache_entry **src, int nr)
300 static struct checkout state = {
306 unsigned short mask = htons(CE_UPDATE);
307 unsigned last_percent = 200, cnt = 0, total = 0;
309 if (update && verbose_update) {
310 for (total = cnt = 0; cnt < nr; cnt++) {
311 struct cache_entry *ce = src[cnt];
312 if (!ce->ce_mode || ce->ce_flags & mask)
316 /* Don't bother doing this for very small updates */
321 fprintf(stderr, "Checking files out...\n");
322 setup_progress_signal();
329 struct cache_entry *ce = *src++;
332 if (!ce->ce_mode || ce->ce_flags & mask) {
335 percent = (cnt * 100) / total;
336 if (percent != last_percent ||
338 fprintf(stderr, "%4u%% (%u/%u) done\r",
339 percent, cnt, total);
340 last_percent = percent;
346 unlink_entry(ce->name);
349 if (ce->ce_flags & mask) {
350 ce->ce_flags &= ~mask;
352 checkout_entry(ce, &state, NULL);
356 signal(SIGALRM, SIG_IGN);
361 static int unpack_trees(merge_fn_t fn)
364 unsigned len = object_list_length(trees);
365 struct tree_entry_list **posns;
367 struct object_list *posn = trees;
371 posns = xmalloc(len * sizeof(struct tree_entry_list *));
372 for (i = 0; i < len; i++) {
373 posns[i] = create_tree_entry_list((struct tree *) posn->item);
376 if (unpack_trees_rec(posns, len, "", fn, &indpos))
380 if (trivial_merges_only && nontrivial_merge)
381 die("Merge requires file-level merging");
383 check_updates(active_cache, active_nr);
387 static int list_tree(unsigned char *sha1)
389 struct tree *tree = parse_tree_indirect(sha1);
392 object_list_append(&tree->object, &trees);
396 static int same(struct cache_entry *a, struct cache_entry *b)
402 return a->ce_mode == b->ce_mode &&
403 !memcmp(a->sha1, b->sha1, 20);
408 * When a CE gets turned into an unmerged entry, we
409 * want it to be up-to-date
411 static void verify_uptodate(struct cache_entry *ce)
415 if (index_only || reset)
418 if (!lstat(ce->name, &st)) {
419 unsigned changed = ce_match_stat(ce, &st, 1);
425 ce->ce_flags |= htons(CE_UPDATE);
430 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
433 static void invalidate_ce_path(struct cache_entry *ce)
436 cache_tree_invalidate_path(active_cache_tree, ce->name);
440 * We do not want to remove or overwrite a working tree file that
443 static void verify_absent(const char *path, const char *action)
447 if (index_only || reset || !update)
449 if (!lstat(path, &st))
450 die("Untracked working tree file '%s' "
451 "would be %s by merge.", path, action);
454 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
456 merge->ce_flags |= htons(CE_UPDATE);
459 * See if we can re-use the old CE directly?
460 * That way we get the uptodate stat info.
462 * This also removes the UPDATE flag on
465 if (same(old, merge)) {
468 verify_uptodate(old);
469 invalidate_ce_path(old);
473 verify_absent(merge->name, "overwritten");
474 invalidate_ce_path(merge);
477 merge->ce_flags &= ~htons(CE_STAGEMASK);
478 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
482 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
485 verify_uptodate(old);
487 verify_absent(ce->name, "removed");
489 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
490 invalidate_ce_path(ce);
494 static int keep_entry(struct cache_entry *ce)
496 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
501 static void show_stage_entry(FILE *o,
502 const char *label, const struct cache_entry *ce)
505 fprintf(o, "%s (missing)\n", label);
507 fprintf(o, "%s%06o %s %d\t%s\n",
510 sha1_to_hex(ce->sha1),
516 static int threeway_merge(struct cache_entry **stages)
518 struct cache_entry *index;
519 struct cache_entry *head;
520 struct cache_entry *remote = stages[head_idx + 1];
523 int remote_match = 0;
524 const char *path = NULL;
526 int df_conflict_head = 0;
527 int df_conflict_remote = 0;
529 int any_anc_missing = 0;
530 int no_anc_exists = 1;
533 for (i = 1; i < head_idx; i++) {
538 path = stages[i]->name;
544 head = stages[head_idx];
546 if (head == &df_conflict_entry) {
547 df_conflict_head = 1;
551 if (remote == &df_conflict_entry) {
552 df_conflict_remote = 1;
563 /* First, if there's a #16 situation, note that to prevent #13
566 if (!same(remote, head)) {
567 for (i = 1; i < head_idx; i++) {
568 if (same(stages[i], head)) {
571 if (same(stages[i], remote)) {
577 /* We start with cases where the index is allowed to match
578 * something other than the head: #14(ALT) and #2ALT, where it
579 * is permitted to match the result instead.
581 /* #14, #14ALT, #2ALT */
582 if (remote && !df_conflict_head && head_match && !remote_match) {
583 if (index && !same(index, remote) && !same(index, head))
585 return merged_entry(remote, index);
588 * If we have an entry in the index cache, then we want to
589 * make sure that it matches head.
591 if (index && !same(index, head)) {
597 if (same(head, remote))
598 return merged_entry(head, index);
600 if (!df_conflict_remote && remote_match && !head_match)
601 return merged_entry(head, index);
605 if (!head && !remote && any_anc_missing)
608 /* Under the new "aggressive" rule, we resolve mostly trivial
609 * cases that we historically had git-merge-one-file resolve.
612 int head_deleted = !head && !df_conflict_head;
613 int remote_deleted = !remote && !df_conflict_remote;
616 * Deleted in one and unchanged in the other.
618 if ((head_deleted && remote_deleted) ||
619 (head_deleted && remote && remote_match) ||
620 (remote_deleted && head && head_match)) {
622 return deleted_entry(index, index);
624 verify_absent(path, "removed");
628 * Added in both, identically.
630 if (no_anc_exists && head && remote && same(head, remote))
631 return merged_entry(head, index);
635 /* Below are "no merge" cases, which require that the index be
636 * up-to-date to avoid the files getting overwritten with
637 * conflict resolution files.
640 verify_uptodate(index);
643 verify_absent(path, "overwritten");
645 nontrivial_merge = 1;
647 /* #2, #3, #4, #6, #7, #9, #11. */
649 if (!head_match || !remote_match) {
650 for (i = 1; i < head_idx; i++) {
652 keep_entry(stages[i]);
660 fprintf(stderr, "read-tree: warning #16 detected\n");
661 show_stage_entry(stderr, "head ", stages[head_match]);
662 show_stage_entry(stderr, "remote ", stages[remote_match]);
665 if (head) { count += keep_entry(head); }
666 if (remote) { count += keep_entry(remote); }
673 * The rule is to "carry forward" what is in the index without losing
674 * information across a "fast forward", favoring a successful merge
675 * over a merge failure when it makes sense. For details of the
676 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
679 static int twoway_merge(struct cache_entry **src)
681 struct cache_entry *current = src[0];
682 struct cache_entry *oldtree = src[1], *newtree = src[2];
685 return error("Cannot do a twoway merge of %d trees",
689 if ((!oldtree && !newtree) || /* 4 and 5 */
690 (!oldtree && newtree &&
691 same(current, newtree)) || /* 6 and 7 */
692 (oldtree && newtree &&
693 same(oldtree, newtree)) || /* 14 and 15 */
694 (oldtree && newtree &&
695 !same(oldtree, newtree) && /* 18 and 19*/
696 same(current, newtree))) {
697 return keep_entry(current);
699 else if (oldtree && !newtree && same(current, oldtree)) {
701 return deleted_entry(oldtree, current);
703 else if (oldtree && newtree &&
704 same(current, oldtree) && !same(current, newtree)) {
706 return merged_entry(newtree, current);
709 /* all other failures */
711 reject_merge(oldtree);
713 reject_merge(current);
715 reject_merge(newtree);
720 return merged_entry(newtree, current);
722 return deleted_entry(oldtree, current);
729 * - take the stat information from stage0, take the data from stage1
731 static int oneway_merge(struct cache_entry **src)
733 struct cache_entry *old = src[0];
734 struct cache_entry *a = src[1];
737 return error("Cannot do a oneway merge of %d trees",
741 return deleted_entry(old, old);
742 if (old && same(old, a)) {
745 if (lstat(old->name, &st) ||
746 ce_match_stat(old, &st, 1))
747 old->ce_flags |= htons(CE_UPDATE);
749 return keep_entry(old);
751 return merged_entry(a, old);
754 static int read_cache_unmerged(void)
757 struct cache_entry **dst;
762 for (i = 0; i < active_nr; i++) {
763 struct cache_entry *ce = active_cache[i];
766 invalidate_ce_path(ce);
773 active_nr -= deleted;
777 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
779 struct tree_desc desc;
782 memcpy(it->sha1, tree->object.sha1, 20);
783 desc.buf = tree->buffer;
784 desc.size = tree->size;
789 const unsigned char *sha1;
791 sha1 = tree_entry_extract(&desc, &name, &mode);
792 update_tree_entry(&desc);
796 struct cache_tree_sub *sub;
797 struct tree *subtree = lookup_tree(sha1);
798 if (!subtree->object.parsed)
800 sub = cache_tree_sub(it, name);
801 sub->cache_tree = cache_tree();
802 prime_cache_tree_rec(sub->cache_tree, subtree);
803 cnt += sub->cache_tree->entry_count;
806 it->entry_count = cnt;
809 static void prime_cache_tree(void)
811 struct tree *tree = (struct tree *)trees->item;
814 active_cache_tree = cache_tree();
815 prime_cache_tree_rec(active_cache_tree, tree);
819 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
821 static struct cache_file cache_file;
823 int cmd_read_tree(int argc, const char **argv, char **envp)
825 int i, newfd, stage = 0;
826 unsigned char sha1[20];
827 merge_fn_t fn = NULL;
829 setup_git_directory();
830 git_config(git_default_config);
832 newfd = hold_index_file_for_update(&cache_file, get_index_file());
834 die("unable to create new cachefile");
836 git_config(git_default_config);
840 for (i = 1; i < argc; i++) {
841 const char *arg = argv[i];
843 /* "-u" means "update", meaning that a merge will update
846 if (!strcmp(arg, "-u")) {
851 if (!strcmp(arg, "-v")) {
856 /* "-i" means "index only", meaning that a merge will
857 * not even look at the working tree.
859 if (!strcmp(arg, "-i")) {
864 /* This differs from "-m" in that we'll silently ignore unmerged entries */
865 if (!strcmp(arg, "--reset")) {
867 usage(read_tree_usage);
871 read_cache_unmerged();
875 if (!strcmp(arg, "--trivial")) {
876 trivial_merges_only = 1;
880 if (!strcmp(arg, "--aggressive")) {
885 /* "-m" stands for "merge", meaning we start in stage 1 */
886 if (!strcmp(arg, "-m")) {
888 usage(read_tree_usage);
889 if (read_cache_unmerged())
890 die("you need to resolve your current index first");
896 /* using -u and -i at the same time makes no sense */
897 if (1 < index_only + update)
898 usage(read_tree_usage);
900 if (get_sha1(arg, sha1))
901 die("Not a valid object name %s", arg);
902 if (list_tree(sha1) < 0)
903 die("failed to unpack tree object %s", arg);
906 if ((update||index_only) && !merge)
907 usage(read_tree_usage);
911 die("just how do you expect me to merge %d trees?", stage-1);
922 cache_tree_free(&active_cache_tree);
927 head_idx = stage - 2;
935 * When reading only one tree (either the most basic form,
936 * "-m ent" or "--reset ent" form), we can obtain a fully
937 * valid cache-tree because the index must match exactly
938 * what came from the tree.
940 if (trees && trees->item && (!merge || (stage == 2))) {
941 cache_tree_free(&active_cache_tree);
945 if (write_cache(newfd, active_cache, active_nr) ||
946 commit_index_file(&cache_file))
947 die("unable to write new index file");