read-trees: refactor the unpack_trees() part
[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"
16da134b 14#include "unpack-trees.h"
d147e501 15#include "builtin.h"
ee6566e8 16
ee6566e8
DB
17static struct object_list *trees = NULL;
18
ee6566e8 19static void reject_merge(struct cache_entry *ce)
43f91266 20{
16da134b 21 die("Entry '%s' would be overwritten by merge. Cannot merge.",
ee6566e8 22 ce->name);
43f91266
LT
23}
24
ee6566e8
DB
25static int list_tree(unsigned char *sha1)
26{
27 struct tree *tree = parse_tree_indirect(sha1);
28 if (!tree)
29 return -1;
30 object_list_append(&tree->object, &trees);
31 return 0;
32}
33
34static int same(struct cache_entry *a, struct cache_entry *b)
35{
36 if (!!a != !!b)
37 return 0;
38 if (!a && !b)
39 return 1;
40 return a->ce_mode == b->ce_mode &&
41 !memcmp(a->sha1, b->sha1, 20);
42}
43
44
02ede67a
LT
45/*
46 * When a CE gets turned into an unmerged entry, we
47 * want it to be up-to-date
48 */
16da134b
JS
49static void verify_uptodate(struct cache_entry *ce,
50 struct unpack_trees_options *o)
02ede67a
LT
51{
52 struct stat st;
53
16da134b 54 if (o->index_only || o->reset)
720d150c
JH
55 return;
56
02ede67a 57 if (!lstat(ce->name, &st)) {
5f73076c 58 unsigned changed = ce_match_stat(ce, &st, 1);
02ede67a
LT
59 if (!changed)
60 return;
61 errno = 0;
62 }
16da134b 63 if (o->reset) {
6d6776cb
LT
64 ce->ce_flags |= htons(CE_UPDATE);
65 return;
66 }
02ede67a
LT
67 if (errno == ENOENT)
68 return;
69 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
70}
71
b34c39cf
JH
72static void invalidate_ce_path(struct cache_entry *ce)
73{
74 if (ce)
75 cache_tree_invalidate_path(active_cache_tree, ce->name);
76}
77
fcc387db
JH
78/*
79 * We do not want to remove or overwrite a working tree file that
80 * is not tracked.
81 */
16da134b
JS
82static void verify_absent(const char *path, const char *action,
83 struct unpack_trees_options *o)
fcc387db
JH
84{
85 struct stat st;
86
16da134b 87 if (o->index_only || o->reset || !o->update)
fcc387db
JH
88 return;
89 if (!lstat(path, &st))
90 die("Untracked working tree file '%s' "
91 "would be %s by merge.", path, action);
92}
93
16da134b
JS
94static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
95 struct unpack_trees_options *o)
d99082e0 96{
d723c690
LT
97 merge->ce_flags |= htons(CE_UPDATE);
98 if (old) {
02ede67a 99 /*
d723c690
LT
100 * See if we can re-use the old CE directly?
101 * That way we get the uptodate stat info.
102 *
103 * This also removes the UPDATE flag on
104 * a match.
02ede67a 105 */
d723c690
LT
106 if (same(old, merge)) {
107 *merge = *old;
ee6566e8 108 } else {
16da134b 109 verify_uptodate(old, o);
b34c39cf 110 invalidate_ce_path(old);
d723c690 111 }
a3a65234 112 }
283c8eef 113 else {
16da134b 114 verify_absent(merge->name, "overwritten", o);
c2b9ae43 115 invalidate_ce_path(merge);
283c8eef 116 }
fcc387db 117
d723c690 118 merge->ce_flags &= ~htons(CE_STAGEMASK);
f8f0b1f0 119 add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
d723c690 120 return 1;
a3a65234
LT
121}
122
16da134b
JS
123static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
124 struct unpack_trees_options *o)
aa16021e
LT
125{
126 if (old)
16da134b 127 verify_uptodate(old, o);
fcc387db 128 else
16da134b 129 verify_absent(ce->name, "removed", o);
aa16021e 130 ce->ce_mode = 0;
f8f0b1f0 131 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
b34c39cf 132 invalidate_ce_path(ce);
aa16021e
LT
133 return 1;
134}
135
ee6566e8 136static int keep_entry(struct cache_entry *ce)
32192e66 137{
ee6566e8
DB
138 add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
139 return 1;
32192e66
JH
140}
141
4d3fe0c5
JH
142#if DBRT_DEBUG
143static void show_stage_entry(FILE *o,
144 const char *label, const struct cache_entry *ce)
145{
2ba6c47b
JH
146 if (!ce)
147 fprintf(o, "%s (missing)\n", label);
148 else
149 fprintf(o, "%s%06o %s %d\t%s\n",
150 label,
151 ntohl(ce->ce_mode),
152 sha1_to_hex(ce->sha1),
153 ce_stage(ce),
154 ce->name);
4d3fe0c5
JH
155}
156#endif
157
16da134b
JS
158static int threeway_merge(struct cache_entry **stages,
159 struct unpack_trees_options *o)
e6ee623b 160{
ee6566e8 161 struct cache_entry *index;
16da134b
JS
162 struct cache_entry *head;
163 struct cache_entry *remote = stages[o->head_idx + 1];
d723c690 164 int count;
ee6566e8
DB
165 int head_match = 0;
166 int remote_match = 0;
fcc387db 167 const char *path = NULL;
d723c690 168
ee6566e8
DB
169 int df_conflict_head = 0;
170 int df_conflict_remote = 0;
171
172 int any_anc_missing = 0;
1b1fdf8c 173 int no_anc_exists = 1;
ee6566e8
DB
174 int i;
175
16da134b 176 for (i = 1; i < o->head_idx; i++) {
ee6566e8
DB
177 if (!stages[i])
178 any_anc_missing = 1;
fcc387db
JH
179 else {
180 if (!path)
181 path = stages[i]->name;
1b1fdf8c 182 no_anc_exists = 0;
fcc387db 183 }
e7f9bc41 184 }
ee6566e8
DB
185
186 index = stages[0];
16da134b 187 head = stages[o->head_idx];
ee6566e8 188
16da134b 189 if (head == &o->df_conflict_entry) {
ee6566e8
DB
190 df_conflict_head = 1;
191 head = NULL;
192 }
193
16da134b 194 if (remote == &o->df_conflict_entry) {
ee6566e8
DB
195 df_conflict_remote = 1;
196 remote = NULL;
197 }
198
fcc387db
JH
199 if (!path && index)
200 path = index->name;
201 if (!path && head)
202 path = head->name;
203 if (!path && remote)
204 path = remote->name;
205
ee6566e8 206 /* First, if there's a #16 situation, note that to prevent #13
fcc387db 207 * and #14.
ee6566e8
DB
208 */
209 if (!same(remote, head)) {
16da134b 210 for (i = 1; i < o->head_idx; i++) {
ee6566e8 211 if (same(stages[i], head)) {
4d3fe0c5 212 head_match = i;
ee6566e8
DB
213 }
214 if (same(stages[i], remote)) {
4d3fe0c5 215 remote_match = i;
ee6566e8 216 }
32192e66 217 }
32192e66 218 }
ee6566e8
DB
219
220 /* We start with cases where the index is allowed to match
221 * something other than the head: #14(ALT) and #2ALT, where it
222 * is permitted to match the result instead.
223 */
224 /* #14, #14ALT, #2ALT */
225 if (remote && !df_conflict_head && head_match && !remote_match) {
226 if (index && !same(index, remote) && !same(index, head))
227 reject_merge(index);
16da134b 228 return merged_entry(remote, index, o);
036d51cc 229 }
d723c690 230 /*
ee6566e8
DB
231 * If we have an entry in the index cache, then we want to
232 * make sure that it matches head.
d723c690 233 */
ee6566e8
DB
234 if (index && !same(index, head)) {
235 reject_merge(index);
e6ee623b 236 }
ee6566e8
DB
237
238 if (head) {
239 /* #5ALT, #15 */
240 if (same(head, remote))
16da134b 241 return merged_entry(head, index, o);
ee6566e8
DB
242 /* #13, #3ALT */
243 if (!df_conflict_remote && remote_match && !head_match)
16da134b 244 return merged_entry(head, index, o);
ee6566e8
DB
245 }
246
247 /* #1 */
248 if (!head && !remote && any_anc_missing)
249 return 0;
250
1b1fdf8c
JH
251 /* Under the new "aggressive" rule, we resolve mostly trivial
252 * cases that we historically had git-merge-one-file resolve.
253 */
16da134b 254 if (o->aggressive) {
1b1fdf8c
JH
255 int head_deleted = !head && !df_conflict_head;
256 int remote_deleted = !remote && !df_conflict_remote;
257 /*
258 * Deleted in both.
259 * Deleted in one and unchanged in the other.
260 */
261 if ((head_deleted && remote_deleted) ||
262 (head_deleted && remote && remote_match) ||
11420380
JH
263 (remote_deleted && head && head_match)) {
264 if (index)
16da134b 265 return deleted_entry(index, index, o);
fcc387db 266 else if (path)
16da134b 267 verify_absent(path, "removed", o);
1b1fdf8c 268 return 0;
11420380 269 }
1b1fdf8c
JH
270 /*
271 * Added in both, identically.
272 */
273 if (no_anc_exists && head && remote && same(head, remote))
16da134b 274 return merged_entry(head, index, o);
1b1fdf8c
JH
275
276 }
277
ee6566e8
DB
278 /* Below are "no merge" cases, which require that the index be
279 * up-to-date to avoid the files getting overwritten with
16da134b 280 * conflict resolution files.
ee6566e8
DB
281 */
282 if (index) {
16da134b 283 verify_uptodate(index, o);
ee6566e8 284 }
fcc387db 285 else if (path)
16da134b 286 verify_absent(path, "overwritten", o);
ee6566e8 287
16da134b 288 o->nontrivial_merge = 1;
23822a35 289
ee6566e8 290 /* #2, #3, #4, #6, #7, #9, #11. */
d723c690 291 count = 0;
ee6566e8 292 if (!head_match || !remote_match) {
16da134b 293 for (i = 1; i < o->head_idx; i++) {
ee6566e8
DB
294 if (stages[i]) {
295 keep_entry(stages[i]);
296 count++;
297 break;
298 }
299 }
300 }
4d3fe0c5
JH
301#if DBRT_DEBUG
302 else {
303 fprintf(stderr, "read-tree: warning #16 detected\n");
304 show_stage_entry(stderr, "head ", stages[head_match]);
305 show_stage_entry(stderr, "remote ", stages[remote_match]);
306 }
307#endif
ee6566e8
DB
308 if (head) { count += keep_entry(head); }
309 if (remote) { count += keep_entry(remote); }
d723c690 310 return count;
e6ee623b
LT
311}
312
220a0b52
LT
313/*
314 * Two-way merge.
315 *
c8596009
JH
316 * The rule is to "carry forward" what is in the index without losing
317 * information across a "fast forward", favoring a successful merge
318 * over a merge failure when it makes sense. For details of the
319 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
320 *
220a0b52 321 */
16da134b
JS
322static int twoway_merge(struct cache_entry **src,
323 struct unpack_trees_options *o)
a3a65234 324{
c8596009
JH
325 struct cache_entry *current = src[0];
326 struct cache_entry *oldtree = src[1], *newtree = src[2];
220a0b52 327
16da134b 328 if (o->merge_size != 2)
bd2afde8 329 return error("Cannot do a twoway merge of %d trees",
16da134b 330 o->merge_size);
e6ee623b 331
c8596009
JH
332 if (current) {
333 if ((!oldtree && !newtree) || /* 4 and 5 */
334 (!oldtree && newtree &&
335 same(current, newtree)) || /* 6 and 7 */
336 (oldtree && newtree &&
337 same(oldtree, newtree)) || /* 14 and 15 */
338 (oldtree && newtree &&
339 !same(oldtree, newtree) && /* 18 and 19*/
340 same(current, newtree))) {
ee6566e8 341 return keep_entry(current);
c8596009
JH
342 }
343 else if (oldtree && !newtree && same(current, oldtree)) {
344 /* 10 or 11 */
16da134b 345 return deleted_entry(oldtree, current, o);
c8596009
JH
346 }
347 else if (oldtree && newtree &&
348 same(current, oldtree) && !same(current, newtree)) {
349 /* 20 or 21 */
16da134b 350 return merged_entry(newtree, current, o);
c8596009 351 }
ee6566e8 352 else {
c8596009 353 /* all other failures */
ee6566e8
DB
354 if (oldtree)
355 reject_merge(oldtree);
356 if (current)
357 reject_merge(current);
358 if (newtree)
359 reject_merge(newtree);
d723c690 360 return -1;
ee6566e8 361 }
e6ee623b 362 }
c8596009 363 else if (newtree)
16da134b 364 return merged_entry(newtree, current, o);
c8596009 365 else
16da134b 366 return deleted_entry(oldtree, current, o);
03efa6d9
JH
367}
368
f4c6f2d3
JH
369/*
370 * Bind merge.
371 *
372 * Keep the index entries at stage0, collapse stage1 but make sure
d6970e42 373 * stage0 does not have anything there.
f4c6f2d3 374 */
16da134b
JS
375static int bind_merge(struct cache_entry **src,
376 struct unpack_trees_options *o)
f4c6f2d3
JH
377{
378 struct cache_entry *old = src[0];
379 struct cache_entry *a = src[1];
380
16da134b 381 if (o->merge_size != 1)
f4c6f2d3 382 return error("Cannot do a bind merge of %d trees\n",
16da134b 383 o->merge_size);
d6970e42 384 if (a && old)
f4c6f2d3 385 die("Entry '%s' overlaps. Cannot bind.", a->name);
d6970e42
JH
386 if (!a)
387 return keep_entry(old);
388 else
16da134b 389 return merged_entry(a, NULL, o);
f4c6f2d3
JH
390}
391
d723c690
LT
392/*
393 * One-way merge.
394 *
395 * The rule is:
396 * - take the stat information from stage0, take the data from stage1
397 */
16da134b
JS
398static int oneway_merge(struct cache_entry **src,
399 struct unpack_trees_options *o)
220a0b52 400{
d723c690
LT
401 struct cache_entry *old = src[0];
402 struct cache_entry *a = src[1];
a3a65234 403
16da134b 404 if (o->merge_size != 1)
bd2afde8 405 return error("Cannot do a oneway merge of %d trees",
16da134b 406 o->merge_size);
a3a65234 407
d723c690 408 if (!a)
16da134b 409 return deleted_entry(old, old, o);
b5b42507 410 if (old && same(old, a)) {
16da134b 411 if (o->reset) {
6d6776cb
LT
412 struct stat st;
413 if (lstat(old->name, &st) ||
414 ce_match_stat(old, &st, 1))
415 old->ce_flags |= htons(CE_UPDATE);
416 }
ee6566e8 417 return keep_entry(old);
b5b42507 418 }
16da134b 419 return merged_entry(a, old, o);
d723c690
LT
420}
421
438195cc
LT
422static int read_cache_unmerged(void)
423{
b0d6e646 424 int i;
438195cc 425 struct cache_entry **dst;
b0d6e646 426 struct cache_entry *last = NULL;
438195cc
LT
427
428 read_cache();
429 dst = active_cache;
438195cc
LT
430 for (i = 0; i < active_nr; i++) {
431 struct cache_entry *ce = active_cache[i];
432 if (ce_stage(ce)) {
b0d6e646
JH
433 if (last && !strcmp(ce->name, last->name))
434 continue;
b34c39cf 435 invalidate_ce_path(ce);
b0d6e646
JH
436 last = ce;
437 ce->ce_mode = 0;
438 ce->ce_flags &= ~htons(CE_STAGEMASK);
438195cc 439 }
b0d6e646 440 *dst++ = ce;
438195cc 441 }
b0d6e646
JH
442 active_nr = dst - active_cache;
443 return !!last;
438195cc
LT
444}
445
7927a55d
JH
446static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
447{
1ccf5a34 448 struct tree_desc desc;
4c068a98 449 struct name_entry entry;
7927a55d 450 int cnt;
283c8eef 451
7927a55d 452 memcpy(it->sha1, tree->object.sha1, 20);
1ccf5a34
LT
453 desc.buf = tree->buffer;
454 desc.size = tree->size;
455 cnt = 0;
4c068a98
LT
456 while (tree_entry(&desc, &entry)) {
457 if (!S_ISDIR(entry.mode))
7927a55d
JH
458 cnt++;
459 else {
460 struct cache_tree_sub *sub;
4c068a98 461 struct tree *subtree = lookup_tree(entry.sha1);
7927a55d
JH
462 if (!subtree->object.parsed)
463 parse_tree(subtree);
4c068a98 464 sub = cache_tree_sub(it, entry.path);
7927a55d
JH
465 sub->cache_tree = cache_tree();
466 prime_cache_tree_rec(sub->cache_tree, subtree);
467 cnt += sub->cache_tree->entry_count;
468 }
469 }
470 it->entry_count = cnt;
471}
472
473static void prime_cache_tree(void)
474{
475 struct tree *tree = (struct tree *)trees->item;
476 if (!tree)
477 return;
478 active_cache_tree = cache_tree();
479 prime_cache_tree_rec(active_cache_tree, tree);
480
481}
482
f4c6f2d3 483static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
c5bac17a 484
021b6e45 485static struct lock_file lock_file;
96cd5429 486
a633fca0 487int cmd_read_tree(int argc, const char **argv, const char *prefix)
e83c5163 488{
6d6776cb 489 int i, newfd, stage = 0;
e83c5163 490 unsigned char sha1[20];
16da134b 491 struct unpack_trees_options opts;
bb233d69 492
16da134b
JS
493 memset(&opts, 0, sizeof(opts));
494 opts.head_idx = -1;
344c52ae 495
16da134b 496 setup_git_directory();
84a9b58c 497 git_config(git_default_config);
53228a5f 498
021b6e45 499 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
83adac3c 500 if (newfd < 0)
021b6e45 501 die("unable to create new index file");
83adac3c 502
39b4ac99
AR
503 git_config(git_default_config);
504
83adac3c
LT
505 for (i = 1; i < argc; i++) {
506 const char *arg = argv[i];
507
720d150c
JH
508 /* "-u" means "update", meaning that a merge will update
509 * the working tree.
510 */
220a0b52 511 if (!strcmp(arg, "-u")) {
16da134b 512 opts.update = 1;
220a0b52
LT
513 continue;
514 }
515
744633cb 516 if (!strcmp(arg, "-v")) {
16da134b 517 opts.verbose_update = 1;
744633cb
JH
518 continue;
519 }
520
720d150c
JH
521 /* "-i" means "index only", meaning that a merge will
522 * not even look at the working tree.
523 */
524 if (!strcmp(arg, "-i")) {
16da134b 525 opts.index_only = 1;
720d150c
JH
526 continue;
527 }
528
f4c6f2d3
JH
529 /* "--prefix=<subdirectory>/" means keep the current index
530 * entries and put the entries from the tree under the
531 * given subdirectory.
532 */
533 if (!strncmp(arg, "--prefix=", 9)) {
16da134b 534 if (stage || opts.merge || opts.prefix)
f4c6f2d3 535 usage(read_tree_usage);
16da134b
JS
536 opts.prefix = arg + 9;
537 opts.merge = 1;
f4c6f2d3
JH
538 stage = 1;
539 if (read_cache_unmerged())
540 die("you need to resolve your current index first");
541 continue;
542 }
543
b0d6e646
JH
544 /* This differs from "-m" in that we'll silently ignore
545 * unmerged entries and overwrite working tree files that
546 * correspond to them.
547 */
438195cc 548 if (!strcmp(arg, "--reset")) {
16da134b 549 if (stage || opts.merge || opts.prefix)
438195cc 550 usage(read_tree_usage);
16da134b
JS
551 opts.reset = 1;
552 opts.merge = 1;
438195cc
LT
553 stage = 1;
554 read_cache_unmerged();
7875b50d 555 continue;
438195cc
LT
556 }
557
23822a35 558 if (!strcmp(arg, "--trivial")) {
16da134b 559 opts.trivial_merges_only = 1;
23822a35
LT
560 continue;
561 }
562
1b1fdf8c 563 if (!strcmp(arg, "--aggressive")) {
16da134b 564 opts.aggressive = 1;
1b1fdf8c
JH
565 continue;
566 }
567
d99082e0 568 /* "-m" stands for "merge", meaning we start in stage 1 */
83adac3c 569 if (!strcmp(arg, "-m")) {
16da134b 570 if (stage || opts.merge || opts.prefix)
438195cc
LT
571 usage(read_tree_usage);
572 if (read_cache_unmerged())
573 die("you need to resolve your current index first");
d99082e0 574 stage = 1;
16da134b 575 opts.merge = 1;
83adac3c
LT
576 continue;
577 }
03efa6d9 578
720d150c 579 /* using -u and -i at the same time makes no sense */
16da134b 580 if (1 < opts.index_only + opts.update)
720d150c
JH
581 usage(read_tree_usage);
582
31fff305
DL
583 if (get_sha1(arg, sha1))
584 die("Not a valid object name %s", arg);
ee6566e8 585 if (list_tree(sha1) < 0)
2de381f9 586 die("failed to unpack tree object %s", arg);
d99082e0 587 stage++;
83adac3c 588 }
16da134b 589 if ((opts.update||opts.index_only) && !opts.merge)
a57f0b58 590 usage(read_tree_usage);
7dd43575 591
16da134b
JS
592 if (opts.prefix) {
593 int pfxlen = strlen(opts.prefix);
f4c6f2d3 594 int pos;
16da134b 595 if (opts.prefix[pfxlen-1] != '/')
f4c6f2d3
JH
596 die("prefix must end with /");
597 if (stage != 2)
598 die("binding merge takes only one tree");
16da134b 599 pos = cache_name_pos(opts.prefix, pfxlen);
f4c6f2d3
JH
600 if (0 <= pos)
601 die("corrupt index file");
602 pos = -pos-1;
603 if (pos < active_nr &&
16da134b
JS
604 !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
605 die("subdirectory '%s' already exists.", opts.prefix);
606 pos = cache_name_pos(opts.prefix, pfxlen-1);
f4c6f2d3 607 if (0 <= pos)
16da134b
JS
608 die("file '%.*s' already exists.",
609 pfxlen-1, opts.prefix);
f4c6f2d3
JH
610 }
611
16da134b 612 if (opts.merge) {
ee6566e8 613 if (stage < 2)
a3a65234 614 die("just how do you expect me to merge %d trees?", stage-1);
ee6566e8
DB
615 switch (stage - 1) {
616 case 1:
16da134b 617 opts.fn = opts.prefix ? bind_merge : oneway_merge;
ee6566e8
DB
618 break;
619 case 2:
16da134b 620 opts.fn = twoway_merge;
ee6566e8
DB
621 break;
622 case 3:
ee6566e8 623 default:
16da134b 624 opts.fn = threeway_merge;
b34c39cf 625 cache_tree_free(&active_cache_tree);
ee6566e8 626 break;
03efa6d9 627 }
ee6566e8 628
ee6566e8 629 if (stage - 1 >= 3)
16da134b 630 opts.head_idx = stage - 2;
ee6566e8 631 else
16da134b 632 opts.head_idx = 1;
ee6566e8
DB
633 }
634
16da134b 635 unpack_trees(trees, &opts);
7927a55d
JH
636
637 /*
638 * When reading only one tree (either the most basic form,
639 * "-m ent" or "--reset ent" form), we can obtain a fully
640 * valid cache-tree because the index must match exactly
641 * what came from the tree.
642 */
16da134b 643 if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
b6c4a480 644 cache_tree_free(&active_cache_tree);
7927a55d
JH
645 prime_cache_tree();
646 }
647
96cd5429 648 if (write_cache(newfd, active_cache, active_nr) ||
6244b249 649 close(newfd) || commit_lock_file(&lock_file))
2de381f9 650 die("unable to write new index file");
9614b8dc 651 return 0;
e83c5163 652}