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