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