1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#define DBRT_DEBUG 1
7
8#include "cache.h"
9
10#include "object.h"
11#include "tree.h"
12
13static int merge = 0;
14static int update = 0;
15static int index_only = 0;
16static int nontrivial_merge = 0;
17static int trivial_merges_only = 0;
18static int aggressive = 0;
19
20static int head_idx = -1;
21static int merge_size = 0;
22
23static struct object_list *trees = NULL;
24
25static struct cache_entry df_conflict_entry = {
26};
27
28static struct tree_entry_list df_conflict_list = {
29 .name = NULL,
30 .next = &df_conflict_list
31};
32
33typedef int (*merge_fn_t)(struct cache_entry **src);
34
35static int entcmp(char *name1, int dir1, char *name2, int dir2)
36{
37 int len1 = strlen(name1);
38 int len2 = strlen(name2);
39 int len = len1 < len2 ? len1 : len2;
40 int ret = memcmp(name1, name2, len);
41 unsigned char c1, c2;
42 if (ret)
43 return ret;
44 c1 = name1[len];
45 c2 = name2[len];
46 if (!c1 && dir1)
47 c1 = '/';
48 if (!c2 && dir2)
49 c2 = '/';
50 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
51 if (c1 && c2 && !ret)
52 ret = len1 - len2;
53 return ret;
54}
55
56static int unpack_trees_rec(struct tree_entry_list **posns, int len,
57 const char *base, merge_fn_t fn, int *indpos)
58{
59 int baselen = strlen(base);
60 int src_size = len + 1;
61 do {
62 int i;
63 char *first;
64 int firstdir = 0;
65 int pathlen;
66 unsigned ce_size;
67 struct tree_entry_list **subposns;
68 struct cache_entry **src;
69 int any_files = 0;
70 int any_dirs = 0;
71 char *cache_name;
72 int ce_stage;
73
74 /* Find the first name in the input. */
75
76 first = NULL;
77 cache_name = NULL;
78
79 /* Check the cache */
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
95 * file case.
96 */
97
98 cache_name = active_cache[*indpos]->name;
99 if (strlen(cache_name) > baselen &&
100 !memcmp(cache_name, base, baselen)) {
101 cache_name += baselen;
102 first = cache_name;
103 } else {
104 cache_name = NULL;
105 }
106 }
107
108#if DBRT_DEBUG > 1
109 if (first)
110 printf("index %s\n", first);
111#endif
112 for (i = 0; i < len; i++) {
113 if (!posns[i] || posns[i] == &df_conflict_list)
114 continue;
115#if DBRT_DEBUG > 1
116 printf("%d %s\n", i + 1, posns[i]->name);
117#endif
118 if (!first || entcmp(first, firstdir,
119 posns[i]->name,
120 posns[i]->directory) > 0) {
121 first = posns[i]->name;
122 firstdir = posns[i]->directory;
123 }
124 }
125 /* No name means we're done */
126 if (!first)
127 return 0;
128
129 pathlen = strlen(first);
130 ce_size = cache_entry_size(baselen + pathlen);
131
132 src = xmalloc(sizeof(struct cache_entry *) * src_size);
133 memset(src, 0, sizeof(struct cache_entry *) * src_size);
134
135 subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
136 memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
137
138 if (cache_name && !strcmp(cache_name, first)) {
139 any_files = 1;
140 src[0] = active_cache[*indpos];
141 remove_cache_entry_at(*indpos);
142 }
143
144 for (i = 0; i < len; i++) {
145 struct cache_entry *ce;
146
147 if (!posns[i] ||
148 (posns[i] != &df_conflict_list &&
149 strcmp(first, posns[i]->name))) {
150 continue;
151 }
152
153 if (posns[i] == &df_conflict_list) {
154 src[i + merge] = &df_conflict_entry;
155 continue;
156 }
157
158 if (posns[i]->directory) {
159 any_dirs = 1;
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;
164 continue;
165 }
166
167 if (!merge)
168 ce_stage = 0;
169 else if (i + 1 < head_idx)
170 ce_stage = 1;
171 else if (i + 1 > head_idx)
172 ce_stage = 3;
173 else
174 ce_stage = 2;
175
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,
180 ce_stage);
181 memcpy(ce->name, base, baselen);
182 memcpy(ce->name + baselen, first, pathlen + 1);
183
184 any_files = 1;
185
186 memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
187 src[i + merge] = ce;
188 subposns[i] = &df_conflict_list;
189 posns[i] = posns[i]->next;
190 }
191 if (any_files) {
192 if (merge) {
193 int ret;
194
195#if DBRT_DEBUG > 1
196 printf("%s:\n", first);
197 for (i = 0; i < src_size; i++) {
198 printf(" %d ", i);
199 if (src[i])
200 printf("%s\n", sha1_to_hex(src[i]->sha1));
201 else
202 printf("\n");
203 }
204#endif
205 ret = fn(src);
206
207#if DBRT_DEBUG > 1
208 printf("Added %d entries\n", ret);
209#endif
210 *indpos += ret;
211 } else {
212 for (i = 0; i < src_size; i++) {
213 if (src[i]) {
214 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
215 }
216 }
217 }
218 }
219 if (any_dirs) {
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,
226 indpos))
227 return -1;
228 free(newbase);
229 }
230 free(subposns);
231 free(src);
232 } while (1);
233}
234
235static void reject_merge(struct cache_entry *ce)
236{
237 die("Entry '%s' would be overwritten by merge. Cannot merge.",
238 ce->name);
239}
240
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.
244 */
245static void unlink_entry(char *name)
246{
247 char *cp, *prev;
248
249 if (unlink(name))
250 return;
251 prev = NULL;
252 while (1) {
253 int status;
254 cp = strrchr(name, '/');
255 if (prev)
256 *prev = '/';
257 if (!cp)
258 break;
259
260 *cp = 0;
261 status = rmdir(name);
262 if (status) {
263 *cp = '/';
264 break;
265 }
266 prev = cp;
267 }
268}
269
270static void check_updates(struct cache_entry **src, int nr)
271{
272 static struct checkout state = {
273 .base_dir = "",
274 .force = 1,
275 .quiet = 1,
276 .refresh_cache = 1,
277 };
278 unsigned short mask = htons(CE_UPDATE);
279 while (nr--) {
280 struct cache_entry *ce = *src++;
281 if (!ce->ce_mode) {
282 if (update)
283 unlink_entry(ce->name);
284 continue;
285 }
286 if (ce->ce_flags & mask) {
287 ce->ce_flags &= ~mask;
288 if (update)
289 checkout_entry(ce, &state);
290 }
291 }
292}
293
294static int unpack_trees(merge_fn_t fn)
295{
296 int indpos = 0;
297 unsigned len = object_list_length(trees);
298 struct tree_entry_list **posns;
299 int i;
300 struct object_list *posn = trees;
301 merge_size = len;
302
303 if (len) {
304 posns = xmalloc(len * sizeof(struct tree_entry_list *));
305 for (i = 0; i < len; i++) {
306 posns[i] = ((struct tree *) posn->item)->entries;
307 posn = posn->next;
308 }
309 if (unpack_trees_rec(posns, len, "", fn, &indpos))
310 return -1;
311 }
312
313 if (trivial_merges_only && nontrivial_merge)
314 die("Merge requires file-level merging");
315
316 check_updates(active_cache, active_nr);
317 return 0;
318}
319
320static int list_tree(unsigned char *sha1)
321{
322 struct tree *tree = parse_tree_indirect(sha1);
323 if (!tree)
324 return -1;
325 object_list_append(&tree->object, &trees);
326 return 0;
327}
328
329static int same(struct cache_entry *a, struct cache_entry *b)
330{
331 if (!!a != !!b)
332 return 0;
333 if (!a && !b)
334 return 1;
335 return a->ce_mode == b->ce_mode &&
336 !memcmp(a->sha1, b->sha1, 20);
337}
338
339
340/*
341 * When a CE gets turned into an unmerged entry, we
342 * want it to be up-to-date
343 */
344static void verify_uptodate(struct cache_entry *ce)
345{
346 struct stat st;
347
348 if (index_only)
349 return;
350
351 if (!lstat(ce->name, &st)) {
352 unsigned changed = ce_match_stat(ce, &st);
353 if (!changed)
354 return;
355 errno = 0;
356 }
357 if (errno == ENOENT)
358 return;
359 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
360}
361
362static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
363{
364 merge->ce_flags |= htons(CE_UPDATE);
365 if (old) {
366 /*
367 * See if we can re-use the old CE directly?
368 * That way we get the uptodate stat info.
369 *
370 * This also removes the UPDATE flag on
371 * a match.
372 */
373 if (same(old, merge)) {
374 *merge = *old;
375 } else {
376 verify_uptodate(old);
377 }
378 }
379 merge->ce_flags &= ~htons(CE_STAGEMASK);
380 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
381 return 1;
382}
383
384static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
385{
386 if (old)
387 verify_uptodate(old);
388 ce->ce_mode = 0;
389 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
390 return 1;
391}
392
393static int keep_entry(struct cache_entry *ce)
394{
395 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
396 return 1;
397}
398
399#if DBRT_DEBUG
400static void show_stage_entry(FILE *o,
401 const char *label, const struct cache_entry *ce)
402{
403 if (!ce)
404 fprintf(o, "%s (missing)\n", label);
405 else
406 fprintf(o, "%s%06o %s %d\t%s\n",
407 label,
408 ntohl(ce->ce_mode),
409 sha1_to_hex(ce->sha1),
410 ce_stage(ce),
411 ce->name);
412}
413#endif
414
415static int threeway_merge(struct cache_entry **stages)
416{
417 struct cache_entry *index;
418 struct cache_entry *head;
419 struct cache_entry *remote = stages[head_idx + 1];
420 int count;
421 int head_match = 0;
422 int remote_match = 0;
423
424 int df_conflict_head = 0;
425 int df_conflict_remote = 0;
426
427 int any_anc_missing = 0;
428 int no_anc_exists = 1;
429 int i;
430
431 for (i = 1; i < head_idx; i++) {
432 if (!stages[i])
433 any_anc_missing = 1;
434 else
435 no_anc_exists = 0;
436 }
437
438 index = stages[0];
439 head = stages[head_idx];
440
441 if (head == &df_conflict_entry) {
442 df_conflict_head = 1;
443 head = NULL;
444 }
445
446 if (remote == &df_conflict_entry) {
447 df_conflict_remote = 1;
448 remote = NULL;
449 }
450
451 /* First, if there's a #16 situation, note that to prevent #13
452 * and #14.
453 */
454 if (!same(remote, head)) {
455 for (i = 1; i < head_idx; i++) {
456 if (same(stages[i], head)) {
457 head_match = i;
458 }
459 if (same(stages[i], remote)) {
460 remote_match = i;
461 }
462 }
463 }
464
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.
468 */
469 /* #14, #14ALT, #2ALT */
470 if (remote && !df_conflict_head && head_match && !remote_match) {
471 if (index && !same(index, remote) && !same(index, head))
472 reject_merge(index);
473 return merged_entry(remote, index);
474 }
475 /*
476 * If we have an entry in the index cache, then we want to
477 * make sure that it matches head.
478 */
479 if (index && !same(index, head)) {
480 reject_merge(index);
481 }
482
483 if (head) {
484 /* #5ALT, #15 */
485 if (same(head, remote))
486 return merged_entry(head, index);
487 /* #13, #3ALT */
488 if (!df_conflict_remote && remote_match && !head_match)
489 return merged_entry(head, index);
490 }
491
492 /* #1 */
493 if (!head && !remote && any_anc_missing)
494 return 0;
495
496 /* Under the new "aggressive" rule, we resolve mostly trivial
497 * cases that we historically had git-merge-one-file resolve.
498 */
499 if (aggressive) {
500 int head_deleted = !head && !df_conflict_head;
501 int remote_deleted = !remote && !df_conflict_remote;
502 /*
503 * Deleted in both.
504 * Deleted in one and unchanged in the other.
505 */
506 if ((head_deleted && remote_deleted) ||
507 (head_deleted && remote && remote_match) ||
508 (remote_deleted && head && head_match))
509 return 0;
510
511 /*
512 * Added in both, identically.
513 */
514 if (no_anc_exists && head && remote && same(head, remote))
515 return merged_entry(head, index);
516
517 }
518
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.
522 */
523 if (index) {
524 verify_uptodate(index);
525 }
526
527 nontrivial_merge = 1;
528
529 /* #2, #3, #4, #6, #7, #9, #11. */
530 count = 0;
531 if (!head_match || !remote_match) {
532 for (i = 1; i < head_idx; i++) {
533 if (stages[i]) {
534 keep_entry(stages[i]);
535 count++;
536 break;
537 }
538 }
539 }
540#if DBRT_DEBUG
541 else {
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]);
545 }
546#endif
547 if (head) { count += keep_entry(head); }
548 if (remote) { count += keep_entry(remote); }
549 return count;
550}
551
552/*
553 * Two-way merge.
554 *
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>.
559 *
560 */
561static int twoway_merge(struct cache_entry **src)
562{
563 struct cache_entry *current = src[0];
564 struct cache_entry *oldtree = src[1], *newtree = src[2];
565
566 if (merge_size != 2)
567 return error("Cannot do a twoway merge of %d trees\n",
568 merge_size);
569
570 if (current) {
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);
580 }
581 else if (oldtree && !newtree && same(current, oldtree)) {
582 /* 10 or 11 */
583 return deleted_entry(oldtree, current);
584 }
585 else if (oldtree && newtree &&
586 same(current, oldtree) && !same(current, newtree)) {
587 /* 20 or 21 */
588 return merged_entry(newtree, current);
589 }
590 else {
591 /* all other failures */
592 if (oldtree)
593 reject_merge(oldtree);
594 if (current)
595 reject_merge(current);
596 if (newtree)
597 reject_merge(newtree);
598 return -1;
599 }
600 }
601 else if (newtree)
602 return merged_entry(newtree, current);
603 else
604 return deleted_entry(oldtree, current);
605}
606
607/*
608 * One-way merge.
609 *
610 * The rule is:
611 * - take the stat information from stage0, take the data from stage1
612 */
613static int oneway_merge(struct cache_entry **src)
614{
615 struct cache_entry *old = src[0];
616 struct cache_entry *a = src[1];
617
618 if (merge_size != 1)
619 return error("Cannot do a oneway merge of %d trees\n",
620 merge_size);
621
622 if (!a)
623 return 0;
624 if (old && same(old, a)) {
625 return keep_entry(old);
626 }
627 return merged_entry(a, NULL);
628}
629
630static int read_cache_unmerged(void)
631{
632 int i, deleted;
633 struct cache_entry **dst;
634
635 read_cache();
636 dst = active_cache;
637 deleted = 0;
638 for (i = 0; i < active_nr; i++) {
639 struct cache_entry *ce = active_cache[i];
640 if (ce_stage(ce)) {
641 deleted++;
642 continue;
643 }
644 if (deleted)
645 *dst = ce;
646 dst++;
647 }
648 active_nr -= deleted;
649 return deleted;
650}
651
652static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
653
654static struct cache_file cache_file;
655
656int main(int argc, char **argv)
657{
658 int i, newfd, reset, stage = 0;
659 unsigned char sha1[20];
660 merge_fn_t fn = NULL;
661
662 setup_git_directory();
663
664 newfd = hold_index_file_for_update(&cache_file, get_index_file());
665 if (newfd < 0)
666 die("unable to create new cachefile");
667
668 git_config(git_default_config);
669
670 merge = 0;
671 reset = 0;
672 for (i = 1; i < argc; i++) {
673 const char *arg = argv[i];
674
675 /* "-u" means "update", meaning that a merge will update
676 * the working tree.
677 */
678 if (!strcmp(arg, "-u")) {
679 update = 1;
680 continue;
681 }
682
683 /* "-i" means "index only", meaning that a merge will
684 * not even look at the working tree.
685 */
686 if (!strcmp(arg, "-i")) {
687 index_only = 1;
688 continue;
689 }
690
691 /* This differs from "-m" in that we'll silently ignore unmerged entries */
692 if (!strcmp(arg, "--reset")) {
693 if (stage || merge)
694 usage(read_tree_usage);
695 reset = 1;
696 merge = 1;
697 stage = 1;
698 read_cache_unmerged();
699 continue;
700 }
701
702 if (!strcmp(arg, "--trivial")) {
703 trivial_merges_only = 1;
704 continue;
705 }
706
707 if (!strcmp(arg, "--aggressive")) {
708 aggressive = 1;
709 continue;
710 }
711
712 /* "-m" stands for "merge", meaning we start in stage 1 */
713 if (!strcmp(arg, "-m")) {
714 if (stage || merge)
715 usage(read_tree_usage);
716 if (read_cache_unmerged())
717 die("you need to resolve your current index first");
718 stage = 1;
719 merge = 1;
720 continue;
721 }
722
723 /* using -u and -i at the same time makes no sense */
724 if (1 < index_only + update)
725 usage(read_tree_usage);
726
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);
731 stage++;
732 }
733 if ((update||index_only) && !merge)
734 usage(read_tree_usage);
735
736 if (merge) {
737 if (stage < 2)
738 die("just how do you expect me to merge %d trees?", stage-1);
739 switch (stage - 1) {
740 case 1:
741 fn = oneway_merge;
742 break;
743 case 2:
744 fn = twoway_merge;
745 break;
746 case 3:
747 fn = threeway_merge;
748 break;
749 default:
750 fn = threeway_merge;
751 break;
752 }
753
754 if (stage - 1 >= 3)
755 head_idx = stage - 2;
756 else
757 head_idx = 1;
758 }
759
760 unpack_trees(fn);
761 if (write_cache(newfd, active_cache, active_nr) ||
762 commit_index_file(&cache_file))
763 die("unable to write new index file");
764 return 0;
765}