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