[PATCH] Add debugging help for case #16 to read-tree.c
[git/git.git] / read-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
4d3fe0c5
JH
6#define DBRT_DEBUG 1
7
e83c5163
LT
8#include "cache.h"
9
ee6566e8
DB
10#include "object.h"
11#include "tree.h"
12
13static int merge = 0;
220a0b52 14static int update = 0;
d99082e0 15
ee6566e8
DB
16static int head_idx = -1;
17static int merge_size = 0;
b12ec373 18
ee6566e8
DB
19static struct object_list *trees = NULL;
20
21static struct cache_entry df_conflict_entry = {
22};
23
24static struct tree_entry_list df_conflict_list = {
25 .name = NULL,
26 .next = &df_conflict_list
27};
28
29typedef int (*merge_fn_t)(struct cache_entry **src);
30
31static int entcmp(char *name1, int dir1, char *name2, int dir2)
32{
33 int len1 = strlen(name1);
34 int len2 = strlen(name2);
35 int len = len1 < len2 ? len1 : len2;
36 int ret = memcmp(name1, name2, len);
37 unsigned char c1, c2;
38 if (ret)
39 return ret;
40 c1 = name1[len];
41 c2 = name2[len];
42 if (!c1 && dir1)
43 c1 = '/';
44 if (!c2 && dir2)
45 c2 = '/';
46 ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
47 if (c1 && c2 && !ret)
48 ret = len1 - len2;
14242464 49 return ret;
b12ec373
JH
50}
51
ee6566e8
DB
52static int unpack_trees_rec(struct tree_entry_list **posns, int len,
53 const char *base, merge_fn_t fn, int *indpos)
ca016f0e 54{
ee6566e8
DB
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
4d3fe0c5 104#if DBRT_DEBUG > 1
ee6566e8
DB
105 if (first)
106 printf("index %s\n", first);
2ab706a3 107#endif
ee6566e8
DB
108 for (i = 0; i < len; i++) {
109 if (!posns[i] || posns[i] == &df_conflict_list)
110 continue;
4d3fe0c5 111#if DBRT_DEBUG > 1
ee6566e8 112 printf("%d %s\n", i + 1, posns[i]->name);
2ab706a3 113#endif
ee6566e8
DB
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
4d3fe0c5 191#if DBRT_DEBUG > 1
ee6566e8
DB
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 }
2ab706a3 200#endif
ee6566e8
DB
201 ret = fn(src);
202
4d3fe0c5 203#if DBRT_DEBUG > 1
ee6566e8 204 printf("Added %d entries\n", ret);
2ab706a3 205#endif
ee6566e8
DB
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);
ca016f0e
LT
228}
229
ee6566e8 230static void reject_merge(struct cache_entry *ce)
43f91266 231{
ee6566e8
DB
232 die("Entry '%s' would be overwritten by merge. Cannot merge.",
233 ce->name);
43f91266
LT
234}
235
ee6566e8
DB
236static 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}
43f91266 259
ee6566e8 260static int unpack_trees(merge_fn_t fn)
d99082e0 261{
ee6566e8
DB
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;
d723c690 272 }
ee6566e8
DB
273 if (unpack_trees_rec(posns, len, "", fn, &indpos))
274 return -1;
275
276 check_updates(active_cache, active_nr);
277 return 0;
d99082e0
LT
278}
279
ee6566e8
DB
280static 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
289static 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
02ede67a
LT
300/*
301 * When a CE gets turned into an unmerged entry, we
302 * want it to be up-to-date
303 */
304static 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
ee6566e8 319static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
d99082e0 320{
d723c690
LT
321 merge->ce_flags |= htons(CE_UPDATE);
322 if (old) {
02ede67a 323 /*
d723c690
LT
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.
02ede67a 329 */
d723c690
LT
330 if (same(old, merge)) {
331 *merge = *old;
ee6566e8 332 } else {
d723c690
LT
333 verify_uptodate(old);
334 }
a3a65234 335 }
d723c690 336 merge->ce_flags &= ~htons(CE_STAGEMASK);
ee6566e8 337 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
d723c690 338 return 1;
a3a65234
LT
339}
340
ee6566e8 341static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
aa16021e
LT
342{
343 if (old)
344 verify_uptodate(old);
345 ce->ce_mode = 0;
ee6566e8 346 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
aa16021e
LT
347 return 1;
348}
349
ee6566e8 350static int keep_entry(struct cache_entry *ce)
32192e66 351{
ee6566e8
DB
352 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
353 return 1;
32192e66
JH
354}
355
4d3fe0c5
JH
356#if DBRT_DEBUG
357static void show_stage_entry(FILE *o,
358 const char *label, const struct cache_entry *ce)
359{
360 fprintf(stderr, "%s%06o %s %d\t%s\n",
361 label,
362 ntohl(ce->ce_mode),
363 sha1_to_hex(ce->sha1),
364 ce_stage(ce),
365 ce->name);
366}
367#endif
368
ee6566e8 369static int threeway_merge(struct cache_entry **stages)
e6ee623b 370{
ee6566e8
DB
371 struct cache_entry *index;
372 struct cache_entry *head;
373 struct cache_entry *remote = stages[head_idx + 1];
d723c690 374 int count;
ee6566e8
DB
375 int head_match = 0;
376 int remote_match = 0;
d723c690 377
ee6566e8
DB
378 int df_conflict_head = 0;
379 int df_conflict_remote = 0;
380
381 int any_anc_missing = 0;
382 int i;
383
384 for (i = 1; i < head_idx; i++) {
385 if (!stages[i])
386 any_anc_missing = 1;
e7f9bc41 387 }
ee6566e8
DB
388
389 index = stages[0];
390 head = stages[head_idx];
391
392 if (head == &df_conflict_entry) {
393 df_conflict_head = 1;
394 head = NULL;
395 }
396
397 if (remote == &df_conflict_entry) {
398 df_conflict_remote = 1;
399 remote = NULL;
400 }
401
402 /* First, if there's a #16 situation, note that to prevent #13
403 * and #14.
404 */
405 if (!same(remote, head)) {
406 for (i = 1; i < head_idx; i++) {
407 if (same(stages[i], head)) {
4d3fe0c5 408 head_match = i;
ee6566e8
DB
409 }
410 if (same(stages[i], remote)) {
4d3fe0c5 411 remote_match = i;
ee6566e8 412 }
32192e66 413 }
32192e66 414 }
ee6566e8
DB
415
416 /* We start with cases where the index is allowed to match
417 * something other than the head: #14(ALT) and #2ALT, where it
418 * is permitted to match the result instead.
419 */
420 /* #14, #14ALT, #2ALT */
421 if (remote && !df_conflict_head && head_match && !remote_match) {
422 if (index && !same(index, remote) && !same(index, head))
423 reject_merge(index);
424 return merged_entry(remote, index);
036d51cc 425 }
d723c690 426 /*
ee6566e8
DB
427 * If we have an entry in the index cache, then we want to
428 * make sure that it matches head.
d723c690 429 */
ee6566e8
DB
430 if (index && !same(index, head)) {
431 reject_merge(index);
e6ee623b 432 }
ee6566e8
DB
433
434 if (head) {
435 /* #5ALT, #15 */
436 if (same(head, remote))
437 return merged_entry(head, index);
438 /* #13, #3ALT */
439 if (!df_conflict_remote && remote_match && !head_match)
440 return merged_entry(head, index);
441 }
442
443 /* #1 */
444 if (!head && !remote && any_anc_missing)
445 return 0;
446
447 /* Below are "no merge" cases, which require that the index be
448 * up-to-date to avoid the files getting overwritten with
449 * conflict resolution files.
450 */
451 if (index) {
452 verify_uptodate(index);
453 }
454
455 /* #2, #3, #4, #6, #7, #9, #11. */
d723c690 456 count = 0;
ee6566e8
DB
457 if (!head_match || !remote_match) {
458 for (i = 1; i < head_idx; i++) {
459 if (stages[i]) {
460 keep_entry(stages[i]);
461 count++;
462 break;
463 }
464 }
465 }
4d3fe0c5
JH
466#if DBRT_DEBUG
467 else {
468 fprintf(stderr, "read-tree: warning #16 detected\n");
469 show_stage_entry(stderr, "head ", stages[head_match]);
470 show_stage_entry(stderr, "remote ", stages[remote_match]);
471 }
472#endif
ee6566e8
DB
473 if (head) { count += keep_entry(head); }
474 if (remote) { count += keep_entry(remote); }
d723c690 475 return count;
e6ee623b
LT
476}
477
220a0b52
LT
478/*
479 * Two-way merge.
480 *
c8596009
JH
481 * The rule is to "carry forward" what is in the index without losing
482 * information across a "fast forward", favoring a successful merge
483 * over a merge failure when it makes sense. For details of the
484 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
485 *
220a0b52 486 */
ee6566e8 487static int twoway_merge(struct cache_entry **src)
a3a65234 488{
c8596009
JH
489 struct cache_entry *current = src[0];
490 struct cache_entry *oldtree = src[1], *newtree = src[2];
220a0b52 491
ee6566e8
DB
492 if (merge_size != 2)
493 return error("Cannot do a twoway merge of %d trees\n",
494 merge_size);
e6ee623b 495
c8596009
JH
496 if (current) {
497 if ((!oldtree && !newtree) || /* 4 and 5 */
498 (!oldtree && newtree &&
499 same(current, newtree)) || /* 6 and 7 */
500 (oldtree && newtree &&
501 same(oldtree, newtree)) || /* 14 and 15 */
502 (oldtree && newtree &&
503 !same(oldtree, newtree) && /* 18 and 19*/
504 same(current, newtree))) {
ee6566e8 505 return keep_entry(current);
c8596009
JH
506 }
507 else if (oldtree && !newtree && same(current, oldtree)) {
508 /* 10 or 11 */
ee6566e8 509 return deleted_entry(oldtree, current);
c8596009
JH
510 }
511 else if (oldtree && newtree &&
512 same(current, oldtree) && !same(current, newtree)) {
513 /* 20 or 21 */
ee6566e8 514 return merged_entry(newtree, current);
c8596009 515 }
ee6566e8 516 else {
c8596009 517 /* all other failures */
ee6566e8
DB
518 if (oldtree)
519 reject_merge(oldtree);
520 if (current)
521 reject_merge(current);
522 if (newtree)
523 reject_merge(newtree);
d723c690 524 return -1;
ee6566e8 525 }
e6ee623b 526 }
c8596009 527 else if (newtree)
ee6566e8 528 return merged_entry(newtree, current);
c8596009 529 else
ee6566e8 530 return deleted_entry(oldtree, current);
03efa6d9
JH
531}
532
d723c690
LT
533/*
534 * One-way merge.
535 *
536 * The rule is:
537 * - take the stat information from stage0, take the data from stage1
538 */
ee6566e8 539static int oneway_merge(struct cache_entry **src)
220a0b52 540{
d723c690
LT
541 struct cache_entry *old = src[0];
542 struct cache_entry *a = src[1];
a3a65234 543
ee6566e8
DB
544 if (merge_size != 1)
545 return error("Cannot do a oneway merge of %d trees\n",
546 merge_size);
a3a65234 547
d723c690
LT
548 if (!a)
549 return 0;
b5b42507 550 if (old && same(old, a)) {
ee6566e8 551 return keep_entry(old);
b5b42507 552 }
ee6566e8 553 return merged_entry(a, NULL);
d723c690
LT
554}
555
438195cc
LT
556static int read_cache_unmerged(void)
557{
558 int i, deleted;
559 struct cache_entry **dst;
560
561 read_cache();
562 dst = active_cache;
563 deleted = 0;
564 for (i = 0; i < active_nr; i++) {
565 struct cache_entry *ce = active_cache[i];
566 if (ce_stage(ce)) {
567 deleted++;
568 continue;
569 }
570 if (deleted)
571 *dst = ce;
572 dst++;
573 }
574 active_nr -= deleted;
575 return deleted;
576}
577
4d1f1190 578static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
c5bac17a 579
96cd5429
JH
580static struct cache_file cache_file;
581
e83c5163
LT
582int main(int argc, char **argv)
583{
ee6566e8 584 int i, newfd, reset, stage = 0;
e83c5163 585 unsigned char sha1[20];
ee6566e8 586 merge_fn_t fn = NULL;
bb233d69 587
96cd5429 588 newfd = hold_index_file_for_update(&cache_file, get_index_file());
83adac3c 589 if (newfd < 0)
2de381f9 590 die("unable to create new cachefile");
83adac3c 591
ca016f0e 592 merge = 0;
438195cc 593 reset = 0;
83adac3c
LT
594 for (i = 1; i < argc; i++) {
595 const char *arg = argv[i];
596
220a0b52
LT
597 /* "-u" means "update", meaning that a merge will update the working directory */
598 if (!strcmp(arg, "-u")) {
599 update = 1;
600 continue;
601 }
602
438195cc
LT
603 /* This differs from "-m" in that we'll silently ignore unmerged entries */
604 if (!strcmp(arg, "--reset")) {
ee6566e8 605 if (stage || merge)
438195cc
LT
606 usage(read_tree_usage);
607 reset = 1;
608 merge = 1;
609 stage = 1;
610 read_cache_unmerged();
7875b50d 611 continue;
438195cc
LT
612 }
613
ee6566e8
DB
614 if (!strcmp(arg, "--head")) {
615 head_idx = stage - 1;
616 fn = threeway_merge;
617 }
618
d99082e0 619 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 620 if (!strcmp(arg, "-m")) {
ee6566e8 621 if (stage || merge)
438195cc
LT
622 usage(read_tree_usage);
623 if (read_cache_unmerged())
624 die("you need to resolve your current index first");
d99082e0 625 stage = 1;
ca016f0e 626 merge = 1;
83adac3c
LT
627 continue;
628 }
03efa6d9 629
3c249c95 630 if (get_sha1(arg, sha1) < 0)
c5bac17a 631 usage(read_tree_usage);
ee6566e8 632 if (list_tree(sha1) < 0)
2de381f9 633 die("failed to unpack tree object %s", arg);
d99082e0 634 stage++;
83adac3c 635 }
a57f0b58
LT
636 if (update && !merge)
637 usage(read_tree_usage);
ee6566e8
DB
638 if (merge && !fn) {
639 if (stage < 2)
a3a65234 640 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
641 switch (stage - 1) {
642 case 1:
643 fn = oneway_merge;
644 break;
645 case 2:
646 fn = twoway_merge;
647 break;
648 case 3:
649 fn = threeway_merge;
650 break;
651 default:
652 fn = threeway_merge;
653 break;
03efa6d9 654 }
ca016f0e 655 }
ee6566e8
DB
656
657 if (head_idx < 0) {
658 if (stage - 1 >= 3)
659 head_idx = stage - 2;
660 else
661 head_idx = 1;
662 }
663
664 unpack_trees(fn);
96cd5429
JH
665 if (write_cache(newfd, active_cache, active_nr) ||
666 commit_index_file(&cache_file))
2de381f9 667 die("unable to write new index file");
9614b8dc 668 return 0;
e83c5163 669}