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