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