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 "tree-walk.h"
13#include "cache-tree.h"
14#include <sys/time.h>
15#include <signal.h>
16#include "builtin.h"
17
18static int reset = 0;
19static int merge = 0;
20static int update = 0;
21static int index_only = 0;
22static int nontrivial_merge = 0;
23static int trivial_merges_only = 0;
24static int aggressive = 0;
25static int verbose_update = 0;
26static volatile int progress_update = 0;
27static const char *prefix = NULL;
28
29static int head_idx = -1;
30static int merge_size = 0;
31
32static struct object_list *trees = NULL;
33
34static struct cache_entry df_conflict_entry = {
35};
36
37struct tree_entry_list {
38 struct tree_entry_list *next;
39 unsigned directory : 1;
40 unsigned executable : 1;
41 unsigned symlink : 1;
42 unsigned int mode;
43 const char *name;
44 const unsigned char *sha1;
45};
46
47static struct tree_entry_list df_conflict_list = {
48 .name = NULL,
49 .next = &df_conflict_list
50};
51
52typedef int (*merge_fn_t)(struct cache_entry **src);
53
54static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
55{
56 struct tree_desc desc;
57 struct tree_entry_list *ret = NULL;
58 struct tree_entry_list **list_p = &ret;
59
60 desc.buf = tree->buffer;
61 desc.size = tree->size;
62
63 while (desc.size) {
64 unsigned mode;
65 const char *path;
66 const unsigned char *sha1;
67 struct tree_entry_list *entry;
68
69 sha1 = tree_entry_extract(&desc, &path, &mode);
70 update_tree_entry(&desc);
71
72 entry = xmalloc(sizeof(struct tree_entry_list));
73 entry->name = path;
74 entry->sha1 = sha1;
75 entry->mode = mode;
76 entry->directory = S_ISDIR(mode) != 0;
77 entry->executable = (mode & S_IXUSR) != 0;
78 entry->symlink = S_ISLNK(mode) != 0;
79 entry->next = NULL;
80
81 *list_p = entry;
82 list_p = &entry->next;
83 }
84 return ret;
85}
86
87static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
88{
89 int len1 = strlen(name1);
90 int len2 = strlen(name2);
91 int len = len1 < len2 ? len1 : len2;
92 int ret = memcmp(name1, name2, len);
93 unsigned char c1, c2;
94 if (ret)
95 return ret;
96 c1 = name1[len];
97 c2 = name2[len];
98 if (!c1 && dir1)
99 c1 = '/';
100 if (!c2 && dir2)
101 c2 = '/';
102 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
103 if (c1 && c2 && !ret)
104 ret = len1 - len2;
105 return ret;
106}
107
108static int unpack_trees_rec(struct tree_entry_list **posns, int len,
109 const char *base, merge_fn_t fn, int *indpos)
110{
111 int baselen = strlen(base);
112 int src_size = len + 1;
113 do {
114 int i;
115 const char *first;
116 int firstdir = 0;
117 int pathlen;
118 unsigned ce_size;
119 struct tree_entry_list **subposns;
120 struct cache_entry **src;
121 int any_files = 0;
122 int any_dirs = 0;
123 char *cache_name;
124 int ce_stage;
125
126 /* Find the first name in the input. */
127
128 first = NULL;
129 cache_name = NULL;
130
131 /* Check the cache */
132 if (merge && *indpos < active_nr) {
133 /* This is a bit tricky: */
134 /* If the index has a subdirectory (with
135 * contents) as the first name, it'll get a
136 * filename like "foo/bar". But that's after
137 * "foo", so the entry in trees will get
138 * handled first, at which point we'll go into
139 * "foo", and deal with "bar" from the index,
140 * because the base will be "foo/". The only
141 * way we can actually have "foo/bar" first of
142 * all the things is if the trees don't
143 * contain "foo" at all, in which case we'll
144 * handle "foo/bar" without going into the
145 * directory, but that's fine (and will return
146 * an error anyway, with the added unknown
147 * file case.
148 */
149
150 cache_name = active_cache[*indpos]->name;
151 if (strlen(cache_name) > baselen &&
152 !memcmp(cache_name, base, baselen)) {
153 cache_name += baselen;
154 first = cache_name;
155 } else {
156 cache_name = NULL;
157 }
158 }
159
160#if DBRT_DEBUG > 1
161 if (first)
162 printf("index %s\n", first);
163#endif
164 for (i = 0; i < len; i++) {
165 if (!posns[i] || posns[i] == &df_conflict_list)
166 continue;
167#if DBRT_DEBUG > 1
168 printf("%d %s\n", i + 1, posns[i]->name);
169#endif
170 if (!first || entcmp(first, firstdir,
171 posns[i]->name,
172 posns[i]->directory) > 0) {
173 first = posns[i]->name;
174 firstdir = posns[i]->directory;
175 }
176 }
177 /* No name means we're done */
178 if (!first)
179 return 0;
180
181 pathlen = strlen(first);
182 ce_size = cache_entry_size(baselen + pathlen);
183
184 src = xcalloc(src_size, sizeof(struct cache_entry *));
185
186 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
187
188 if (cache_name && !strcmp(cache_name, first)) {
189 any_files = 1;
190 src[0] = active_cache[*indpos];
191 remove_cache_entry_at(*indpos);
192 }
193
194 for (i = 0; i < len; i++) {
195 struct cache_entry *ce;
196
197 if (!posns[i] ||
198 (posns[i] != &df_conflict_list &&
199 strcmp(first, posns[i]->name))) {
200 continue;
201 }
202
203 if (posns[i] == &df_conflict_list) {
204 src[i + merge] = &df_conflict_entry;
205 continue;
206 }
207
208 if (posns[i]->directory) {
209 struct tree *tree = lookup_tree(posns[i]->sha1);
210 any_dirs = 1;
211 parse_tree(tree);
212 subposns[i] = create_tree_entry_list(tree);
213 posns[i] = posns[i]->next;
214 src[i + merge] = &df_conflict_entry;
215 continue;
216 }
217
218 if (!merge)
219 ce_stage = 0;
220 else if (i + 1 < head_idx)
221 ce_stage = 1;
222 else if (i + 1 > head_idx)
223 ce_stage = 3;
224 else
225 ce_stage = 2;
226
227 ce = xcalloc(1, ce_size);
228 ce->ce_mode = create_ce_mode(posns[i]->mode);
229 ce->ce_flags = create_ce_flags(baselen + pathlen,
230 ce_stage);
231 memcpy(ce->name, base, baselen);
232 memcpy(ce->name + baselen, first, pathlen + 1);
233
234 any_files = 1;
235
236 memcpy(ce->sha1, posns[i]->sha1, 20);
237 src[i + merge] = ce;
238 subposns[i] = &df_conflict_list;
239 posns[i] = posns[i]->next;
240 }
241 if (any_files) {
242 if (merge) {
243 int ret;
244
245#if DBRT_DEBUG > 1
246 printf("%s:\n", first);
247 for (i = 0; i < src_size; i++) {
248 printf(" %d ", i);
249 if (src[i])
250 printf("%s\n", sha1_to_hex(src[i]->sha1));
251 else
252 printf("\n");
253 }
254#endif
255 ret = fn(src);
256
257#if DBRT_DEBUG > 1
258 printf("Added %d entries\n", ret);
259#endif
260 *indpos += ret;
261 } else {
262 for (i = 0; i < src_size; i++) {
263 if (src[i]) {
264 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
265 }
266 }
267 }
268 }
269 if (any_dirs) {
270 char *newbase = xmalloc(baselen + 2 + pathlen);
271 memcpy(newbase, base, baselen);
272 memcpy(newbase + baselen, first, pathlen);
273 newbase[baselen + pathlen] = '/';
274 newbase[baselen + pathlen + 1] = '\0';
275 if (unpack_trees_rec(subposns, len, newbase, fn,
276 indpos))
277 return -1;
278 free(newbase);
279 }
280 free(subposns);
281 free(src);
282 } while (1);
283}
284
285static void reject_merge(struct cache_entry *ce)
286{
287 die("Entry '%s' would be overwritten by merge. Cannot merge.",
288 ce->name);
289}
290
291/* Unlink the last component and attempt to remove leading
292 * directories, in case this unlink is the removal of the
293 * last entry in the directory -- empty directories are removed.
294 */
295static void unlink_entry(char *name)
296{
297 char *cp, *prev;
298
299 if (unlink(name))
300 return;
301 prev = NULL;
302 while (1) {
303 int status;
304 cp = strrchr(name, '/');
305 if (prev)
306 *prev = '/';
307 if (!cp)
308 break;
309
310 *cp = 0;
311 status = rmdir(name);
312 if (status) {
313 *cp = '/';
314 break;
315 }
316 prev = cp;
317 }
318}
319
320static void progress_interval(int signum)
321{
322 progress_update = 1;
323}
324
325static void setup_progress_signal(void)
326{
327 struct sigaction sa;
328 struct itimerval v;
329
330 memset(&sa, 0, sizeof(sa));
331 sa.sa_handler = progress_interval;
332 sigemptyset(&sa.sa_mask);
333 sa.sa_flags = SA_RESTART;
334 sigaction(SIGALRM, &sa, NULL);
335
336 v.it_interval.tv_sec = 1;
337 v.it_interval.tv_usec = 0;
338 v.it_value = v.it_interval;
339 setitimer(ITIMER_REAL, &v, NULL);
340}
341
342static void check_updates(struct cache_entry **src, int nr)
343{
344 static struct checkout state = {
345 .base_dir = "",
346 .force = 1,
347 .quiet = 1,
348 .refresh_cache = 1,
349 };
350 unsigned short mask = htons(CE_UPDATE);
351 unsigned last_percent = 200, cnt = 0, total = 0;
352
353 if (update && verbose_update) {
354 for (total = cnt = 0; cnt < nr; cnt++) {
355 struct cache_entry *ce = src[cnt];
356 if (!ce->ce_mode || ce->ce_flags & mask)
357 total++;
358 }
359
360 /* Don't bother doing this for very small updates */
361 if (total < 250)
362 total = 0;
363
364 if (total) {
365 fprintf(stderr, "Checking files out...\n");
366 setup_progress_signal();
367 progress_update = 1;
368 }
369 cnt = 0;
370 }
371
372 while (nr--) {
373 struct cache_entry *ce = *src++;
374
375 if (total) {
376 if (!ce->ce_mode || ce->ce_flags & mask) {
377 unsigned percent;
378 cnt++;
379 percent = (cnt * 100) / total;
380 if (percent != last_percent ||
381 progress_update) {
382 fprintf(stderr, "%4u%% (%u/%u) done\r",
383 percent, cnt, total);
384 last_percent = percent;
385 }
386 }
387 }
388 if (!ce->ce_mode) {
389 if (update)
390 unlink_entry(ce->name);
391 continue;
392 }
393 if (ce->ce_flags & mask) {
394 ce->ce_flags &= ~mask;
395 if (update)
396 checkout_entry(ce, &state, NULL);
397 }
398 }
399 if (total) {
400 signal(SIGALRM, SIG_IGN);
401 fputc('\n', stderr);
402 }
403}
404
405static int unpack_trees(merge_fn_t fn)
406{
407 int indpos = 0;
408 unsigned len = object_list_length(trees);
409 struct tree_entry_list **posns;
410 int i;
411 struct object_list *posn = trees;
412 merge_size = len;
413
414 if (len) {
415 posns = xmalloc(len * sizeof(struct tree_entry_list *));
416 for (i = 0; i < len; i++) {
417 posns[i] = create_tree_entry_list((struct tree *) posn->item);
418 posn = posn->next;
419 }
420 if (unpack_trees_rec(posns, len, prefix ? prefix : "",
421 fn, &indpos))
422 return -1;
423 }
424
425 if (trivial_merges_only && nontrivial_merge)
426 die("Merge requires file-level merging");
427
428 check_updates(active_cache, active_nr);
429 return 0;
430}
431
432static int list_tree(unsigned char *sha1)
433{
434 struct tree *tree = parse_tree_indirect(sha1);
435 if (!tree)
436 return -1;
437 object_list_append(&tree->object, &trees);
438 return 0;
439}
440
441static int same(struct cache_entry *a, struct cache_entry *b)
442{
443 if (!!a != !!b)
444 return 0;
445 if (!a && !b)
446 return 1;
447 return a->ce_mode == b->ce_mode &&
448 !memcmp(a->sha1, b->sha1, 20);
449}
450
451
452/*
453 * When a CE gets turned into an unmerged entry, we
454 * want it to be up-to-date
455 */
456static void verify_uptodate(struct cache_entry *ce)
457{
458 struct stat st;
459
460 if (index_only || reset)
461 return;
462
463 if (!lstat(ce->name, &st)) {
464 unsigned changed = ce_match_stat(ce, &st, 1);
465 if (!changed)
466 return;
467 errno = 0;
468 }
469 if (reset) {
470 ce->ce_flags |= htons(CE_UPDATE);
471 return;
472 }
473 if (errno == ENOENT)
474 return;
475 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
476}
477
478static void invalidate_ce_path(struct cache_entry *ce)
479{
480 if (ce)
481 cache_tree_invalidate_path(active_cache_tree, ce->name);
482}
483
484/*
485 * We do not want to remove or overwrite a working tree file that
486 * is not tracked.
487 */
488static void verify_absent(const char *path, const char *action)
489{
490 struct stat st;
491
492 if (index_only || reset || !update)
493 return;
494 if (!lstat(path, &st))
495 die("Untracked working tree file '%s' "
496 "would be %s by merge.", path, action);
497}
498
499static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
500{
501 merge->ce_flags |= htons(CE_UPDATE);
502 if (old) {
503 /*
504 * See if we can re-use the old CE directly?
505 * That way we get the uptodate stat info.
506 *
507 * This also removes the UPDATE flag on
508 * a match.
509 */
510 if (same(old, merge)) {
511 *merge = *old;
512 } else {
513 verify_uptodate(old);
514 invalidate_ce_path(old);
515 }
516 }
517 else {
518 verify_absent(merge->name, "overwritten");
519 invalidate_ce_path(merge);
520 }
521
522 merge->ce_flags &= ~htons(CE_STAGEMASK);
523 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
524 return 1;
525}
526
527static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
528{
529 if (old)
530 verify_uptodate(old);
531 else
532 verify_absent(ce->name, "removed");
533 ce->ce_mode = 0;
534 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
535 invalidate_ce_path(ce);
536 return 1;
537}
538
539static int keep_entry(struct cache_entry *ce)
540{
541 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
542 return 1;
543}
544
545#if DBRT_DEBUG
546static void show_stage_entry(FILE *o,
547 const char *label, const struct cache_entry *ce)
548{
549 if (!ce)
550 fprintf(o, "%s (missing)\n", label);
551 else
552 fprintf(o, "%s%06o %s %d\t%s\n",
553 label,
554 ntohl(ce->ce_mode),
555 sha1_to_hex(ce->sha1),
556 ce_stage(ce),
557 ce->name);
558}
559#endif
560
561static int threeway_merge(struct cache_entry **stages)
562{
563 struct cache_entry *index;
564 struct cache_entry *head;
565 struct cache_entry *remote = stages[head_idx + 1];
566 int count;
567 int head_match = 0;
568 int remote_match = 0;
569 const char *path = NULL;
570
571 int df_conflict_head = 0;
572 int df_conflict_remote = 0;
573
574 int any_anc_missing = 0;
575 int no_anc_exists = 1;
576 int i;
577
578 for (i = 1; i < head_idx; i++) {
579 if (!stages[i])
580 any_anc_missing = 1;
581 else {
582 if (!path)
583 path = stages[i]->name;
584 no_anc_exists = 0;
585 }
586 }
587
588 index = stages[0];
589 head = stages[head_idx];
590
591 if (head == &df_conflict_entry) {
592 df_conflict_head = 1;
593 head = NULL;
594 }
595
596 if (remote == &df_conflict_entry) {
597 df_conflict_remote = 1;
598 remote = NULL;
599 }
600
601 if (!path && index)
602 path = index->name;
603 if (!path && head)
604 path = head->name;
605 if (!path && remote)
606 path = remote->name;
607
608 /* First, if there's a #16 situation, note that to prevent #13
609 * and #14.
610 */
611 if (!same(remote, head)) {
612 for (i = 1; i < head_idx; i++) {
613 if (same(stages[i], head)) {
614 head_match = i;
615 }
616 if (same(stages[i], remote)) {
617 remote_match = i;
618 }
619 }
620 }
621
622 /* We start with cases where the index is allowed to match
623 * something other than the head: #14(ALT) and #2ALT, where it
624 * is permitted to match the result instead.
625 */
626 /* #14, #14ALT, #2ALT */
627 if (remote && !df_conflict_head && head_match && !remote_match) {
628 if (index && !same(index, remote) && !same(index, head))
629 reject_merge(index);
630 return merged_entry(remote, index);
631 }
632 /*
633 * If we have an entry in the index cache, then we want to
634 * make sure that it matches head.
635 */
636 if (index && !same(index, head)) {
637 reject_merge(index);
638 }
639
640 if (head) {
641 /* #5ALT, #15 */
642 if (same(head, remote))
643 return merged_entry(head, index);
644 /* #13, #3ALT */
645 if (!df_conflict_remote && remote_match && !head_match)
646 return merged_entry(head, index);
647 }
648
649 /* #1 */
650 if (!head && !remote && any_anc_missing)
651 return 0;
652
653 /* Under the new "aggressive" rule, we resolve mostly trivial
654 * cases that we historically had git-merge-one-file resolve.
655 */
656 if (aggressive) {
657 int head_deleted = !head && !df_conflict_head;
658 int remote_deleted = !remote && !df_conflict_remote;
659 /*
660 * Deleted in both.
661 * Deleted in one and unchanged in the other.
662 */
663 if ((head_deleted && remote_deleted) ||
664 (head_deleted && remote && remote_match) ||
665 (remote_deleted && head && head_match)) {
666 if (index)
667 return deleted_entry(index, index);
668 else if (path)
669 verify_absent(path, "removed");
670 return 0;
671 }
672 /*
673 * Added in both, identically.
674 */
675 if (no_anc_exists && head && remote && same(head, remote))
676 return merged_entry(head, index);
677
678 }
679
680 /* Below are "no merge" cases, which require that the index be
681 * up-to-date to avoid the files getting overwritten with
682 * conflict resolution files.
683 */
684 if (index) {
685 verify_uptodate(index);
686 }
687 else if (path)
688 verify_absent(path, "overwritten");
689
690 nontrivial_merge = 1;
691
692 /* #2, #3, #4, #6, #7, #9, #11. */
693 count = 0;
694 if (!head_match || !remote_match) {
695 for (i = 1; i < head_idx; i++) {
696 if (stages[i]) {
697 keep_entry(stages[i]);
698 count++;
699 break;
700 }
701 }
702 }
703#if DBRT_DEBUG
704 else {
705 fprintf(stderr, "read-tree: warning #16 detected\n");
706 show_stage_entry(stderr, "head ", stages[head_match]);
707 show_stage_entry(stderr, "remote ", stages[remote_match]);
708 }
709#endif
710 if (head) { count += keep_entry(head); }
711 if (remote) { count += keep_entry(remote); }
712 return count;
713}
714
715/*
716 * Two-way merge.
717 *
718 * The rule is to "carry forward" what is in the index without losing
719 * information across a "fast forward", favoring a successful merge
720 * over a merge failure when it makes sense. For details of the
721 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
722 *
723 */
724static int twoway_merge(struct cache_entry **src)
725{
726 struct cache_entry *current = src[0];
727 struct cache_entry *oldtree = src[1], *newtree = src[2];
728
729 if (merge_size != 2)
730 return error("Cannot do a twoway merge of %d trees",
731 merge_size);
732
733 if (current) {
734 if ((!oldtree && !newtree) || /* 4 and 5 */
735 (!oldtree && newtree &&
736 same(current, newtree)) || /* 6 and 7 */
737 (oldtree && newtree &&
738 same(oldtree, newtree)) || /* 14 and 15 */
739 (oldtree && newtree &&
740 !same(oldtree, newtree) && /* 18 and 19*/
741 same(current, newtree))) {
742 return keep_entry(current);
743 }
744 else if (oldtree && !newtree && same(current, oldtree)) {
745 /* 10 or 11 */
746 return deleted_entry(oldtree, current);
747 }
748 else if (oldtree && newtree &&
749 same(current, oldtree) && !same(current, newtree)) {
750 /* 20 or 21 */
751 return merged_entry(newtree, current);
752 }
753 else {
754 /* all other failures */
755 if (oldtree)
756 reject_merge(oldtree);
757 if (current)
758 reject_merge(current);
759 if (newtree)
760 reject_merge(newtree);
761 return -1;
762 }
763 }
764 else if (newtree)
765 return merged_entry(newtree, current);
766 else
767 return deleted_entry(oldtree, current);
768}
769
770/*
771 * Bind merge.
772 *
773 * Keep the index entries at stage0, collapse stage1 but make sure
774 * stage0 does not have anything there.
775 */
776static int bind_merge(struct cache_entry **src)
777{
778 struct cache_entry *old = src[0];
779 struct cache_entry *a = src[1];
780
781 if (merge_size != 1)
782 return error("Cannot do a bind merge of %d trees\n",
783 merge_size);
784 if (a && old)
785 die("Entry '%s' overlaps. Cannot bind.", a->name);
786 if (!a)
787 return keep_entry(old);
788 else
789 return merged_entry(a, NULL);
790}
791
792/*
793 * One-way merge.
794 *
795 * The rule is:
796 * - take the stat information from stage0, take the data from stage1
797 */
798static int oneway_merge(struct cache_entry **src)
799{
800 struct cache_entry *old = src[0];
801 struct cache_entry *a = src[1];
802
803 if (merge_size != 1)
804 return error("Cannot do a oneway merge of %d trees",
805 merge_size);
806
807 if (!a) {
808 invalidate_ce_path(old);
809 return deleted_entry(old, old);
810 }
811 if (old && same(old, a)) {
812 if (reset) {
813 struct stat st;
814 if (lstat(old->name, &st) ||
815 ce_match_stat(old, &st, 1))
816 old->ce_flags |= htons(CE_UPDATE);
817 }
818 return keep_entry(old);
819 }
820 return merged_entry(a, old);
821}
822
823static int read_cache_unmerged(void)
824{
825 int i, deleted;
826 struct cache_entry **dst;
827
828 read_cache();
829 dst = active_cache;
830 deleted = 0;
831 for (i = 0; i < active_nr; i++) {
832 struct cache_entry *ce = active_cache[i];
833 if (ce_stage(ce)) {
834 deleted++;
835 invalidate_ce_path(ce);
836 continue;
837 }
838 if (deleted)
839 *dst = ce;
840 dst++;
841 }
842 active_nr -= deleted;
843 return deleted;
844}
845
846static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
847{
848 struct tree_desc desc;
849 int cnt;
850
851 memcpy(it->sha1, tree->object.sha1, 20);
852 desc.buf = tree->buffer;
853 desc.size = tree->size;
854 cnt = 0;
855 while (desc.size) {
856 unsigned mode;
857 const char *name;
858 const unsigned char *sha1;
859
860 sha1 = tree_entry_extract(&desc, &name, &mode);
861 update_tree_entry(&desc);
862 if (!S_ISDIR(mode))
863 cnt++;
864 else {
865 struct cache_tree_sub *sub;
866 struct tree *subtree = lookup_tree(sha1);
867 if (!subtree->object.parsed)
868 parse_tree(subtree);
869 sub = cache_tree_sub(it, name);
870 sub->cache_tree = cache_tree();
871 prime_cache_tree_rec(sub->cache_tree, subtree);
872 cnt += sub->cache_tree->entry_count;
873 }
874 }
875 it->entry_count = cnt;
876}
877
878static void prime_cache_tree(void)
879{
880 struct tree *tree = (struct tree *)trees->item;
881 if (!tree)
882 return;
883 active_cache_tree = cache_tree();
884 prime_cache_tree_rec(active_cache_tree, tree);
885
886}
887
888static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
889
890static struct cache_file cache_file;
891
892int cmd_read_tree(int argc, const char **argv, char **envp)
893{
894 int i, newfd, stage = 0;
895 unsigned char sha1[20];
896 merge_fn_t fn = NULL;
897
898 setup_git_directory();
899 git_config(git_default_config);
900
901 newfd = hold_index_file_for_update(&cache_file, get_index_file());
902 if (newfd < 0)
903 die("unable to create new cachefile");
904
905 git_config(git_default_config);
906
907 merge = 0;
908 reset = 0;
909 for (i = 1; i < argc; i++) {
910 const char *arg = argv[i];
911
912 /* "-u" means "update", meaning that a merge will update
913 * the working tree.
914 */
915 if (!strcmp(arg, "-u")) {
916 update = 1;
917 continue;
918 }
919
920 if (!strcmp(arg, "-v")) {
921 verbose_update = 1;
922 continue;
923 }
924
925 /* "-i" means "index only", meaning that a merge will
926 * not even look at the working tree.
927 */
928 if (!strcmp(arg, "-i")) {
929 index_only = 1;
930 continue;
931 }
932
933 /* "--prefix=<subdirectory>/" means keep the current index
934 * entries and put the entries from the tree under the
935 * given subdirectory.
936 */
937 if (!strncmp(arg, "--prefix=", 9)) {
938 if (stage || merge || prefix)
939 usage(read_tree_usage);
940 prefix = arg + 9;
941 merge = 1;
942 stage = 1;
943 if (read_cache_unmerged())
944 die("you need to resolve your current index first");
945 continue;
946 }
947
948 /* This differs from "-m" in that we'll silently ignore unmerged entries */
949 if (!strcmp(arg, "--reset")) {
950 if (stage || merge || prefix)
951 usage(read_tree_usage);
952 reset = 1;
953 merge = 1;
954 stage = 1;
955 read_cache_unmerged();
956 continue;
957 }
958
959 if (!strcmp(arg, "--trivial")) {
960 trivial_merges_only = 1;
961 continue;
962 }
963
964 if (!strcmp(arg, "--aggressive")) {
965 aggressive = 1;
966 continue;
967 }
968
969 /* "-m" stands for "merge", meaning we start in stage 1 */
970 if (!strcmp(arg, "-m")) {
971 if (stage || merge || prefix)
972 usage(read_tree_usage);
973 if (read_cache_unmerged())
974 die("you need to resolve your current index first");
975 stage = 1;
976 merge = 1;
977 continue;
978 }
979
980 /* using -u and -i at the same time makes no sense */
981 if (1 < index_only + update)
982 usage(read_tree_usage);
983
984 if (get_sha1(arg, sha1))
985 die("Not a valid object name %s", arg);
986 if (list_tree(sha1) < 0)
987 die("failed to unpack tree object %s", arg);
988 stage++;
989 }
990 if ((update||index_only) && !merge)
991 usage(read_tree_usage);
992
993 if (prefix) {
994 int pfxlen = strlen(prefix);
995 int pos;
996 if (prefix[pfxlen-1] != '/')
997 die("prefix must end with /");
998 if (stage != 2)
999 die("binding merge takes only one tree");
1000 pos = cache_name_pos(prefix, pfxlen);
1001 if (0 <= pos)
1002 die("corrupt index file");
1003 pos = -pos-1;
1004 if (pos < active_nr &&
1005 !strncmp(active_cache[pos]->name, prefix, pfxlen))
1006 die("subdirectory '%s' already exists.", prefix);
1007 pos = cache_name_pos(prefix, pfxlen-1);
1008 if (0 <= pos)
1009 die("file '%.*s' already exists.", pfxlen-1, prefix);
1010 }
1011
1012 if (merge) {
1013 if (stage < 2)
1014 die("just how do you expect me to merge %d trees?", stage-1);
1015 switch (stage - 1) {
1016 case 1:
1017 fn = prefix ? bind_merge : oneway_merge;
1018 break;
1019 case 2:
1020 fn = twoway_merge;
1021 break;
1022 case 3:
1023 default:
1024 fn = threeway_merge;
1025 cache_tree_free(&active_cache_tree);
1026 break;
1027 }
1028
1029 if (stage - 1 >= 3)
1030 head_idx = stage - 2;
1031 else
1032 head_idx = 1;
1033 }
1034
1035 unpack_trees(fn);
1036
1037 /*
1038 * When reading only one tree (either the most basic form,
1039 * "-m ent" or "--reset ent" form), we can obtain a fully
1040 * valid cache-tree because the index must match exactly
1041 * what came from the tree.
1042 */
1043 if (trees && trees->item && (!merge || (stage == 2))) {
1044 cache_tree_free(&active_cache_tree);
1045 prime_cache_tree();
1046 }
1047
1048 if (write_cache(newfd, active_cache, active_nr) ||
1049 commit_index_file(&cache_file))
1050 die("unable to write new index file");
1051 return 0;
1052}