2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
16 static int update = 0;
17 static int index_only = 0;
18 static int nontrivial_merge = 0;
19 static int trivial_merges_only = 0;
20 static int aggressive = 0;
21 static int verbose_update = 0;
22 static volatile int progress_update = 0;
24 static int head_idx = -1;
25 static int merge_size = 0;
27 static struct object_list *trees = NULL;
29 static struct cache_entry df_conflict_entry = {
32 static struct tree_entry_list df_conflict_list = {
34 .next = &df_conflict_list
37 typedef int (*merge_fn_t)(struct cache_entry **src);
39 static int entcmp(char *name1, int dir1, char *name2, int dir2)
41 int len1 = strlen(name1);
42 int len2 = strlen(name2);
43 int len = len1 < len2 ? len1 : len2;
44 int ret = memcmp(name1, name2, len);
54 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
60 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
61 const char *base, merge_fn_t fn, int *indpos)
63 int baselen = strlen(base);
64 int src_size = len + 1;
71 struct tree_entry_list **subposns;
72 struct cache_entry **src;
78 /* Find the first name in the input. */
84 if (merge && *indpos < active_nr) {
85 /* This is a bit tricky: */
86 /* If the index has a subdirectory (with
87 * contents) as the first name, it'll get a
88 * filename like "foo/bar". But that's after
89 * "foo", so the entry in trees will get
90 * handled first, at which point we'll go into
91 * "foo", and deal with "bar" from the index,
92 * because the base will be "foo/". The only
93 * way we can actually have "foo/bar" first of
94 * all the things is if the trees don't
95 * contain "foo" at all, in which case we'll
96 * handle "foo/bar" without going into the
97 * directory, but that's fine (and will return
98 * an error anyway, with the added unknown
102 cache_name = active_cache[*indpos]->name;
103 if (strlen(cache_name) > baselen &&
104 !memcmp(cache_name, base, baselen)) {
105 cache_name += baselen;
114 printf("index %s\n", first);
116 for (i = 0; i < len; i++) {
117 if (!posns[i] || posns[i] == &df_conflict_list)
120 printf("%d %s\n", i + 1, posns[i]->name);
122 if (!first || entcmp(first, firstdir,
124 posns[i]->directory) > 0) {
125 first = posns[i]->name;
126 firstdir = posns[i]->directory;
129 /* No name means we're done */
133 pathlen = strlen(first);
134 ce_size = cache_entry_size(baselen + pathlen);
136 src = xmalloc(sizeof(struct cache_entry *) * src_size);
137 memset(src, 0, sizeof(struct cache_entry *) * src_size);
139 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
140 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
142 if (cache_name && !strcmp(cache_name, first)) {
144 src[0] = active_cache[*indpos];
145 remove_cache_entry_at(*indpos);
148 for (i = 0; i < len; i++) {
149 struct cache_entry *ce;
152 (posns[i] != &df_conflict_list &&
153 strcmp(first, posns[i]->name))) {
157 if (posns[i] == &df_conflict_list) {
158 src[i + merge] = &df_conflict_entry;
162 if (posns[i]->directory) {
164 parse_tree(posns[i]->item.tree);
165 subposns[i] = posns[i]->item.tree->entries;
166 posns[i] = posns[i]->next;
167 src[i + merge] = &df_conflict_entry;
173 else if (i + 1 < head_idx)
175 else if (i + 1 > head_idx)
180 ce = xmalloc(ce_size);
181 memset(ce, 0, 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)
276 signal(SIGALRM, progress_interval);
280 static void check_updates(struct cache_entry **src, int nr)
282 static struct checkout state = {
288 unsigned short mask = htons(CE_UPDATE);
289 unsigned last_percent = 200, cnt = 0, total = 0;
291 if (update && verbose_update) {
294 for (total = cnt = 0; cnt < nr; cnt++) {
295 struct cache_entry *ce = src[cnt];
296 if (!ce->ce_mode || ce->ce_flags & mask)
300 /* Don't bother doing this for very small updates */
305 v.it_interval.tv_sec = 1;
306 v.it_interval.tv_usec = 0;
307 v.it_value = v.it_interval;
308 signal(SIGALRM, progress_interval);
309 setitimer(ITIMER_REAL, &v, NULL);
310 fprintf(stderr, "Checking files out...\n");
317 struct cache_entry *ce = *src++;
320 if (!ce->ce_mode || ce->ce_flags & mask) {
323 percent = (cnt * 100) / total;
324 if (percent != last_percent ||
326 fprintf(stderr, "%4u%% (%u/%u) done\r",
327 percent, cnt, total);
328 last_percent = percent;
334 unlink_entry(ce->name);
337 if (ce->ce_flags & mask) {
338 ce->ce_flags &= ~mask;
340 checkout_entry(ce, &state, NULL);
345 signal(SIGALRM, SIG_IGN);
349 static int unpack_trees(merge_fn_t fn)
352 unsigned len = object_list_length(trees);
353 struct tree_entry_list **posns;
355 struct object_list *posn = trees;
359 posns = xmalloc(len * sizeof(struct tree_entry_list *));
360 for (i = 0; i < len; i++) {
361 posns[i] = ((struct tree *) posn->item)->entries;
364 if (unpack_trees_rec(posns, len, "", fn, &indpos))
368 if (trivial_merges_only && nontrivial_merge)
369 die("Merge requires file-level merging");
371 check_updates(active_cache, active_nr);
375 static int list_tree(unsigned char *sha1)
377 struct tree *tree = parse_tree_indirect(sha1);
380 object_list_append(&tree->object, &trees);
384 static int same(struct cache_entry *a, struct cache_entry *b)
390 return a->ce_mode == b->ce_mode &&
391 !memcmp(a->sha1, b->sha1, 20);
396 * When a CE gets turned into an unmerged entry, we
397 * want it to be up-to-date
399 static void verify_uptodate(struct cache_entry *ce)
406 if (!lstat(ce->name, &st)) {
407 unsigned changed = ce_match_stat(ce, &st, 1);
414 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
417 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
419 merge->ce_flags |= htons(CE_UPDATE);
422 * See if we can re-use the old CE directly?
423 * That way we get the uptodate stat info.
425 * This also removes the UPDATE flag on
428 if (same(old, merge)) {
431 verify_uptodate(old);
434 merge->ce_flags &= ~htons(CE_STAGEMASK);
435 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
439 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
442 verify_uptodate(old);
444 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
448 static int keep_entry(struct cache_entry *ce)
450 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
455 static void show_stage_entry(FILE *o,
456 const char *label, const struct cache_entry *ce)
459 fprintf(o, "%s (missing)\n", label);
461 fprintf(o, "%s%06o %s %d\t%s\n",
464 sha1_to_hex(ce->sha1),
470 static int threeway_merge(struct cache_entry **stages)
472 struct cache_entry *index;
473 struct cache_entry *head;
474 struct cache_entry *remote = stages[head_idx + 1];
477 int remote_match = 0;
479 int df_conflict_head = 0;
480 int df_conflict_remote = 0;
482 int any_anc_missing = 0;
483 int no_anc_exists = 1;
486 for (i = 1; i < head_idx; i++) {
494 head = stages[head_idx];
496 if (head == &df_conflict_entry) {
497 df_conflict_head = 1;
501 if (remote == &df_conflict_entry) {
502 df_conflict_remote = 1;
506 /* First, if there's a #16 situation, note that to prevent #13
509 if (!same(remote, head)) {
510 for (i = 1; i < head_idx; i++) {
511 if (same(stages[i], head)) {
514 if (same(stages[i], remote)) {
520 /* We start with cases where the index is allowed to match
521 * something other than the head: #14(ALT) and #2ALT, where it
522 * is permitted to match the result instead.
524 /* #14, #14ALT, #2ALT */
525 if (remote && !df_conflict_head && head_match && !remote_match) {
526 if (index && !same(index, remote) && !same(index, head))
528 return merged_entry(remote, index);
531 * If we have an entry in the index cache, then we want to
532 * make sure that it matches head.
534 if (index && !same(index, head)) {
540 if (same(head, remote))
541 return merged_entry(head, index);
543 if (!df_conflict_remote && remote_match && !head_match)
544 return merged_entry(head, index);
548 if (!head && !remote && any_anc_missing)
551 /* Under the new "aggressive" rule, we resolve mostly trivial
552 * cases that we historically had git-merge-one-file resolve.
555 int head_deleted = !head && !df_conflict_head;
556 int remote_deleted = !remote && !df_conflict_remote;
559 * Deleted in one and unchanged in the other.
561 if ((head_deleted && remote_deleted) ||
562 (head_deleted && remote && remote_match) ||
563 (remote_deleted && head && head_match)) {
565 return deleted_entry(index, index);
569 * Added in both, identically.
571 if (no_anc_exists && head && remote && same(head, remote))
572 return merged_entry(head, index);
576 /* Below are "no merge" cases, which require that the index be
577 * up-to-date to avoid the files getting overwritten with
578 * conflict resolution files.
581 verify_uptodate(index);
584 nontrivial_merge = 1;
586 /* #2, #3, #4, #6, #7, #9, #11. */
588 if (!head_match || !remote_match) {
589 for (i = 1; i < head_idx; i++) {
591 keep_entry(stages[i]);
599 fprintf(stderr, "read-tree: warning #16 detected\n");
600 show_stage_entry(stderr, "head ", stages[head_match]);
601 show_stage_entry(stderr, "remote ", stages[remote_match]);
604 if (head) { count += keep_entry(head); }
605 if (remote) { count += keep_entry(remote); }
612 * The rule is to "carry forward" what is in the index without losing
613 * information across a "fast forward", favoring a successful merge
614 * over a merge failure when it makes sense. For details of the
615 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
618 static int twoway_merge(struct cache_entry **src)
620 struct cache_entry *current = src[0];
621 struct cache_entry *oldtree = src[1], *newtree = src[2];
624 return error("Cannot do a twoway merge of %d trees",
628 if ((!oldtree && !newtree) || /* 4 and 5 */
629 (!oldtree && newtree &&
630 same(current, newtree)) || /* 6 and 7 */
631 (oldtree && newtree &&
632 same(oldtree, newtree)) || /* 14 and 15 */
633 (oldtree && newtree &&
634 !same(oldtree, newtree) && /* 18 and 19*/
635 same(current, newtree))) {
636 return keep_entry(current);
638 else if (oldtree && !newtree && same(current, oldtree)) {
640 return deleted_entry(oldtree, current);
642 else if (oldtree && newtree &&
643 same(current, oldtree) && !same(current, newtree)) {
645 return merged_entry(newtree, current);
648 /* all other failures */
650 reject_merge(oldtree);
652 reject_merge(current);
654 reject_merge(newtree);
659 return merged_entry(newtree, current);
661 return deleted_entry(oldtree, current);
668 * - take the stat information from stage0, take the data from stage1
670 static int oneway_merge(struct cache_entry **src)
672 struct cache_entry *old = src[0];
673 struct cache_entry *a = src[1];
676 return error("Cannot do a oneway merge of %d trees",
681 if (old && same(old, a)) {
682 return keep_entry(old);
684 return merged_entry(a, NULL);
687 static int read_cache_unmerged(void)
690 struct cache_entry **dst;
695 for (i = 0; i < active_nr; i++) {
696 struct cache_entry *ce = active_cache[i];
705 active_nr -= deleted;
709 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
711 static struct cache_file cache_file;
713 int main(int argc, char **argv)
715 int i, newfd, reset, stage = 0;
716 unsigned char sha1[20];
717 merge_fn_t fn = NULL;
719 setup_git_directory();
720 git_config(git_default_config);
722 newfd = hold_index_file_for_update(&cache_file, get_index_file());
724 die("unable to create new cachefile");
726 git_config(git_default_config);
730 for (i = 1; i < argc; i++) {
731 const char *arg = argv[i];
733 /* "-u" means "update", meaning that a merge will update
736 if (!strcmp(arg, "-u")) {
741 if (!strcmp(arg, "-v")) {
746 /* "-i" means "index only", meaning that a merge will
747 * not even look at the working tree.
749 if (!strcmp(arg, "-i")) {
754 /* This differs from "-m" in that we'll silently ignore unmerged entries */
755 if (!strcmp(arg, "--reset")) {
757 usage(read_tree_usage);
761 read_cache_unmerged();
765 if (!strcmp(arg, "--trivial")) {
766 trivial_merges_only = 1;
770 if (!strcmp(arg, "--aggressive")) {
775 /* "-m" stands for "merge", meaning we start in stage 1 */
776 if (!strcmp(arg, "-m")) {
778 usage(read_tree_usage);
779 if (read_cache_unmerged())
780 die("you need to resolve your current index first");
786 /* using -u and -i at the same time makes no sense */
787 if (1 < index_only + update)
788 usage(read_tree_usage);
790 if (get_sha1(arg, sha1) < 0)
791 usage(read_tree_usage);
792 if (list_tree(sha1) < 0)
793 die("failed to unpack tree object %s", arg);
796 if ((update||index_only) && !merge)
797 usage(read_tree_usage);
801 die("just how do you expect me to merge %d trees?", stage-1);
818 head_idx = stage - 2;
824 if (write_cache(newfd, active_cache, active_nr) ||
825 commit_index_file(&cache_file))
826 die("unable to write new index file");