Merge with master.kernel.org:/pub/scm/git/git.git
[git.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 static void check_updates(struct cache_entry **src, int nr)
241 {
242         static struct checkout state = {
243                 .base_dir = "",
244                 .force = 1,
245                 .quiet = 1,
246                 .refresh_cache = 1,
247         };
248         unsigned short mask = htons(CE_UPDATE);
249         while (nr--) {
250                 struct cache_entry *ce = *src++;
251                 if (!ce->ce_mode) {
252                         if (update)
253                                 unlink(ce->name);
254                         continue;
255                 }
256                 if (ce->ce_flags & mask) {
257                         ce->ce_flags &= ~mask;
258                         if (update)
259                                 checkout_entry(ce, &state);
260                 }
261         }
262 }
263
264 static int unpack_trees(merge_fn_t fn)
265 {
266         int indpos = 0;
267         unsigned len = object_list_length(trees);
268         struct tree_entry_list **posns = 
269                 xmalloc(len * sizeof(struct tree_entry_list *));
270         int i;
271         struct object_list *posn = trees;
272         merge_size = len;
273         for (i = 0; i < len; i++) {
274                 posns[i] = ((struct tree *) posn->item)->entries;
275                 posn = posn->next;
276         }
277         if (unpack_trees_rec(posns, len, "", fn, &indpos))
278                 return -1;
279
280         if (trivial_merges_only && nontrivial_merge)
281                 die("Merge requires file-level merging");
282
283         check_updates(active_cache, active_nr);
284         return 0;
285 }
286
287 static int list_tree(unsigned char *sha1)
288 {
289         struct tree *tree = parse_tree_indirect(sha1);
290         if (!tree)
291                 return -1;
292         object_list_append(&tree->object, &trees);
293         return 0;
294 }
295
296 static int same(struct cache_entry *a, struct cache_entry *b)
297 {
298         if (!!a != !!b)
299                 return 0;
300         if (!a && !b)
301                 return 1;
302         return a->ce_mode == b->ce_mode && 
303                 !memcmp(a->sha1, b->sha1, 20);
304 }
305
306
307 /*
308  * When a CE gets turned into an unmerged entry, we
309  * want it to be up-to-date
310  */
311 static void verify_uptodate(struct cache_entry *ce)
312 {
313         struct stat st;
314
315         if (index_only)
316                 return;
317
318         if (!lstat(ce->name, &st)) {
319                 unsigned changed = ce_match_stat(ce, &st);
320                 if (!changed)
321                         return;
322                 errno = 0;
323         }
324         if (errno == ENOENT)
325                 return;
326         die("Entry '%s' not uptodate. Cannot merge.", ce->name);
327 }
328
329 static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
330 {
331         merge->ce_flags |= htons(CE_UPDATE);
332         if (old) {
333                 /*
334                  * See if we can re-use the old CE directly?
335                  * That way we get the uptodate stat info.
336                  *
337                  * This also removes the UPDATE flag on
338                  * a match.
339                  */
340                 if (same(old, merge)) {
341                         *merge = *old;
342                 } else {
343                         verify_uptodate(old);
344                 }
345         }
346         merge->ce_flags &= ~htons(CE_STAGEMASK);
347         add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
348         return 1;
349 }
350
351 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
352 {
353         if (old)
354                 verify_uptodate(old);
355         ce->ce_mode = 0;
356         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
357         return 1;
358 }
359
360 static int keep_entry(struct cache_entry *ce)
361 {
362         add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
363         return 1;
364 }
365
366 #if DBRT_DEBUG
367 static void show_stage_entry(FILE *o,
368                              const char *label, const struct cache_entry *ce)
369 {
370         if (!ce)
371                 fprintf(o, "%s (missing)\n", label);
372         else
373                 fprintf(o, "%s%06o %s %d\t%s\n",
374                         label,
375                         ntohl(ce->ce_mode),
376                         sha1_to_hex(ce->sha1),
377                         ce_stage(ce),
378                         ce->name);
379 }
380 #endif
381
382 static int threeway_merge(struct cache_entry **stages)
383 {
384         struct cache_entry *index;
385         struct cache_entry *head; 
386         struct cache_entry *remote = stages[head_idx + 1];
387         int count;
388         int head_match = 0;
389         int remote_match = 0;
390
391         int df_conflict_head = 0;
392         int df_conflict_remote = 0;
393
394         int any_anc_missing = 0;
395         int i;
396
397         for (i = 1; i < head_idx; i++) {
398                 if (!stages[i])
399                         any_anc_missing = 1;
400         }
401
402         index = stages[0];
403         head = stages[head_idx];
404
405         if (head == &df_conflict_entry) {
406                 df_conflict_head = 1;
407                 head = NULL;
408         }
409
410         if (remote == &df_conflict_entry) {
411                 df_conflict_remote = 1;
412                 remote = NULL;
413         }
414
415         /* First, if there's a #16 situation, note that to prevent #13
416          * and #14. 
417          */
418         if (!same(remote, head)) {
419                 for (i = 1; i < head_idx; i++) {
420                         if (same(stages[i], head)) {
421                                 head_match = i;
422                         }
423                         if (same(stages[i], remote)) {
424                                 remote_match = i;
425                         }
426                 }
427         }
428
429         /* We start with cases where the index is allowed to match
430          * something other than the head: #14(ALT) and #2ALT, where it
431          * is permitted to match the result instead.
432          */
433         /* #14, #14ALT, #2ALT */
434         if (remote && !df_conflict_head && head_match && !remote_match) {
435                 if (index && !same(index, remote) && !same(index, head))
436                         reject_merge(index);
437                 return merged_entry(remote, index);
438         }
439         /*
440          * If we have an entry in the index cache, then we want to
441          * make sure that it matches head.
442          */
443         if (index && !same(index, head)) {
444                 reject_merge(index);
445         }
446
447         if (head) {
448                 /* #5ALT, #15 */
449                 if (same(head, remote))
450                         return merged_entry(head, index);
451                 /* #13, #3ALT */
452                 if (!df_conflict_remote && remote_match && !head_match)
453                         return merged_entry(head, index);
454         }
455
456         /* #1 */
457         if (!head && !remote && any_anc_missing)
458                 return 0;
459
460         /* Below are "no merge" cases, which require that the index be
461          * up-to-date to avoid the files getting overwritten with
462          * conflict resolution files. 
463          */
464         if (index) {
465                 verify_uptodate(index);
466         }
467
468         nontrivial_merge = 1;
469
470         /* #2, #3, #4, #6, #7, #9, #11. */
471         count = 0;
472         if (!head_match || !remote_match) {
473                 for (i = 1; i < head_idx; i++) {
474                         if (stages[i]) {
475                                 keep_entry(stages[i]);
476                                 count++;
477                                 break;
478                         }
479                 }
480         }
481 #if DBRT_DEBUG
482         else {
483                 fprintf(stderr, "read-tree: warning #16 detected\n");
484                 show_stage_entry(stderr, "head   ", stages[head_match]);
485                 show_stage_entry(stderr, "remote ", stages[remote_match]);
486         }
487 #endif
488         if (head) { count += keep_entry(head); }
489         if (remote) { count += keep_entry(remote); }
490         return count;
491 }
492
493 /*
494  * Two-way merge.
495  *
496  * The rule is to "carry forward" what is in the index without losing
497  * information across a "fast forward", favoring a successful merge
498  * over a merge failure when it makes sense.  For details of the
499  * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
500  *
501  */
502 static int twoway_merge(struct cache_entry **src)
503 {
504         struct cache_entry *current = src[0];
505         struct cache_entry *oldtree = src[1], *newtree = src[2];
506
507         if (merge_size != 2)
508                 return error("Cannot do a twoway merge of %d trees\n",
509                              merge_size);
510
511         if (current) {
512                 if ((!oldtree && !newtree) || /* 4 and 5 */
513                     (!oldtree && newtree &&
514                      same(current, newtree)) || /* 6 and 7 */
515                     (oldtree && newtree &&
516                      same(oldtree, newtree)) || /* 14 and 15 */
517                     (oldtree && newtree &&
518                      !same(oldtree, newtree) && /* 18 and 19*/
519                      same(current, newtree))) {
520                         return keep_entry(current);
521                 }
522                 else if (oldtree && !newtree && same(current, oldtree)) {
523                         /* 10 or 11 */
524                         return deleted_entry(oldtree, current);
525                 }
526                 else if (oldtree && newtree &&
527                          same(current, oldtree) && !same(current, newtree)) {
528                         /* 20 or 21 */
529                         return merged_entry(newtree, current);
530                 }
531                 else {
532                         /* all other failures */
533                         if (oldtree)
534                                 reject_merge(oldtree);
535                         if (current)
536                                 reject_merge(current);
537                         if (newtree)
538                                 reject_merge(newtree);
539                         return -1;
540                 }
541         }
542         else if (newtree)
543                 return merged_entry(newtree, current);
544         else
545                 return deleted_entry(oldtree, current);
546 }
547
548 /*
549  * One-way merge.
550  *
551  * The rule is:
552  * - take the stat information from stage0, take the data from stage1
553  */
554 static int oneway_merge(struct cache_entry **src)
555 {
556         struct cache_entry *old = src[0];
557         struct cache_entry *a = src[1];
558
559         if (merge_size != 1)
560                 return error("Cannot do a oneway merge of %d trees\n",
561                              merge_size);
562
563         if (!a)
564                 return 0;
565         if (old && same(old, a)) {
566                 return keep_entry(old);
567         }
568         return merged_entry(a, NULL);
569 }
570
571 static int read_cache_unmerged(void)
572 {
573         int i, deleted;
574         struct cache_entry **dst;
575
576         read_cache();
577         dst = active_cache;
578         deleted = 0;
579         for (i = 0; i < active_nr; i++) {
580                 struct cache_entry *ce = active_cache[i];
581                 if (ce_stage(ce)) {
582                         deleted++;
583                         continue;
584                 }
585                 if (deleted)
586                         *dst = ce;
587                 dst++;
588         }
589         active_nr -= deleted;
590         return deleted;
591 }
592
593 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u | -i] <sha1> [<sha2> [<sha3>]])";
594
595 static struct cache_file cache_file;
596
597 int main(int argc, char **argv)
598 {
599         int i, newfd, reset, stage = 0;
600         unsigned char sha1[20];
601         merge_fn_t fn = NULL;
602
603         newfd = hold_index_file_for_update(&cache_file, get_index_file());
604         if (newfd < 0)
605                 die("unable to create new cachefile");
606
607         merge = 0;
608         reset = 0;
609         for (i = 1; i < argc; i++) {
610                 const char *arg = argv[i];
611
612                 /* "-u" means "update", meaning that a merge will update
613                  * the working tree.
614                  */
615                 if (!strcmp(arg, "-u")) {
616                         update = 1;
617                         continue;
618                 }
619
620                 /* "-i" means "index only", meaning that a merge will
621                  * not even look at the working tree.
622                  */
623                 if (!strcmp(arg, "-i")) {
624                         index_only = 1;
625                         continue;
626                 }
627
628                 /* This differs from "-m" in that we'll silently ignore unmerged entries */
629                 if (!strcmp(arg, "--reset")) {
630                         if (stage || merge)
631                                 usage(read_tree_usage);
632                         reset = 1;
633                         merge = 1;
634                         stage = 1;
635                         read_cache_unmerged();
636                         continue;
637                 }
638
639                 if (!strcmp(arg, "--trivial")) {
640                         trivial_merges_only = 1;
641                         continue;
642                 }
643
644                 /* "-m" stands for "merge", meaning we start in stage 1 */
645                 if (!strcmp(arg, "-m")) {
646                         if (stage || merge)
647                                 usage(read_tree_usage);
648                         if (read_cache_unmerged())
649                                 die("you need to resolve your current index first");
650                         stage = 1;
651                         merge = 1;
652                         continue;
653                 }
654
655                 /* using -u and -i at the same time makes no sense */
656                 if (1 < index_only + update)
657                         usage(read_tree_usage);
658
659                 if (get_sha1(arg, sha1) < 0)
660                         usage(read_tree_usage);
661                 if (list_tree(sha1) < 0)
662                         die("failed to unpack tree object %s", arg);
663                 stage++;
664         }
665         if ((update||index_only) && !merge)
666                 usage(read_tree_usage);
667
668         if (merge) {
669                 if (stage < 2)
670                         die("just how do you expect me to merge %d trees?", stage-1);
671                 switch (stage - 1) {
672                 case 1:
673                         fn = oneway_merge;
674                         break;
675                 case 2:
676                         fn = twoway_merge;
677                         break;
678                 case 3:
679                         fn = threeway_merge;
680                         break;
681                 default:
682                         fn = threeway_merge;
683                         break;
684                 }
685
686                 if (stage - 1 >= 3)
687                         head_idx = stage - 2;
688                 else
689                         head_idx = 1;
690         }
691
692         unpack_trees(fn);
693         if (write_cache(newfd, active_cache, active_nr) ||
694             commit_index_file(&cache_file))
695                 die("unable to write new index file");
696         return 0;
697 }