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