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