]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-read-tree.c
Merge branch 'jc/waitpid' into next
[thirdparty/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
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;
eff97e3f 379 progress_update = 0;
744633cb
JH
380 }
381 }
382 }
ee6566e8
DB
383 if (!ce->ce_mode) {
384 if (update)
340e4f88 385 unlink_entry(ce->name);
ee6566e8
DB
386 continue;
387 }
388 if (ce->ce_flags & mask) {
389 ce->ce_flags &= ~mask;
390 if (update)
de84f99c 391 checkout_entry(ce, &state, NULL);
ee6566e8
DB
392 }
393 }
744633cb 394 if (total) {
744633cb 395 signal(SIGALRM, SIG_IGN);
72fdfb50 396 fputc('\n', stderr);
744633cb 397 }
ee6566e8 398}
43f91266 399
ee6566e8 400static int unpack_trees(merge_fn_t fn)
d99082e0 401{
ee6566e8
DB
402 int indpos = 0;
403 unsigned len = object_list_length(trees);
7e4a2a84 404 struct tree_entry_list **posns;
ee6566e8
DB
405 int i;
406 struct object_list *posn = trees;
407 merge_size = len;
7e4a2a84
JH
408
409 if (len) {
410 posns = xmalloc(len * sizeof(struct tree_entry_list *));
411 for (i = 0; i < len; i++) {
2d9c58c6 412 posns[i] = create_tree_entry_list((struct tree *) posn->item);
7e4a2a84
JH
413 posn = posn->next;
414 }
f4c6f2d3
JH
415 if (unpack_trees_rec(posns, len, prefix ? prefix : "",
416 fn, &indpos))
7e4a2a84 417 return -1;
d723c690 418 }
ee6566e8 419
23822a35
LT
420 if (trivial_merges_only && nontrivial_merge)
421 die("Merge requires file-level merging");
422
ee6566e8
DB
423 check_updates(active_cache, active_nr);
424 return 0;
d99082e0
LT
425}
426
ee6566e8
DB
427static int list_tree(unsigned char *sha1)
428{
429 struct tree *tree = parse_tree_indirect(sha1);
430 if (!tree)
431 return -1;
432 object_list_append(&tree->object, &trees);
433 return 0;
434}
435
436static int same(struct cache_entry *a, struct cache_entry *b)
437{
438 if (!!a != !!b)
439 return 0;
440 if (!a && !b)
441 return 1;
442 return a->ce_mode == b->ce_mode &&
443 !memcmp(a->sha1, b->sha1, 20);
444}
445
446
02ede67a
LT
447/*
448 * When a CE gets turned into an unmerged entry, we
449 * want it to be up-to-date
450 */
451static void verify_uptodate(struct cache_entry *ce)
452{
453 struct stat st;
454
fcc387db 455 if (index_only || reset)
720d150c
JH
456 return;
457
02ede67a 458 if (!lstat(ce->name, &st)) {
5f73076c 459 unsigned changed = ce_match_stat(ce, &st, 1);
02ede67a
LT
460 if (!changed)
461 return;
462 errno = 0;
463 }
6d6776cb
LT
464 if (reset) {
465 ce->ce_flags |= htons(CE_UPDATE);
466 return;
467 }
02ede67a
LT
468 if (errno == ENOENT)
469 return;
470 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
471}
472
b34c39cf
JH
473static void invalidate_ce_path(struct cache_entry *ce)
474{
475 if (ce)
476 cache_tree_invalidate_path(active_cache_tree, ce->name);
477}
478
fcc387db
JH
479/*
480 * We do not want to remove or overwrite a working tree file that
481 * is not tracked.
482 */
483static void verify_absent(const char *path, const char *action)
484{
485 struct stat st;
486
487 if (index_only || reset || !update)
488 return;
489 if (!lstat(path, &st))
490 die("Untracked working tree file '%s' "
491 "would be %s by merge.", path, action);
492}
493
ee6566e8 494static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
d99082e0 495{
d723c690
LT
496 merge->ce_flags |= htons(CE_UPDATE);
497 if (old) {
02ede67a 498 /*
d723c690
LT
499 * See if we can re-use the old CE directly?
500 * That way we get the uptodate stat info.
501 *
502 * This also removes the UPDATE flag on
503 * a match.
02ede67a 504 */
d723c690
LT
505 if (same(old, merge)) {
506 *merge = *old;
ee6566e8 507 } else {
d723c690 508 verify_uptodate(old);
b34c39cf 509 invalidate_ce_path(old);
d723c690 510 }
a3a65234 511 }
283c8eef 512 else {
fcc387db 513 verify_absent(merge->name, "overwritten");
c2b9ae43 514 invalidate_ce_path(merge);
283c8eef 515 }
fcc387db 516
d723c690 517 merge->ce_flags &= ~htons(CE_STAGEMASK);
ee6566e8 518 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
d723c690 519 return 1;
a3a65234
LT
520}
521
ee6566e8 522static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
aa16021e
LT
523{
524 if (old)
525 verify_uptodate(old);
fcc387db
JH
526 else
527 verify_absent(ce->name, "removed");
aa16021e 528 ce->ce_mode = 0;
ee6566e8 529 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
b34c39cf 530 invalidate_ce_path(ce);
aa16021e
LT
531 return 1;
532}
533
ee6566e8 534static int keep_entry(struct cache_entry *ce)
32192e66 535{
ee6566e8
DB
536 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
537 return 1;
32192e66
JH
538}
539
4d3fe0c5
JH
540#if DBRT_DEBUG
541static void show_stage_entry(FILE *o,
542 const char *label, const struct cache_entry *ce)
543{
2ba6c47b
JH
544 if (!ce)
545 fprintf(o, "%s (missing)\n", label);
546 else
547 fprintf(o, "%s%06o %s %d\t%s\n",
548 label,
549 ntohl(ce->ce_mode),
550 sha1_to_hex(ce->sha1),
551 ce_stage(ce),
552 ce->name);
4d3fe0c5
JH
553}
554#endif
555
ee6566e8 556static int threeway_merge(struct cache_entry **stages)
e6ee623b 557{
ee6566e8
DB
558 struct cache_entry *index;
559 struct cache_entry *head;
560 struct cache_entry *remote = stages[head_idx + 1];
d723c690 561 int count;
ee6566e8
DB
562 int head_match = 0;
563 int remote_match = 0;
fcc387db 564 const char *path = NULL;
d723c690 565
ee6566e8
DB
566 int df_conflict_head = 0;
567 int df_conflict_remote = 0;
568
569 int any_anc_missing = 0;
1b1fdf8c 570 int no_anc_exists = 1;
ee6566e8
DB
571 int i;
572
573 for (i = 1; i < head_idx; i++) {
574 if (!stages[i])
575 any_anc_missing = 1;
fcc387db
JH
576 else {
577 if (!path)
578 path = stages[i]->name;
1b1fdf8c 579 no_anc_exists = 0;
fcc387db 580 }
e7f9bc41 581 }
ee6566e8
DB
582
583 index = stages[0];
584 head = stages[head_idx];
585
586 if (head == &df_conflict_entry) {
587 df_conflict_head = 1;
588 head = NULL;
589 }
590
591 if (remote == &df_conflict_entry) {
592 df_conflict_remote = 1;
593 remote = NULL;
594 }
595
fcc387db
JH
596 if (!path && index)
597 path = index->name;
598 if (!path && head)
599 path = head->name;
600 if (!path && remote)
601 path = remote->name;
602
ee6566e8 603 /* First, if there's a #16 situation, note that to prevent #13
fcc387db 604 * and #14.
ee6566e8
DB
605 */
606 if (!same(remote, head)) {
607 for (i = 1; i < head_idx; i++) {
608 if (same(stages[i], head)) {
4d3fe0c5 609 head_match = i;
ee6566e8
DB
610 }
611 if (same(stages[i], remote)) {
4d3fe0c5 612 remote_match = i;
ee6566e8 613 }
32192e66 614 }
32192e66 615 }
ee6566e8
DB
616
617 /* We start with cases where the index is allowed to match
618 * something other than the head: #14(ALT) and #2ALT, where it
619 * is permitted to match the result instead.
620 */
621 /* #14, #14ALT, #2ALT */
622 if (remote && !df_conflict_head && head_match && !remote_match) {
623 if (index && !same(index, remote) && !same(index, head))
624 reject_merge(index);
625 return merged_entry(remote, index);
036d51cc 626 }
d723c690 627 /*
ee6566e8
DB
628 * If we have an entry in the index cache, then we want to
629 * make sure that it matches head.
d723c690 630 */
ee6566e8
DB
631 if (index && !same(index, head)) {
632 reject_merge(index);
e6ee623b 633 }
ee6566e8
DB
634
635 if (head) {
636 /* #5ALT, #15 */
637 if (same(head, remote))
638 return merged_entry(head, index);
639 /* #13, #3ALT */
640 if (!df_conflict_remote && remote_match && !head_match)
641 return merged_entry(head, index);
642 }
643
644 /* #1 */
645 if (!head && !remote && any_anc_missing)
646 return 0;
647
1b1fdf8c
JH
648 /* Under the new "aggressive" rule, we resolve mostly trivial
649 * cases that we historically had git-merge-one-file resolve.
650 */
651 if (aggressive) {
652 int head_deleted = !head && !df_conflict_head;
653 int remote_deleted = !remote && !df_conflict_remote;
654 /*
655 * Deleted in both.
656 * Deleted in one and unchanged in the other.
657 */
658 if ((head_deleted && remote_deleted) ||
659 (head_deleted && remote && remote_match) ||
11420380
JH
660 (remote_deleted && head && head_match)) {
661 if (index)
662 return deleted_entry(index, index);
fcc387db
JH
663 else if (path)
664 verify_absent(path, "removed");
1b1fdf8c 665 return 0;
11420380 666 }
1b1fdf8c
JH
667 /*
668 * Added in both, identically.
669 */
670 if (no_anc_exists && head && remote && same(head, remote))
671 return merged_entry(head, index);
672
673 }
674
ee6566e8
DB
675 /* Below are "no merge" cases, which require that the index be
676 * up-to-date to avoid the files getting overwritten with
677 * conflict resolution files.
678 */
679 if (index) {
680 verify_uptodate(index);
681 }
fcc387db
JH
682 else if (path)
683 verify_absent(path, "overwritten");
ee6566e8 684
23822a35
LT
685 nontrivial_merge = 1;
686
ee6566e8 687 /* #2, #3, #4, #6, #7, #9, #11. */
d723c690 688 count = 0;
ee6566e8
DB
689 if (!head_match || !remote_match) {
690 for (i = 1; i < head_idx; i++) {
691 if (stages[i]) {
692 keep_entry(stages[i]);
693 count++;
694 break;
695 }
696 }
697 }
4d3fe0c5
JH
698#if DBRT_DEBUG
699 else {
700 fprintf(stderr, "read-tree: warning #16 detected\n");
701 show_stage_entry(stderr, "head ", stages[head_match]);
702 show_stage_entry(stderr, "remote ", stages[remote_match]);
703 }
704#endif
ee6566e8
DB
705 if (head) { count += keep_entry(head); }
706 if (remote) { count += keep_entry(remote); }
d723c690 707 return count;
e6ee623b
LT
708}
709
220a0b52
LT
710/*
711 * Two-way merge.
712 *
c8596009
JH
713 * The rule is to "carry forward" what is in the index without losing
714 * information across a "fast forward", favoring a successful merge
715 * over a merge failure when it makes sense. For details of the
716 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
717 *
220a0b52 718 */
ee6566e8 719static int twoway_merge(struct cache_entry **src)
a3a65234 720{
c8596009
JH
721 struct cache_entry *current = src[0];
722 struct cache_entry *oldtree = src[1], *newtree = src[2];
220a0b52 723
ee6566e8 724 if (merge_size != 2)
bd2afde8 725 return error("Cannot do a twoway merge of %d trees",
ee6566e8 726 merge_size);
e6ee623b 727
c8596009
JH
728 if (current) {
729 if ((!oldtree && !newtree) || /* 4 and 5 */
730 (!oldtree && newtree &&
731 same(current, newtree)) || /* 6 and 7 */
732 (oldtree && newtree &&
733 same(oldtree, newtree)) || /* 14 and 15 */
734 (oldtree && newtree &&
735 !same(oldtree, newtree) && /* 18 and 19*/
736 same(current, newtree))) {
ee6566e8 737 return keep_entry(current);
c8596009
JH
738 }
739 else if (oldtree && !newtree && same(current, oldtree)) {
740 /* 10 or 11 */
ee6566e8 741 return deleted_entry(oldtree, current);
c8596009
JH
742 }
743 else if (oldtree && newtree &&
744 same(current, oldtree) && !same(current, newtree)) {
745 /* 20 or 21 */
ee6566e8 746 return merged_entry(newtree, current);
c8596009 747 }
ee6566e8 748 else {
c8596009 749 /* all other failures */
ee6566e8
DB
750 if (oldtree)
751 reject_merge(oldtree);
752 if (current)
753 reject_merge(current);
754 if (newtree)
755 reject_merge(newtree);
d723c690 756 return -1;
ee6566e8 757 }
e6ee623b 758 }
c8596009 759 else if (newtree)
ee6566e8 760 return merged_entry(newtree, current);
c8596009 761 else
ee6566e8 762 return deleted_entry(oldtree, current);
03efa6d9
JH
763}
764
f4c6f2d3
JH
765/*
766 * Bind merge.
767 *
768 * Keep the index entries at stage0, collapse stage1 but make sure
d6970e42 769 * stage0 does not have anything there.
f4c6f2d3
JH
770 */
771static int bind_merge(struct cache_entry **src)
772{
773 struct cache_entry *old = src[0];
774 struct cache_entry *a = src[1];
775
776 if (merge_size != 1)
777 return error("Cannot do a bind merge of %d trees\n",
778 merge_size);
d6970e42 779 if (a && old)
f4c6f2d3 780 die("Entry '%s' overlaps. Cannot bind.", a->name);
d6970e42
JH
781 if (!a)
782 return keep_entry(old);
783 else
784 return merged_entry(a, NULL);
f4c6f2d3
JH
785}
786
d723c690
LT
787/*
788 * One-way merge.
789 *
790 * The rule is:
791 * - take the stat information from stage0, take the data from stage1
792 */
ee6566e8 793static int oneway_merge(struct cache_entry **src)
220a0b52 794{
d723c690
LT
795 struct cache_entry *old = src[0];
796 struct cache_entry *a = src[1];
a3a65234 797
ee6566e8 798 if (merge_size != 1)
bd2afde8 799 return error("Cannot do a oneway merge of %d trees",
ee6566e8 800 merge_size);
a3a65234 801
d723c690 802 if (!a)
fcc387db 803 return deleted_entry(old, old);
b5b42507 804 if (old && same(old, a)) {
6d6776cb
LT
805 if (reset) {
806 struct stat st;
807 if (lstat(old->name, &st) ||
808 ce_match_stat(old, &st, 1))
809 old->ce_flags |= htons(CE_UPDATE);
810 }
ee6566e8 811 return keep_entry(old);
b5b42507 812 }
fcc387db 813 return merged_entry(a, old);
d723c690
LT
814}
815
438195cc
LT
816static int read_cache_unmerged(void)
817{
b0d6e646 818 int i;
438195cc 819 struct cache_entry **dst;
b0d6e646 820 struct cache_entry *last = NULL;
438195cc
LT
821
822 read_cache();
823 dst = active_cache;
438195cc
LT
824 for (i = 0; i < active_nr; i++) {
825 struct cache_entry *ce = active_cache[i];
826 if (ce_stage(ce)) {
b0d6e646
JH
827 if (last && !strcmp(ce->name, last->name))
828 continue;
b34c39cf 829 invalidate_ce_path(ce);
b0d6e646
JH
830 last = ce;
831 ce->ce_mode = 0;
832 ce->ce_flags &= ~htons(CE_STAGEMASK);
438195cc 833 }
b0d6e646 834 *dst++ = ce;
438195cc 835 }
b0d6e646
JH
836 active_nr = dst - active_cache;
837 return !!last;
438195cc
LT
838}
839
7927a55d
JH
840static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
841{
1ccf5a34 842 struct tree_desc desc;
4c068a98 843 struct name_entry entry;
7927a55d 844 int cnt;
283c8eef 845
7927a55d 846 memcpy(it->sha1, tree->object.sha1, 20);
1ccf5a34
LT
847 desc.buf = tree->buffer;
848 desc.size = tree->size;
849 cnt = 0;
4c068a98
LT
850 while (tree_entry(&desc, &entry)) {
851 if (!S_ISDIR(entry.mode))
7927a55d
JH
852 cnt++;
853 else {
854 struct cache_tree_sub *sub;
4c068a98 855 struct tree *subtree = lookup_tree(entry.sha1);
7927a55d
JH
856 if (!subtree->object.parsed)
857 parse_tree(subtree);
4c068a98 858 sub = cache_tree_sub(it, entry.path);
7927a55d
JH
859 sub->cache_tree = cache_tree();
860 prime_cache_tree_rec(sub->cache_tree, subtree);
861 cnt += sub->cache_tree->entry_count;
862 }
863 }
864 it->entry_count = cnt;
865}
866
867static void prime_cache_tree(void)
868{
869 struct tree *tree = (struct tree *)trees->item;
870 if (!tree)
871 return;
872 active_cache_tree = cache_tree();
873 prime_cache_tree_rec(active_cache_tree, tree);
874
875}
876
f4c6f2d3 877static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
c5bac17a 878
021b6e45 879static struct lock_file lock_file;
96cd5429 880
d147e501 881int cmd_read_tree(int argc, const char **argv, char **envp)
e83c5163 882{
6d6776cb 883 int i, newfd, stage = 0;
e83c5163 884 unsigned char sha1[20];
ee6566e8 885 merge_fn_t fn = NULL;
bb233d69 886
53228a5f 887 setup_git_directory();
84a9b58c 888 git_config(git_default_config);
53228a5f 889
021b6e45 890 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
83adac3c 891 if (newfd < 0)
021b6e45 892 die("unable to create new index file");
83adac3c 893
39b4ac99
AR
894 git_config(git_default_config);
895
ca016f0e 896 merge = 0;
438195cc 897 reset = 0;
83adac3c
LT
898 for (i = 1; i < argc; i++) {
899 const char *arg = argv[i];
900
720d150c
JH
901 /* "-u" means "update", meaning that a merge will update
902 * the working tree.
903 */
220a0b52
LT
904 if (!strcmp(arg, "-u")) {
905 update = 1;
906 continue;
907 }
908
744633cb
JH
909 if (!strcmp(arg, "-v")) {
910 verbose_update = 1;
911 continue;
912 }
913
720d150c
JH
914 /* "-i" means "index only", meaning that a merge will
915 * not even look at the working tree.
916 */
917 if (!strcmp(arg, "-i")) {
918 index_only = 1;
919 continue;
920 }
921
f4c6f2d3
JH
922 /* "--prefix=<subdirectory>/" means keep the current index
923 * entries and put the entries from the tree under the
924 * given subdirectory.
925 */
926 if (!strncmp(arg, "--prefix=", 9)) {
927 if (stage || merge || prefix)
928 usage(read_tree_usage);
929 prefix = arg + 9;
930 merge = 1;
931 stage = 1;
932 if (read_cache_unmerged())
933 die("you need to resolve your current index first");
934 continue;
935 }
936
b0d6e646
JH
937 /* This differs from "-m" in that we'll silently ignore
938 * unmerged entries and overwrite working tree files that
939 * correspond to them.
940 */
438195cc 941 if (!strcmp(arg, "--reset")) {
f4c6f2d3 942 if (stage || merge || prefix)
438195cc
LT
943 usage(read_tree_usage);
944 reset = 1;
945 merge = 1;
946 stage = 1;
947 read_cache_unmerged();
7875b50d 948 continue;
438195cc
LT
949 }
950
23822a35
LT
951 if (!strcmp(arg, "--trivial")) {
952 trivial_merges_only = 1;
953 continue;
954 }
955
1b1fdf8c
JH
956 if (!strcmp(arg, "--aggressive")) {
957 aggressive = 1;
958 continue;
959 }
960
d99082e0 961 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 962 if (!strcmp(arg, "-m")) {
f4c6f2d3 963 if (stage || merge || prefix)
438195cc
LT
964 usage(read_tree_usage);
965 if (read_cache_unmerged())
966 die("you need to resolve your current index first");
d99082e0 967 stage = 1;
ca016f0e 968 merge = 1;
83adac3c
LT
969 continue;
970 }
03efa6d9 971
720d150c
JH
972 /* using -u and -i at the same time makes no sense */
973 if (1 < index_only + update)
974 usage(read_tree_usage);
975
31fff305
DL
976 if (get_sha1(arg, sha1))
977 die("Not a valid object name %s", arg);
ee6566e8 978 if (list_tree(sha1) < 0)
2de381f9 979 die("failed to unpack tree object %s", arg);
d99082e0 980 stage++;
83adac3c 981 }
f318dd22 982 if ((update||index_only) && !merge)
a57f0b58 983 usage(read_tree_usage);
7dd43575 984
f4c6f2d3
JH
985 if (prefix) {
986 int pfxlen = strlen(prefix);
987 int pos;
988 if (prefix[pfxlen-1] != '/')
989 die("prefix must end with /");
990 if (stage != 2)
991 die("binding merge takes only one tree");
992 pos = cache_name_pos(prefix, pfxlen);
993 if (0 <= pos)
994 die("corrupt index file");
995 pos = -pos-1;
996 if (pos < active_nr &&
997 !strncmp(active_cache[pos]->name, prefix, pfxlen))
998 die("subdirectory '%s' already exists.", prefix);
999 pos = cache_name_pos(prefix, pfxlen-1);
1000 if (0 <= pos)
1001 die("file '%.*s' already exists.", pfxlen-1, prefix);
1002 }
1003
7dd43575 1004 if (merge) {
ee6566e8 1005 if (stage < 2)
a3a65234 1006 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
1007 switch (stage - 1) {
1008 case 1:
f4c6f2d3 1009 fn = prefix ? bind_merge : oneway_merge;
ee6566e8
DB
1010 break;
1011 case 2:
1012 fn = twoway_merge;
1013 break;
1014 case 3:
ee6566e8
DB
1015 default:
1016 fn = threeway_merge;
b34c39cf 1017 cache_tree_free(&active_cache_tree);
ee6566e8 1018 break;
03efa6d9 1019 }
ee6566e8 1020
ee6566e8
DB
1021 if (stage - 1 >= 3)
1022 head_idx = stage - 2;
1023 else
1024 head_idx = 1;
1025 }
1026
1027 unpack_trees(fn);
7927a55d
JH
1028
1029 /*
1030 * When reading only one tree (either the most basic form,
1031 * "-m ent" or "--reset ent" form), we can obtain a fully
1032 * valid cache-tree because the index must match exactly
1033 * what came from the tree.
1034 */
75c3a5cc 1035 if (trees && trees->item && !prefix && (!merge || (stage == 2))) {
b6c4a480 1036 cache_tree_free(&active_cache_tree);
7927a55d
JH
1037 prime_cache_tree();
1038 }
1039
96cd5429 1040 if (write_cache(newfd, active_cache, active_nr) ||
021b6e45 1041 commit_lock_file(&lock_file))
2de381f9 1042 die("unable to write new index file");
9614b8dc 1043 return 0;
e83c5163 1044}