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