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