git
/
unpack-trees.c
3061 строка · 84.3 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "advice.h"5#include "strvec.h"6#include "repository.h"7#include "parse.h"8#include "dir.h"9#include "environment.h"10#include "gettext.h"11#include "hex.h"12#include "name-hash.h"13#include "tree.h"14#include "tree-walk.h"15#include "cache-tree.h"16#include "unpack-trees.h"17#include "progress.h"18#include "refs.h"19#include "attr.h"20#include "read-cache.h"21#include "split-index.h"22#include "sparse-index.h"23#include "submodule.h"24#include "submodule-config.h"25#include "symlinks.h"26#include "trace2.h"27#include "fsmonitor.h"28#include "object-store-ll.h"29#include "promisor-remote.h"30#include "entry.h"31#include "parallel-checkout.h"32#include "setup.h"33
34/*
35* Error messages expected by scripts out of plumbing commands such as
36* read-tree. Non-scripted Porcelain is not required to use these messages
37* and in fact are encouraged to reword them to better suit their particular
38* situation better. See how "git checkout" and "git merge" replaces
39* them using setup_unpack_trees_porcelain(), for example.
40*/
41static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {42/* ERROR_WOULD_OVERWRITE */43"Entry '%s' would be overwritten by merge. Cannot merge.",44
45/* ERROR_NOT_UPTODATE_FILE */46"Entry '%s' not uptodate. Cannot merge.",47
48/* ERROR_NOT_UPTODATE_DIR */49"Updating '%s' would lose untracked files in it",50
51/* ERROR_CWD_IN_THE_WAY */52"Refusing to remove '%s' since it is the current working directory.",53
54/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */55"Untracked working tree file '%s' would be overwritten by merge.",56
57/* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */58"Untracked working tree file '%s' would be removed by merge.",59
60/* ERROR_BIND_OVERLAP */61"Entry '%s' overlaps with '%s'. Cannot bind.",62
63/* ERROR_WOULD_LOSE_SUBMODULE */64"Submodule '%s' cannot checkout new HEAD.",65
66/* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */67"",68
69/* WARNING_SPARSE_NOT_UPTODATE_FILE */70"Path '%s' not uptodate; will not remove from working tree.",71
72/* WARNING_SPARSE_UNMERGED_FILE */73"Path '%s' unmerged; will not remove from working tree.",74
75/* WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN */76"Path '%s' already present; will not overwrite with sparse update.",77};78
79#define ERRORMSG(o,type) \80( ((o) && (o)->internal.msgs[(type)]) \81? ((o)->internal.msgs[(type)]) \82: (unpack_plumbing_errors[(type)]) )83
84static const char *super_prefixed(const char *path, const char *super_prefix)85{
86/*87* It is necessary and sufficient to have two static buffers
88* here, as the return value of this function is fed to
89* error() using the unpack_*_errors[] templates we see above.
90*/
91static struct strbuf buf[2] = {STRBUF_INIT, STRBUF_INIT};92static int super_prefix_len = -1;93static unsigned idx = ARRAY_SIZE(buf) - 1;94
95if (super_prefix_len < 0) {96if (!super_prefix) {97super_prefix_len = 0;98} else {99int i;100for (i = 0; i < ARRAY_SIZE(buf); i++)101strbuf_addstr(&buf[i], super_prefix);102super_prefix_len = buf[0].len;103}104}105
106if (!super_prefix_len)107return path;108
109if (++idx >= ARRAY_SIZE(buf))110idx = 0;111
112strbuf_setlen(&buf[idx], super_prefix_len);113strbuf_addstr(&buf[idx], path);114
115return buf[idx].buf;116}
117
118void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,119const char *cmd)120{
121int i;122const char **msgs = opts->internal.msgs;123const char *msg;124
125strvec_init(&opts->internal.msgs_to_free);126
127if (!strcmp(cmd, "checkout"))128msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)129? _("Your local changes to the following files would be overwritten by checkout:\n%%s"130"Please commit your changes or stash them before you switch branches.")131: _("Your local changes to the following files would be overwritten by checkout:\n%%s");132else if (!strcmp(cmd, "merge"))133msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)134? _("Your local changes to the following files would be overwritten by merge:\n%%s"135"Please commit your changes or stash them before you merge.")136: _("Your local changes to the following files would be overwritten by merge:\n%%s");137else138msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)139? _("Your local changes to the following files would be overwritten by %s:\n%%s"140"Please commit your changes or stash them before you %s.")141: _("Your local changes to the following files would be overwritten by %s:\n%%s");142msgs[ERROR_WOULD_OVERWRITE] = msgs[ERROR_NOT_UPTODATE_FILE] =143strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);144
145msgs[ERROR_NOT_UPTODATE_DIR] =146_("Updating the following directories would lose untracked files in them:\n%s");147
148msgs[ERROR_CWD_IN_THE_WAY] =149_("Refusing to remove the current working directory:\n%s");150
151if (!strcmp(cmd, "checkout"))152msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)153? _("The following untracked working tree files would be removed by checkout:\n%%s"154"Please move or remove them before you switch branches.")155: _("The following untracked working tree files would be removed by checkout:\n%%s");156else if (!strcmp(cmd, "merge"))157msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)158? _("The following untracked working tree files would be removed by merge:\n%%s"159"Please move or remove them before you merge.")160: _("The following untracked working tree files would be removed by merge:\n%%s");161else162msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)163? _("The following untracked working tree files would be removed by %s:\n%%s"164"Please move or remove them before you %s.")165: _("The following untracked working tree files would be removed by %s:\n%%s");166msgs[ERROR_WOULD_LOSE_UNTRACKED_REMOVED] =167strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);168
169if (!strcmp(cmd, "checkout"))170msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)171? _("The following untracked working tree files would be overwritten by checkout:\n%%s"172"Please move or remove them before you switch branches.")173: _("The following untracked working tree files would be overwritten by checkout:\n%%s");174else if (!strcmp(cmd, "merge"))175msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)176? _("The following untracked working tree files would be overwritten by merge:\n%%s"177"Please move or remove them before you merge.")178: _("The following untracked working tree files would be overwritten by merge:\n%%s");179else180msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)181? _("The following untracked working tree files would be overwritten by %s:\n%%s"182"Please move or remove them before you %s.")183: _("The following untracked working tree files would be overwritten by %s:\n%%s");184msgs[ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN] =185strvec_pushf(&opts->internal.msgs_to_free, msg, cmd, cmd);186
187/*188* Special case: ERROR_BIND_OVERLAP refers to a pair of paths, we
189* cannot easily display it as a list.
190*/
191msgs[ERROR_BIND_OVERLAP] = _("Entry '%s' overlaps with '%s'. Cannot bind.");192
193msgs[ERROR_WOULD_LOSE_SUBMODULE] =194_("Cannot update submodule:\n%s");195
196msgs[WARNING_SPARSE_NOT_UPTODATE_FILE] =197_("The following paths are not up to date and were left despite sparse patterns:\n%s");198msgs[WARNING_SPARSE_UNMERGED_FILE] =199_("The following paths are unmerged and were left despite sparse patterns:\n%s");200msgs[WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN] =201_("The following paths were already present and thus not updated despite sparse patterns:\n%s");202
203opts->internal.show_all_errors = 1;204/* rejected paths may not have a static buffer */205for (i = 0; i < ARRAY_SIZE(opts->internal.unpack_rejects); i++)206opts->internal.unpack_rejects[i].strdup_strings = 1;207}
208
209void clear_unpack_trees_porcelain(struct unpack_trees_options *opts)210{
211strvec_clear(&opts->internal.msgs_to_free);212memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs));213discard_index(&opts->internal.result);214}
215
216static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce,217unsigned int set, unsigned int clear)218{
219clear |= CE_HASHED;220
221if (set & CE_REMOVE)222set |= CE_WT_REMOVE;223
224ce->ce_flags = (ce->ce_flags & ~clear) | set;225return add_index_entry(&o->internal.result, ce,226ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);227}
228
229static void add_entry(struct unpack_trees_options *o,230const struct cache_entry *ce,231unsigned int set, unsigned int clear)232{
233do_add_entry(o, dup_cache_entry(ce, &o->internal.result), set, clear);234}
235
236/*
237* add error messages on path <path>
238* corresponding to the type <e> with the message <msg>
239* indicating if it should be display in porcelain or not
240*/
241static int add_rejected_path(struct unpack_trees_options *o,242enum unpack_trees_error_types e,243const char *path)244{
245if (o->quiet)246return -1;247
248if (!o->internal.show_all_errors)249return error(ERRORMSG(o, e), super_prefixed(path,250o->super_prefix));251
252/*253* Otherwise, insert in a list for future display by
254* display_(error|warning)_msgs()
255*/
256string_list_append(&o->internal.unpack_rejects[e], path);257return -1;258}
259
260/*
261* display all the error messages stored in a nice way
262*/
263static void display_error_msgs(struct unpack_trees_options *o)264{
265int e;266unsigned error_displayed = 0;267for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {268struct string_list *rejects = &o->internal.unpack_rejects[e];269
270if (rejects->nr > 0) {271int i;272struct strbuf path = STRBUF_INIT;273
274error_displayed = 1;275for (i = 0; i < rejects->nr; i++)276strbuf_addf(&path, "\t%s\n", rejects->items[i].string);277error(ERRORMSG(o, e), super_prefixed(path.buf,278o->super_prefix));279strbuf_release(&path);280}281string_list_clear(rejects, 0);282}283if (error_displayed)284fprintf(stderr, _("Aborting\n"));285}
286
287/*
288* display all the warning messages stored in a nice way
289*/
290static void display_warning_msgs(struct unpack_trees_options *o)291{
292int e;293unsigned warning_displayed = 0;294for (e = NB_UNPACK_TREES_ERROR_TYPES + 1;295e < NB_UNPACK_TREES_WARNING_TYPES; e++) {296struct string_list *rejects = &o->internal.unpack_rejects[e];297
298if (rejects->nr > 0) {299int i;300struct strbuf path = STRBUF_INIT;301
302warning_displayed = 1;303for (i = 0; i < rejects->nr; i++)304strbuf_addf(&path, "\t%s\n", rejects->items[i].string);305warning(ERRORMSG(o, e), super_prefixed(path.buf,306o->super_prefix));307strbuf_release(&path);308}309string_list_clear(rejects, 0);310}311if (warning_displayed)312fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));313}
314static int check_submodule_move_head(const struct cache_entry *ce,315const char *old_id,316const char *new_id,317struct unpack_trees_options *o)318{
319unsigned flags = SUBMODULE_MOVE_HEAD_DRY_RUN;320const struct submodule *sub = submodule_from_ce(ce);321
322if (!sub)323return 0;324
325if (o->reset)326flags |= SUBMODULE_MOVE_HEAD_FORCE;327
328if (submodule_move_head(ce->name, o->super_prefix, old_id, new_id,329flags))330return add_rejected_path(o, ERROR_WOULD_LOSE_SUBMODULE, ce->name);331return 0;332}
333
334/*
335* Perform the loading of the repository's gitmodules file. This function is
336* used by 'check_update()' to perform loading of the gitmodules file in two
337* different situations:
338* (1) before removing entries from the working tree if the gitmodules file has
339* been marked for removal. This situation is specified by 'state' == NULL.
340* (2) before checking out entries to the working tree if the gitmodules file
341* has been marked for update. This situation is specified by 'state' != NULL.
342*/
343static void load_gitmodules_file(struct index_state *index,344struct checkout *state)345{
346int pos = index_name_pos(index, GITMODULES_FILE, strlen(GITMODULES_FILE));347
348if (pos >= 0) {349struct cache_entry *ce = index->cache[pos];350if (!state && ce->ce_flags & CE_WT_REMOVE) {351repo_read_gitmodules(the_repository, 0);352} else if (state && (ce->ce_flags & CE_UPDATE)) {353submodule_free(the_repository);354checkout_entry(ce, state, NULL, NULL);355repo_read_gitmodules(the_repository, 0);356}357}358}
359
360static struct progress *get_progress(struct unpack_trees_options *o,361struct index_state *index)362{
363unsigned cnt = 0, total = 0;364
365if (!o->update || !o->verbose_update)366return NULL;367
368for (; cnt < index->cache_nr; cnt++) {369const struct cache_entry *ce = index->cache[cnt];370if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))371total++;372}373
374return start_delayed_progress(_("Updating files"), total);375}
376
377static void setup_collided_checkout_detection(struct checkout *state,378struct index_state *index)379{
380int i;381
382state->clone = 1;383for (i = 0; i < index->cache_nr; i++)384index->cache[i]->ce_flags &= ~CE_MATCHED;385}
386
387static void report_collided_checkout(struct index_state *index)388{
389struct string_list list = STRING_LIST_INIT_NODUP;390int i;391
392for (i = 0; i < index->cache_nr; i++) {393struct cache_entry *ce = index->cache[i];394
395if (!(ce->ce_flags & CE_MATCHED))396continue;397
398string_list_append(&list, ce->name);399ce->ce_flags &= ~CE_MATCHED;400}401
402list.cmp = fspathcmp;403string_list_sort(&list);404
405if (list.nr) {406warning(_("the following paths have collided (e.g. case-sensitive paths\n"407"on a case-insensitive filesystem) and only one from the same\n"408"colliding group is in the working tree:\n"));409
410for (i = 0; i < list.nr; i++)411fprintf(stderr, " '%s'\n", list.items[i].string);412}413
414string_list_clear(&list, 0);415}
416
417static int must_checkout(const struct cache_entry *ce)418{
419return ce->ce_flags & CE_UPDATE;420}
421
422static int check_updates(struct unpack_trees_options *o,423struct index_state *index)424{
425unsigned cnt = 0;426int errs = 0;427struct progress *progress;428struct checkout state = CHECKOUT_INIT;429int i, pc_workers, pc_threshold;430
431trace_performance_enter();432state.super_prefix = o->super_prefix;433state.force = 1;434state.quiet = 1;435state.refresh_cache = 1;436state.istate = index;437clone_checkout_metadata(&state.meta, &o->meta, NULL);438
439if (!o->update || o->dry_run) {440remove_marked_cache_entries(index, 0);441trace_performance_leave("check_updates");442return 0;443}444
445if (o->clone)446setup_collided_checkout_detection(&state, index);447
448progress = get_progress(o, index);449
450/* Start with clean cache to avoid using any possibly outdated info. */451invalidate_lstat_cache();452
453git_attr_set_direction(GIT_ATTR_CHECKOUT);454
455if (should_update_submodules())456load_gitmodules_file(index, NULL);457
458for (i = 0; i < index->cache_nr; i++) {459const struct cache_entry *ce = index->cache[i];460
461if (ce->ce_flags & CE_WT_REMOVE) {462display_progress(progress, ++cnt);463unlink_entry(ce, o->super_prefix);464}465}466
467remove_marked_cache_entries(index, 0);468remove_scheduled_dirs();469
470if (should_update_submodules())471load_gitmodules_file(index, &state);472
473if (repo_has_promisor_remote(the_repository))474/*475* Prefetch the objects that are to be checked out in the loop
476* below.
477*/
478prefetch_cache_entries(index, must_checkout);479
480get_parallel_checkout_configs(&pc_workers, &pc_threshold);481
482enable_delayed_checkout(&state);483if (pc_workers > 1)484init_parallel_checkout();485for (i = 0; i < index->cache_nr; i++) {486struct cache_entry *ce = index->cache[i];487
488if (must_checkout(ce)) {489size_t last_pc_queue_size = pc_queue_size();490
491if (ce->ce_flags & CE_WT_REMOVE)492BUG("both update and delete flags are set on %s",493ce->name);494ce->ce_flags &= ~CE_UPDATE;495errs |= checkout_entry(ce, &state, NULL, NULL);496
497if (last_pc_queue_size == pc_queue_size())498display_progress(progress, ++cnt);499}500}501if (pc_workers > 1)502errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,503progress, &cnt);504stop_progress(&progress);505errs |= finish_delayed_checkout(&state, o->verbose_update);506git_attr_set_direction(GIT_ATTR_CHECKIN);507
508if (o->clone)509report_collided_checkout(index);510
511trace_performance_leave("check_updates");512return errs != 0;513}
514
515static int verify_uptodate_sparse(const struct cache_entry *ce,516struct unpack_trees_options *o);517static int verify_absent_sparse(const struct cache_entry *ce,518enum unpack_trees_error_types,519struct unpack_trees_options *o);520
521static int apply_sparse_checkout(struct index_state *istate,522struct cache_entry *ce,523struct unpack_trees_options *o)524{
525int was_skip_worktree = ce_skip_worktree(ce);526
527if (ce->ce_flags & CE_NEW_SKIP_WORKTREE)528ce->ce_flags |= CE_SKIP_WORKTREE;529else530ce->ce_flags &= ~CE_SKIP_WORKTREE;531if (was_skip_worktree != ce_skip_worktree(ce)) {532ce->ce_flags |= CE_UPDATE_IN_BASE;533mark_fsmonitor_invalid(istate, ce);534istate->cache_changed |= CE_ENTRY_CHANGED;535}536
537/*538* if (!was_skip_worktree && !ce_skip_worktree()) {
539* This is perfectly normal. Move on;
540* }
541*/
542
543/*544* Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
545* area as a result of ce_skip_worktree() shortcuts in
546* verify_absent() and verify_uptodate().
547* Make sure they don't modify worktree if they are already
548* outside checkout area
549*/
550if (was_skip_worktree && ce_skip_worktree(ce)) {551ce->ce_flags &= ~CE_UPDATE;552
553/*554* By default, when CE_REMOVE is on, CE_WT_REMOVE is also
555* on to get that file removed from both index and worktree.
556* If that file is already outside worktree area, don't
557* bother remove it.
558*/
559if (ce->ce_flags & CE_REMOVE)560ce->ce_flags &= ~CE_WT_REMOVE;561}562
563if (!was_skip_worktree && ce_skip_worktree(ce)) {564/*565* If CE_UPDATE is set, verify_uptodate() must be called already
566* also stat info may have lost after merged_entry() so calling
567* verify_uptodate() again may fail
568*/
569if (!(ce->ce_flags & CE_UPDATE) &&570verify_uptodate_sparse(ce, o)) {571ce->ce_flags &= ~CE_SKIP_WORKTREE;572return -1;573}574ce->ce_flags |= CE_WT_REMOVE;575ce->ce_flags &= ~CE_UPDATE;576}577if (was_skip_worktree && !ce_skip_worktree(ce)) {578if (verify_absent_sparse(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))579return -1;580ce->ce_flags |= CE_UPDATE;581}582return 0;583}
584
585static int warn_conflicted_path(struct index_state *istate,586int i,587struct unpack_trees_options *o)588{
589char *conflicting_path = istate->cache[i]->name;590int count = 0;591
592add_rejected_path(o, WARNING_SPARSE_UNMERGED_FILE, conflicting_path);593
594/* Find out how many higher stage entries are at same path */595while ((++count) + i < istate->cache_nr &&596!strcmp(conflicting_path, istate->cache[count + i]->name))597; /* do nothing */598
599return count;600}
601
602static inline int call_unpack_fn(const struct cache_entry * const *src,603struct unpack_trees_options *o)604{
605int ret = o->fn(src, o);606if (ret > 0)607ret = 0;608return ret;609}
610
611static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)612{
613ce->ce_flags |= CE_UNPACKED;614
615if (o->internal.cache_bottom < o->src_index->cache_nr &&616o->src_index->cache[o->internal.cache_bottom] == ce) {617int bottom = o->internal.cache_bottom;618
619while (bottom < o->src_index->cache_nr &&620o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)621bottom++;622o->internal.cache_bottom = bottom;623}624}
625
626static void mark_all_ce_unused(struct index_state *index)627{
628int i;629for (i = 0; i < index->cache_nr; i++)630index->cache[i]->ce_flags &= ~(CE_UNPACKED | CE_ADDED | CE_NEW_SKIP_WORKTREE);631}
632
633static int locate_in_src_index(const struct cache_entry *ce,634struct unpack_trees_options *o)635{
636struct index_state *index = o->src_index;637int len = ce_namelen(ce);638int pos = index_name_pos(index, ce->name, len);639if (pos < 0)640pos = -1 - pos;641return pos;642}
643
644/*
645* We call unpack_index_entry() with an unmerged cache entry
646* only in diff-index, and it wants a single callback. Skip
647* the other unmerged entry with the same name.
648*/
649static void mark_ce_used_same_name(struct cache_entry *ce,650struct unpack_trees_options *o)651{
652struct index_state *index = o->src_index;653int len = ce_namelen(ce);654int pos;655
656for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {657struct cache_entry *next = index->cache[pos];658if (len != ce_namelen(next) ||659memcmp(ce->name, next->name, len))660break;661mark_ce_used(next, o);662}663}
664
665static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)666{
667const struct index_state *index = o->src_index;668int pos = o->internal.cache_bottom;669
670while (pos < index->cache_nr) {671struct cache_entry *ce = index->cache[pos];672if (!(ce->ce_flags & CE_UNPACKED))673return ce;674pos++;675}676return NULL;677}
678
679static void add_same_unmerged(const struct cache_entry *ce,680struct unpack_trees_options *o)681{
682struct index_state *index = o->src_index;683int len = ce_namelen(ce);684int pos = index_name_pos(index, ce->name, len);685
686if (0 <= pos)687die("programming error in a caller of mark_ce_used_same_name");688for (pos = -pos - 1; pos < index->cache_nr; pos++) {689struct cache_entry *next = index->cache[pos];690if (len != ce_namelen(next) ||691memcmp(ce->name, next->name, len))692break;693add_entry(o, next, 0, 0);694mark_ce_used(next, o);695}696}
697
698static int unpack_index_entry(struct cache_entry *ce,699struct unpack_trees_options *o)700{
701const struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };702int ret;703
704src[0] = ce;705
706mark_ce_used(ce, o);707if (ce_stage(ce)) {708if (o->skip_unmerged) {709add_entry(o, ce, 0, 0);710return 0;711}712}713ret = call_unpack_fn(src, o);714if (ce_stage(ce))715mark_ce_used_same_name(ce, o);716return ret;717}
718
719static int find_cache_pos(struct traverse_info *, const char *p, size_t len);720
721static void restore_cache_bottom(struct traverse_info *info, int bottom)722{
723struct unpack_trees_options *o = info->data;724
725if (o->diff_index_cached)726return;727o->internal.cache_bottom = bottom;728}
729
730static int switch_cache_bottom(struct traverse_info *info)731{
732struct unpack_trees_options *o = info->data;733int ret, pos;734
735if (o->diff_index_cached)736return 0;737ret = o->internal.cache_bottom;738pos = find_cache_pos(info->prev, info->name, info->namelen);739
740if (pos < -1)741o->internal.cache_bottom = -2 - pos;742else if (pos < 0)743o->internal.cache_bottom = o->src_index->cache_nr;744return ret;745}
746
747static inline int are_same_oid(struct name_entry *name_j, struct name_entry *name_k)748{
749return !is_null_oid(&name_j->oid) && !is_null_oid(&name_k->oid) && oideq(&name_j->oid, &name_k->oid);750}
751
752static int all_trees_same_as_cache_tree(int n, unsigned long dirmask,753struct name_entry *names,754struct traverse_info *info)755{
756struct unpack_trees_options *o = info->data;757int i;758
759if (!o->merge || dirmask != ((1 << n) - 1))760return 0;761
762for (i = 1; i < n; i++)763if (!are_same_oid(names, names + i))764return 0;765
766return cache_tree_matches_traversal(o->src_index->cache_tree, names, info);767}
768
769static int index_pos_by_traverse_info(struct name_entry *names,770struct traverse_info *info)771{
772struct unpack_trees_options *o = info->data;773struct strbuf name = STRBUF_INIT;774int pos;775
776strbuf_make_traverse_path(&name, info, names->path, names->pathlen);777strbuf_addch(&name, '/');778pos = index_name_pos(o->src_index, name.buf, name.len);779if (pos >= 0) {780if (!o->src_index->sparse_index ||781!(o->src_index->cache[pos]->ce_flags & CE_SKIP_WORKTREE))782BUG("This is a directory and should not exist in index");783} else {784pos = -pos - 1;785}786if (pos >= o->src_index->cache_nr ||787!starts_with(o->src_index->cache[pos]->name, name.buf) ||788(pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf)))789BUG("pos %d doesn't point to the first entry of %s in index",790pos, name.buf);791strbuf_release(&name);792return pos;793}
794
795/*
796* Fast path if we detect that all trees are the same as cache-tree at this
797* path. We'll walk these trees in an iterative loop using cache-tree/index
798* instead of ODB since we already know what these trees contain.
799*/
800static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,801struct traverse_info *info)802{
803struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };804struct unpack_trees_options *o = info->data;805struct cache_entry *tree_ce = NULL;806int ce_len = 0;807int i, d;808
809if (!o->merge)810BUG("We need cache-tree to do this optimization");811
812/*813* Do what unpack_callback() and unpack_single_entry() normally
814* do. But we walk all paths in an iterative loop instead.
815*
816* D/F conflicts and higher stage entries are not a concern
817* because cache-tree would be invalidated and we would never
818* get here in the first place.
819*/
820for (i = 0; i < nr_entries; i++) {821int new_ce_len, len, rc;822
823src[0] = o->src_index->cache[pos + i];824
825len = ce_namelen(src[0]);826new_ce_len = cache_entry_size(len);827
828if (new_ce_len > ce_len) {829new_ce_len <<= 1;830tree_ce = xrealloc(tree_ce, new_ce_len);831memset(tree_ce, 0, new_ce_len);832ce_len = new_ce_len;833
834tree_ce->ce_flags = create_ce_flags(0);835
836for (d = 1; d <= nr_names; d++)837src[d] = tree_ce;838}839
840tree_ce->ce_mode = src[0]->ce_mode;841tree_ce->ce_namelen = len;842oidcpy(&tree_ce->oid, &src[0]->oid);843memcpy(tree_ce->name, src[0]->name, len + 1);844
845rc = call_unpack_fn((const struct cache_entry * const *)src, o);846if (rc < 0) {847free(tree_ce);848return rc;849}850
851mark_ce_used(src[0], o);852}853free(tree_ce);854if (o->internal.debug_unpack)855printf("Unpacked %d entries from %s to %s using cache-tree\n",856nr_entries,857o->src_index->cache[pos]->name,858o->src_index->cache[pos + nr_entries - 1]->name);859return 0;860}
861
862static int traverse_trees_recursive(int n, unsigned long dirmask,863unsigned long df_conflicts,864struct name_entry *names,865struct traverse_info *info)866{
867struct unpack_trees_options *o = info->data;868int i, ret, bottom;869int nr_buf = 0;870struct tree_desc *t;871void **buf;872struct traverse_info newinfo;873struct name_entry *p;874int nr_entries;875
876nr_entries = all_trees_same_as_cache_tree(n, dirmask, names, info);877if (nr_entries > 0) {878int pos = index_pos_by_traverse_info(names, info);879
880if (!o->merge || df_conflicts)881BUG("Wrong condition to get here buddy");882
883/*884* All entries up to 'pos' must have been processed
885* (i.e. marked CE_UNPACKED) at this point. But to be safe,
886* save and restore cache_bottom anyway to not miss
887* unprocessed entries before 'pos'.
888*/
889bottom = o->internal.cache_bottom;890ret = traverse_by_cache_tree(pos, nr_entries, n, info);891o->internal.cache_bottom = bottom;892return ret;893}894
895p = names;896while (!p->mode)897p++;898
899newinfo = *info;900newinfo.prev = info;901newinfo.pathspec = info->pathspec;902newinfo.name = p->path;903newinfo.namelen = p->pathlen;904newinfo.mode = p->mode;905newinfo.pathlen = st_add3(newinfo.pathlen, tree_entry_len(p), 1);906newinfo.df_conflicts |= df_conflicts;907
908ALLOC_ARRAY(t, n);909ALLOC_ARRAY(buf, n);910
911/*912* Fetch the tree from the ODB for each peer directory in the
913* n commits.
914*
915* For 2- and 3-way traversals, we try to avoid hitting the
916* ODB twice for the same OID. This should yield a nice speed
917* up in checkouts and merges when the commits are similar.
918*
919* We don't bother doing the full O(n^2) search for larger n,
920* because wider traversals don't happen that often and we
921* avoid the search setup.
922*
923* When 2 peer OIDs are the same, we just copy the tree
924* descriptor data. This implicitly borrows the buffer
925* data from the earlier cell.
926*/
927for (i = 0; i < n; i++, dirmask >>= 1) {928if (i > 0 && are_same_oid(&names[i], &names[i - 1]))929t[i] = t[i - 1];930else if (i > 1 && are_same_oid(&names[i], &names[i - 2]))931t[i] = t[i - 2];932else {933const struct object_id *oid = NULL;934if (dirmask & 1)935oid = &names[i].oid;936buf[nr_buf++] = fill_tree_descriptor(the_repository, t + i, oid);937}938}939
940bottom = switch_cache_bottom(&newinfo);941ret = traverse_trees(o->src_index, n, t, &newinfo);942restore_cache_bottom(&newinfo, bottom);943
944for (i = 0; i < nr_buf; i++)945free(buf[i]);946free(buf);947free(t);948
949return ret;950}
951
952/*
953* Compare the traverse-path to the cache entry without actually
954* having to generate the textual representation of the traverse
955* path.
956*
957* NOTE! This *only* compares up to the size of the traverse path
958* itself - the caller needs to do the final check for the cache
959* entry having more data at the end!
960*/
961static int do_compare_entry_piecewise(const struct cache_entry *ce,962const struct traverse_info *info,963const char *name, size_t namelen,964unsigned mode)965{
966int pathlen, ce_len;967const char *ce_name;968
969if (info->prev) {970int cmp = do_compare_entry_piecewise(ce, info->prev,971info->name, info->namelen,972info->mode);973if (cmp)974return cmp;975}976pathlen = info->pathlen;977ce_len = ce_namelen(ce);978
979/* If ce_len < pathlen then we must have previously hit "name == directory" entry */980if (ce_len < pathlen)981return -1;982
983ce_len -= pathlen;984ce_name = ce->name + pathlen;985
986return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);987}
988
989static int do_compare_entry(const struct cache_entry *ce,990const struct traverse_info *info,991const char *name, size_t namelen,992unsigned mode)993{
994int pathlen, ce_len;995const char *ce_name;996int cmp;997unsigned ce_mode;998
999/*1000* If we have not precomputed the traverse path, it is quicker
1001* to avoid doing so. But if we have precomputed it,
1002* it is quicker to use the precomputed version.
1003*/
1004if (!info->traverse_path)1005return do_compare_entry_piecewise(ce, info, name, namelen, mode);1006
1007cmp = strncmp(ce->name, info->traverse_path, info->pathlen);1008if (cmp)1009return cmp;1010
1011pathlen = info->pathlen;1012ce_len = ce_namelen(ce);1013
1014if (ce_len < pathlen)1015return -1;1016
1017ce_len -= pathlen;1018ce_name = ce->name + pathlen;1019
1020ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG;1021return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode);1022}
1023
1024static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)1025{
1026int cmp = do_compare_entry(ce, info, n->path, n->pathlen, n->mode);1027if (cmp)1028return cmp;1029
1030/*1031* At this point, we know that we have a prefix match. If ce
1032* is a sparse directory, then allow an exact match. This only
1033* works when the input name is a directory, since ce->name
1034* ends in a directory separator.
1035*/
1036if (S_ISSPARSEDIR(ce->ce_mode) &&1037ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1)1038return 0;1039
1040/*1041* Even if the beginning compared identically, the ce should
1042* compare as bigger than a directory leading up to it!
1043*/
1044return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));1045}
1046
1047static int ce_in_traverse_path(const struct cache_entry *ce,1048const struct traverse_info *info)1049{
1050if (!info->prev)1051return 1;1052if (do_compare_entry(ce, info->prev,1053info->name, info->namelen, info->mode))1054return 0;1055/*1056* If ce (blob) is the same name as the path (which is a tree
1057* we will be descending into), it won't be inside it.
1058*/
1059return (info->pathlen < ce_namelen(ce));1060}
1061
1062static struct cache_entry *create_ce_entry(const struct traverse_info *info,1063const struct name_entry *n,1064int stage,1065struct index_state *istate,1066int is_transient,1067int is_sparse_directory)1068{
1069size_t len = traverse_path_len(info, tree_entry_len(n));1070size_t alloc_len = is_sparse_directory ? len + 1 : len;1071struct cache_entry *ce =1072is_transient ?1073make_empty_transient_cache_entry(alloc_len, NULL) :1074make_empty_cache_entry(istate, alloc_len);1075
1076ce->ce_mode = create_ce_mode(n->mode);1077ce->ce_flags = create_ce_flags(stage);1078ce->ce_namelen = len;1079oidcpy(&ce->oid, &n->oid);1080/* len+1 because the cache_entry allocates space for NUL */1081make_traverse_path(ce->name, len + 1, info, n->path, n->pathlen);1082
1083if (is_sparse_directory) {1084ce->name[len] = '/';1085ce->name[len + 1] = '\0';1086ce->ce_namelen++;1087ce->ce_flags |= CE_SKIP_WORKTREE;1088}1089
1090return ce;1091}
1092
1093/*
1094* Determine whether the path specified by 'p' should be unpacked as a new
1095* sparse directory in a sparse index. A new sparse directory 'A/':
1096* - must be outside the sparse cone.
1097* - must not already be in the index (i.e., no index entry with name 'A/'
1098* exists).
1099* - must not have any child entries in the index (i.e., no index entry
1100* 'A/<something>' exists).
1101* If 'p' meets the above requirements, return 1; otherwise, return 0.
1102*/
1103static int entry_is_new_sparse_dir(const struct traverse_info *info,1104const struct name_entry *p)1105{
1106int res, pos;1107struct strbuf dirpath = STRBUF_INIT;1108struct unpack_trees_options *o = info->data;1109
1110if (!S_ISDIR(p->mode))1111return 0;1112
1113/*1114* If the path is inside the sparse cone, it can't be a sparse directory.
1115*/
1116strbuf_add(&dirpath, info->traverse_path, info->pathlen);1117strbuf_add(&dirpath, p->path, p->pathlen);1118strbuf_addch(&dirpath, '/');1119if (path_in_cone_mode_sparse_checkout(dirpath.buf, o->src_index)) {1120res = 0;1121goto cleanup;1122}1123
1124pos = index_name_pos_sparse(o->src_index, dirpath.buf, dirpath.len);1125if (pos >= 0) {1126/* Path is already in the index, not a new sparse dir */1127res = 0;1128goto cleanup;1129}1130
1131/* Where would this sparse dir be inserted into the index? */1132pos = -pos - 1;1133if (pos >= o->src_index->cache_nr) {1134/*1135* Sparse dir would be inserted at the end of the index, so we
1136* know it has no child entries.
1137*/
1138res = 1;1139goto cleanup;1140}1141
1142/*1143* If the dir has child entries in the index, the first would be at the
1144* position the sparse directory would be inserted. If the entry at this
1145* position is inside the dir, not a new sparse dir.
1146*/
1147res = strncmp(o->src_index->cache[pos]->name, dirpath.buf, dirpath.len);1148
1149cleanup:1150strbuf_release(&dirpath);1151return res;1152}
1153
1154/*
1155* Note that traverse_by_cache_tree() duplicates some logic in this function
1156* without actually calling it. If you change the logic here you may need to
1157* check and change there as well.
1158*/
1159static int unpack_single_entry(int n, unsigned long mask,1160unsigned long dirmask,1161struct cache_entry **src,1162const struct name_entry *names,1163const struct traverse_info *info,1164int *is_new_sparse_dir)1165{
1166int i;1167struct unpack_trees_options *o = info->data;1168unsigned long conflicts = info->df_conflicts | dirmask;1169const struct name_entry *p = names;1170
1171*is_new_sparse_dir = 0;1172if (mask == dirmask && !src[0]) {1173/*1174* If we're not in a sparse index, we can't unpack a directory
1175* without recursing into it, so we return.
1176*/
1177if (!o->src_index->sparse_index)1178return 0;1179
1180/* Find first entry with a real name (we could use "mask" too) */1181while (!p->mode)1182p++;1183
1184/*1185* If the directory is completely missing from the index but
1186* would otherwise be a sparse directory, we should unpack it.
1187* If not, we'll return and continue recursively traversing the
1188* tree.
1189*/
1190*is_new_sparse_dir = entry_is_new_sparse_dir(info, p);1191if (!*is_new_sparse_dir)1192return 0;1193}1194
1195/*1196* When we are unpacking a sparse directory, then this isn't necessarily
1197* a directory-file conflict.
1198*/
1199if (mask == dirmask &&1200(*is_new_sparse_dir || (src[0] && S_ISSPARSEDIR(src[0]->ce_mode))))1201conflicts = 0;1202
1203/*1204* Ok, we've filled in up to any potential index entry in src[0],
1205* now do the rest.
1206*/
1207for (i = 0; i < n; i++) {1208int stage;1209unsigned int bit = 1ul << i;1210if (conflicts & bit) {1211src[i + o->merge] = o->df_conflict_entry;1212continue;1213}1214if (!(mask & bit))1215continue;1216if (!o->merge)1217stage = 0;1218else if (i + 1 < o->head_idx)1219stage = 1;1220else if (i + 1 > o->head_idx)1221stage = 3;1222else1223stage = 2;1224
1225/*1226* If the merge bit is set, then the cache entries are
1227* discarded in the following block. In this case,
1228* construct "transient" cache_entries, as they are
1229* not stored in the index. otherwise construct the
1230* cache entry from the index aware logic.
1231*/
1232src[i + o->merge] = create_ce_entry(info, names + i, stage,1233&o->internal.result,1234o->merge, bit & dirmask);1235}1236
1237if (o->merge) {1238int rc = call_unpack_fn((const struct cache_entry * const *)src,1239o);1240for (i = 0; i < n; i++) {1241struct cache_entry *ce = src[i + o->merge];1242if (ce != o->df_conflict_entry)1243discard_cache_entry(ce);1244}1245return rc;1246}1247
1248for (i = 0; i < n; i++)1249if (src[i] && src[i] != o->df_conflict_entry)1250if (do_add_entry(o, src[i], 0, 0))1251return -1;1252
1253return 0;1254}
1255
1256static int unpack_failed(struct unpack_trees_options *o, const char *message)1257{
1258discard_index(&o->internal.result);1259if (!o->quiet && !o->exiting_early) {1260if (message)1261return error("%s", message);1262return -1;1263}1264return -1;1265}
1266
1267/*
1268* The tree traversal is looking at name p. If we have a matching entry,
1269* return it. If name p is a directory in the index, do not return
1270* anything, as we will want to match it when the traversal descends into
1271* the directory.
1272*/
1273static int find_cache_pos(struct traverse_info *info,1274const char *p, size_t p_len)1275{
1276int pos;1277struct unpack_trees_options *o = info->data;1278struct index_state *index = o->src_index;1279int pfxlen = info->pathlen;1280
1281for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) {1282const struct cache_entry *ce = index->cache[pos];1283const char *ce_name, *ce_slash;1284int cmp, ce_len;1285
1286if (ce->ce_flags & CE_UNPACKED) {1287/*1288* cache_bottom entry is already unpacked, so
1289* we can never match it; don't check it
1290* again.
1291*/
1292if (pos == o->internal.cache_bottom)1293++o->internal.cache_bottom;1294continue;1295}1296if (!ce_in_traverse_path(ce, info)) {1297/*1298* Check if we can skip future cache checks
1299* (because we're already past all possible
1300* entries in the traverse path).
1301*/
1302if (info->traverse_path) {1303if (strncmp(ce->name, info->traverse_path,1304info->pathlen) > 0)1305break;1306}1307continue;1308}1309ce_name = ce->name + pfxlen;1310ce_slash = strchr(ce_name, '/');1311if (ce_slash)1312ce_len = ce_slash - ce_name;1313else1314ce_len = ce_namelen(ce) - pfxlen;1315cmp = name_compare(p, p_len, ce_name, ce_len);1316/*1317* Exact match; if we have a directory we need to
1318* delay returning it.
1319*/
1320if (!cmp)1321return ce_slash ? -2 - pos : pos;1322if (0 < cmp)1323continue; /* keep looking */1324/*1325* ce_name sorts after p->path; could it be that we
1326* have files under p->path directory in the index?
1327* E.g. ce_name == "t-i", and p->path == "t"; we may
1328* have "t/a" in the index.
1329*/
1330if (p_len < ce_len && !memcmp(ce_name, p, p_len) &&1331ce_name[p_len] < '/')1332continue; /* keep looking */1333break;1334}1335return -1;1336}
1337
1338/*
1339* Given a sparse directory entry 'ce', compare ce->name to
1340* info->traverse_path + p->path + '/' if info->traverse_path
1341* is non-empty.
1342*
1343* Compare ce->name to p->path + '/' otherwise. Note that
1344* ce->name must end in a trailing '/' because it is a sparse
1345* directory entry.
1346*/
1347static int sparse_dir_matches_path(const struct cache_entry *ce,1348struct traverse_info *info,1349const struct name_entry *p)1350{
1351assert(S_ISSPARSEDIR(ce->ce_mode));1352assert(ce->name[ce->ce_namelen - 1] == '/');1353
1354if (info->pathlen)1355return ce->ce_namelen == info->pathlen + p->pathlen + 1 &&1356ce->name[info->pathlen - 1] == '/' &&1357!strncmp(ce->name, info->traverse_path, info->pathlen) &&1358!strncmp(ce->name + info->pathlen, p->path, p->pathlen);1359return ce->ce_namelen == p->pathlen + 1 &&1360!strncmp(ce->name, p->path, p->pathlen);1361}
1362
1363static struct cache_entry *find_cache_entry(struct traverse_info *info,1364const struct name_entry *p)1365{
1366const char *path;1367int pos = find_cache_pos(info, p->path, p->pathlen);1368struct unpack_trees_options *o = info->data;1369
1370if (0 <= pos)1371return o->src_index->cache[pos];1372
1373/*1374* Check for a sparse-directory entry named "path/".
1375* Due to the input p->path not having a trailing
1376* slash, the negative 'pos' value overshoots the
1377* expected position, hence "-2" instead of "-1".
1378*/
1379pos = -pos - 2;1380
1381if (pos < 0 || pos >= o->src_index->cache_nr)1382return NULL;1383
1384/*1385* Due to lexicographic sorting and sparse directory
1386* entries ending with a trailing slash, our path as a
1387* sparse directory (e.g "subdir/") and our path as a
1388* file (e.g. "subdir") might be separated by other
1389* paths (e.g. "subdir-").
1390*/
1391while (pos >= 0) {1392struct cache_entry *ce = o->src_index->cache[pos];1393
1394if (!skip_prefix(ce->name, info->traverse_path, &path) ||1395strncmp(path, p->path, p->pathlen) ||1396path[p->pathlen] != '/')1397return NULL;1398
1399if (S_ISSPARSEDIR(ce->ce_mode) &&1400sparse_dir_matches_path(ce, info, p))1401return ce;1402
1403pos--;1404}1405
1406return NULL;1407}
1408
1409static void debug_path(struct traverse_info *info)1410{
1411if (info->prev) {1412debug_path(info->prev);1413if (*info->prev->name)1414putchar('/');1415}1416printf("%s", info->name);1417}
1418
1419static void debug_name_entry(int i, struct name_entry *n)1420{
1421printf("ent#%d %06o %s\n", i,1422n->path ? n->mode : 0,1423n->path ? n->path : "(missing)");1424}
1425
1426static void debug_unpack_callback(int n,1427unsigned long mask,1428unsigned long dirmask,1429struct name_entry *names,1430struct traverse_info *info)1431{
1432int i;1433printf("* unpack mask %lu, dirmask %lu, cnt %d ",1434mask, dirmask, n);1435debug_path(info);1436putchar('\n');1437for (i = 0; i < n; i++)1438debug_name_entry(i, names + i);1439}
1440
1441/*
1442* Returns true if and only if the given cache_entry is a
1443* sparse-directory entry that matches the given name_entry
1444* from the tree walk at the given traverse_info.
1445*/
1446static int is_sparse_directory_entry(struct cache_entry *ce,1447const struct name_entry *name,1448struct traverse_info *info)1449{
1450if (!ce || !name || !S_ISSPARSEDIR(ce->ce_mode))1451return 0;1452
1453return sparse_dir_matches_path(ce, info, name);1454}
1455
1456static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)1457{
1458struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };1459struct unpack_trees_options *o = info->data;1460int ret, is_new_sparse_dir;1461
1462assert(o->merge);1463
1464/*1465* Unlike in 'unpack_callback', where src[0] is derived from the index when
1466* merging, src[0] is a transient cache entry derived from the first tree
1467* provided. Create the temporary entry as if it came from a non-sparse index.
1468*/
1469if (!is_null_oid(&names[0].oid)) {1470src[0] = create_ce_entry(info, &names[0], 0,1471&o->internal.result, 1,1472dirmask & (1ul << 0));1473src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);1474}1475
1476/*1477* 'unpack_single_entry' assumes that src[0] is derived directly from
1478* the index, rather than from an entry in 'names'. This is *not* true when
1479* merging a sparse directory, in which case names[0] is the "index" source
1480* entry. To match the expectations of 'unpack_single_entry', shift past the
1481* "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1482* 'dirmask' accordingly.
1483*/
1484ret = unpack_single_entry(n - 1, mask >> 1, dirmask >> 1, src, names + 1, info, &is_new_sparse_dir);1485
1486if (src[0])1487discard_cache_entry(src[0]);1488
1489return ret >= 0 ? mask : -1;1490}
1491
1492/*
1493* Note that traverse_by_cache_tree() duplicates some logic in this function
1494* without actually calling it. If you change the logic here you may need to
1495* check and change there as well.
1496*/
1497static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)1498{
1499struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };1500struct unpack_trees_options *o = info->data;1501const struct name_entry *p = names;1502int is_new_sparse_dir;1503
1504/* Find first entry with a real name (we could use "mask" too) */1505while (!p->mode)1506p++;1507
1508if (o->internal.debug_unpack)1509debug_unpack_callback(n, mask, dirmask, names, info);1510
1511/* Are we supposed to look at the index too? */1512if (o->merge) {1513while (1) {1514int cmp;1515struct cache_entry *ce;1516
1517if (o->diff_index_cached)1518ce = next_cache_entry(o);1519else1520ce = find_cache_entry(info, p);1521
1522if (!ce)1523break;1524cmp = compare_entry(ce, info, p);1525if (cmp < 0) {1526if (unpack_index_entry(ce, o) < 0)1527return unpack_failed(o, NULL);1528continue;1529}1530if (!cmp) {1531if (ce_stage(ce)) {1532/*1533* If we skip unmerged index
1534* entries, we'll skip this
1535* entry *and* the tree
1536* entries associated with it!
1537*/
1538if (o->skip_unmerged) {1539add_same_unmerged(ce, o);1540return mask;1541}1542}1543src[0] = ce;1544}1545break;1546}1547}1548
1549if (unpack_single_entry(n, mask, dirmask, src, names, info, &is_new_sparse_dir))1550return -1;1551
1552if (o->merge && src[0]) {1553if (ce_stage(src[0]))1554mark_ce_used_same_name(src[0], o);1555else1556mark_ce_used(src[0], o);1557}1558
1559/* Now handle any directories.. */1560if (dirmask) {1561/* special case: "diff-index --cached" looking at a tree */1562if (o->diff_index_cached &&1563n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {1564int matches;1565matches = cache_tree_matches_traversal(o->src_index->cache_tree,1566names, info);1567/*1568* Everything under the name matches; skip the
1569* entire hierarchy. diff_index_cached codepath
1570* special cases D/F conflicts in such a way that
1571* it does not do any look-ahead, so this is safe.
1572*/
1573if (matches) {1574/*1575* Only increment the cache_bottom if the
1576* directory isn't a sparse directory index
1577* entry (if it is, it was already incremented)
1578* in 'mark_ce_used()'
1579*/
1580if (!src[0] || !S_ISSPARSEDIR(src[0]->ce_mode))1581o->internal.cache_bottom += matches;1582return mask;1583}1584}1585
1586if (!is_sparse_directory_entry(src[0], p, info) &&1587!is_new_sparse_dir &&1588traverse_trees_recursive(n, dirmask, mask & ~dirmask,1589names, info) < 0) {1590return -1;1591}1592
1593return mask;1594}1595
1596return mask;1597}
1598
1599static int clear_ce_flags_1(struct index_state *istate,1600struct cache_entry **cache, int nr,1601struct strbuf *prefix,1602int select_mask, int clear_mask,1603struct pattern_list *pl,1604enum pattern_match_result default_match,1605int progress_nr);1606
1607/* Whole directory matching */
1608static int clear_ce_flags_dir(struct index_state *istate,1609struct cache_entry **cache, int nr,1610struct strbuf *prefix,1611char *basename,1612int select_mask, int clear_mask,1613struct pattern_list *pl,1614enum pattern_match_result default_match,1615int progress_nr)1616{
1617struct cache_entry **cache_end;1618int dtype = DT_DIR;1619int rc;1620enum pattern_match_result ret, orig_ret;1621orig_ret = path_matches_pattern_list(prefix->buf, prefix->len,1622basename, &dtype, pl, istate);1623
1624strbuf_addch(prefix, '/');1625
1626/* If undecided, use matching result of parent dir in defval */1627if (orig_ret == UNDECIDED)1628ret = default_match;1629else1630ret = orig_ret;1631
1632for (cache_end = cache; cache_end != cache + nr; cache_end++) {1633struct cache_entry *ce = *cache_end;1634if (strncmp(ce->name, prefix->buf, prefix->len))1635break;1636}1637
1638if (pl->use_cone_patterns && orig_ret == MATCHED_RECURSIVE) {1639struct cache_entry **ce = cache;1640rc = cache_end - cache;1641
1642while (ce < cache_end) {1643(*ce)->ce_flags &= ~clear_mask;1644ce++;1645}1646} else if (pl->use_cone_patterns && orig_ret == NOT_MATCHED) {1647rc = cache_end - cache;1648} else {1649rc = clear_ce_flags_1(istate, cache, cache_end - cache,1650prefix,1651select_mask, clear_mask,1652pl, ret,1653progress_nr);1654}1655
1656strbuf_setlen(prefix, prefix->len - 1);1657return rc;1658}
1659
1660/*
1661* Traverse the index, find every entry that matches according to
1662* o->pl. Do "ce_flags &= ~clear_mask" on those entries. Return the
1663* number of traversed entries.
1664*
1665* If select_mask is non-zero, only entries whose ce_flags has on of
1666* those bits enabled are traversed.
1667*
1668* cache : pointer to an index entry
1669* prefix_len : an offset to its path
1670*
1671* The current path ("prefix") including the trailing '/' is
1672* cache[0]->name[0..(prefix_len-1)]
1673* Top level path has prefix_len zero.
1674*/
1675static int clear_ce_flags_1(struct index_state *istate,1676struct cache_entry **cache, int nr,1677struct strbuf *prefix,1678int select_mask, int clear_mask,1679struct pattern_list *pl,1680enum pattern_match_result default_match,1681int progress_nr)1682{
1683struct cache_entry **cache_end = nr ? cache + nr : cache;1684
1685/*1686* Process all entries that have the given prefix and meet
1687* select_mask condition
1688*/
1689while(cache != cache_end) {1690struct cache_entry *ce = *cache;1691const char *name, *slash;1692int len, dtype;1693enum pattern_match_result ret;1694
1695display_progress(istate->progress, progress_nr);1696
1697if (select_mask && !(ce->ce_flags & select_mask)) {1698cache++;1699progress_nr++;1700continue;1701}1702
1703if (prefix->len && strncmp(ce->name, prefix->buf, prefix->len))1704break;1705
1706name = ce->name + prefix->len;1707slash = strchr(name, '/');1708
1709/* If it's a directory, try whole directory match first */1710if (slash) {1711int processed;1712
1713len = slash - name;1714strbuf_add(prefix, name, len);1715
1716processed = clear_ce_flags_dir(istate, cache, cache_end - cache,1717prefix,1718prefix->buf + prefix->len - len,1719select_mask, clear_mask,1720pl, default_match,1721progress_nr);1722
1723/* clear_c_f_dir eats a whole dir already? */1724if (processed) {1725cache += processed;1726progress_nr += processed;1727strbuf_setlen(prefix, prefix->len - len);1728continue;1729}1730
1731strbuf_addch(prefix, '/');1732processed = clear_ce_flags_1(istate, cache, cache_end - cache,1733prefix,1734select_mask, clear_mask, pl,1735default_match, progress_nr);1736
1737cache += processed;1738progress_nr += processed;1739
1740strbuf_setlen(prefix, prefix->len - len - 1);1741continue;1742}1743
1744/* Non-directory */1745dtype = ce_to_dtype(ce);1746ret = path_matches_pattern_list(ce->name,1747ce_namelen(ce),1748name, &dtype, pl, istate);1749if (ret == UNDECIDED)1750ret = default_match;1751if (ret == MATCHED || ret == MATCHED_RECURSIVE)1752ce->ce_flags &= ~clear_mask;1753cache++;1754progress_nr++;1755}1756
1757display_progress(istate->progress, progress_nr);1758return nr - (cache_end - cache);1759}
1760
1761static int clear_ce_flags(struct index_state *istate,1762int select_mask, int clear_mask,1763struct pattern_list *pl,1764int show_progress)1765{
1766static struct strbuf prefix = STRBUF_INIT;1767char label[100];1768int rval;1769
1770strbuf_reset(&prefix);1771if (show_progress)1772istate->progress = start_delayed_progress(1773_("Updating index flags"),1774istate->cache_nr);1775
1776xsnprintf(label, sizeof(label), "clear_ce_flags(0x%08lx,0x%08lx)",1777(unsigned long)select_mask, (unsigned long)clear_mask);1778trace2_region_enter("unpack_trees", label, the_repository);1779rval = clear_ce_flags_1(istate,1780istate->cache,1781istate->cache_nr,1782&prefix,1783select_mask, clear_mask,1784pl, 0, 0);1785trace2_region_leave("unpack_trees", label, the_repository);1786
1787stop_progress(&istate->progress);1788return rval;1789}
1790
1791/*
1792* Set/Clear CE_NEW_SKIP_WORKTREE according to $GIT_DIR/info/sparse-checkout
1793*/
1794static void mark_new_skip_worktree(struct pattern_list *pl,1795struct index_state *istate,1796int select_flag, int skip_wt_flag,1797int show_progress)1798{
1799int i;1800
1801/*1802* 1. Pretend the narrowest worktree: only unmerged entries
1803* are checked out
1804*/
1805for (i = 0; i < istate->cache_nr; i++) {1806struct cache_entry *ce = istate->cache[i];1807
1808if (select_flag && !(ce->ce_flags & select_flag))1809continue;1810
1811if (!ce_stage(ce) && !(ce->ce_flags & CE_CONFLICTED))1812ce->ce_flags |= skip_wt_flag;1813else1814ce->ce_flags &= ~skip_wt_flag;1815}1816
1817/*1818* 2. Widen worktree according to sparse-checkout file.
1819* Matched entries will have skip_wt_flag cleared (i.e. "in")
1820*/
1821clear_ce_flags(istate, select_flag, skip_wt_flag, pl, show_progress);1822}
1823
1824static void populate_from_existing_patterns(struct unpack_trees_options *o,1825struct pattern_list *pl)1826{
1827if (get_sparse_checkout_patterns(pl) < 0)1828o->skip_sparse_checkout = 1;1829else1830o->internal.pl = pl;1831}
1832
1833static void update_sparsity_for_prefix(const char *prefix,1834struct index_state *istate)1835{
1836int prefix_len = strlen(prefix);1837struct strbuf ce_prefix = STRBUF_INIT;1838
1839if (!istate->sparse_index)1840return;1841
1842while (prefix_len > 0 && prefix[prefix_len - 1] == '/')1843prefix_len--;1844
1845if (prefix_len <= 0)1846BUG("Invalid prefix passed to update_sparsity_for_prefix");1847
1848strbuf_grow(&ce_prefix, prefix_len + 1);1849strbuf_add(&ce_prefix, prefix, prefix_len);1850strbuf_addch(&ce_prefix, '/');1851
1852/*1853* If the prefix points to a sparse directory or a path inside a sparse
1854* directory, the index should be expanded. This is accomplished in one
1855* of two ways:
1856* - if the prefix is inside a sparse directory, it will be expanded by
1857* the 'ensure_full_index(...)' call in 'index_name_pos(...)'.
1858* - if the prefix matches an existing sparse directory entry,
1859* 'index_name_pos(...)' will return its index position, triggering
1860* the 'ensure_full_index(...)' below.
1861*/
1862if (!path_in_cone_mode_sparse_checkout(ce_prefix.buf, istate) &&1863index_name_pos(istate, ce_prefix.buf, ce_prefix.len) >= 0)1864ensure_full_index(istate);1865
1866strbuf_release(&ce_prefix);1867}
1868
1869static int verify_absent(const struct cache_entry *,1870enum unpack_trees_error_types,1871struct unpack_trees_options *);1872/*
1873* N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the
1874* resulting index, -2 on failure to reflect the changes to the work tree.
1875*
1876* CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally
1877*/
1878int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)1879{
1880struct repository *repo = the_repository;1881int i, ret;1882static struct cache_entry *dfc;1883struct pattern_list pl;1884int free_pattern_list = 0;1885struct dir_struct dir = DIR_INIT;1886
1887if (o->reset == UNPACK_RESET_INVALID)1888BUG("o->reset had a value of 1; should be UNPACK_TREES_*_UNTRACKED");1889
1890if (len > MAX_UNPACK_TREES)1891die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);1892if (o->internal.dir)1893BUG("o->internal.dir is for internal use only");1894if (o->internal.pl)1895BUG("o->internal.pl is for internal use only");1896if (o->df_conflict_entry)1897BUG("o->df_conflict_entry is an output only field");1898
1899trace_performance_enter();1900trace2_region_enter("unpack_trees", "unpack_trees", the_repository);1901
1902prepare_repo_settings(repo);1903if (repo->settings.command_requires_full_index) {1904ensure_full_index(o->src_index);1905if (o->dst_index)1906ensure_full_index(o->dst_index);1907}1908
1909if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED &&1910o->preserve_ignored)1911BUG("UNPACK_RESET_OVERWRITE_UNTRACKED incompatible with preserved ignored files");1912
1913if (!o->preserve_ignored) {1914o->internal.dir = &dir;1915o->internal.dir->flags |= DIR_SHOW_IGNORED;1916setup_standard_excludes(o->internal.dir);1917}1918
1919if (o->prefix)1920update_sparsity_for_prefix(o->prefix, o->src_index);1921
1922if (!core_apply_sparse_checkout || !o->update)1923o->skip_sparse_checkout = 1;1924if (!o->skip_sparse_checkout) {1925memset(&pl, 0, sizeof(pl));1926free_pattern_list = 1;1927populate_from_existing_patterns(o, &pl);1928}1929
1930index_state_init(&o->internal.result, o->src_index->repo);1931o->internal.result.initialized = 1;1932o->internal.result.timestamp.sec = o->src_index->timestamp.sec;1933o->internal.result.timestamp.nsec = o->src_index->timestamp.nsec;1934o->internal.result.version = o->src_index->version;1935if (!o->src_index->split_index) {1936o->internal.result.split_index = NULL;1937} else if (o->src_index == o->dst_index) {1938/*1939* o->dst_index (and thus o->src_index) will be discarded
1940* and overwritten with o->internal.result at the end of
1941* this function, so just use src_index's split_index to
1942* avoid having to create a new one.
1943*/
1944o->internal.result.split_index = o->src_index->split_index;1945if (o->src_index->cache_changed & SPLIT_INDEX_ORDERED)1946o->internal.result.cache_changed |= SPLIT_INDEX_ORDERED;1947o->internal.result.split_index->refcount++;1948} else {1949o->internal.result.split_index =1950init_split_index(&o->internal.result);1951}1952oidcpy(&o->internal.result.oid, &o->src_index->oid);1953o->internal.merge_size = len;1954mark_all_ce_unused(o->src_index);1955
1956o->internal.result.fsmonitor_last_update =1957xstrdup_or_null(o->src_index->fsmonitor_last_update);1958o->internal.result.fsmonitor_has_run_once = o->src_index->fsmonitor_has_run_once;1959
1960if (!o->src_index->initialized &&1961!repo->settings.command_requires_full_index &&1962is_sparse_index_allowed(&o->internal.result, 0))1963o->internal.result.sparse_index = 1;1964
1965/*1966* Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries
1967*/
1968if (!o->skip_sparse_checkout)1969mark_new_skip_worktree(o->internal.pl, o->src_index, 0,1970CE_NEW_SKIP_WORKTREE, o->verbose_update);1971
1972if (!dfc)1973dfc = xcalloc(1, cache_entry_size(0));1974o->df_conflict_entry = dfc;1975
1976if (len) {1977const char *prefix = o->prefix ? o->prefix : "";1978struct traverse_info info;1979
1980setup_traverse_info(&info, prefix);1981info.fn = unpack_callback;1982info.data = o;1983info.show_all_errors = o->internal.show_all_errors;1984info.pathspec = o->pathspec;1985
1986if (o->prefix) {1987/*1988* Unpack existing index entries that sort before the
1989* prefix the tree is spliced into. Note that o->merge
1990* is always true in this case.
1991*/
1992while (1) {1993struct cache_entry *ce = next_cache_entry(o);1994if (!ce)1995break;1996if (ce_in_traverse_path(ce, &info))1997break;1998if (unpack_index_entry(ce, o) < 0)1999goto return_failed;2000}2001}2002
2003trace_performance_enter();2004trace2_region_enter("unpack_trees", "traverse_trees", the_repository);2005ret = traverse_trees(o->src_index, len, t, &info);2006trace2_region_leave("unpack_trees", "traverse_trees", the_repository);2007trace_performance_leave("traverse_trees");2008if (ret < 0)2009goto return_failed;2010}2011
2012/* Any left-over entries in the index? */2013if (o->merge) {2014while (1) {2015struct cache_entry *ce = next_cache_entry(o);2016if (!ce)2017break;2018if (unpack_index_entry(ce, o) < 0)2019goto return_failed;2020}2021}2022mark_all_ce_unused(o->src_index);2023
2024if (o->trivial_merges_only && o->internal.nontrivial_merge) {2025ret = unpack_failed(o, "Merge requires file-level merging");2026goto done;2027}2028
2029if (!o->skip_sparse_checkout) {2030/*2031* Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1
2032* If they will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE
2033* so apply_sparse_checkout() won't attempt to remove it from worktree
2034*/
2035mark_new_skip_worktree(o->internal.pl, &o->internal.result,2036CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE,2037o->verbose_update);2038
2039ret = 0;2040for (i = 0; i < o->internal.result.cache_nr; i++) {2041struct cache_entry *ce = o->internal.result.cache[i];2042
2043/*2044* Entries marked with CE_ADDED in merged_entry() do not have
2045* verify_absent() check (the check is effectively disabled
2046* because CE_NEW_SKIP_WORKTREE is set unconditionally).
2047*
2048* Do the real check now because we have had
2049* correct CE_NEW_SKIP_WORKTREE
2050*/
2051if (ce->ce_flags & CE_ADDED &&2052verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))2053ret = 1;2054
2055if (apply_sparse_checkout(&o->internal.result, ce, o))2056ret = 1;2057}2058if (ret == 1) {2059/*2060* Inability to sparsify or de-sparsify individual
2061* paths is not an error, but just a warning.
2062*/
2063if (o->internal.show_all_errors)2064display_warning_msgs(o);2065ret = 0;2066}2067}2068
2069ret = check_updates(o, &o->internal.result) ? (-2) : 0;2070if (o->dst_index) {2071move_index_extensions(&o->internal.result, o->src_index);2072if (!ret) {2073if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))2074cache_tree_verify(the_repository,2075&o->internal.result);2076if (!o->skip_cache_tree_update &&2077!cache_tree_fully_valid(o->internal.result.cache_tree))2078cache_tree_update(&o->internal.result,2079WRITE_TREE_SILENT |2080WRITE_TREE_REPAIR);2081}2082
2083o->internal.result.updated_workdir = 1;2084discard_index(o->dst_index);2085*o->dst_index = o->internal.result;2086memset(&o->internal.result, 0, sizeof(o->internal.result));2087} else {2088discard_index(&o->internal.result);2089}2090o->src_index = NULL;2091
2092done:2093if (free_pattern_list)2094clear_pattern_list(&pl);2095if (o->internal.dir) {2096dir_clear(o->internal.dir);2097o->internal.dir = NULL;2098}2099trace2_region_leave("unpack_trees", "unpack_trees", the_repository);2100trace_performance_leave("unpack_trees");2101return ret;2102
2103return_failed:2104if (o->internal.show_all_errors)2105display_error_msgs(o);2106mark_all_ce_unused(o->src_index);2107ret = unpack_failed(o, NULL);2108if (o->exiting_early)2109ret = 0;2110goto done;2111}
2112
2113/*
2114* Update SKIP_WORKTREE bits according to sparsity patterns, and update
2115* working directory to match.
2116*
2117* CE_NEW_SKIP_WORKTREE is used internally.
2118*/
2119enum update_sparsity_result update_sparsity(struct unpack_trees_options *o,2120struct pattern_list *pl)2121{
2122enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;2123int i;2124unsigned old_show_all_errors;2125int free_pattern_list = 0;2126
2127old_show_all_errors = o->internal.show_all_errors;2128o->internal.show_all_errors = 1;2129index_state_init(&o->internal.result, o->src_index->repo);2130
2131/* Sanity checks */2132if (!o->update || o->index_only || o->skip_sparse_checkout)2133BUG("update_sparsity() is for reflecting sparsity patterns in working directory");2134if (o->src_index != o->dst_index || o->fn)2135BUG("update_sparsity() called wrong");2136
2137trace_performance_enter();2138
2139/* If we weren't given patterns, use the recorded ones */2140if (!pl) {2141free_pattern_list = 1;2142pl = xcalloc(1, sizeof(*pl));2143populate_from_existing_patterns(o, pl);2144}2145o->internal.pl = pl;2146
2147/* Expand sparse directories as needed */2148expand_index(o->src_index, o->internal.pl);2149
2150/* Set NEW_SKIP_WORKTREE on existing entries. */2151mark_all_ce_unused(o->src_index);2152mark_new_skip_worktree(o->internal.pl, o->src_index, 0,2153CE_NEW_SKIP_WORKTREE, o->verbose_update);2154
2155/* Then loop over entries and update/remove as needed */2156ret = UPDATE_SPARSITY_SUCCESS;2157for (i = 0; i < o->src_index->cache_nr; i++) {2158struct cache_entry *ce = o->src_index->cache[i];2159
2160
2161if (ce_stage(ce)) {2162/* -1 because for loop will increment by 1 */2163i += warn_conflicted_path(o->src_index, i, o) - 1;2164ret = UPDATE_SPARSITY_WARNINGS;2165continue;2166}2167
2168if (apply_sparse_checkout(o->src_index, ce, o))2169ret = UPDATE_SPARSITY_WARNINGS;2170}2171
2172if (check_updates(o, o->src_index))2173ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;2174
2175display_warning_msgs(o);2176o->internal.show_all_errors = old_show_all_errors;2177if (free_pattern_list) {2178clear_pattern_list(pl);2179free(pl);2180o->internal.pl = NULL;2181}2182trace_performance_leave("update_sparsity");2183return ret;2184}
2185
2186/* Here come the merge functions */
2187
2188static int reject_merge(const struct cache_entry *ce,2189struct unpack_trees_options *o)2190{
2191return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);2192}
2193
2194static int same(const struct cache_entry *a, const struct cache_entry *b)2195{
2196if (!!a != !!b)2197return 0;2198if (!a && !b)2199return 1;2200if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)2201return 0;2202return a->ce_mode == b->ce_mode &&2203oideq(&a->oid, &b->oid);2204}
2205
2206
2207/*
2208* When a CE gets turned into an unmerged entry, we
2209* want it to be up-to-date
2210*/
2211static int verify_uptodate_1(const struct cache_entry *ce,2212struct unpack_trees_options *o,2213enum unpack_trees_error_types error_type)2214{
2215struct stat st;2216
2217if (o->index_only)2218return 0;2219
2220/*2221* CE_VALID and CE_SKIP_WORKTREE cheat, we better check again
2222* if this entry is truly up-to-date because this file may be
2223* overwritten.
2224*/
2225if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))2226; /* keep checking */2227else if (o->reset || ce_uptodate(ce))2228return 0;2229
2230if (!lstat(ce->name, &st)) {2231int flags = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE;2232unsigned changed = ie_match_stat(o->src_index, ce, &st, flags);2233
2234if (submodule_from_ce(ce)) {2235int r = check_submodule_move_head(ce,2236"HEAD", oid_to_hex(&ce->oid), o);2237if (r)2238return add_rejected_path(o, error_type, ce->name);2239return 0;2240}2241
2242if (!changed)2243return 0;2244/*2245* Historic default policy was to allow submodule to be out
2246* of sync wrt the superproject index. If the submodule was
2247* not considered interesting above, we don't care here.
2248*/
2249if (S_ISGITLINK(ce->ce_mode))2250return 0;2251
2252errno = 0;2253}2254if (errno == ENOENT)2255return 0;2256return add_rejected_path(o, error_type, ce->name);2257}
2258
2259int verify_uptodate(const struct cache_entry *ce,2260struct unpack_trees_options *o)2261{
2262if (!o->skip_sparse_checkout &&2263(ce->ce_flags & CE_SKIP_WORKTREE) &&2264(ce->ce_flags & CE_NEW_SKIP_WORKTREE))2265return 0;2266return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);2267}
2268
2269static int verify_uptodate_sparse(const struct cache_entry *ce,2270struct unpack_trees_options *o)2271{
2272return verify_uptodate_1(ce, o, WARNING_SPARSE_NOT_UPTODATE_FILE);2273}
2274
2275/*
2276* TODO: We should actually invalidate o->internal.result, not src_index [1].
2277* But since cache tree and untracked cache both are not copied to
2278* o->internal.result until unpacking is complete, we invalidate them on
2279* src_index instead with the assumption that they will be copied to
2280* dst_index at the end.
2281*
2282* [1] src_index->cache_tree is also used in unpack_callback() so if
2283* we invalidate o->internal.result, we need to update it to use
2284* o->internal.result.cache_tree as well.
2285*/
2286static void invalidate_ce_path(const struct cache_entry *ce,2287struct unpack_trees_options *o)2288{
2289if (!ce)2290return;2291cache_tree_invalidate_path(o->src_index, ce->name);2292untracked_cache_invalidate_path(o->src_index, ce->name, 1);2293}
2294
2295/*
2296* Check that checking out ce->sha1 in subdir ce->name is not
2297* going to overwrite any working files.
2298*/
2299static int verify_clean_submodule(const char *old_sha1,2300const struct cache_entry *ce,2301struct unpack_trees_options *o)2302{
2303if (!submodule_from_ce(ce))2304return 0;2305
2306return check_submodule_move_head(ce, old_sha1,2307oid_to_hex(&ce->oid), o);2308}
2309
2310static int verify_clean_subdirectory(const struct cache_entry *ce,2311struct unpack_trees_options *o)2312{
2313/*2314* we are about to extract "ce->name"; we would not want to lose
2315* anything in the existing directory there.
2316*/
2317int namelen;2318int i;2319struct dir_struct d;2320char *pathbuf;2321int cnt = 0;2322
2323if (S_ISGITLINK(ce->ce_mode)) {2324struct object_id oid;2325int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name,2326"HEAD", &oid);2327/*2328* If we are not going to update the submodule, then
2329* we don't care.
2330*/
2331if (!sub_head && oideq(&oid, &ce->oid))2332return 0;2333return verify_clean_submodule(sub_head ? NULL : oid_to_hex(&oid),2334ce, o);2335}2336
2337/*2338* First let's make sure we do not have a local modification
2339* in that directory.
2340*/
2341namelen = ce_namelen(ce);2342for (i = locate_in_src_index(ce, o);2343i < o->src_index->cache_nr;2344i++) {2345struct cache_entry *ce2 = o->src_index->cache[i];2346int len = ce_namelen(ce2);2347if (len < namelen ||2348strncmp(ce->name, ce2->name, namelen) ||2349ce2->name[namelen] != '/')2350break;2351/*2352* ce2->name is an entry in the subdirectory to be
2353* removed.
2354*/
2355if (!ce_stage(ce2)) {2356if (verify_uptodate(ce2, o))2357return -1;2358add_entry(o, ce2, CE_REMOVE, 0);2359invalidate_ce_path(ce, o);2360mark_ce_used(ce2, o);2361}2362cnt++;2363}2364
2365/* Do not lose a locally present file that is not ignored. */2366pathbuf = xstrfmt("%.*s/", namelen, ce->name);2367
2368memset(&d, 0, sizeof(d));2369if (o->internal.dir)2370setup_standard_excludes(&d);2371i = read_directory(&d, o->src_index, pathbuf, namelen+1, NULL);2372dir_clear(&d);2373free(pathbuf);2374if (i)2375return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);2376
2377/* Do not lose startup_info->original_cwd */2378if (startup_info->original_cwd &&2379!strcmp(startup_info->original_cwd, ce->name))2380return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);2381
2382return cnt;2383}
2384
2385/*
2386* This gets called when there was no index entry for the tree entry 'dst',
2387* but we found a file in the working tree that 'lstat()' said was fine,
2388* and we're on a case-insensitive filesystem.
2389*
2390* See if we can find a case-insensitive match in the index that also
2391* matches the stat information, and assume it's that other file!
2392*/
2393static int icase_exists(struct unpack_trees_options *o, const char *name, int len, struct stat *st)2394{
2395const struct cache_entry *src;2396
2397src = index_file_exists(o->src_index, name, len, 1);2398return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);2399}
2400
2401enum absent_checking_type {2402COMPLETELY_ABSENT,2403ABSENT_ANY_DIRECTORY
2404};2405
2406static int check_ok_to_remove(const char *name, int len, int dtype,2407const struct cache_entry *ce, struct stat *st,2408enum unpack_trees_error_types error_type,2409enum absent_checking_type absent_type,2410struct unpack_trees_options *o)2411{
2412const struct cache_entry *result;2413
2414/*2415* It may be that the 'lstat()' succeeded even though
2416* target 'ce' was absent, because there is an old
2417* entry that is different only in case..
2418*
2419* Ignore that lstat() if it matches.
2420*/
2421if (ignore_case && icase_exists(o, name, len, st))2422return 0;2423
2424if (o->internal.dir &&2425is_excluded(o->internal.dir, o->src_index, name, &dtype))2426/*2427* ce->name is explicitly excluded, so it is Ok to
2428* overwrite it.
2429*/
2430return 0;2431if (S_ISDIR(st->st_mode)) {2432/*2433* We are checking out path "foo" and
2434* found "foo/." in the working tree.
2435* This is tricky -- if we have modified
2436* files that are in "foo/" we would lose
2437* them.
2438*/
2439if (verify_clean_subdirectory(ce, o) < 0)2440return -1;2441return 0;2442}2443
2444/* If we only care about directories, then we can remove */2445if (absent_type == ABSENT_ANY_DIRECTORY)2446return 0;2447
2448/*2449* The previous round may already have decided to
2450* delete this path, which is in a subdirectory that
2451* is being replaced with a blob.
2452*/
2453result = index_file_exists(&o->internal.result, name, len, 0);2454if (result) {2455if (result->ce_flags & CE_REMOVE)2456return 0;2457}2458
2459return add_rejected_path(o, error_type, name);2460}
2461
2462/*
2463* We do not want to remove or overwrite a working tree file that
2464* is not tracked, unless it is ignored.
2465*/
2466static int verify_absent_1(const struct cache_entry *ce,2467enum unpack_trees_error_types error_type,2468enum absent_checking_type absent_type,2469struct unpack_trees_options *o)2470{
2471int len;2472struct stat st;2473
2474if (o->index_only || !o->update)2475return 0;2476
2477if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {2478/* Avoid nuking startup_info->original_cwd... */2479if (startup_info->original_cwd &&2480!strcmp(startup_info->original_cwd, ce->name))2481return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,2482ce->name);2483/* ...but nuke anything else. */2484return 0;2485}2486
2487len = check_leading_path(ce->name, ce_namelen(ce), 0);2488if (!len)2489return 0;2490else if (len > 0) {2491char *path;2492int ret;2493
2494path = xmemdupz(ce->name, len);2495if (lstat(path, &st))2496ret = error_errno("cannot stat '%s'", path);2497else {2498if (submodule_from_ce(ce))2499ret = check_submodule_move_head(ce,2500oid_to_hex(&ce->oid),2501NULL, o);2502else2503ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,2504&st, error_type,2505absent_type, o);2506}2507free(path);2508return ret;2509} else if (lstat(ce->name, &st)) {2510if (errno != ENOENT)2511return error_errno("cannot stat '%s'", ce->name);2512return 0;2513} else {2514if (submodule_from_ce(ce))2515return check_submodule_move_head(ce, oid_to_hex(&ce->oid),2516NULL, o);2517
2518return check_ok_to_remove(ce->name, ce_namelen(ce),2519ce_to_dtype(ce), ce, &st,2520error_type, absent_type, o);2521}2522}
2523
2524static int verify_absent(const struct cache_entry *ce,2525enum unpack_trees_error_types error_type,2526struct unpack_trees_options *o)2527{
2528if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))2529return 0;2530return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);2531}
2532
2533static int verify_absent_if_directory(const struct cache_entry *ce,2534enum unpack_trees_error_types error_type,2535struct unpack_trees_options *o)2536{
2537if (!o->skip_sparse_checkout && (ce->ce_flags & CE_NEW_SKIP_WORKTREE))2538return 0;2539return verify_absent_1(ce, error_type, ABSENT_ANY_DIRECTORY, o);2540}
2541
2542static int verify_absent_sparse(const struct cache_entry *ce,2543enum unpack_trees_error_types error_type,2544struct unpack_trees_options *o)2545{
2546return verify_absent_1(ce, error_type, COMPLETELY_ABSENT, o);2547}
2548
2549static int merged_entry(const struct cache_entry *ce,2550const struct cache_entry *old,2551struct unpack_trees_options *o)2552{
2553int update = CE_UPDATE;2554struct cache_entry *merge = dup_cache_entry(ce, &o->internal.result);2555
2556if (!old) {2557/*2558* New index entries. In sparse checkout, the following
2559* verify_absent() will be delayed until after
2560* traverse_trees() finishes in unpack_trees(), then:
2561*
2562* - CE_NEW_SKIP_WORKTREE will be computed correctly
2563* - verify_absent() be called again, this time with
2564* correct CE_NEW_SKIP_WORKTREE
2565*
2566* verify_absent() call here does nothing in sparse
2567* checkout (i.e. o->skip_sparse_checkout == 0)
2568*/
2569update |= CE_ADDED;2570merge->ce_flags |= CE_NEW_SKIP_WORKTREE;2571
2572if (verify_absent(merge,2573ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2574discard_cache_entry(merge);2575return -1;2576}2577invalidate_ce_path(merge, o);2578
2579if (submodule_from_ce(ce) && file_exists(ce->name)) {2580int ret = check_submodule_move_head(ce, NULL,2581oid_to_hex(&ce->oid),2582o);2583if (ret)2584return ret;2585}2586
2587} else if (!(old->ce_flags & CE_CONFLICTED)) {2588/*2589* See if we can re-use the old CE directly?
2590* That way we get the uptodate stat info.
2591*
2592* This also removes the UPDATE flag on a match; otherwise
2593* we will end up overwriting local changes in the work tree.
2594*/
2595if (same(old, merge)) {2596copy_cache_entry(merge, old);2597update = 0;2598} else {2599if (verify_uptodate(old, o)) {2600discard_cache_entry(merge);2601return -1;2602}2603/* Migrate old flags over */2604update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);2605invalidate_ce_path(old, o);2606}2607
2608if (submodule_from_ce(ce) && file_exists(ce->name)) {2609int ret = check_submodule_move_head(ce, oid_to_hex(&old->oid),2610oid_to_hex(&ce->oid),2611o);2612if (ret)2613return ret;2614}2615} else {2616/*2617* Previously unmerged entry left as an existence
2618* marker by read_index_unmerged();
2619*/
2620if (verify_absent_if_directory(merge,2621ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {2622discard_cache_entry(merge);2623return -1;2624}2625
2626invalidate_ce_path(old, o);2627}2628
2629if (do_add_entry(o, merge, update, CE_STAGEMASK) < 0)2630return -1;2631return 1;2632}
2633
2634static int merged_sparse_dir(const struct cache_entry * const *src, int n,2635struct unpack_trees_options *o)2636{
2637struct tree_desc t[MAX_UNPACK_TREES + 1];2638void * tree_bufs[MAX_UNPACK_TREES + 1];2639struct traverse_info info;2640int i, ret;2641
2642/*2643* Create the tree traversal information for traversing into *only* the
2644* sparse directory.
2645*/
2646setup_traverse_info(&info, src[0]->name);2647info.fn = unpack_sparse_callback;2648info.data = o;2649info.show_all_errors = o->internal.show_all_errors;2650info.pathspec = o->pathspec;2651
2652/* Get the tree descriptors of the sparse directory in each of the merging trees */2653for (i = 0; i < n; i++)2654tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i],2655src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL);2656
2657ret = traverse_trees(o->src_index, n, t, &info);2658
2659for (i = 0; i < n; i++)2660free(tree_bufs[i]);2661
2662return ret;2663}
2664
2665static int deleted_entry(const struct cache_entry *ce,2666const struct cache_entry *old,2667struct unpack_trees_options *o)2668{
2669/* Did it exist in the index? */2670if (!old) {2671if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2672return -1;2673return 0;2674} else if (verify_absent_if_directory(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o)) {2675return -1;2676}2677
2678if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))2679return -1;2680add_entry(o, ce, CE_REMOVE, 0);2681invalidate_ce_path(ce, o);2682return 1;2683}
2684
2685static int keep_entry(const struct cache_entry *ce,2686struct unpack_trees_options *o)2687{
2688add_entry(o, ce, 0, 0);2689if (ce_stage(ce))2690invalidate_ce_path(ce, o);2691return 1;2692}
2693
2694#if DBRT_DEBUG2695static void show_stage_entry(FILE *o,2696const char *label, const struct cache_entry *ce)2697{
2698if (!ce)2699fprintf(o, "%s (missing)\n", label);2700else2701fprintf(o, "%s%06o %s %d\t%s\n",2702label,2703ce->ce_mode,2704oid_to_hex(&ce->oid),2705ce_stage(ce),2706ce->name);2707}
2708#endif2709
2710int threeway_merge(const struct cache_entry * const *stages,2711struct unpack_trees_options *o)2712{
2713const struct cache_entry *index;2714const struct cache_entry *head;2715const struct cache_entry *remote = stages[o->head_idx + 1];2716int count;2717int head_match = 0;2718int remote_match = 0;2719
2720int df_conflict_head = 0;2721int df_conflict_remote = 0;2722
2723int any_anc_missing = 0;2724int no_anc_exists = 1;2725int i;2726
2727for (i = 1; i < o->head_idx; i++) {2728if (!stages[i] || stages[i] == o->df_conflict_entry)2729any_anc_missing = 1;2730else2731no_anc_exists = 0;2732}2733
2734index = stages[0];2735head = stages[o->head_idx];2736
2737if (head == o->df_conflict_entry) {2738df_conflict_head = 1;2739head = NULL;2740}2741
2742if (remote == o->df_conflict_entry) {2743df_conflict_remote = 1;2744remote = NULL;2745}2746
2747/*2748* First, if there's a #16 situation, note that to prevent #13
2749* and #14.
2750*/
2751if (!same(remote, head)) {2752for (i = 1; i < o->head_idx; i++) {2753if (same(stages[i], head)) {2754head_match = i;2755}2756if (same(stages[i], remote)) {2757remote_match = i;2758}2759}2760}2761
2762/*2763* We start with cases where the index is allowed to match
2764* something other than the head: #14(ALT) and #2ALT, where it
2765* is permitted to match the result instead.
2766*/
2767/* #14, #14ALT, #2ALT */2768if (remote && !df_conflict_head && head_match && !remote_match) {2769if (index && !same(index, remote) && !same(index, head)) {2770if (S_ISSPARSEDIR(index->ce_mode))2771return merged_sparse_dir(stages, 4, o);2772else2773return reject_merge(index, o);2774}2775return merged_entry(remote, index, o);2776}2777/*2778* If we have an entry in the index cache, then we want to
2779* make sure that it matches head.
2780*/
2781if (index && !same(index, head)) {2782if (S_ISSPARSEDIR(index->ce_mode))2783return merged_sparse_dir(stages, 4, o);2784else2785return reject_merge(index, o);2786}2787
2788if (head) {2789/* #5ALT, #15 */2790if (same(head, remote))2791return merged_entry(head, index, o);2792/* #13, #3ALT */2793if (!df_conflict_remote && remote_match && !head_match)2794return merged_entry(head, index, o);2795}2796
2797/* #1 */2798if (!head && !remote && any_anc_missing)2799return 0;2800
2801/*2802* Under the "aggressive" rule, we resolve mostly trivial
2803* cases that we historically had git-merge-one-file resolve.
2804*/
2805if (o->aggressive) {2806int head_deleted = !head;2807int remote_deleted = !remote;2808const struct cache_entry *ce = NULL;2809
2810if (index)2811ce = index;2812else if (head)2813ce = head;2814else if (remote)2815ce = remote;2816else {2817for (i = 1; i < o->head_idx; i++) {2818if (stages[i] && stages[i] != o->df_conflict_entry) {2819ce = stages[i];2820break;2821}2822}2823}2824
2825/*2826* Deleted in both.
2827* Deleted in one and unchanged in the other.
2828*/
2829if ((head_deleted && remote_deleted) ||2830(head_deleted && remote && remote_match) ||2831(remote_deleted && head && head_match)) {2832if (index)2833return deleted_entry(index, index, o);2834if (ce && !head_deleted) {2835if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))2836return -1;2837}2838return 0;2839}2840/*2841* Added in both, identically.
2842*/
2843if (no_anc_exists && head && remote && same(head, remote))2844return merged_entry(head, index, o);2845
2846}2847
2848/* Handle "no merge" cases (see t/t1000-read-tree-m-3way.sh) */2849if (index) {2850/*2851* If we've reached the "no merge" cases and we're merging
2852* a sparse directory, we may have an "edit/edit" conflict that
2853* can be resolved by individually merging directory contents.
2854*/
2855if (S_ISSPARSEDIR(index->ce_mode))2856return merged_sparse_dir(stages, 4, o);2857
2858/*2859* If we're not merging a sparse directory, ensure the index is
2860* up-to-date to avoid files getting overwritten with conflict
2861* resolution files
2862*/
2863if (verify_uptodate(index, o))2864return -1;2865}2866
2867o->internal.nontrivial_merge = 1;2868
2869/* #2, #3, #4, #6, #7, #9, #10, #11. */2870count = 0;2871if (!head_match || !remote_match) {2872for (i = 1; i < o->head_idx; i++) {2873if (stages[i] && stages[i] != o->df_conflict_entry) {2874keep_entry(stages[i], o);2875count++;2876break;2877}2878}2879}2880#if DBRT_DEBUG2881else {2882fprintf(stderr, "read-tree: warning #16 detected\n");2883show_stage_entry(stderr, "head ", stages[head_match]);2884show_stage_entry(stderr, "remote ", stages[remote_match]);2885}2886#endif2887if (head) { count += keep_entry(head, o); }2888if (remote) { count += keep_entry(remote, o); }2889return count;2890}
2891
2892/*
2893* Two-way merge.
2894*
2895* The rule is to "carry forward" what is in the index without losing
2896* information across a "fast-forward", favoring a successful merge
2897* over a merge failure when it makes sense. For details of the
2898* "carry forward" rule, please see <Documentation/git-read-tree.txt>.
2899*
2900*/
2901int twoway_merge(const struct cache_entry * const *src,2902struct unpack_trees_options *o)2903{
2904const struct cache_entry *current = src[0];2905const struct cache_entry *oldtree = src[1];2906const struct cache_entry *newtree = src[2];2907
2908if (o->internal.merge_size != 2)2909return error("Cannot do a twoway merge of %d trees",2910o->internal.merge_size);2911
2912if (oldtree == o->df_conflict_entry)2913oldtree = NULL;2914if (newtree == o->df_conflict_entry)2915newtree = NULL;2916
2917if (current) {2918if (current->ce_flags & CE_CONFLICTED) {2919if (same(oldtree, newtree) || o->reset) {2920if (!newtree)2921return deleted_entry(current, current, o);2922else2923return merged_entry(newtree, current, o);2924}2925return reject_merge(current, o);2926} else if ((!oldtree && !newtree) || /* 4 and 5 */2927(!oldtree && newtree &&2928same(current, newtree)) || /* 6 and 7 */2929(oldtree && newtree &&2930same(oldtree, newtree)) || /* 14 and 15 */2931(oldtree && newtree &&2932!same(oldtree, newtree) && /* 18 and 19 */2933same(current, newtree))) {2934return keep_entry(current, o);2935} else if (oldtree && !newtree && same(current, oldtree)) {2936/* 10 or 11 */2937return deleted_entry(oldtree, current, o);2938} else if (oldtree && newtree &&2939same(current, oldtree) && !same(current, newtree)) {2940/* 20 or 21 */2941return merged_entry(newtree, current, o);2942} else if (current && !oldtree && newtree &&2943S_ISSPARSEDIR(current->ce_mode) != S_ISSPARSEDIR(newtree->ce_mode) &&2944ce_stage(current) == 0) {2945/*2946* This case is a directory/file conflict across the sparse-index
2947* boundary. When we are changing from one path to another via
2948* 'git checkout', then we want to replace one entry with another
2949* via merged_entry(). If there are staged changes, then we should
2950* reject the merge instead.
2951*/
2952return merged_entry(newtree, current, o);2953} else if (S_ISSPARSEDIR(current->ce_mode)) {2954/*2955* The sparse directories differ, but we don't know whether that's
2956* because of two different files in the directory being modified
2957* (can be trivially merged) or if there is a real file conflict.
2958* Merge the sparse directory by OID to compare file-by-file.
2959*/
2960return merged_sparse_dir(src, 3, o);2961} else2962return reject_merge(current, o);2963}2964else if (newtree) {2965if (oldtree && !o->initial_checkout) {2966/*2967* deletion of the path was staged;
2968*/
2969if (same(oldtree, newtree))2970return 1;2971return reject_merge(oldtree, o);2972}2973return merged_entry(newtree, current, o);2974}2975return deleted_entry(oldtree, current, o);2976}
2977
2978/*
2979* Bind merge.
2980*
2981* Keep the index entries at stage0, collapse stage1 but make sure
2982* stage0 does not have anything there.
2983*/
2984int bind_merge(const struct cache_entry * const *src,2985struct unpack_trees_options *o)2986{
2987const struct cache_entry *old = src[0];2988const struct cache_entry *a = src[1];2989
2990if (o->internal.merge_size != 1)2991return error("Cannot do a bind merge of %d trees",2992o->internal.merge_size);2993if (a && old)2994return o->quiet ? -1 :2995error(ERRORMSG(o, ERROR_BIND_OVERLAP),2996super_prefixed(a->name, o->super_prefix),2997super_prefixed(old->name, o->super_prefix));2998if (!a)2999return keep_entry(old, o);3000else3001return merged_entry(a, NULL, o);3002}
3003
3004/*
3005* One-way merge.
3006*
3007* The rule is:
3008* - take the stat information from stage0, take the data from stage1
3009*/
3010int oneway_merge(const struct cache_entry * const *src,3011struct unpack_trees_options *o)3012{
3013const struct cache_entry *old = src[0];3014const struct cache_entry *a = src[1];3015
3016if (o->internal.merge_size != 1)3017return error("Cannot do a oneway merge of %d trees",3018o->internal.merge_size);3019
3020if (!a || a == o->df_conflict_entry)3021return deleted_entry(old, old, o);3022
3023if (old && same(old, a)) {3024int update = 0;3025if (o->reset && o->update && !ce_uptodate(old) && !ce_skip_worktree(old) &&3026!(old->ce_flags & CE_FSMONITOR_VALID)) {3027struct stat st;3028if (lstat(old->name, &st) ||3029ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))3030update |= CE_UPDATE;3031}3032if (o->update && S_ISGITLINK(old->ce_mode) &&3033should_update_submodules() && !verify_uptodate(old, o))3034update |= CE_UPDATE;3035add_entry(o, old, update, CE_STAGEMASK);3036return 0;3037}3038return merged_entry(a, old, o);3039}
3040
3041/*
3042* Merge worktree and untracked entries in a stash entry.
3043*
3044* Ignore all index entries. Collapse remaining trees but make sure that they
3045* don't have any conflicting files.
3046*/
3047int stash_worktree_untracked_merge(const struct cache_entry * const *src,3048struct unpack_trees_options *o)3049{
3050const struct cache_entry *worktree = src[1];3051const struct cache_entry *untracked = src[2];3052
3053if (o->internal.merge_size != 2)3054BUG("invalid merge_size: %d", o->internal.merge_size);3055
3056if (worktree && untracked)3057return error(_("worktree and untracked commit have duplicate entries: %s"),3058super_prefixed(worktree->name, o->super_prefix));3059
3060return merged_entry(worktree ? worktree : untracked, NULL, o);3061}
3062