e3258317d66e1025687351ec0ebde99de3c39dbe
1#define NO_THE_INDEX_COMPATIBILITY_MACROS
2#include "cache.h"
3#include "dir.h"
4#include "tree.h"
5#include "tree-walk.h"
6#include "cache-tree.h"
7#include "unpack-trees.h"
8#include "progress.h"
9#include "refs.h"
10#include "attr.h"
11
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" and "git merge" replaces
17 * them using set_porcelain_error_msgs(), for example.
18 */
19const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
20 /* ERROR_WOULD_OVERWRITE */
21 "Entry '%s' would be overwritten by merge. Cannot merge.",
22
23 /* ERROR_NOT_UPTODATE_FILE */
24 "Entry '%s' not uptodate. Cannot merge.",
25
26 /* ERROR_NOT_UPTODATE_DIR */
27 "Updating '%s' would lose untracked files in it",
28
29 /* ERROR_WOULD_LOSE_UNTRACKED */
30 "Untracked working tree file '%s' would be %s by merge.",
31
32 /* ERROR_BIND_OVERLAP */
33 "Entry '%s' overlaps with '%s'. Cannot bind.",
34
35 /* ERROR_SPARSE_NOT_UPTODATE_FILE */
36 "Entry '%s' not uptodate. Cannot update sparse checkout.",
37
38 /* ERROR_WOULD_LOSE_ORPHANED */
39 "Working tree file '%s' would be %s by sparse checkout update.",
40};
41
42#define ERRORMSG(o,type) \
43 ( ((o) && (o)->msgs[(type)]) \
44 ? ((o)->msgs[(type)]) \
45 : (unpack_plumbing_errors[(type)]) )
46
47static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
48 unsigned int set, unsigned int clear)
49{
50 unsigned int size = ce_size(ce);
51 struct cache_entry *new = xmalloc(size);
52
53 clear |= CE_HASHED | CE_UNHASHED;
54
55 memcpy(new, ce, size);
56 new->next = NULL;
57 new->ce_flags = (new->ce_flags & ~clear) | set;
58 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
59}
60
61/*
62 * Unlink the last component and schedule the leading directories for
63 * removal, such that empty directories get removed.
64 */
65static void unlink_entry(struct cache_entry *ce)
66{
67 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
68 return;
69 if (remove_or_warn(ce->ce_mode, ce->name))
70 return;
71 schedule_dir_for_removal(ce->name, ce_namelen(ce));
72}
73
74static struct checkout state;
75static int check_updates(struct unpack_trees_options *o)
76{
77 unsigned cnt = 0, total = 0;
78 struct progress *progress = NULL;
79 struct index_state *index = &o->result;
80 int i;
81 int errs = 0;
82
83 if (o->update && o->verbose_update) {
84 for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
85 struct cache_entry *ce = index->cache[cnt];
86 if (ce->ce_flags & (CE_UPDATE | CE_REMOVE | CE_WT_REMOVE))
87 total++;
88 }
89
90 progress = start_progress_delay("Checking out files",
91 total, 50, 1);
92 cnt = 0;
93 }
94
95 if (o->update)
96 git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
97 for (i = 0; i < index->cache_nr; i++) {
98 struct cache_entry *ce = index->cache[i];
99
100 if (ce->ce_flags & CE_WT_REMOVE) {
101 display_progress(progress, ++cnt);
102 if (o->update)
103 unlink_entry(ce);
104 continue;
105 }
106
107 if (ce->ce_flags & CE_REMOVE) {
108 display_progress(progress, ++cnt);
109 if (o->update)
110 unlink_entry(ce);
111 }
112 }
113 remove_marked_cache_entries(&o->result);
114 remove_scheduled_dirs();
115
116 for (i = 0; i < index->cache_nr; i++) {
117 struct cache_entry *ce = index->cache[i];
118
119 if (ce->ce_flags & CE_UPDATE) {
120 display_progress(progress, ++cnt);
121 ce->ce_flags &= ~CE_UPDATE;
122 if (o->update) {
123 errs |= checkout_entry(ce, &state, NULL);
124 }
125 }
126 }
127 stop_progress(&progress);
128 if (o->update)
129 git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
130 return errs != 0;
131}
132
133static int verify_uptodate_sparse(struct cache_entry *ce, struct unpack_trees_options *o);
134static int verify_absent_sparse(struct cache_entry *ce, const char *action, struct unpack_trees_options *o);
135
136static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_trees_options *o)
137{
138 const char *basename;
139
140 if (ce_stage(ce))
141 return 0;
142
143 basename = strrchr(ce->name, '/');
144 basename = basename ? basename+1 : ce->name;
145 return excluded_from_list(ce->name, ce_namelen(ce), basename, NULL, o->el) <= 0;
146}
147
148static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_options *o)
149{
150 int was_skip_worktree = ce_skip_worktree(ce);
151
152 if (will_have_skip_worktree(ce, o))
153 ce->ce_flags |= CE_SKIP_WORKTREE;
154 else
155 ce->ce_flags &= ~CE_SKIP_WORKTREE;
156
157 /*
158 * We only care about files getting into the checkout area
159 * If merge strategies want to remove some, go ahead, this
160 * flag will be removed eventually in unpack_trees() if it's
161 * outside checkout area.
162 */
163 if (ce->ce_flags & CE_REMOVE)
164 return 0;
165
166 if (!was_skip_worktree && ce_skip_worktree(ce)) {
167 /*
168 * If CE_UPDATE is set, verify_uptodate() must be called already
169 * also stat info may have lost after merged_entry() so calling
170 * verify_uptodate() again may fail
171 */
172 if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
173 return -1;
174 ce->ce_flags |= CE_WT_REMOVE;
175 }
176 if (was_skip_worktree && !ce_skip_worktree(ce)) {
177 if (verify_absent_sparse(ce, "overwritten", o))
178 return -1;
179 ce->ce_flags |= CE_UPDATE;
180 }
181 return 0;
182}
183
184static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
185{
186 int ret = o->fn(src, o);
187 if (ret > 0)
188 ret = 0;
189 return ret;
190}
191
192static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
193{
194 ce->ce_flags |= CE_UNPACKED;
195
196 if (o->cache_bottom < o->src_index->cache_nr &&
197 o->src_index->cache[o->cache_bottom] == ce) {
198 int bottom = o->cache_bottom;
199 while (bottom < o->src_index->cache_nr &&
200 o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
201 bottom++;
202 o->cache_bottom = bottom;
203 }
204}
205
206static void mark_all_ce_unused(struct index_state *index)
207{
208 int i;
209 for (i = 0; i < index->cache_nr; i++)
210 index->cache[i]->ce_flags &= ~CE_UNPACKED;
211}
212
213static int locate_in_src_index(struct cache_entry *ce,
214 struct unpack_trees_options *o)
215{
216 struct index_state *index = o->src_index;
217 int len = ce_namelen(ce);
218 int pos = index_name_pos(index, ce->name, len);
219 if (pos < 0)
220 pos = -1 - pos;
221 return pos;
222}
223
224/*
225 * We call unpack_index_entry() with an unmerged cache entry
226 * only in diff-index, and it wants a single callback. Skip
227 * the other unmerged entry with the same name.
228 */
229static void mark_ce_used_same_name(struct cache_entry *ce,
230 struct unpack_trees_options *o)
231{
232 struct index_state *index = o->src_index;
233 int len = ce_namelen(ce);
234 int pos;
235
236 for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
237 struct cache_entry *next = index->cache[pos];
238 if (len != ce_namelen(next) ||
239 memcmp(ce->name, next->name, len))
240 break;
241 mark_ce_used(next, o);
242 }
243}
244
245static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
246{
247 const struct index_state *index = o->src_index;
248 int pos = o->cache_bottom;
249
250 while (pos < index->cache_nr) {
251 struct cache_entry *ce = index->cache[pos];
252 if (!(ce->ce_flags & CE_UNPACKED))
253 return ce;
254 pos++;
255 }
256 return NULL;
257}
258
259static void add_same_unmerged(struct cache_entry *ce,
260 struct unpack_trees_options *o)
261{
262 struct index_state *index = o->src_index;
263 int len = ce_namelen(ce);
264 int pos = index_name_pos(index, ce->name, len);
265
266 if (0 <= pos)
267 die("programming error in a caller of mark_ce_used_same_name");
268 for (pos = -pos - 1; pos < index->cache_nr; pos++) {
269 struct cache_entry *next = index->cache[pos];
270 if (len != ce_namelen(next) ||
271 memcmp(ce->name, next->name, len))
272 break;
273 add_entry(o, next, 0, 0);
274 mark_ce_used(next, o);
275 }
276}
277
278static int unpack_index_entry(struct cache_entry *ce,
279 struct unpack_trees_options *o)
280{
281 struct cache_entry *src[5] = { NULL };
282 int ret;
283
284 src[0] = ce;
285
286 mark_ce_used(ce, o);
287 if (ce_stage(ce)) {
288 if (o->skip_unmerged) {
289 add_entry(o, ce, 0, 0);
290 return 0;
291 }
292 }
293 ret = call_unpack_fn(src, o);
294 if (ce_stage(ce))
295 mark_ce_used_same_name(ce, o);
296 return ret;
297}
298
299static int find_cache_pos(struct traverse_info *, const struct name_entry *);
300
301static void restore_cache_bottom(struct traverse_info *info, int bottom)
302{
303 struct unpack_trees_options *o = info->data;
304
305 if (o->diff_index_cached)
306 return;
307 o->cache_bottom = bottom;
308}
309
310static int switch_cache_bottom(struct traverse_info *info)
311{
312 struct unpack_trees_options *o = info->data;
313 int ret, pos;
314
315 if (o->diff_index_cached)
316 return 0;
317 ret = o->cache_bottom;
318 pos = find_cache_pos(info->prev, &info->name);
319
320 if (pos < -1)
321 o->cache_bottom = -2 - pos;
322 else if (pos < 0)
323 o->cache_bottom = o->src_index->cache_nr;
324 return ret;
325}
326
327static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
328{
329 int i, ret, bottom;
330 struct tree_desc t[MAX_UNPACK_TREES];
331 struct traverse_info newinfo;
332 struct name_entry *p;
333
334 p = names;
335 while (!p->mode)
336 p++;
337
338 newinfo = *info;
339 newinfo.prev = info;
340 newinfo.name = *p;
341 newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
342 newinfo.conflicts |= df_conflicts;
343
344 for (i = 0; i < n; i++, dirmask >>= 1) {
345 const unsigned char *sha1 = NULL;
346 if (dirmask & 1)
347 sha1 = names[i].sha1;
348 fill_tree_descriptor(t+i, sha1);
349 }
350
351 bottom = switch_cache_bottom(&newinfo);
352 ret = traverse_trees(n, t, &newinfo);
353 restore_cache_bottom(&newinfo, bottom);
354 return ret;
355}
356
357/*
358 * Compare the traverse-path to the cache entry without actually
359 * having to generate the textual representation of the traverse
360 * path.
361 *
362 * NOTE! This *only* compares up to the size of the traverse path
363 * itself - the caller needs to do the final check for the cache
364 * entry having more data at the end!
365 */
366static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
367{
368 int len, pathlen, ce_len;
369 const char *ce_name;
370
371 if (info->prev) {
372 int cmp = do_compare_entry(ce, info->prev, &info->name);
373 if (cmp)
374 return cmp;
375 }
376 pathlen = info->pathlen;
377 ce_len = ce_namelen(ce);
378
379 /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
380 if (ce_len < pathlen)
381 return -1;
382
383 ce_len -= pathlen;
384 ce_name = ce->name + pathlen;
385
386 len = tree_entry_len(n->path, n->sha1);
387 return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
388}
389
390static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
391{
392 int cmp = do_compare_entry(ce, info, n);
393 if (cmp)
394 return cmp;
395
396 /*
397 * Even if the beginning compared identically, the ce should
398 * compare as bigger than a directory leading up to it!
399 */
400 return ce_namelen(ce) > traverse_path_len(info, n);
401}
402
403static int ce_in_traverse_path(const struct cache_entry *ce,
404 const struct traverse_info *info)
405{
406 if (!info->prev)
407 return 1;
408 if (do_compare_entry(ce, info->prev, &info->name))
409 return 0;
410 /*
411 * If ce (blob) is the same name as the path (which is a tree
412 * we will be descending into), it won't be inside it.
413 */
414 return (info->pathlen < ce_namelen(ce));
415}
416
417static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
418{
419 int len = traverse_path_len(info, n);
420 struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
421
422 ce->ce_mode = create_ce_mode(n->mode);
423 ce->ce_flags = create_ce_flags(len, stage);
424 hashcpy(ce->sha1, n->sha1);
425 make_traverse_path(ce->name, info, n);
426
427 return ce;
428}
429
430static int unpack_nondirectories(int n, unsigned long mask,
431 unsigned long dirmask,
432 struct cache_entry **src,
433 const struct name_entry *names,
434 const struct traverse_info *info)
435{
436 int i;
437 struct unpack_trees_options *o = info->data;
438 unsigned long conflicts;
439
440 /* Do we have *only* directories? Nothing to do */
441 if (mask == dirmask && !src[0])
442 return 0;
443
444 conflicts = info->conflicts;
445 if (o->merge)
446 conflicts >>= 1;
447 conflicts |= dirmask;
448
449 /*
450 * Ok, we've filled in up to any potential index entry in src[0],
451 * now do the rest.
452 */
453 for (i = 0; i < n; i++) {
454 int stage;
455 unsigned int bit = 1ul << i;
456 if (conflicts & bit) {
457 src[i + o->merge] = o->df_conflict_entry;
458 continue;
459 }
460 if (!(mask & bit))
461 continue;
462 if (!o->merge)
463 stage = 0;
464 else if (i + 1 < o->head_idx)
465 stage = 1;
466 else if (i + 1 > o->head_idx)
467 stage = 3;
468 else
469 stage = 2;
470 src[i + o->merge] = create_ce_entry(info, names + i, stage);
471 }
472
473 if (o->merge)
474 return call_unpack_fn(src, o);
475
476 for (i = 0; i < n; i++)
477 if (src[i] && src[i] != o->df_conflict_entry)
478 add_entry(o, src[i], 0, 0);
479 return 0;
480}
481
482static int unpack_failed(struct unpack_trees_options *o, const char *message)
483{
484 discard_index(&o->result);
485 if (!o->gently) {
486 if (message)
487 return error("%s", message);
488 return -1;
489 }
490 return -1;
491}
492
493/* NEEDSWORK: give this a better name and share with tree-walk.c */
494static int name_compare(const char *a, int a_len,
495 const char *b, int b_len)
496{
497 int len = (a_len < b_len) ? a_len : b_len;
498 int cmp = memcmp(a, b, len);
499 if (cmp)
500 return cmp;
501 return (a_len - b_len);
502}
503
504/*
505 * The tree traversal is looking at name p. If we have a matching entry,
506 * return it. If name p is a directory in the index, do not return
507 * anything, as we will want to match it when the traversal descends into
508 * the directory.
509 */
510static int find_cache_pos(struct traverse_info *info,
511 const struct name_entry *p)
512{
513 int pos;
514 struct unpack_trees_options *o = info->data;
515 struct index_state *index = o->src_index;
516 int pfxlen = info->pathlen;
517 int p_len = tree_entry_len(p->path, p->sha1);
518
519 for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
520 struct cache_entry *ce = index->cache[pos];
521 const char *ce_name, *ce_slash;
522 int cmp, ce_len;
523
524 if (ce->ce_flags & CE_UNPACKED) {
525 /*
526 * cache_bottom entry is already unpacked, so
527 * we can never match it; don't check it
528 * again.
529 */
530 if (pos == o->cache_bottom)
531 ++o->cache_bottom;
532 continue;
533 }
534 if (!ce_in_traverse_path(ce, info))
535 continue;
536 ce_name = ce->name + pfxlen;
537 ce_slash = strchr(ce_name, '/');
538 if (ce_slash)
539 ce_len = ce_slash - ce_name;
540 else
541 ce_len = ce_namelen(ce) - pfxlen;
542 cmp = name_compare(p->path, p_len, ce_name, ce_len);
543 /*
544 * Exact match; if we have a directory we need to
545 * delay returning it.
546 */
547 if (!cmp)
548 return ce_slash ? -2 - pos : pos;
549 if (0 < cmp)
550 continue; /* keep looking */
551 /*
552 * ce_name sorts after p->path; could it be that we
553 * have files under p->path directory in the index?
554 * E.g. ce_name == "t-i", and p->path == "t"; we may
555 * have "t/a" in the index.
556 */
557 if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&
558 ce_name[p_len] < '/')
559 continue; /* keep looking */
560 break;
561 }
562 return -1;
563}
564
565static struct cache_entry *find_cache_entry(struct traverse_info *info,
566 const struct name_entry *p)
567{
568 int pos = find_cache_pos(info, p);
569 struct unpack_trees_options *o = info->data;
570
571 if (0 <= pos)
572 return o->src_index->cache[pos];
573 else
574 return NULL;
575}
576
577static void debug_path(struct traverse_info *info)
578{
579 if (info->prev) {
580 debug_path(info->prev);
581 if (*info->prev->name.path)
582 putchar('/');
583 }
584 printf("%s", info->name.path);
585}
586
587static void debug_name_entry(int i, struct name_entry *n)
588{
589 printf("ent#%d %06o %s\n", i,
590 n->path ? n->mode : 0,
591 n->path ? n->path : "(missing)");
592}
593
594static void debug_unpack_callback(int n,
595 unsigned long mask,
596 unsigned long dirmask,
597 struct name_entry *names,
598 struct traverse_info *info)
599{
600 int i;
601 printf("* unpack mask %lu, dirmask %lu, cnt %d ",
602 mask, dirmask, n);
603 debug_path(info);
604 putchar('\n');
605 for (i = 0; i < n; i++)
606 debug_name_entry(i, names + i);
607}
608
609static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
610{
611 struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
612 struct unpack_trees_options *o = info->data;
613 const struct name_entry *p = names;
614
615 /* Find first entry with a real name (we could use "mask" too) */
616 while (!p->mode)
617 p++;
618
619 if (o->debug_unpack)
620 debug_unpack_callback(n, mask, dirmask, names, info);
621
622 /* Are we supposed to look at the index too? */
623 if (o->merge) {
624 while (1) {
625 int cmp;
626 struct cache_entry *ce;
627
628 if (o->diff_index_cached)
629 ce = next_cache_entry(o);
630 else
631 ce = find_cache_entry(info, p);
632
633 if (!ce)
634 break;
635 cmp = compare_entry(ce, info, p);
636 if (cmp < 0) {
637 if (unpack_index_entry(ce, o) < 0)
638 return unpack_failed(o, NULL);
639 continue;
640 }
641 if (!cmp) {
642 if (ce_stage(ce)) {
643 /*
644 * If we skip unmerged index
645 * entries, we'll skip this
646 * entry *and* the tree
647 * entries associated with it!
648 */
649 if (o->skip_unmerged) {
650 add_same_unmerged(ce, o);
651 return mask;
652 }
653 }
654 src[0] = ce;
655 }
656 break;
657 }
658 }
659
660 if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
661 return -1;
662
663 if (src[0]) {
664 if (ce_stage(src[0]))
665 mark_ce_used_same_name(src[0], o);
666 else
667 mark_ce_used(src[0], o);
668 }
669
670 /* Now handle any directories.. */
671 if (dirmask) {
672 unsigned long conflicts = mask & ~dirmask;
673 if (o->merge) {
674 conflicts <<= 1;
675 if (src[0])
676 conflicts |= 1;
677 }
678
679 /* special case: "diff-index --cached" looking at a tree */
680 if (o->diff_index_cached &&
681 n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
682 int matches;
683 matches = cache_tree_matches_traversal(o->src_index->cache_tree,
684 names, info);
685 /*
686 * Everything under the name matches; skip the
687 * entire hierarchy. diff_index_cached codepath
688 * special cases D/F conflicts in such a way that
689 * it does not do any look-ahead, so this is safe.
690 */
691 if (matches) {
692 o->cache_bottom += matches;
693 return mask;
694 }
695 }
696
697 if (traverse_trees_recursive(n, dirmask, conflicts,
698 names, info) < 0)
699 return -1;
700 return mask;
701 }
702
703 return mask;
704}
705
706/*
707 * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
708 * resulting index, -2 on failure to reflect the changes to the work tree.
709 */
710int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
711{
712 int i, ret;
713 static struct cache_entry *dfc;
714 struct exclude_list el;
715
716 if (len > MAX_UNPACK_TREES)
717 die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
718 memset(&state, 0, sizeof(state));
719 state.base_dir = "";
720 state.force = 1;
721 state.quiet = 1;
722 state.refresh_cache = 1;
723
724 memset(&el, 0, sizeof(el));
725 if (!core_apply_sparse_checkout || !o->update)
726 o->skip_sparse_checkout = 1;
727 if (!o->skip_sparse_checkout) {
728 if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0)
729 o->skip_sparse_checkout = 1;
730 else
731 o->el = ⪙
732 }
733
734 memset(&o->result, 0, sizeof(o->result));
735 o->result.initialized = 1;
736 o->result.timestamp.sec = o->src_index->timestamp.sec;
737 o->result.timestamp.nsec = o->src_index->timestamp.nsec;
738 o->merge_size = len;
739 mark_all_ce_unused(o->src_index);
740
741 if (!dfc)
742 dfc = xcalloc(1, cache_entry_size(0));
743 o->df_conflict_entry = dfc;
744
745 if (len) {
746 const char *prefix = o->prefix ? o->prefix : "";
747 struct traverse_info info;
748
749 setup_traverse_info(&info, prefix);
750 info.fn = unpack_callback;
751 info.data = o;
752
753 if (o->prefix) {
754 /*
755 * Unpack existing index entries that sort before the
756 * prefix the tree is spliced into. Note that o->merge
757 * is always true in this case.
758 */
759 while (1) {
760 struct cache_entry *ce = next_cache_entry(o);
761 if (!ce)
762 break;
763 if (ce_in_traverse_path(ce, &info))
764 break;
765 if (unpack_index_entry(ce, o) < 0)
766 goto return_failed;
767 }
768 }
769
770 if (traverse_trees(len, t, &info) < 0)
771 goto return_failed;
772 }
773
774 /* Any left-over entries in the index? */
775 if (o->merge) {
776 while (1) {
777 struct cache_entry *ce = next_cache_entry(o);
778 if (!ce)
779 break;
780 if (unpack_index_entry(ce, o) < 0)
781 goto return_failed;
782 }
783 }
784 mark_all_ce_unused(o->src_index);
785
786 if (o->trivial_merges_only && o->nontrivial_merge) {
787 ret = unpack_failed(o, "Merge requires file-level merging");
788 goto done;
789 }
790
791 if (!o->skip_sparse_checkout) {
792 int empty_worktree = 1;
793 for (i = 0;i < o->result.cache_nr;i++) {
794 struct cache_entry *ce = o->result.cache[i];
795
796 if (apply_sparse_checkout(ce, o)) {
797 ret = -1;
798 goto done;
799 }
800 /*
801 * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
802 * area as a result of ce_skip_worktree() shortcuts in
803 * verify_absent() and verify_uptodate(). Clear them.
804 */
805 if (ce_skip_worktree(ce))
806 ce->ce_flags &= ~(CE_UPDATE | CE_REMOVE);
807 else
808 empty_worktree = 0;
809
810 }
811 if (o->result.cache_nr && empty_worktree) {
812 ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
813 goto done;
814 }
815 }
816
817 o->src_index = NULL;
818 ret = check_updates(o) ? (-2) : 0;
819 if (o->dst_index)
820 *o->dst_index = o->result;
821
822done:
823 for (i = 0;i < el.nr;i++)
824 free(el.excludes[i]);
825 if (el.excludes)
826 free(el.excludes);
827
828 return ret;
829
830return_failed:
831 mark_all_ce_unused(o->src_index);
832 ret = unpack_failed(o, NULL);
833 goto done;
834}
835
836/* Here come the merge functions */
837
838static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
839{
840 return error(ERRORMSG(o, ERROR_WOULD_OVERWRITE), ce->name);
841}
842
843static int same(struct cache_entry *a, struct cache_entry *b)
844{
845 if (!!a != !!b)
846 return 0;
847 if (!a && !b)
848 return 1;
849 if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
850 return 0;
851 return a->ce_mode == b->ce_mode &&
852 !hashcmp(a->sha1, b->sha1);
853}
854
855
856/*
857 * When a CE gets turned into an unmerged entry, we
858 * want it to be up-to-date
859 */
860static int verify_uptodate_1(struct cache_entry *ce,
861 struct unpack_trees_options *o,
862 const char *error_msg)
863{
864 struct stat st;
865
866 if (o->index_only || (!((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) && (o->reset || ce_uptodate(ce))))
867 return 0;
868
869 if (!lstat(ce->name, &st)) {
870 unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
871 if (!changed)
872 return 0;
873 /*
874 * NEEDSWORK: the current default policy is to allow
875 * submodule to be out of sync wrt the supermodule
876 * index. This needs to be tightened later for
877 * submodules that are marked to be automatically
878 * checked out.
879 */
880 if (S_ISGITLINK(ce->ce_mode))
881 return 0;
882 errno = 0;
883 }
884 if (errno == ENOENT)
885 return 0;
886 return o->gently ? -1 :
887 error(error_msg, ce->name);
888}
889
890static int verify_uptodate(struct cache_entry *ce,
891 struct unpack_trees_options *o)
892{
893 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
894 return 0;
895 return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_NOT_UPTODATE_FILE));
896}
897
898static int verify_uptodate_sparse(struct cache_entry *ce,
899 struct unpack_trees_options *o)
900{
901 return verify_uptodate_1(ce, o, ERRORMSG(o, ERROR_SPARSE_NOT_UPTODATE_FILE));
902}
903
904static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
905{
906 if (ce)
907 cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
908}
909
910/*
911 * Check that checking out ce->sha1 in subdir ce->name is not
912 * going to overwrite any working files.
913 *
914 * Currently, git does not checkout subprojects during a superproject
915 * checkout, so it is not going to overwrite anything.
916 */
917static int verify_clean_submodule(struct cache_entry *ce, const char *action,
918 struct unpack_trees_options *o)
919{
920 return 0;
921}
922
923static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
924 struct unpack_trees_options *o)
925{
926 /*
927 * we are about to extract "ce->name"; we would not want to lose
928 * anything in the existing directory there.
929 */
930 int namelen;
931 int i;
932 struct dir_struct d;
933 char *pathbuf;
934 int cnt = 0;
935 unsigned char sha1[20];
936
937 if (S_ISGITLINK(ce->ce_mode) &&
938 resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
939 /* If we are not going to update the submodule, then
940 * we don't care.
941 */
942 if (!hashcmp(sha1, ce->sha1))
943 return 0;
944 return verify_clean_submodule(ce, action, o);
945 }
946
947 /*
948 * First let's make sure we do not have a local modification
949 * in that directory.
950 */
951 namelen = strlen(ce->name);
952 for (i = locate_in_src_index(ce, o);
953 i < o->src_index->cache_nr;
954 i++) {
955 struct cache_entry *ce2 = o->src_index->cache[i];
956 int len = ce_namelen(ce2);
957 if (len < namelen ||
958 strncmp(ce->name, ce2->name, namelen) ||
959 ce2->name[namelen] != '/')
960 break;
961 /*
962 * ce2->name is an entry in the subdirectory to be
963 * removed.
964 */
965 if (!ce_stage(ce2)) {
966 if (verify_uptodate(ce2, o))
967 return -1;
968 add_entry(o, ce2, CE_REMOVE, 0);
969 mark_ce_used(ce2, o);
970 }
971 cnt++;
972 }
973
974 /*
975 * Then we need to make sure that we do not lose a locally
976 * present file that is not ignored.
977 */
978 pathbuf = xmalloc(namelen + 2);
979 memcpy(pathbuf, ce->name, namelen);
980 strcpy(pathbuf+namelen, "/");
981
982 memset(&d, 0, sizeof(d));
983 if (o->dir)
984 d.exclude_per_dir = o->dir->exclude_per_dir;
985 i = read_directory(&d, pathbuf, namelen+1, NULL);
986 if (i)
987 return o->gently ? -1 :
988 error(ERRORMSG(o, ERROR_NOT_UPTODATE_DIR), ce->name);
989 free(pathbuf);
990 return cnt;
991}
992
993/*
994 * This gets called when there was no index entry for the tree entry 'dst',
995 * but we found a file in the working tree that 'lstat()' said was fine,
996 * and we're on a case-insensitive filesystem.
997 *
998 * See if we can find a case-insensitive match in the index that also
999 * matches the stat information, and assume it's that other file!
1000 */
1001static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
1002{
1003 struct cache_entry *src;
1004
1005 src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
1006 return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
1007}
1008
1009/*
1010 * We do not want to remove or overwrite a working tree file that
1011 * is not tracked, unless it is ignored.
1012 */
1013static int verify_absent_1(struct cache_entry *ce, const char *action,
1014 struct unpack_trees_options *o,
1015 const char *error_msg)
1016{
1017 struct stat st;
1018
1019 if (o->index_only || o->reset || !o->update)
1020 return 0;
1021
1022 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
1023 return 0;
1024
1025 if (!lstat(ce->name, &st)) {
1026 int dtype = ce_to_dtype(ce);
1027 struct cache_entry *result;
1028
1029 /*
1030 * It may be that the 'lstat()' succeeded even though
1031 * target 'ce' was absent, because there is an old
1032 * entry that is different only in case..
1033 *
1034 * Ignore that lstat() if it matches.
1035 */
1036 if (ignore_case && icase_exists(o, ce, &st))
1037 return 0;
1038
1039 if (o->dir && excluded(o->dir, ce->name, &dtype))
1040 /*
1041 * ce->name is explicitly excluded, so it is Ok to
1042 * overwrite it.
1043 */
1044 return 0;
1045 if (S_ISDIR(st.st_mode)) {
1046 /*
1047 * We are checking out path "foo" and
1048 * found "foo/." in the working tree.
1049 * This is tricky -- if we have modified
1050 * files that are in "foo/" we would lose
1051 * them.
1052 */
1053 if (verify_clean_subdirectory(ce, action, o) < 0)
1054 return -1;
1055 return 0;
1056 }
1057
1058 /*
1059 * The previous round may already have decided to
1060 * delete this path, which is in a subdirectory that
1061 * is being replaced with a blob.
1062 */
1063 result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
1064 if (result) {
1065 if (result->ce_flags & CE_REMOVE)
1066 return 0;
1067 }
1068
1069 return o->gently ? -1 :
1070 error(ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED), ce->name, action);
1071 }
1072 return 0;
1073}
1074static int verify_absent(struct cache_entry *ce, const char *action,
1075 struct unpack_trees_options *o)
1076{
1077 if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
1078 return 0;
1079 return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_UNTRACKED));
1080}
1081
1082static int verify_absent_sparse(struct cache_entry *ce, const char *action,
1083 struct unpack_trees_options *o)
1084{
1085 return verify_absent_1(ce, action, o, ERRORMSG(o, ERROR_WOULD_LOSE_ORPHANED));
1086}
1087
1088static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1089 struct unpack_trees_options *o)
1090{
1091 int update = CE_UPDATE;
1092
1093 if (!old) {
1094 if (verify_absent(merge, "overwritten", o))
1095 return -1;
1096 invalidate_ce_path(merge, o);
1097 } else if (!(old->ce_flags & CE_CONFLICTED)) {
1098 /*
1099 * See if we can re-use the old CE directly?
1100 * That way we get the uptodate stat info.
1101 *
1102 * This also removes the UPDATE flag on a match; otherwise
1103 * we will end up overwriting local changes in the work tree.
1104 */
1105 if (same(old, merge)) {
1106 copy_cache_entry(merge, old);
1107 update = 0;
1108 } else {
1109 if (verify_uptodate(old, o))
1110 return -1;
1111 if (ce_skip_worktree(old))
1112 update |= CE_SKIP_WORKTREE;
1113 invalidate_ce_path(old, o);
1114 }
1115 } else {
1116 /*
1117 * Previously unmerged entry left as an existence
1118 * marker by read_index_unmerged();
1119 */
1120 invalidate_ce_path(old, o);
1121 }
1122
1123 add_entry(o, merge, update, CE_STAGEMASK);
1124 return 1;
1125}
1126
1127static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
1128 struct unpack_trees_options *o)
1129{
1130 /* Did it exist in the index? */
1131 if (!old) {
1132 if (verify_absent(ce, "removed", o))
1133 return -1;
1134 return 0;
1135 }
1136 if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
1137 return -1;
1138 add_entry(o, ce, CE_REMOVE, 0);
1139 invalidate_ce_path(ce, o);
1140 return 1;
1141}
1142
1143static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
1144{
1145 add_entry(o, ce, 0, 0);
1146 return 1;
1147}
1148
1149#if DBRT_DEBUG
1150static void show_stage_entry(FILE *o,
1151 const char *label, const struct cache_entry *ce)
1152{
1153 if (!ce)
1154 fprintf(o, "%s (missing)\n", label);
1155 else
1156 fprintf(o, "%s%06o %s %d\t%s\n",
1157 label,
1158 ce->ce_mode,
1159 sha1_to_hex(ce->sha1),
1160 ce_stage(ce),
1161 ce->name);
1162}
1163#endif
1164
1165int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
1166{
1167 struct cache_entry *index;
1168 struct cache_entry *head;
1169 struct cache_entry *remote = stages[o->head_idx + 1];
1170 int count;
1171 int head_match = 0;
1172 int remote_match = 0;
1173
1174 int df_conflict_head = 0;
1175 int df_conflict_remote = 0;
1176
1177 int any_anc_missing = 0;
1178 int no_anc_exists = 1;
1179 int i;
1180
1181 for (i = 1; i < o->head_idx; i++) {
1182 if (!stages[i] || stages[i] == o->df_conflict_entry)
1183 any_anc_missing = 1;
1184 else
1185 no_anc_exists = 0;
1186 }
1187
1188 index = stages[0];
1189 head = stages[o->head_idx];
1190
1191 if (head == o->df_conflict_entry) {
1192 df_conflict_head = 1;
1193 head = NULL;
1194 }
1195
1196 if (remote == o->df_conflict_entry) {
1197 df_conflict_remote = 1;
1198 remote = NULL;
1199 }
1200
1201 /*
1202 * First, if there's a #16 situation, note that to prevent #13
1203 * and #14.
1204 */
1205 if (!same(remote, head)) {
1206 for (i = 1; i < o->head_idx; i++) {
1207 if (same(stages[i], head)) {
1208 head_match = i;
1209 }
1210 if (same(stages[i], remote)) {
1211 remote_match = i;
1212 }
1213 }
1214 }
1215
1216 /*
1217 * We start with cases where the index is allowed to match
1218 * something other than the head: #14(ALT) and #2ALT, where it
1219 * is permitted to match the result instead.
1220 */
1221 /* #14, #14ALT, #2ALT */
1222 if (remote && !df_conflict_head && head_match && !remote_match) {
1223 if (index && !same(index, remote) && !same(index, head))
1224 return o->gently ? -1 : reject_merge(index, o);
1225 return merged_entry(remote, index, o);
1226 }
1227 /*
1228 * If we have an entry in the index cache, then we want to
1229 * make sure that it matches head.
1230 */
1231 if (index && !same(index, head))
1232 return o->gently ? -1 : reject_merge(index, o);
1233
1234 if (head) {
1235 /* #5ALT, #15 */
1236 if (same(head, remote))
1237 return merged_entry(head, index, o);
1238 /* #13, #3ALT */
1239 if (!df_conflict_remote && remote_match && !head_match)
1240 return merged_entry(head, index, o);
1241 }
1242
1243 /* #1 */
1244 if (!head && !remote && any_anc_missing)
1245 return 0;
1246
1247 /*
1248 * Under the "aggressive" rule, we resolve mostly trivial
1249 * cases that we historically had git-merge-one-file resolve.
1250 */
1251 if (o->aggressive) {
1252 int head_deleted = !head;
1253 int remote_deleted = !remote;
1254 struct cache_entry *ce = NULL;
1255
1256 if (index)
1257 ce = index;
1258 else if (head)
1259 ce = head;
1260 else if (remote)
1261 ce = remote;
1262 else {
1263 for (i = 1; i < o->head_idx; i++) {
1264 if (stages[i] && stages[i] != o->df_conflict_entry) {
1265 ce = stages[i];
1266 break;
1267 }
1268 }
1269 }
1270
1271 /*
1272 * Deleted in both.
1273 * Deleted in one and unchanged in the other.
1274 */
1275 if ((head_deleted && remote_deleted) ||
1276 (head_deleted && remote && remote_match) ||
1277 (remote_deleted && head && head_match)) {
1278 if (index)
1279 return deleted_entry(index, index, o);
1280 if (ce && !head_deleted) {
1281 if (verify_absent(ce, "removed", o))
1282 return -1;
1283 }
1284 return 0;
1285 }
1286 /*
1287 * Added in both, identically.
1288 */
1289 if (no_anc_exists && head && remote && same(head, remote))
1290 return merged_entry(head, index, o);
1291
1292 }
1293
1294 /* Below are "no merge" cases, which require that the index be
1295 * up-to-date to avoid the files getting overwritten with
1296 * conflict resolution files.
1297 */
1298 if (index) {
1299 if (verify_uptodate(index, o))
1300 return -1;
1301 }
1302
1303 o->nontrivial_merge = 1;
1304
1305 /* #2, #3, #4, #6, #7, #9, #10, #11. */
1306 count = 0;
1307 if (!head_match || !remote_match) {
1308 for (i = 1; i < o->head_idx; i++) {
1309 if (stages[i] && stages[i] != o->df_conflict_entry) {
1310 keep_entry(stages[i], o);
1311 count++;
1312 break;
1313 }
1314 }
1315 }
1316#if DBRT_DEBUG
1317 else {
1318 fprintf(stderr, "read-tree: warning #16 detected\n");
1319 show_stage_entry(stderr, "head ", stages[head_match]);
1320 show_stage_entry(stderr, "remote ", stages[remote_match]);
1321 }
1322#endif
1323 if (head) { count += keep_entry(head, o); }
1324 if (remote) { count += keep_entry(remote, o); }
1325 return count;
1326}
1327
1328/*
1329 * Two-way merge.
1330 *
1331 * The rule is to "carry forward" what is in the index without losing
1332 * information across a "fast-forward", favoring a successful merge
1333 * over a merge failure when it makes sense. For details of the
1334 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1335 *
1336 */
1337int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1338{
1339 struct cache_entry *current = src[0];
1340 struct cache_entry *oldtree = src[1];
1341 struct cache_entry *newtree = src[2];
1342
1343 if (o->merge_size != 2)
1344 return error("Cannot do a twoway merge of %d trees",
1345 o->merge_size);
1346
1347 if (oldtree == o->df_conflict_entry)
1348 oldtree = NULL;
1349 if (newtree == o->df_conflict_entry)
1350 newtree = NULL;
1351
1352 if (current) {
1353 if ((!oldtree && !newtree) || /* 4 and 5 */
1354 (!oldtree && newtree &&
1355 same(current, newtree)) || /* 6 and 7 */
1356 (oldtree && newtree &&
1357 same(oldtree, newtree)) || /* 14 and 15 */
1358 (oldtree && newtree &&
1359 !same(oldtree, newtree) && /* 18 and 19 */
1360 same(current, newtree))) {
1361 return keep_entry(current, o);
1362 }
1363 else if (oldtree && !newtree && same(current, oldtree)) {
1364 /* 10 or 11 */
1365 return deleted_entry(oldtree, current, o);
1366 }
1367 else if (oldtree && newtree &&
1368 same(current, oldtree) && !same(current, newtree)) {
1369 /* 20 or 21 */
1370 return merged_entry(newtree, current, o);
1371 }
1372 else {
1373 /* all other failures */
1374 if (oldtree)
1375 return o->gently ? -1 : reject_merge(oldtree, o);
1376 if (current)
1377 return o->gently ? -1 : reject_merge(current, o);
1378 if (newtree)
1379 return o->gently ? -1 : reject_merge(newtree, o);
1380 return -1;
1381 }
1382 }
1383 else if (newtree) {
1384 if (oldtree && !o->initial_checkout) {
1385 /*
1386 * deletion of the path was staged;
1387 */
1388 if (same(oldtree, newtree))
1389 return 1;
1390 return reject_merge(oldtree, o);
1391 }
1392 return merged_entry(newtree, current, o);
1393 }
1394 return deleted_entry(oldtree, current, o);
1395}
1396
1397/*
1398 * Bind merge.
1399 *
1400 * Keep the index entries at stage0, collapse stage1 but make sure
1401 * stage0 does not have anything there.
1402 */
1403int bind_merge(struct cache_entry **src,
1404 struct unpack_trees_options *o)
1405{
1406 struct cache_entry *old = src[0];
1407 struct cache_entry *a = src[1];
1408
1409 if (o->merge_size != 1)
1410 return error("Cannot do a bind merge of %d trees\n",
1411 o->merge_size);
1412 if (a && old)
1413 return o->gently ? -1 :
1414 error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);
1415 if (!a)
1416 return keep_entry(old, o);
1417 else
1418 return merged_entry(a, NULL, o);
1419}
1420
1421/*
1422 * One-way merge.
1423 *
1424 * The rule is:
1425 * - take the stat information from stage0, take the data from stage1
1426 */
1427int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1428{
1429 struct cache_entry *old = src[0];
1430 struct cache_entry *a = src[1];
1431
1432 if (o->merge_size != 1)
1433 return error("Cannot do a oneway merge of %d trees",
1434 o->merge_size);
1435
1436 if (!a || a == o->df_conflict_entry)
1437 return deleted_entry(old, old, o);
1438
1439 if (old && same(old, a)) {
1440 int update = 0;
1441 if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) {
1442 struct stat st;
1443 if (lstat(old->name, &st) ||
1444 ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
1445 update |= CE_UPDATE;
1446 }
1447 add_entry(o, old, update, 0);
1448 return 0;
1449 }
1450 return merged_entry(a, old, o);
1451}