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