2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
14 static int update = 0;
15 static int index_only = 0;
16 static int nontrivial_merge = 0;
17 static int trivial_merges_only = 0;
18 static int aggressive = 0;
20 static int head_idx = -1;
21 static int merge_size = 0;
23 static struct object_list *trees = NULL;
25 static struct cache_entry df_conflict_entry = {
28 static struct tree_entry_list df_conflict_list = {
30 .next = &df_conflict_list
33 typedef int (*merge_fn_t)(struct cache_entry **src);
35 static int entcmp(char *name1, int dir1, char *name2, int dir2)
37 int len1 = strlen(name1);
38 int len2 = strlen(name2);
39 int len = len1 < len2 ? len1 : len2;
40 int ret = memcmp(name1, name2, len);
50 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
56 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
57 const char *base, merge_fn_t fn, int *indpos)
59 int baselen = strlen(base);
60 int src_size = len + 1;
67 struct tree_entry_list **subposns;
68 struct cache_entry **src;
74 /* Find the first name in the input. */
80 if (merge && *indpos < active_nr) {
81 /* This is a bit tricky: */
82 /* If the index has a subdirectory (with
83 * contents) as the first name, it'll get a
84 * filename like "foo/bar". But that's after
85 * "foo", so the entry in trees will get
86 * handled first, at which point we'll go into
87 * "foo", and deal with "bar" from the index,
88 * because the base will be "foo/". The only
89 * way we can actually have "foo/bar" first of
90 * all the things is if the trees don't
91 * contain "foo" at all, in which case we'll
92 * handle "foo/bar" without going into the
93 * directory, but that's fine (and will return
94 * an error anyway, with the added unknown
98 cache_name = active_cache[*indpos]->name;
99 if (strlen(cache_name) > baselen &&
100 !memcmp(cache_name, base, baselen)) {
101 cache_name += baselen;
110 printf("index %s\n", first);
112 for (i = 0; i < len; i++) {
113 if (!posns[i] || posns[i] == &df_conflict_list)
116 printf("%d %s\n", i + 1, posns[i]->name);
118 if (!first || entcmp(first, firstdir,
120 posns[i]->directory) > 0) {
121 first = posns[i]->name;
122 firstdir = posns[i]->directory;
125 /* No name means we're done */
129 pathlen = strlen(first);
130 ce_size = cache_entry_size(baselen + pathlen);
132 src = xmalloc(sizeof(struct cache_entry *) * src_size);
133 memset(src, 0, sizeof(struct cache_entry *) * src_size);
135 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
136 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
138 if (cache_name && !strcmp(cache_name, first)) {
140 src[0] = active_cache[*indpos];
141 remove_cache_entry_at(*indpos);
144 for (i = 0; i < len; i++) {
145 struct cache_entry *ce;
148 (posns[i] != &df_conflict_list &&
149 strcmp(first, posns[i]->name))) {
153 if (posns[i] == &df_conflict_list) {
154 src[i + merge] = &df_conflict_entry;
158 if (posns[i]->directory) {
160 parse_tree(posns[i]->item.tree);
161 subposns[i] = posns[i]->item.tree->entries;
162 posns[i] = posns[i]->next;
163 src[i + merge] = &df_conflict_entry;
169 else if (i + 1 < head_idx)
171 else if (i + 1 > head_idx)
176 ce = xmalloc(ce_size);
177 memset(ce, 0, ce_size);
178 ce->ce_mode = create_ce_mode(posns[i]->mode);
179 ce->ce_flags = create_ce_flags(baselen + pathlen,
181 memcpy(ce->name, base, baselen);
182 memcpy(ce->name + baselen, first, pathlen + 1);
186 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
188 subposns[i] = &df_conflict_list;
189 posns[i] = posns[i]->next;
196 printf("%s:\n", first);
197 for (i = 0; i < src_size; i++) {
200 printf("%s\n", sha1_to_hex(src[i]->sha1));
208 printf("Added %d entries\n", ret);
212 for (i = 0; i < src_size; i++) {
214 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
220 char *newbase = xmalloc(baselen + 2 + pathlen);
221 memcpy(newbase, base, baselen);
222 memcpy(newbase + baselen, first, pathlen);
223 newbase[baselen + pathlen] = '/';
224 newbase[baselen + pathlen + 1] = '\0';
225 if (unpack_trees_rec(subposns, len, newbase, fn,
235 static void reject_merge(struct cache_entry *ce)
237 die("Entry '%s' would be overwritten by merge. Cannot merge.",
241 /* Unlink the last component and attempt to remove leading
242 * directories, in case this unlink is the removal of the
243 * last entry in the directory -- empty directories are removed.
245 static void unlink_entry(char *name)
254 cp = strrchr(name, '/');
261 status = rmdir(name);
270 static void check_updates(struct cache_entry **src, int nr)
272 static struct checkout state = {
278 unsigned short mask = htons(CE_UPDATE);
280 struct cache_entry *ce = *src++;
283 unlink_entry(ce->name);
286 if (ce->ce_flags & mask) {
287 ce->ce_flags &= ~mask;
289 checkout_entry(ce, &state);
294 static int unpack_trees(merge_fn_t fn)
297 unsigned len = object_list_length(trees);
298 struct tree_entry_list **posns;
300 struct object_list *posn = trees;
304 posns = xmalloc(len * sizeof(struct tree_entry_list *));
305 for (i = 0; i < len; i++) {
306 posns[i] = ((struct tree *) posn->item)->entries;
309 if (unpack_trees_rec(posns, len, "", fn, &indpos))
313 if (trivial_merges_only && nontrivial_merge)
314 die("Merge requires file-level merging");
316 check_updates(active_cache, active_nr);
320 static int list_tree(unsigned char *sha1)
322 struct tree *tree = parse_tree_indirect(sha1);
325 object_list_append(&tree->object, &trees);
329 static int same(struct cache_entry *a, struct cache_entry *b)
335 return a->ce_mode == b->ce_mode &&
336 !memcmp(a->sha1, b->sha1, 20);
341 * When a CE gets turned into an unmerged entry, we
342 * want it to be up-to-date
344 static void verify_uptodate(struct cache_entry *ce)
351 if (!lstat(ce->name, &st)) {
352 unsigned changed = ce_match_stat(ce, &st, 1);
359 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
362 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
364 merge->ce_flags |= htons(CE_UPDATE);
367 * See if we can re-use the old CE directly?
368 * That way we get the uptodate stat info.
370 * This also removes the UPDATE flag on
373 if (same(old, merge)) {
376 verify_uptodate(old);
379 merge->ce_flags &= ~htons(CE_STAGEMASK);
380 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
384 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
387 verify_uptodate(old);
389 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
393 static int keep_entry(struct cache_entry *ce)
395 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
400 static void show_stage_entry(FILE *o,
401 const char *label, const struct cache_entry *ce)
404 fprintf(o, "%s (missing)\n", label);
406 fprintf(o, "%s%06o %s %d\t%s\n",
409 sha1_to_hex(ce->sha1),
415 static int threeway_merge(struct cache_entry **stages)
417 struct cache_entry *index;
418 struct cache_entry *head;
419 struct cache_entry *remote = stages[head_idx + 1];
422 int remote_match = 0;
424 int df_conflict_head = 0;
425 int df_conflict_remote = 0;
427 int any_anc_missing = 0;
428 int no_anc_exists = 1;
431 for (i = 1; i < head_idx; i++) {
439 head = stages[head_idx];
441 if (head == &df_conflict_entry) {
442 df_conflict_head = 1;
446 if (remote == &df_conflict_entry) {
447 df_conflict_remote = 1;
451 /* First, if there's a #16 situation, note that to prevent #13
454 if (!same(remote, head)) {
455 for (i = 1; i < head_idx; i++) {
456 if (same(stages[i], head)) {
459 if (same(stages[i], remote)) {
465 /* We start with cases where the index is allowed to match
466 * something other than the head: #14(ALT) and #2ALT, where it
467 * is permitted to match the result instead.
469 /* #14, #14ALT, #2ALT */
470 if (remote && !df_conflict_head && head_match && !remote_match) {
471 if (index && !same(index, remote) && !same(index, head))
473 return merged_entry(remote, index);
476 * If we have an entry in the index cache, then we want to
477 * make sure that it matches head.
479 if (index && !same(index, head)) {
485 if (same(head, remote))
486 return merged_entry(head, index);
488 if (!df_conflict_remote && remote_match && !head_match)
489 return merged_entry(head, index);
493 if (!head && !remote && any_anc_missing)
496 /* Under the new "aggressive" rule, we resolve mostly trivial
497 * cases that we historically had git-merge-one-file resolve.
500 int head_deleted = !head && !df_conflict_head;
501 int remote_deleted = !remote && !df_conflict_remote;
504 * Deleted in one and unchanged in the other.
506 if ((head_deleted && remote_deleted) ||
507 (head_deleted && remote && remote_match) ||
508 (remote_deleted && head && head_match))
512 * Added in both, identically.
514 if (no_anc_exists && head && remote && same(head, remote))
515 return merged_entry(head, index);
519 /* Below are "no merge" cases, which require that the index be
520 * up-to-date to avoid the files getting overwritten with
521 * conflict resolution files.
524 verify_uptodate(index);
527 nontrivial_merge = 1;
529 /* #2, #3, #4, #6, #7, #9, #11. */
531 if (!head_match || !remote_match) {
532 for (i = 1; i < head_idx; i++) {
534 keep_entry(stages[i]);
542 fprintf(stderr, "read-tree: warning #16 detected\n");
543 show_stage_entry(stderr, "head ", stages[head_match]);
544 show_stage_entry(stderr, "remote ", stages[remote_match]);
547 if (head) { count += keep_entry(head); }
548 if (remote) { count += keep_entry(remote); }
555 * The rule is to "carry forward" what is in the index without losing
556 * information across a "fast forward", favoring a successful merge
557 * over a merge failure when it makes sense. For details of the
558 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
561 static int twoway_merge(struct cache_entry **src)
563 struct cache_entry *current = src[0];
564 struct cache_entry *oldtree = src[1], *newtree = src[2];
567 return error("Cannot do a twoway merge of %d trees\n",
571 if ((!oldtree && !newtree) || /* 4 and 5 */
572 (!oldtree && newtree &&
573 same(current, newtree)) || /* 6 and 7 */
574 (oldtree && newtree &&
575 same(oldtree, newtree)) || /* 14 and 15 */
576 (oldtree && newtree &&
577 !same(oldtree, newtree) && /* 18 and 19*/
578 same(current, newtree))) {
579 return keep_entry(current);
581 else if (oldtree && !newtree && same(current, oldtree)) {
583 return deleted_entry(oldtree, current);
585 else if (oldtree && newtree &&
586 same(current, oldtree) && !same(current, newtree)) {
588 return merged_entry(newtree, current);
591 /* all other failures */
593 reject_merge(oldtree);
595 reject_merge(current);
597 reject_merge(newtree);
602 return merged_entry(newtree, current);
604 return deleted_entry(oldtree, current);
611 * - take the stat information from stage0, take the data from stage1
613 static int oneway_merge(struct cache_entry **src)
615 struct cache_entry *old = src[0];
616 struct cache_entry *a = src[1];
619 return error("Cannot do a oneway merge of %d trees\n",
624 if (old && same(old, a)) {
625 return keep_entry(old);
627 return merged_entry(a, NULL);
630 static int read_cache_unmerged(void)
633 struct cache_entry **dst;
638 for (i = 0; i < active_nr; i++) {
639 struct cache_entry *ce = active_cache[i];
648 active_nr -= deleted;
652 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
654 static struct cache_file cache_file;
656 int main(int argc, char **argv)
658 int i, newfd, reset, stage = 0;
659 unsigned char sha1[20];
660 merge_fn_t fn = NULL;
662 setup_git_directory();
664 newfd = hold_index_file_for_update(&cache_file, get_index_file());
666 die("unable to create new cachefile");
668 git_config(git_default_config);
672 for (i = 1; i < argc; i++) {
673 const char *arg = argv[i];
675 /* "-u" means "update", meaning that a merge will update
678 if (!strcmp(arg, "-u")) {
683 /* "-i" means "index only", meaning that a merge will
684 * not even look at the working tree.
686 if (!strcmp(arg, "-i")) {
691 /* This differs from "-m" in that we'll silently ignore unmerged entries */
692 if (!strcmp(arg, "--reset")) {
694 usage(read_tree_usage);
698 read_cache_unmerged();
702 if (!strcmp(arg, "--trivial")) {
703 trivial_merges_only = 1;
707 if (!strcmp(arg, "--aggressive")) {
712 /* "-m" stands for "merge", meaning we start in stage 1 */
713 if (!strcmp(arg, "-m")) {
715 usage(read_tree_usage);
716 if (read_cache_unmerged())
717 die("you need to resolve your current index first");
723 /* using -u and -i at the same time makes no sense */
724 if (1 < index_only + update)
725 usage(read_tree_usage);
727 if (get_sha1(arg, sha1) < 0)
728 usage(read_tree_usage);
729 if (list_tree(sha1) < 0)
730 die("failed to unpack tree object %s", arg);
733 if ((update||index_only) && !merge)
734 usage(read_tree_usage);
738 die("just how do you expect me to merge %d trees?", stage-1);
755 head_idx = stage - 2;
761 if (write_cache(newfd, active_cache, active_nr) ||
762 commit_index_file(&cache_file))
763 die("unable to write new index file");