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