Remove last vestiges of generic tree_entry_list
[git/gitweb.git] / builtin-read-tree.c
blob00cdb5a6d9388c3a20e106c2047e7f6580b05ec6
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define DBRT_DEBUG 1
8 #include "cache.h"
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"
18 static int reset = 0;
19 static int merge = 0;
20 static int update = 0;
21 static int index_only = 0;
22 static int nontrivial_merge = 0;
23 static int trivial_merges_only = 0;
24 static int aggressive = 0;
25 static int verbose_update = 0;
26 static volatile int progress_update = 0;
28 static int head_idx = -1;
29 static int merge_size = 0;
31 static struct object_list *trees = NULL;
33 static struct cache_entry df_conflict_entry = {
36 struct tree_entry_list {
37 struct tree_entry_list *next;
38 unsigned directory : 1;
39 unsigned executable : 1;
40 unsigned symlink : 1;
41 unsigned int mode;
42 const char *name;
43 const unsigned char *sha1;
46 static struct tree_entry_list df_conflict_list = {
47 .name = NULL,
48 .next = &df_conflict_list
51 typedef int (*merge_fn_t)(struct cache_entry **src);
53 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
55 struct tree_desc desc;
56 struct tree_entry_list *ret = NULL;
57 struct tree_entry_list **list_p = &ret;
59 desc.buf = tree->buffer;
60 desc.size = tree->size;
62 while (desc.size) {
63 unsigned mode;
64 const char *path;
65 const unsigned char *sha1;
66 struct tree_entry_list *entry;
68 sha1 = tree_entry_extract(&desc, &path, &mode);
69 update_tree_entry(&desc);
71 entry = xmalloc(sizeof(struct tree_entry_list));
72 entry->name = path;
73 entry->sha1 = sha1;
74 entry->mode = mode;
75 entry->directory = S_ISDIR(mode) != 0;
76 entry->executable = (mode & S_IXUSR) != 0;
77 entry->symlink = S_ISLNK(mode) != 0;
78 entry->next = NULL;
80 *list_p = entry;
81 list_p = &entry->next;
83 return ret;
86 static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
88 int len1 = strlen(name1);
89 int len2 = strlen(name2);
90 int len = len1 < len2 ? len1 : len2;
91 int ret = memcmp(name1, name2, len);
92 unsigned char c1, c2;
93 if (ret)
94 return ret;
95 c1 = name1[len];
96 c2 = name2[len];
97 if (!c1 && dir1)
98 c1 = '/';
99 if (!c2 && dir2)
100 c2 = '/';
101 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
102 if (c1 && c2 && !ret)
103 ret = len1 - len2;
104 return ret;
107 static int unpack_trees_rec(struct tree_entry_list **posns, int len,
108 const char *base, merge_fn_t fn, int *indpos)
110 int baselen = strlen(base);
111 int src_size = len + 1;
112 do {
113 int i;
114 const char *first;
115 int firstdir = 0;
116 int pathlen;
117 unsigned ce_size;
118 struct tree_entry_list **subposns;
119 struct cache_entry **src;
120 int any_files = 0;
121 int any_dirs = 0;
122 char *cache_name;
123 int ce_stage;
125 /* Find the first name in the input. */
127 first = NULL;
128 cache_name = NULL;
130 /* Check the cache */
131 if (merge && *indpos < active_nr) {
132 /* This is a bit tricky: */
133 /* If the index has a subdirectory (with
134 * contents) as the first name, it'll get a
135 * filename like "foo/bar". But that's after
136 * "foo", so the entry in trees will get
137 * handled first, at which point we'll go into
138 * "foo", and deal with "bar" from the index,
139 * because the base will be "foo/". The only
140 * way we can actually have "foo/bar" first of
141 * all the things is if the trees don't
142 * contain "foo" at all, in which case we'll
143 * handle "foo/bar" without going into the
144 * directory, but that's fine (and will return
145 * an error anyway, with the added unknown
146 * file case.
149 cache_name = active_cache[*indpos]->name;
150 if (strlen(cache_name) > baselen &&
151 !memcmp(cache_name, base, baselen)) {
152 cache_name += baselen;
153 first = cache_name;
154 } else {
155 cache_name = NULL;
159 #if DBRT_DEBUG > 1
160 if (first)
161 printf("index %s\n", first);
162 #endif
163 for (i = 0; i < len; i++) {
164 if (!posns[i] || posns[i] == &df_conflict_list)
165 continue;
166 #if DBRT_DEBUG > 1
167 printf("%d %s\n", i + 1, posns[i]->name);
168 #endif
169 if (!first || entcmp(first, firstdir,
170 posns[i]->name,
171 posns[i]->directory) > 0) {
172 first = posns[i]->name;
173 firstdir = posns[i]->directory;
176 /* No name means we're done */
177 if (!first)
178 return 0;
180 pathlen = strlen(first);
181 ce_size = cache_entry_size(baselen + pathlen);
183 src = xcalloc(src_size, sizeof(struct cache_entry *));
185 subposns = xcalloc(len, sizeof(struct tree_list_entry *));
187 if (cache_name && !strcmp(cache_name, first)) {
188 any_files = 1;
189 src[0] = active_cache[*indpos];
190 remove_cache_entry_at(*indpos);
193 for (i = 0; i < len; i++) {
194 struct cache_entry *ce;
196 if (!posns[i] ||
197 (posns[i] != &df_conflict_list &&
198 strcmp(first, posns[i]->name))) {
199 continue;
202 if (posns[i] == &df_conflict_list) {
203 src[i + merge] = &df_conflict_entry;
204 continue;
207 if (posns[i]->directory) {
208 struct tree *tree = lookup_tree(posns[i]->sha1);
209 any_dirs = 1;
210 parse_tree(tree);
211 subposns[i] = create_tree_entry_list(tree);
212 posns[i] = posns[i]->next;
213 src[i + merge] = &df_conflict_entry;
214 continue;
217 if (!merge)
218 ce_stage = 0;
219 else if (i + 1 < head_idx)
220 ce_stage = 1;
221 else if (i + 1 > head_idx)
222 ce_stage = 3;
223 else
224 ce_stage = 2;
226 ce = xcalloc(1, ce_size);
227 ce->ce_mode = create_ce_mode(posns[i]->mode);
228 ce->ce_flags = create_ce_flags(baselen + pathlen,
229 ce_stage);
230 memcpy(ce->name, base, baselen);
231 memcpy(ce->name + baselen, first, pathlen + 1);
233 any_files = 1;
235 memcpy(ce->sha1, posns[i]->sha1, 20);
236 src[i + merge] = ce;
237 subposns[i] = &df_conflict_list;
238 posns[i] = posns[i]->next;
240 if (any_files) {
241 if (merge) {
242 int ret;
244 #if DBRT_DEBUG > 1
245 printf("%s:\n", first);
246 for (i = 0; i < src_size; i++) {
247 printf(" %d ", i);
248 if (src[i])
249 printf("%s\n", sha1_to_hex(src[i]->sha1));
250 else
251 printf("\n");
253 #endif
254 ret = fn(src);
256 #if DBRT_DEBUG > 1
257 printf("Added %d entries\n", ret);
258 #endif
259 *indpos += ret;
260 } else {
261 for (i = 0; i < src_size; i++) {
262 if (src[i]) {
263 add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
268 if (any_dirs) {
269 char *newbase = xmalloc(baselen + 2 + pathlen);
270 memcpy(newbase, base, baselen);
271 memcpy(newbase + baselen, first, pathlen);
272 newbase[baselen + pathlen] = '/';
273 newbase[baselen + pathlen + 1] = '\0';
274 if (unpack_trees_rec(subposns, len, newbase, fn,
275 indpos))
276 return -1;
277 free(newbase);
279 free(subposns);
280 free(src);
281 } while (1);
284 static void reject_merge(struct cache_entry *ce)
286 die("Entry '%s' would be overwritten by merge. Cannot merge.",
287 ce->name);
290 /* Unlink the last component and attempt to remove leading
291 * directories, in case this unlink is the removal of the
292 * last entry in the directory -- empty directories are removed.
294 static void unlink_entry(char *name)
296 char *cp, *prev;
298 if (unlink(name))
299 return;
300 prev = NULL;
301 while (1) {
302 int status;
303 cp = strrchr(name, '/');
304 if (prev)
305 *prev = '/';
306 if (!cp)
307 break;
309 *cp = 0;
310 status = rmdir(name);
311 if (status) {
312 *cp = '/';
313 break;
315 prev = cp;
319 static void progress_interval(int signum)
321 progress_update = 1;
324 static void setup_progress_signal(void)
326 struct sigaction sa;
327 struct itimerval v;
329 memset(&sa, 0, sizeof(sa));
330 sa.sa_handler = progress_interval;
331 sigemptyset(&sa.sa_mask);
332 sa.sa_flags = SA_RESTART;
333 sigaction(SIGALRM, &sa, NULL);
335 v.it_interval.tv_sec = 1;
336 v.it_interval.tv_usec = 0;
337 v.it_value = v.it_interval;
338 setitimer(ITIMER_REAL, &v, NULL);
341 static void check_updates(struct cache_entry **src, int nr)
343 static struct checkout state = {
344 .base_dir = "",
345 .force = 1,
346 .quiet = 1,
347 .refresh_cache = 1,
349 unsigned short mask = htons(CE_UPDATE);
350 unsigned last_percent = 200, cnt = 0, total = 0;
352 if (update && verbose_update) {
353 for (total = cnt = 0; cnt < nr; cnt++) {
354 struct cache_entry *ce = src[cnt];
355 if (!ce->ce_mode || ce->ce_flags & mask)
356 total++;
359 /* Don't bother doing this for very small updates */
360 if (total < 250)
361 total = 0;
363 if (total) {
364 fprintf(stderr, "Checking files out...\n");
365 setup_progress_signal();
366 progress_update = 1;
368 cnt = 0;
371 while (nr--) {
372 struct cache_entry *ce = *src++;
374 if (total) {
375 if (!ce->ce_mode || ce->ce_flags & mask) {
376 unsigned percent;
377 cnt++;
378 percent = (cnt * 100) / total;
379 if (percent != last_percent ||
380 progress_update) {
381 fprintf(stderr, "%4u%% (%u/%u) done\r",
382 percent, cnt, total);
383 last_percent = percent;
387 if (!ce->ce_mode) {
388 if (update)
389 unlink_entry(ce->name);
390 continue;
392 if (ce->ce_flags & mask) {
393 ce->ce_flags &= ~mask;
394 if (update)
395 checkout_entry(ce, &state, NULL);
398 if (total) {
399 signal(SIGALRM, SIG_IGN);
400 fputc('\n', stderr);
404 static int unpack_trees(merge_fn_t fn)
406 int indpos = 0;
407 unsigned len = object_list_length(trees);
408 struct tree_entry_list **posns;
409 int i;
410 struct object_list *posn = trees;
411 merge_size = len;
413 if (len) {
414 posns = xmalloc(len * sizeof(struct tree_entry_list *));
415 for (i = 0; i < len; i++) {
416 posns[i] = create_tree_entry_list((struct tree *) posn->item);
417 posn = posn->next;
419 if (unpack_trees_rec(posns, len, "", fn, &indpos))
420 return -1;
423 if (trivial_merges_only && nontrivial_merge)
424 die("Merge requires file-level merging");
426 check_updates(active_cache, active_nr);
427 return 0;
430 static int list_tree(unsigned char *sha1)
432 struct tree *tree = parse_tree_indirect(sha1);
433 if (!tree)
434 return -1;
435 object_list_append(&tree->object, &trees);
436 return 0;
439 static int same(struct cache_entry *a, struct cache_entry *b)
441 if (!!a != !!b)
442 return 0;
443 if (!a && !b)
444 return 1;
445 return a->ce_mode == b->ce_mode &&
446 !memcmp(a->sha1, b->sha1, 20);
451 * When a CE gets turned into an unmerged entry, we
452 * want it to be up-to-date
454 static void verify_uptodate(struct cache_entry *ce)
456 struct stat st;
458 if (index_only || reset)
459 return;
461 if (!lstat(ce->name, &st)) {
462 unsigned changed = ce_match_stat(ce, &st, 1);
463 if (!changed)
464 return;
465 errno = 0;
467 if (reset) {
468 ce->ce_flags |= htons(CE_UPDATE);
469 return;
471 if (errno == ENOENT)
472 return;
473 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
476 static void invalidate_ce_path(struct cache_entry *ce)
478 if (ce)
479 cache_tree_invalidate_path(active_cache_tree, ce->name);
483 * We do not want to remove or overwrite a working tree file that
484 * is not tracked.
486 static void verify_absent(const char *path, const char *action)
488 struct stat st;
490 if (index_only || reset || !update)
491 return;
492 if (!lstat(path, &st))
493 die("Untracked working tree file '%s' "
494 "would be %s by merge.", path, action);
497 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
499 merge->ce_flags |= htons(CE_UPDATE);
500 if (old) {
502 * See if we can re-use the old CE directly?
503 * That way we get the uptodate stat info.
505 * This also removes the UPDATE flag on
506 * a match.
508 if (same(old, merge)) {
509 *merge = *old;
510 } else {
511 verify_uptodate(old);
512 invalidate_ce_path(old);
515 else {
516 verify_absent(merge->name, "overwritten");
517 invalidate_ce_path(merge);
520 merge->ce_flags &= ~htons(CE_STAGEMASK);
521 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
522 return 1;
525 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
527 if (old)
528 verify_uptodate(old);
529 else
530 verify_absent(ce->name, "removed");
531 ce->ce_mode = 0;
532 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
533 invalidate_ce_path(ce);
534 return 1;
537 static int keep_entry(struct cache_entry *ce)
539 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
540 return 1;
543 #if DBRT_DEBUG
544 static void show_stage_entry(FILE *o,
545 const char *label, const struct cache_entry *ce)
547 if (!ce)
548 fprintf(o, "%s (missing)\n", label);
549 else
550 fprintf(o, "%s%06o %s %d\t%s\n",
551 label,
552 ntohl(ce->ce_mode),
553 sha1_to_hex(ce->sha1),
554 ce_stage(ce),
555 ce->name);
557 #endif
559 static int threeway_merge(struct cache_entry **stages)
561 struct cache_entry *index;
562 struct cache_entry *head;
563 struct cache_entry *remote = stages[head_idx + 1];
564 int count;
565 int head_match = 0;
566 int remote_match = 0;
567 const char *path = NULL;
569 int df_conflict_head = 0;
570 int df_conflict_remote = 0;
572 int any_anc_missing = 0;
573 int no_anc_exists = 1;
574 int i;
576 for (i = 1; i < head_idx; i++) {
577 if (!stages[i])
578 any_anc_missing = 1;
579 else {
580 if (!path)
581 path = stages[i]->name;
582 no_anc_exists = 0;
586 index = stages[0];
587 head = stages[head_idx];
589 if (head == &df_conflict_entry) {
590 df_conflict_head = 1;
591 head = NULL;
594 if (remote == &df_conflict_entry) {
595 df_conflict_remote = 1;
596 remote = NULL;
599 if (!path && index)
600 path = index->name;
601 if (!path && head)
602 path = head->name;
603 if (!path && remote)
604 path = remote->name;
606 /* First, if there's a #16 situation, note that to prevent #13
607 * and #14.
609 if (!same(remote, head)) {
610 for (i = 1; i < head_idx; i++) {
611 if (same(stages[i], head)) {
612 head_match = i;
614 if (same(stages[i], remote)) {
615 remote_match = i;
620 /* We start with cases where the index is allowed to match
621 * something other than the head: #14(ALT) and #2ALT, where it
622 * is permitted to match the result instead.
624 /* #14, #14ALT, #2ALT */
625 if (remote && !df_conflict_head && head_match && !remote_match) {
626 if (index && !same(index, remote) && !same(index, head))
627 reject_merge(index);
628 return merged_entry(remote, index);
631 * If we have an entry in the index cache, then we want to
632 * make sure that it matches head.
634 if (index && !same(index, head)) {
635 reject_merge(index);
638 if (head) {
639 /* #5ALT, #15 */
640 if (same(head, remote))
641 return merged_entry(head, index);
642 /* #13, #3ALT */
643 if (!df_conflict_remote && remote_match && !head_match)
644 return merged_entry(head, index);
647 /* #1 */
648 if (!head && !remote && any_anc_missing)
649 return 0;
651 /* Under the new "aggressive" rule, we resolve mostly trivial
652 * cases that we historically had git-merge-one-file resolve.
654 if (aggressive) {
655 int head_deleted = !head && !df_conflict_head;
656 int remote_deleted = !remote && !df_conflict_remote;
658 * Deleted in both.
659 * Deleted in one and unchanged in the other.
661 if ((head_deleted && remote_deleted) ||
662 (head_deleted && remote && remote_match) ||
663 (remote_deleted && head && head_match)) {
664 if (index)
665 return deleted_entry(index, index);
666 else if (path)
667 verify_absent(path, "removed");
668 return 0;
671 * Added in both, identically.
673 if (no_anc_exists && head && remote && same(head, remote))
674 return merged_entry(head, index);
678 /* Below are "no merge" cases, which require that the index be
679 * up-to-date to avoid the files getting overwritten with
680 * conflict resolution files.
682 if (index) {
683 verify_uptodate(index);
685 else if (path)
686 verify_absent(path, "overwritten");
688 nontrivial_merge = 1;
690 /* #2, #3, #4, #6, #7, #9, #11. */
691 count = 0;
692 if (!head_match || !remote_match) {
693 for (i = 1; i < head_idx; i++) {
694 if (stages[i]) {
695 keep_entry(stages[i]);
696 count++;
697 break;
701 #if DBRT_DEBUG
702 else {
703 fprintf(stderr, "read-tree: warning #16 detected\n");
704 show_stage_entry(stderr, "head ", stages[head_match]);
705 show_stage_entry(stderr, "remote ", stages[remote_match]);
707 #endif
708 if (head) { count += keep_entry(head); }
709 if (remote) { count += keep_entry(remote); }
710 return count;
714 * Two-way merge.
716 * The rule is to "carry forward" what is in the index without losing
717 * information across a "fast forward", favoring a successful merge
718 * over a merge failure when it makes sense. For details of the
719 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
722 static int twoway_merge(struct cache_entry **src)
724 struct cache_entry *current = src[0];
725 struct cache_entry *oldtree = src[1], *newtree = src[2];
727 if (merge_size != 2)
728 return error("Cannot do a twoway merge of %d trees",
729 merge_size);
731 if (current) {
732 if ((!oldtree && !newtree) || /* 4 and 5 */
733 (!oldtree && newtree &&
734 same(current, newtree)) || /* 6 and 7 */
735 (oldtree && newtree &&
736 same(oldtree, newtree)) || /* 14 and 15 */
737 (oldtree && newtree &&
738 !same(oldtree, newtree) && /* 18 and 19*/
739 same(current, newtree))) {
740 return keep_entry(current);
742 else if (oldtree && !newtree && same(current, oldtree)) {
743 /* 10 or 11 */
744 return deleted_entry(oldtree, current);
746 else if (oldtree && newtree &&
747 same(current, oldtree) && !same(current, newtree)) {
748 /* 20 or 21 */
749 return merged_entry(newtree, current);
751 else {
752 /* all other failures */
753 if (oldtree)
754 reject_merge(oldtree);
755 if (current)
756 reject_merge(current);
757 if (newtree)
758 reject_merge(newtree);
759 return -1;
762 else if (newtree)
763 return merged_entry(newtree, current);
764 else
765 return deleted_entry(oldtree, current);
769 * One-way merge.
771 * The rule is:
772 * - take the stat information from stage0, take the data from stage1
774 static int oneway_merge(struct cache_entry **src)
776 struct cache_entry *old = src[0];
777 struct cache_entry *a = src[1];
779 if (merge_size != 1)
780 return error("Cannot do a oneway merge of %d trees",
781 merge_size);
783 if (!a)
784 return deleted_entry(old, old);
785 if (old && same(old, a)) {
786 if (reset) {
787 struct stat st;
788 if (lstat(old->name, &st) ||
789 ce_match_stat(old, &st, 1))
790 old->ce_flags |= htons(CE_UPDATE);
792 return keep_entry(old);
794 return merged_entry(a, old);
797 static int read_cache_unmerged(void)
799 int i, deleted;
800 struct cache_entry **dst;
802 read_cache();
803 dst = active_cache;
804 deleted = 0;
805 for (i = 0; i < active_nr; i++) {
806 struct cache_entry *ce = active_cache[i];
807 if (ce_stage(ce)) {
808 deleted++;
809 invalidate_ce_path(ce);
810 continue;
812 if (deleted)
813 *dst = ce;
814 dst++;
816 active_nr -= deleted;
817 return deleted;
820 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
822 struct tree_desc desc;
823 int cnt;
825 memcpy(it->sha1, tree->object.sha1, 20);
826 desc.buf = tree->buffer;
827 desc.size = tree->size;
828 cnt = 0;
829 while (desc.size) {
830 unsigned mode;
831 const char *name;
832 const unsigned char *sha1;
834 sha1 = tree_entry_extract(&desc, &name, &mode);
835 update_tree_entry(&desc);
836 if (!S_ISDIR(mode))
837 cnt++;
838 else {
839 struct cache_tree_sub *sub;
840 struct tree *subtree = lookup_tree(sha1);
841 if (!subtree->object.parsed)
842 parse_tree(subtree);
843 sub = cache_tree_sub(it, name);
844 sub->cache_tree = cache_tree();
845 prime_cache_tree_rec(sub->cache_tree, subtree);
846 cnt += sub->cache_tree->entry_count;
849 it->entry_count = cnt;
852 static void prime_cache_tree(void)
854 struct tree *tree = (struct tree *)trees->item;
855 if (!tree)
856 return;
857 active_cache_tree = cache_tree();
858 prime_cache_tree_rec(active_cache_tree, tree);
862 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
864 static struct cache_file cache_file;
866 int cmd_read_tree(int argc, const char **argv, char **envp)
868 int i, newfd, stage = 0;
869 unsigned char sha1[20];
870 merge_fn_t fn = NULL;
872 setup_git_directory();
873 git_config(git_default_config);
875 newfd = hold_index_file_for_update(&cache_file, get_index_file());
876 if (newfd < 0)
877 die("unable to create new cachefile");
879 git_config(git_default_config);
881 merge = 0;
882 reset = 0;
883 for (i = 1; i < argc; i++) {
884 const char *arg = argv[i];
886 /* "-u" means "update", meaning that a merge will update
887 * the working tree.
889 if (!strcmp(arg, "-u")) {
890 update = 1;
891 continue;
894 if (!strcmp(arg, "-v")) {
895 verbose_update = 1;
896 continue;
899 /* "-i" means "index only", meaning that a merge will
900 * not even look at the working tree.
902 if (!strcmp(arg, "-i")) {
903 index_only = 1;
904 continue;
907 /* This differs from "-m" in that we'll silently ignore unmerged entries */
908 if (!strcmp(arg, "--reset")) {
909 if (stage || merge)
910 usage(read_tree_usage);
911 reset = 1;
912 merge = 1;
913 stage = 1;
914 read_cache_unmerged();
915 continue;
918 if (!strcmp(arg, "--trivial")) {
919 trivial_merges_only = 1;
920 continue;
923 if (!strcmp(arg, "--aggressive")) {
924 aggressive = 1;
925 continue;
928 /* "-m" stands for "merge", meaning we start in stage 1 */
929 if (!strcmp(arg, "-m")) {
930 if (stage || merge)
931 usage(read_tree_usage);
932 if (read_cache_unmerged())
933 die("you need to resolve your current index first");
934 stage = 1;
935 merge = 1;
936 continue;
939 /* using -u and -i at the same time makes no sense */
940 if (1 < index_only + update)
941 usage(read_tree_usage);
943 if (get_sha1(arg, sha1))
944 die("Not a valid object name %s", arg);
945 if (list_tree(sha1) < 0)
946 die("failed to unpack tree object %s", arg);
947 stage++;
949 if ((update||index_only) && !merge)
950 usage(read_tree_usage);
952 if (merge) {
953 if (stage < 2)
954 die("just how do you expect me to merge %d trees?", stage-1);
955 switch (stage - 1) {
956 case 1:
957 fn = oneway_merge;
958 break;
959 case 2:
960 fn = twoway_merge;
961 break;
962 case 3:
963 default:
964 fn = threeway_merge;
965 cache_tree_free(&active_cache_tree);
966 break;
969 if (stage - 1 >= 3)
970 head_idx = stage - 2;
971 else
972 head_idx = 1;
975 unpack_trees(fn);
978 * When reading only one tree (either the most basic form,
979 * "-m ent" or "--reset ent" form), we can obtain a fully
980 * valid cache-tree because the index must match exactly
981 * what came from the tree.
983 if (trees && trees->item && (!merge || (stage == 2))) {
984 cache_tree_free(&active_cache_tree);
985 prime_cache_tree();
988 if (write_cache(newfd, active_cache, active_nr) ||
989 commit_index_file(&cache_file))
990 die("unable to write new index file");
991 return 0;