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;
19 static int head_idx = -1;
20 static int merge_size = 0;
22 static struct object_list *trees = NULL;
24 static struct cache_entry df_conflict_entry = {
27 static struct tree_entry_list df_conflict_list = {
29 .next = &df_conflict_list
32 typedef int (*merge_fn_t)(struct cache_entry **src);
34 static int entcmp(char *name1, int dir1, char *name2, int dir2)
36 int len1 = strlen(name1);
37 int len2 = strlen(name2);
38 int len = len1 < len2 ? len1 : len2;
39 int ret = memcmp(name1, name2, len);
49 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
55 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
56 const char *base, merge_fn_t fn, int *indpos)
58 int baselen = strlen(base);
59 int src_size = len + 1;
66 struct tree_entry_list **subposns;
67 struct cache_entry **src;
73 /* Find the first name in the input. */
79 if (merge && *indpos < active_nr) {
80 /* This is a bit tricky: */
81 /* If the index has a subdirectory (with
82 * contents) as the first name, it'll get a
83 * filename like "foo/bar". But that's after
84 * "foo", so the entry in trees will get
85 * handled first, at which point we'll go into
86 * "foo", and deal with "bar" from the index,
87 * because the base will be "foo/". The only
88 * way we can actually have "foo/bar" first of
89 * all the things is if the trees don't
90 * contain "foo" at all, in which case we'll
91 * handle "foo/bar" without going into the
92 * directory, but that's fine (and will return
93 * an error anyway, with the added unknown
97 cache_name = active_cache[*indpos]->name;
98 if (strlen(cache_name) > baselen &&
99 !memcmp(cache_name, base, baselen)) {
100 cache_name += baselen;
109 printf("index %s\n", first);
111 for (i = 0; i < len; i++) {
112 if (!posns[i] || posns[i] == &df_conflict_list)
115 printf("%d %s\n", i + 1, posns[i]->name);
117 if (!first || entcmp(first, firstdir,
119 posns[i]->directory) > 0) {
120 first = posns[i]->name;
121 firstdir = posns[i]->directory;
124 /* No name means we're done */
128 pathlen = strlen(first);
129 ce_size = cache_entry_size(baselen + pathlen);
131 src = xmalloc(sizeof(struct cache_entry *) * src_size);
132 memset(src, 0, sizeof(struct cache_entry *) * src_size);
134 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
135 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
137 if (cache_name && !strcmp(cache_name, first)) {
139 src[0] = active_cache[*indpos];
140 remove_cache_entry_at(*indpos);
143 for (i = 0; i < len; i++) {
144 struct cache_entry *ce;
147 (posns[i] != &df_conflict_list &&
148 strcmp(first, posns[i]->name))) {
152 if (posns[i] == &df_conflict_list) {
153 src[i + merge] = &df_conflict_entry;
157 if (posns[i]->directory) {
159 parse_tree(posns[i]->item.tree);
160 subposns[i] = posns[i]->item.tree->entries;
161 posns[i] = posns[i]->next;
162 src[i + merge] = &df_conflict_entry;
168 else if (i + 1 < head_idx)
170 else if (i + 1 > head_idx)
175 ce = xmalloc(ce_size);
176 memset(ce, 0, ce_size);
177 ce->ce_mode = create_ce_mode(posns[i]->mode);
178 ce->ce_flags = create_ce_flags(baselen + pathlen,
180 memcpy(ce->name, base, baselen);
181 memcpy(ce->name + baselen, first, pathlen + 1);
185 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
187 subposns[i] = &df_conflict_list;
188 posns[i] = posns[i]->next;
195 printf("%s:\n", first);
196 for (i = 0; i < src_size; i++) {
199 printf("%s\n", sha1_to_hex(src[i]->sha1));
207 printf("Added %d entries\n", ret);
211 for (i = 0; i < src_size; i++) {
213 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
219 char *newbase = xmalloc(baselen + 2 + pathlen);
220 memcpy(newbase, base, baselen);
221 memcpy(newbase + baselen, first, pathlen);
222 newbase[baselen + pathlen] = '/';
223 newbase[baselen + pathlen + 1] = '\0';
224 if (unpack_trees_rec(subposns, len, newbase, fn,
234 static void reject_merge(struct cache_entry *ce)
236 die("Entry '%s' would be overwritten by merge. Cannot merge.",
240 static void check_updates(struct cache_entry **src, int nr)
242 static struct checkout state = {
248 unsigned short mask = htons(CE_UPDATE);
250 struct cache_entry *ce = *src++;
256 if (ce->ce_flags & mask) {
257 ce->ce_flags &= ~mask;
259 checkout_entry(ce, &state);
264 static int unpack_trees(merge_fn_t fn)
267 unsigned len = object_list_length(trees);
268 struct tree_entry_list **posns =
269 xmalloc(len * sizeof(struct tree_entry_list *));
271 struct object_list *posn = trees;
273 for (i = 0; i < len; i++) {
274 posns[i] = ((struct tree *) posn->item)->entries;
277 if (unpack_trees_rec(posns, len, "", fn, &indpos))
280 if (trivial_merges_only && nontrivial_merge)
281 die("Merge requires file-level merging");
283 check_updates(active_cache, active_nr);
287 static int list_tree(unsigned char *sha1)
289 struct tree *tree = parse_tree_indirect(sha1);
292 object_list_append(&tree->object, &trees);
296 static int same(struct cache_entry *a, struct cache_entry *b)
302 return a->ce_mode == b->ce_mode &&
303 !memcmp(a->sha1, b->sha1, 20);
308 * When a CE gets turned into an unmerged entry, we
309 * want it to be up-to-date
311 static void verify_uptodate(struct cache_entry *ce)
318 if (!lstat(ce->name, &st)) {
319 unsigned changed = ce_match_stat(ce, &st);
326 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
329 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
331 merge->ce_flags |= htons(CE_UPDATE);
334 * See if we can re-use the old CE directly?
335 * That way we get the uptodate stat info.
337 * This also removes the UPDATE flag on
340 if (same(old, merge)) {
343 verify_uptodate(old);
346 merge->ce_flags &= ~htons(CE_STAGEMASK);
347 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
351 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
354 verify_uptodate(old);
356 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
360 static int keep_entry(struct cache_entry *ce)
362 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
367 static void show_stage_entry(FILE *o,
368 const char *label, const struct cache_entry *ce)
371 fprintf(o, "%s (missing)\n", label);
373 fprintf(o, "%s%06o %s %d\t%s\n",
376 sha1_to_hex(ce->sha1),
382 static int threeway_merge(struct cache_entry **stages)
384 struct cache_entry *index;
385 struct cache_entry *head;
386 struct cache_entry *remote = stages[head_idx + 1];
389 int remote_match = 0;
391 int df_conflict_head = 0;
392 int df_conflict_remote = 0;
394 int any_anc_missing = 0;
397 for (i = 1; i < head_idx; i++) {
403 head = stages[head_idx];
405 if (head == &df_conflict_entry) {
406 df_conflict_head = 1;
410 if (remote == &df_conflict_entry) {
411 df_conflict_remote = 1;
415 /* First, if there's a #16 situation, note that to prevent #13
418 if (!same(remote, head)) {
419 for (i = 1; i < head_idx; i++) {
420 if (same(stages[i], head)) {
423 if (same(stages[i], remote)) {
429 /* We start with cases where the index is allowed to match
430 * something other than the head: #14(ALT) and #2ALT, where it
431 * is permitted to match the result instead.
433 /* #14, #14ALT, #2ALT */
434 if (remote && !df_conflict_head && head_match && !remote_match) {
435 if (index && !same(index, remote) && !same(index, head))
437 return merged_entry(remote, index);
440 * If we have an entry in the index cache, then we want to
441 * make sure that it matches head.
443 if (index && !same(index, head)) {
449 if (same(head, remote))
450 return merged_entry(head, index);
452 if (!df_conflict_remote && remote_match && !head_match)
453 return merged_entry(head, index);
457 if (!head && !remote && any_anc_missing)
460 /* Below are "no merge" cases, which require that the index be
461 * up-to-date to avoid the files getting overwritten with
462 * conflict resolution files.
465 verify_uptodate(index);
468 nontrivial_merge = 1;
470 /* #2, #3, #4, #6, #7, #9, #11. */
472 if (!head_match || !remote_match) {
473 for (i = 1; i < head_idx; i++) {
475 keep_entry(stages[i]);
483 fprintf(stderr, "read-tree: warning #16 detected\n");
484 show_stage_entry(stderr, "head ", stages[head_match]);
485 show_stage_entry(stderr, "remote ", stages[remote_match]);
488 if (head) { count += keep_entry(head); }
489 if (remote) { count += keep_entry(remote); }
496 * The rule is to "carry forward" what is in the index without losing
497 * information across a "fast forward", favoring a successful merge
498 * over a merge failure when it makes sense. For details of the
499 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
502 static int twoway_merge(struct cache_entry **src)
504 struct cache_entry *current = src[0];
505 struct cache_entry *oldtree = src[1], *newtree = src[2];
508 return error("Cannot do a twoway merge of %d trees\n",
512 if ((!oldtree && !newtree) || /* 4 and 5 */
513 (!oldtree && newtree &&
514 same(current, newtree)) || /* 6 and 7 */
515 (oldtree && newtree &&
516 same(oldtree, newtree)) || /* 14 and 15 */
517 (oldtree && newtree &&
518 !same(oldtree, newtree) && /* 18 and 19*/
519 same(current, newtree))) {
520 return keep_entry(current);
522 else if (oldtree && !newtree && same(current, oldtree)) {
524 return deleted_entry(oldtree, current);
526 else if (oldtree && newtree &&
527 same(current, oldtree) && !same(current, newtree)) {
529 return merged_entry(newtree, current);
532 /* all other failures */
534 reject_merge(oldtree);
536 reject_merge(current);
538 reject_merge(newtree);
543 return merged_entry(newtree, current);
545 return deleted_entry(oldtree, current);
552 * - take the stat information from stage0, take the data from stage1
554 static int oneway_merge(struct cache_entry **src)
556 struct cache_entry *old = src[0];
557 struct cache_entry *a = src[1];
560 return error("Cannot do a oneway merge of %d trees\n",
565 if (old && same(old, a)) {
566 return keep_entry(old);
568 return merged_entry(a, NULL);
571 static int read_cache_unmerged(void)
574 struct cache_entry **dst;
579 for (i = 0; i < active_nr; i++) {
580 struct cache_entry *ce = active_cache[i];
589 active_nr -= deleted;
593 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
595 static struct cache_file cache_file;
597 int main(int argc, char **argv)
599 int i, newfd, reset, stage = 0;
600 unsigned char sha1[20];
601 merge_fn_t fn = NULL;
603 newfd = hold_index_file_for_update(&cache_file, get_index_file());
605 die("unable to create new cachefile");
609 for (i = 1; i < argc; i++) {
610 const char *arg = argv[i];
612 /* "-u" means "update", meaning that a merge will update
615 if (!strcmp(arg, "-u")) {
620 /* "-i" means "index only", meaning that a merge will
621 * not even look at the working tree.
623 if (!strcmp(arg, "-i")) {
628 /* This differs from "-m" in that we'll silently ignore unmerged entries */
629 if (!strcmp(arg, "--reset")) {
631 usage(read_tree_usage);
635 read_cache_unmerged();
639 if (!strcmp(arg, "--trivial")) {
640 trivial_merges_only = 1;
644 /* "-m" stands for "merge", meaning we start in stage 1 */
645 if (!strcmp(arg, "-m")) {
647 usage(read_tree_usage);
648 if (read_cache_unmerged())
649 die("you need to resolve your current index first");
655 /* using -u and -i at the same time makes no sense */
656 if (1 < index_only + update)
657 usage(read_tree_usage);
659 if (get_sha1(arg, sha1) < 0)
660 usage(read_tree_usage);
661 if (list_tree(sha1) < 0)
662 die("failed to unpack tree object %s", arg);
665 if ((update||index_only) && !merge)
666 usage(read_tree_usage);
670 die("just how do you expect me to merge %d trees?", stage-1);
687 head_idx = stage - 2;
693 if (write_cache(newfd, active_cache, active_nr) ||
694 commit_index_file(&cache_file))
695 die("unable to write new index file");