Avoid some unnecessary lstat() calls
[git/git.git] / unpack-trees.c
CommitLineData
bc052d7f 1#define NO_THE_INDEX_COMPATIBILITY_MACROS
16da134b 2#include "cache.h"
f8a9d428 3#include "dir.h"
16da134b
JS
4#include "tree.h"
5#include "tree-walk.h"
076b0adc 6#include "cache-tree.h"
16da134b 7#include "unpack-trees.h"
96a02f8f 8#include "progress.h"
0cf73755 9#include "refs.h"
16da134b 10
34110cd4
LT
11static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
12 unsigned int set, unsigned int clear)
b48d5a05 13{
34110cd4
LT
14 unsigned int size = ce_size(ce);
15 struct cache_entry *new = xmalloc(size);
16
17 clear |= CE_HASHED | CE_UNHASHED;
18
19 memcpy(new, ce, size);
20 new->next = NULL;
21 new->ce_flags = (new->ce_flags & ~clear) | set;
22 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
b48d5a05
LT
23}
24
16da134b
JS
25/* Unlink the last component and attempt to remove leading
26 * directories, in case this unlink is the removal of the
27 * last entry in the directory -- empty directories are removed.
28 */
16a4c617 29static void unlink_entry(char *name, char *last_symlink)
16da134b
JS
30{
31 char *cp, *prev;
32
16a4c617
JH
33 if (has_symlink_leading_path(name, last_symlink))
34 return;
16da134b
JS
35 if (unlink(name))
36 return;
37 prev = NULL;
38 while (1) {
39 int status;
40 cp = strrchr(name, '/');
41 if (prev)
42 *prev = '/';
43 if (!cp)
44 break;
45
46 *cp = 0;
47 status = rmdir(name);
48 if (status) {
49 *cp = '/';
50 break;
51 }
52 prev = cp;
53 }
54}
55
16da134b 56static struct checkout state;
c4758d3c 57static int check_updates(struct unpack_trees_options *o)
16da134b 58{
96a02f8f 59 unsigned cnt = 0, total = 0;
dc6a0757 60 struct progress *progress = NULL;
16a4c617 61 char last_symlink[PATH_MAX];
34110cd4 62 struct index_state *index = &o->result;
33ecf7eb 63 int i;
c4758d3c 64 int errs = 0;
16da134b
JS
65
66 if (o->update && o->verbose_update) {
34110cd4
LT
67 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
68 struct cache_entry *ce = index->cache[cnt];
7a51ed66 69 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE))
16da134b
JS
70 total++;
71 }
72
dc6a0757 73 progress = start_progress_delay("Checking out files",
e8548645 74 total, 50, 1);
16da134b
JS
75 cnt = 0;
76 }
77
16a4c617 78 *last_symlink = '\0';
34110cd4
LT
79 for (i = 0; i < index->cache_nr; i++) {
80 struct cache_entry *ce = index->cache[i];
16da134b 81
7a51ed66 82 if (ce->ce_flags & CE_REMOVE) {
1fa6ead4 83 display_progress(progress, ++cnt);
16da134b 84 if (o->update)
16a4c617 85 unlink_entry(ce->name, last_symlink);
34110cd4 86 remove_index_entry_at(&o->result, i);
33ecf7eb 87 i--;
16da134b
JS
88 continue;
89 }
1fa6ead4
LT
90 }
91
92 for (i = 0; i < index->cache_nr; i++) {
93 struct cache_entry *ce = index->cache[i];
94
7a51ed66 95 if (ce->ce_flags & CE_UPDATE) {
1fa6ead4 96 display_progress(progress, ++cnt);
7a51ed66 97 ce->ce_flags &= ~CE_UPDATE;
16a4c617 98 if (o->update) {
c4758d3c 99 errs |= checkout_entry(ce, &state, NULL);
16a4c617
JH
100 *last_symlink = '\0';
101 }
16da134b
JS
102 }
103 }
4d4fcc54 104 stop_progress(&progress);
c4758d3c 105 return errs != 0;
16da134b
JS
106}
107
34110cd4 108static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
01904572 109{
34110cd4
LT
110 int ret = o->fn(src, o);
111 if (ret > 0)
01904572 112 ret = 0;
01904572
LT
113 return ret;
114}
115
116static int unpack_index_entry(struct cache_entry *ce, struct unpack_trees_options *o)
117{
118 struct cache_entry *src[5] = { ce, };
34110cd4
LT
119
120 o->pos++;
01904572
LT
121 if (ce_stage(ce)) {
122 if (o->skip_unmerged) {
34110cd4
LT
123 add_entry(o, ce, 0, 0);
124 return 0;
01904572 125 }
01904572 126 }
34110cd4 127 return call_unpack_fn(src, o);
01904572
LT
128}
129
130int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
16da134b 131{
16da134b 132 int i;
ca885a4f 133 struct tree_desc t[MAX_UNPACK_TREES];
01904572
LT
134 struct traverse_info newinfo;
135 struct name_entry *p;
136
137 p = names;
138 while (!p->mode)
139 p++;
140
141 newinfo = *info;
142 newinfo.prev = info;
143 newinfo.name = *p;
144 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
145 newinfo.conflicts |= df_conflicts;
146
147 for (i = 0; i < n; i++, dirmask >>= 1) {
148 const unsigned char *sha1 = NULL;
149 if (dirmask & 1)
150 sha1 = names[i].sha1;
151 fill_tree_descriptor(t+i, sha1);
152 }
542c264b 153 return traverse_trees(n, t, &newinfo);
01904572
LT
154}
155
156/*
157 * Compare the traverse-path to the cache entry without actually
158 * having to generate the textual representation of the traverse
159 * path.
160 *
161 * NOTE! This *only* compares up to the size of the traverse path
162 * itself - the caller needs to do the final check for the cache
163 * entry having more data at the end!
164 */
165static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
166{
167 int len, pathlen, ce_len;
168 const char *ce_name;
169
170 if (info->prev) {
171 int cmp = do_compare_entry(ce, info->prev, &info->name);
172 if (cmp)
173 return cmp;
174 }
175 pathlen = info->pathlen;
176 ce_len = ce_namelen(ce);
177
178 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
179 if (ce_len < pathlen)
180 return -1;
181
182 ce_len -= pathlen;
183 ce_name = ce->name + pathlen;
184
185 len = tree_entry_len(n->path, n->sha1);
186 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
187}
188
189static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
190{
191 int cmp = do_compare_entry(ce, info, n);
192 if (cmp)
193 return cmp;
194
195 /*
196 * Even if the beginning compared identically, the ce should
197 * compare as bigger than a directory leading up to it!
198 */
199 return ce_namelen(ce) > traverse_path_len(info, n);
200}
201
202static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
203{
204 int len = traverse_path_len(info, n);
205 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
206
207 ce->ce_mode = create_ce_mode(n->mode);
208 ce->ce_flags = create_ce_flags(len, stage);
209 hashcpy(ce->sha1, n->sha1);
210 make_traverse_path(ce->name, info, n);
211
212 return ce;
213}
214
215static int unpack_nondirectories(int n, unsigned long mask, unsigned long dirmask, struct cache_entry *src[5],
34110cd4 216 const struct name_entry *names, const struct traverse_info *info)
01904572
LT
217{
218 int i;
219 struct unpack_trees_options *o = info->data;
220 unsigned long conflicts;
221
222 /* Do we have *only* directories? Nothing to do */
223 if (mask == dirmask && !src[0])
224 return 0;
225
226 conflicts = info->conflicts;
227 if (o->merge)
228 conflicts >>= 1;
229 conflicts |= dirmask;
230
231 /*
232 * Ok, we've filled in up to any potential index entry in src[0],
233 * now do the rest.
234 */
235 for (i = 0; i < n; i++) {
236 int stage;
237 unsigned int bit = 1ul << i;
238 if (conflicts & bit) {
239 src[i + o->merge] = o->df_conflict_entry;
240 continue;
241 }
242 if (!(mask & bit))
243 continue;
244 if (!o->merge)
245 stage = 0;
246 else if (i + 1 < o->head_idx)
247 stage = 1;
248 else if (i + 1 > o->head_idx)
249 stage = 3;
250 else
251 stage = 2;
252 src[i + o->merge] = create_ce_entry(info, names + i, stage);
253 }
254
255 if (o->merge)
34110cd4 256 return call_unpack_fn(src, o);
01904572
LT
257
258 n += o->merge;
01904572 259 for (i = 0; i < n; i++)
34110cd4 260 add_entry(o, src[i], 0, 0);
01904572
LT
261 return 0;
262}
263
264static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
265{
266 struct cache_entry *src[5] = { NULL, };
267 struct unpack_trees_options *o = info->data;
01904572
LT
268 const struct name_entry *p = names;
269
270 /* Find first entry with a real name (we could use "mask" too) */
271 while (!p->mode)
272 p++;
273
274 /* Are we supposed to look at the index too? */
275 if (o->merge) {
34110cd4
LT
276 while (o->pos < o->src_index->cache_nr) {
277 struct cache_entry *ce = o->src_index->cache[o->pos];
01904572
LT
278 int cmp = compare_entry(ce, info, p);
279 if (cmp < 0) {
280 if (unpack_index_entry(ce, o) < 0)
281 return -1;
282 continue;
283 }
284 if (!cmp) {
34110cd4 285 o->pos++;
01904572
LT
286 if (ce_stage(ce)) {
287 /*
288 * If we skip unmerged index entries, we'll skip this
289 * entry *and* the tree entries associated with it!
290 */
34110cd4
LT
291 if (o->skip_unmerged) {
292 add_entry(o, ce, 0, 0);
01904572 293 return mask;
34110cd4 294 }
01904572
LT
295 }
296 src[0] = ce;
01904572
LT
297 }
298 break;
299 }
300 }
301
34110cd4 302 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
01904572
LT
303 return -1;
304
305 /* Now handle any directories.. */
306 if (dirmask) {
307 unsigned long conflicts = mask & ~dirmask;
308 if (o->merge) {
309 conflicts <<= 1;
310 if (src[0])
311 conflicts |= 1;
312 }
542c264b
JH
313 if (traverse_trees_recursive(n, dirmask, conflicts,
314 names, info) < 0)
315 return -1;
01904572
LT
316 return mask;
317 }
318
319 return mask;
320}
321
322static int unpack_failed(struct unpack_trees_options *o, const char *message)
323{
1caeacc1 324 discard_index(&o->result);
01904572
LT
325 if (!o->gently) {
326 if (message)
327 return error(message);
328 return -1;
329 }
01904572
LT
330 return -1;
331}
332
333int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
334{
0fb1eaa8 335 static struct cache_entry *dfc;
16da134b 336
ca885a4f
JH
337 if (len > MAX_UNPACK_TREES)
338 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
076b0adc 339 memset(&state, 0, sizeof(state));
16da134b
JS
340 state.base_dir = "";
341 state.force = 1;
342 state.quiet = 1;
343 state.refresh_cache = 1;
344
34110cd4 345 memset(&o->result, 0, sizeof(o->result));
fac4b328
LT
346 if (o->src_index)
347 o->result.timestamp = o->src_index->timestamp;
16da134b 348 o->merge_size = len;
0fb1eaa8
JH
349
350 if (!dfc)
351 dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
352 o->df_conflict_entry = dfc;
16da134b
JS
353
354 if (len) {
01904572
LT
355 const char *prefix = o->prefix ? o->prefix : "";
356 struct traverse_info info;
357
358 setup_traverse_info(&info, prefix);
359 info.fn = unpack_callback;
360 info.data = o;
361
362 if (traverse_trees(len, t, &info) < 0)
363 return unpack_failed(o, NULL);
16da134b
JS
364 }
365
01904572
LT
366 /* Any left-over entries in the index? */
367 if (o->merge) {
34110cd4
LT
368 while (o->pos < o->src_index->cache_nr) {
369 struct cache_entry *ce = o->src_index->cache[o->pos];
01904572
LT
370 if (unpack_index_entry(ce, o) < 0)
371 return unpack_failed(o, NULL);
17e46426 372 }
17e46426 373 }
16da134b 374
01904572
LT
375 if (o->trivial_merges_only && o->nontrivial_merge)
376 return unpack_failed(o, "Merge requires file-level merging");
377
34110cd4 378 o->src_index = NULL;
c4758d3c
JH
379 if (check_updates(o))
380 return -1;
34110cd4
LT
381 if (o->dst_index)
382 *o->dst_index = o->result;
16da134b
JS
383 return 0;
384}
076b0adc
JS
385
386/* Here come the merge functions */
387
203a2fe1 388static int reject_merge(struct cache_entry *ce)
076b0adc 389{
203a2fe1
DB
390 return error("Entry '%s' would be overwritten by merge. Cannot merge.",
391 ce->name);
076b0adc
JS
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 &&
a89fccd2 401 !hashcmp(a->sha1, b->sha1);
076b0adc
JS
402}
403
404
405/*
406 * When a CE gets turned into an unmerged entry, we
407 * want it to be up-to-date
408 */
203a2fe1 409static int verify_uptodate(struct cache_entry *ce,
076b0adc
JS
410 struct unpack_trees_options *o)
411{
412 struct stat st;
413
414 if (o->index_only || o->reset)
203a2fe1 415 return 0;
076b0adc
JS
416
417 if (!lstat(ce->name, &st)) {
34110cd4 418 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID);
076b0adc 419 if (!changed)
203a2fe1 420 return 0;
936492d3
JH
421 /*
422 * NEEDSWORK: the current default policy is to allow
423 * submodule to be out of sync wrt the supermodule
424 * index. This needs to be tightened later for
425 * submodules that are marked to be automatically
426 * checked out.
427 */
7a51ed66 428 if (S_ISGITLINK(ce->ce_mode))
203a2fe1 429 return 0;
076b0adc
JS
430 errno = 0;
431 }
076b0adc 432 if (errno == ENOENT)
203a2fe1 433 return 0;
17e46426
DB
434 return o->gently ? -1 :
435 error("Entry '%s' not uptodate. Cannot merge.", ce->name);
076b0adc
JS
436}
437
bc052d7f 438static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
076b0adc
JS
439{
440 if (ce)
34110cd4 441 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
076b0adc
JS
442}
443
0cf73755
SV
444/*
445 * Check that checking out ce->sha1 in subdir ce->name is not
446 * going to overwrite any working files.
447 *
448 * Currently, git does not checkout subprojects during a superproject
449 * checkout, so it is not going to overwrite anything.
450 */
451static int verify_clean_submodule(struct cache_entry *ce, const char *action,
452 struct unpack_trees_options *o)
453{
454 return 0;
455}
456
457static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
c8193534
JH
458 struct unpack_trees_options *o)
459{
460 /*
0cf73755 461 * we are about to extract "ce->name"; we would not want to lose
c8193534
JH
462 * anything in the existing directory there.
463 */
464 int namelen;
465 int pos, i;
466 struct dir_struct d;
467 char *pathbuf;
468 int cnt = 0;
0cf73755
SV
469 unsigned char sha1[20];
470
7a51ed66 471 if (S_ISGITLINK(ce->ce_mode) &&
0cf73755
SV
472 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
473 /* If we are not going to update the submodule, then
474 * we don't care.
475 */
476 if (!hashcmp(sha1, ce->sha1))
477 return 0;
478 return verify_clean_submodule(ce, action, o);
479 }
c8193534
JH
480
481 /*
482 * First let's make sure we do not have a local modification
483 * in that directory.
484 */
0cf73755 485 namelen = strlen(ce->name);
34110cd4 486 pos = index_name_pos(o->src_index, ce->name, namelen);
c8193534
JH
487 if (0 <= pos)
488 return cnt; /* we have it as nondirectory */
489 pos = -pos - 1;
34110cd4
LT
490 for (i = pos; i < o->src_index->cache_nr; i++) {
491 struct cache_entry *ce = o->src_index->cache[i];
c8193534
JH
492 int len = ce_namelen(ce);
493 if (len < namelen ||
0cf73755 494 strncmp(ce->name, ce->name, namelen) ||
c8193534
JH
495 ce->name[namelen] != '/')
496 break;
497 /*
498 * ce->name is an entry in the subdirectory.
499 */
500 if (!ce_stage(ce)) {
203a2fe1
DB
501 if (verify_uptodate(ce, o))
502 return -1;
34110cd4 503 add_entry(o, ce, CE_REMOVE, 0);
c8193534
JH
504 }
505 cnt++;
506 }
507
508 /*
509 * Then we need to make sure that we do not lose a locally
510 * present file that is not ignored.
511 */
512 pathbuf = xmalloc(namelen + 2);
0cf73755 513 memcpy(pathbuf, ce->name, namelen);
c8193534
JH
514 strcpy(pathbuf+namelen, "/");
515
516 memset(&d, 0, sizeof(d));
517 if (o->dir)
518 d.exclude_per_dir = o->dir->exclude_per_dir;
0cf73755 519 i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
c8193534 520 if (i)
17e46426
DB
521 return o->gently ? -1 :
522 error("Updating '%s' would lose untracked files in it",
523 ce->name);
c8193534
JH
524 free(pathbuf);
525 return cnt;
526}
527
32260ad5
LT
528/*
529 * This gets called when there was no index entry for the tree entry 'dst',
530 * but we found a file in the working tree that 'lstat()' said was fine,
531 * and we're on a case-insensitive filesystem.
532 *
533 * See if we can find a case-insensitive match in the index that also
534 * matches the stat information, and assume it's that other file!
535 */
536static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
537{
538 struct cache_entry *src;
539
540 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
541 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID);
542}
543
076b0adc
JS
544/*
545 * We do not want to remove or overwrite a working tree file that
f8a9d428 546 * is not tracked, unless it is ignored.
076b0adc 547 */
203a2fe1
DB
548static int verify_absent(struct cache_entry *ce, const char *action,
549 struct unpack_trees_options *o)
076b0adc
JS
550{
551 struct stat st;
552
553 if (o->index_only || o->reset || !o->update)
203a2fe1 554 return 0;
c8193534 555
0cf73755 556 if (has_symlink_leading_path(ce->name, NULL))
203a2fe1 557 return 0;
ec0603e1 558
0cf73755 559 if (!lstat(ce->name, &st)) {
c8193534 560 int cnt;
6831a88a 561 int dtype = ce_to_dtype(ce);
df292c79 562 struct cache_entry *result;
c8193534 563
32260ad5
LT
564 /*
565 * It may be that the 'lstat()' succeeded even though
566 * target 'ce' was absent, because there is an old
567 * entry that is different only in case..
568 *
569 * Ignore that lstat() if it matches.
570 */
571 if (ignore_case && icase_exists(o, ce, &st))
572 return 0;
573
6831a88a 574 if (o->dir && excluded(o->dir, ce->name, &dtype))
c8193534 575 /*
0cf73755 576 * ce->name is explicitly excluded, so it is Ok to
c8193534
JH
577 * overwrite it.
578 */
203a2fe1 579 return 0;
c8193534
JH
580 if (S_ISDIR(st.st_mode)) {
581 /*
582 * We are checking out path "foo" and
583 * found "foo/." in the working tree.
584 * This is tricky -- if we have modified
585 * files that are in "foo/" we would lose
586 * it.
587 */
0cf73755 588 cnt = verify_clean_subdirectory(ce, action, o);
c8193534
JH
589
590 /*
591 * If this removed entries from the index,
592 * what that means is:
593 *
594 * (1) the caller unpack_trees_rec() saw path/foo
595 * in the index, and it has not removed it because
596 * it thinks it is handling 'path' as blob with
597 * D/F conflict;
598 * (2) we will return "ok, we placed a merged entry
599 * in the index" which would cause o->pos to be
600 * incremented by one;
601 * (3) however, original o->pos now has 'path/foo'
602 * marked with "to be removed".
603 *
604 * We need to increment it by the number of
605 * deleted entries here.
606 */
607 o->pos += cnt;
203a2fe1 608 return 0;
c8193534
JH
609 }
610
611 /*
612 * The previous round may already have decided to
613 * delete this path, which is in a subdirectory that
614 * is being replaced with a blob.
615 */
cd2fef59 616 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
df292c79
LT
617 if (result) {
618 if (result->ce_flags & CE_REMOVE)
203a2fe1 619 return 0;
c8193534
JH
620 }
621
17e46426
DB
622 return o->gently ? -1 :
623 error("Untracked working tree file '%s' "
624 "would be %s by merge.", ce->name, action);
c8193534 625 }
203a2fe1 626 return 0;
076b0adc
JS
627}
628
629static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
630 struct unpack_trees_options *o)
631{
7f8ab8dc
LT
632 int update = CE_UPDATE;
633
076b0adc
JS
634 if (old) {
635 /*
636 * See if we can re-use the old CE directly?
637 * That way we get the uptodate stat info.
638 *
7f8ab8dc
LT
639 * This also removes the UPDATE flag on a match; otherwise
640 * we will end up overwriting local changes in the work tree.
076b0adc
JS
641 */
642 if (same(old, merge)) {
eb7a2f1d 643 copy_cache_entry(merge, old);
7f8ab8dc 644 update = 0;
076b0adc 645 } else {
203a2fe1
DB
646 if (verify_uptodate(old, o))
647 return -1;
bc052d7f 648 invalidate_ce_path(old, o);
076b0adc
JS
649 }
650 }
651 else {
203a2fe1
DB
652 if (verify_absent(merge, "overwritten", o))
653 return -1;
bc052d7f 654 invalidate_ce_path(merge, o);
076b0adc
JS
655 }
656
7f8ab8dc 657 add_entry(o, merge, update, CE_STAGEMASK);
076b0adc
JS
658 return 1;
659}
660
661static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
662 struct unpack_trees_options *o)
663{
34110cd4
LT
664 /* Did it exist in the index? */
665 if (!old) {
203a2fe1
DB
666 if (verify_absent(ce, "removed", o))
667 return -1;
34110cd4
LT
668 return 0;
669 }
670 if (verify_uptodate(old, o))
671 return -1;
672 add_entry(o, ce, CE_REMOVE, 0);
bc052d7f 673 invalidate_ce_path(ce, o);
076b0adc
JS
674 return 1;
675}
676
7f7932ab 677static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
076b0adc 678{
34110cd4 679 add_entry(o, ce, 0, 0);
076b0adc
JS
680 return 1;
681}
682
683#if DBRT_DEBUG
684static void show_stage_entry(FILE *o,
685 const char *label, const struct cache_entry *ce)
686{
687 if (!ce)
688 fprintf(o, "%s (missing)\n", label);
689 else
690 fprintf(o, "%s%06o %s %d\t%s\n",
691 label,
7a51ed66 692 ce->ce_mode,
076b0adc
JS
693 sha1_to_hex(ce->sha1),
694 ce_stage(ce),
695 ce->name);
696}
697#endif
698
34110cd4 699int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
076b0adc
JS
700{
701 struct cache_entry *index;
702 struct cache_entry *head;
703 struct cache_entry *remote = stages[o->head_idx + 1];
704 int count;
705 int head_match = 0;
706 int remote_match = 0;
076b0adc
JS
707
708 int df_conflict_head = 0;
709 int df_conflict_remote = 0;
710
711 int any_anc_missing = 0;
712 int no_anc_exists = 1;
713 int i;
714
715 for (i = 1; i < o->head_idx; i++) {
4c4caafc 716 if (!stages[i] || stages[i] == o->df_conflict_entry)
076b0adc 717 any_anc_missing = 1;
4c4caafc 718 else
076b0adc 719 no_anc_exists = 0;
076b0adc
JS
720 }
721
722 index = stages[0];
723 head = stages[o->head_idx];
724
725 if (head == o->df_conflict_entry) {
726 df_conflict_head = 1;
727 head = NULL;
728 }
729
730 if (remote == o->df_conflict_entry) {
731 df_conflict_remote = 1;
732 remote = NULL;
733 }
734
076b0adc
JS
735 /* First, if there's a #16 situation, note that to prevent #13
736 * and #14.
737 */
738 if (!same(remote, head)) {
739 for (i = 1; i < o->head_idx; i++) {
740 if (same(stages[i], head)) {
741 head_match = i;
742 }
743 if (same(stages[i], remote)) {
744 remote_match = i;
745 }
746 }
747 }
748
749 /* We start with cases where the index is allowed to match
750 * something other than the head: #14(ALT) and #2ALT, where it
751 * is permitted to match the result instead.
752 */
753 /* #14, #14ALT, #2ALT */
754 if (remote && !df_conflict_head && head_match && !remote_match) {
755 if (index && !same(index, remote) && !same(index, head))
17e46426 756 return o->gently ? -1 : reject_merge(index);
076b0adc
JS
757 return merged_entry(remote, index, o);
758 }
759 /*
760 * If we have an entry in the index cache, then we want to
761 * make sure that it matches head.
762 */
4e7c4571 763 if (index && !same(index, head))
17e46426 764 return o->gently ? -1 : reject_merge(index);
076b0adc
JS
765
766 if (head) {
767 /* #5ALT, #15 */
768 if (same(head, remote))
769 return merged_entry(head, index, o);
770 /* #13, #3ALT */
771 if (!df_conflict_remote && remote_match && !head_match)
772 return merged_entry(head, index, o);
773 }
774
775 /* #1 */
34110cd4 776 if (!head && !remote && any_anc_missing)
076b0adc
JS
777 return 0;
778
779 /* Under the new "aggressive" rule, we resolve mostly trivial
780 * cases that we historically had git-merge-one-file resolve.
781 */
782 if (o->aggressive) {
783 int head_deleted = !head && !df_conflict_head;
784 int remote_deleted = !remote && !df_conflict_remote;
0cf73755 785 struct cache_entry *ce = NULL;
4c4caafc
JH
786
787 if (index)
0cf73755 788 ce = index;
4c4caafc 789 else if (head)
0cf73755 790 ce = head;
4c4caafc 791 else if (remote)
0cf73755 792 ce = remote;
4c4caafc
JH
793 else {
794 for (i = 1; i < o->head_idx; i++) {
795 if (stages[i] && stages[i] != o->df_conflict_entry) {
0cf73755 796 ce = stages[i];
4c4caafc
JH
797 break;
798 }
799 }
800 }
801
076b0adc
JS
802 /*
803 * Deleted in both.
804 * Deleted in one and unchanged in the other.
805 */
806 if ((head_deleted && remote_deleted) ||
807 (head_deleted && remote && remote_match) ||
808 (remote_deleted && head && head_match)) {
809 if (index)
810 return deleted_entry(index, index, o);
34110cd4 811 if (ce && !head_deleted) {
203a2fe1
DB
812 if (verify_absent(ce, "removed", o))
813 return -1;
814 }
076b0adc
JS
815 return 0;
816 }
817 /*
818 * Added in both, identically.
819 */
820 if (no_anc_exists && head && remote && same(head, remote))
821 return merged_entry(head, index, o);
822
823 }
824
825 /* Below are "no merge" cases, which require that the index be
826 * up-to-date to avoid the files getting overwritten with
827 * conflict resolution files.
828 */
829 if (index) {
203a2fe1
DB
830 if (verify_uptodate(index, o))
831 return -1;
076b0adc 832 }
076b0adc
JS
833
834 o->nontrivial_merge = 1;
835
ea4b52a8 836 /* #2, #3, #4, #6, #7, #9, #10, #11. */
076b0adc
JS
837 count = 0;
838 if (!head_match || !remote_match) {
839 for (i = 1; i < o->head_idx; i++) {
4c4caafc 840 if (stages[i] && stages[i] != o->df_conflict_entry) {
7f7932ab 841 keep_entry(stages[i], o);
076b0adc
JS
842 count++;
843 break;
844 }
845 }
846 }
847#if DBRT_DEBUG
848 else {
849 fprintf(stderr, "read-tree: warning #16 detected\n");
850 show_stage_entry(stderr, "head ", stages[head_match]);
851 show_stage_entry(stderr, "remote ", stages[remote_match]);
852 }
853#endif
7f7932ab
JH
854 if (head) { count += keep_entry(head, o); }
855 if (remote) { count += keep_entry(remote, o); }
076b0adc
JS
856 return count;
857}
858
859/*
860 * Two-way merge.
861 *
862 * The rule is to "carry forward" what is in the index without losing
863 * information across a "fast forward", favoring a successful merge
864 * over a merge failure when it makes sense. For details of the
865 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
866 *
867 */
34110cd4 868int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
076b0adc
JS
869{
870 struct cache_entry *current = src[0];
b8ba1535
JH
871 struct cache_entry *oldtree = src[1];
872 struct cache_entry *newtree = src[2];
076b0adc
JS
873
874 if (o->merge_size != 2)
875 return error("Cannot do a twoway merge of %d trees",
876 o->merge_size);
877
b8ba1535
JH
878 if (oldtree == o->df_conflict_entry)
879 oldtree = NULL;
880 if (newtree == o->df_conflict_entry)
881 newtree = NULL;
882
076b0adc
JS
883 if (current) {
884 if ((!oldtree && !newtree) || /* 4 and 5 */
885 (!oldtree && newtree &&
886 same(current, newtree)) || /* 6 and 7 */
887 (oldtree && newtree &&
888 same(oldtree, newtree)) || /* 14 and 15 */
889 (oldtree && newtree &&
b8ba1535 890 !same(oldtree, newtree) && /* 18 and 19 */
076b0adc 891 same(current, newtree))) {
7f7932ab 892 return keep_entry(current, o);
076b0adc
JS
893 }
894 else if (oldtree && !newtree && same(current, oldtree)) {
895 /* 10 or 11 */
896 return deleted_entry(oldtree, current, o);
897 }
898 else if (oldtree && newtree &&
899 same(current, oldtree) && !same(current, newtree)) {
900 /* 20 or 21 */
901 return merged_entry(newtree, current, o);
902 }
903 else {
904 /* all other failures */
905 if (oldtree)
17e46426 906 return o->gently ? -1 : reject_merge(oldtree);
076b0adc 907 if (current)
17e46426 908 return o->gently ? -1 : reject_merge(current);
076b0adc 909 if (newtree)
17e46426 910 return o->gently ? -1 : reject_merge(newtree);
076b0adc
JS
911 return -1;
912 }
913 }
914 else if (newtree)
915 return merged_entry(newtree, current, o);
d699676d 916 return deleted_entry(oldtree, current, o);
076b0adc
JS
917}
918
919/*
920 * Bind merge.
921 *
922 * Keep the index entries at stage0, collapse stage1 but make sure
923 * stage0 does not have anything there.
924 */
925int bind_merge(struct cache_entry **src,
34110cd4 926 struct unpack_trees_options *o)
076b0adc
JS
927{
928 struct cache_entry *old = src[0];
929 struct cache_entry *a = src[1];
930
931 if (o->merge_size != 1)
932 return error("Cannot do a bind merge of %d trees\n",
933 o->merge_size);
934 if (a && old)
17e46426 935 return o->gently ? -1 :
34110cd4 936 error("Entry '%s' overlaps with '%s'. Cannot bind.", a->name, old->name);
076b0adc 937 if (!a)
7f7932ab 938 return keep_entry(old, o);
076b0adc
JS
939 else
940 return merged_entry(a, NULL, o);
941}
942
943/*
944 * One-way merge.
945 *
946 * The rule is:
947 * - take the stat information from stage0, take the data from stage1
948 */
34110cd4 949int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
076b0adc
JS
950{
951 struct cache_entry *old = src[0];
952 struct cache_entry *a = src[1];
953
954 if (o->merge_size != 1)
955 return error("Cannot do a oneway merge of %d trees",
956 o->merge_size);
957
34110cd4 958 if (!a)
076b0adc 959 return deleted_entry(old, old, o);
34110cd4 960
076b0adc 961 if (old && same(old, a)) {
34110cd4 962 int update = 0;
076b0adc
JS
963 if (o->reset) {
964 struct stat st;
965 if (lstat(old->name, &st) ||
34110cd4
LT
966 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID))
967 update |= CE_UPDATE;
076b0adc 968 }
34110cd4
LT
969 add_entry(o, old, update, 0);
970 return 0;
076b0adc
JS
971 }
972 return merged_entry(a, old, o);
973}