|
4 | 4 | #include "tree.h" |
5 | 5 | #include "pathspec.h" |
6 | 6 | #include "trace2.h" |
| 7 | +#include "cache-tree.h" |
| 8 | +#include "config.h" |
| 9 | +#include "dir.h" |
| 10 | +#include "fsmonitor.h" |
| 11 | + |
| 12 | +static struct cache_entry *construct_sparse_dir_entry( |
| 13 | + struct index_state *istate, |
| 14 | + const char *sparse_dir, |
| 15 | + struct cache_tree *tree) |
| 16 | +{ |
| 17 | + struct cache_entry *de; |
| 18 | + |
| 19 | + de = make_cache_entry(istate, S_IFDIR, &tree->oid, sparse_dir, 0, 0); |
| 20 | + |
| 21 | + de->ce_flags |= CE_SKIP_WORKTREE; |
| 22 | + return de; |
| 23 | +} |
| 24 | + |
| 25 | +/* |
| 26 | + * Returns the number of entries "inserted" into the index. |
| 27 | + */ |
| 28 | +static int convert_to_sparse_rec(struct index_state *istate, |
| 29 | + int num_converted, |
| 30 | + int start, int end, |
| 31 | + const char *ct_path, size_t ct_pathlen, |
| 32 | + struct cache_tree *ct) |
| 33 | +{ |
| 34 | + int i, can_convert = 1; |
| 35 | + int start_converted = num_converted; |
| 36 | + enum pattern_match_result match; |
| 37 | + int dtype; |
| 38 | + struct strbuf child_path = STRBUF_INIT; |
| 39 | + struct pattern_list *pl = istate->sparse_checkout_patterns; |
| 40 | + |
| 41 | + /* |
| 42 | + * Is the current path outside of the sparse cone? |
| 43 | + * Then check if the region can be replaced by a sparse |
| 44 | + * directory entry (everything is sparse and merged). |
| 45 | + */ |
| 46 | + match = path_matches_pattern_list(ct_path, ct_pathlen, |
| 47 | + NULL, &dtype, pl, istate); |
| 48 | + if (match != NOT_MATCHED) |
| 49 | + can_convert = 0; |
| 50 | + |
| 51 | + for (i = start; can_convert && i < end; i++) { |
| 52 | + struct cache_entry *ce = istate->cache[i]; |
| 53 | + |
| 54 | + if (ce_stage(ce) || |
| 55 | + !(ce->ce_flags & CE_SKIP_WORKTREE)) |
| 56 | + can_convert = 0; |
| 57 | + } |
| 58 | + |
| 59 | + if (can_convert) { |
| 60 | + struct cache_entry *se; |
| 61 | + se = construct_sparse_dir_entry(istate, ct_path, ct); |
| 62 | + |
| 63 | + istate->cache[num_converted++] = se; |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + for (i = start; i < end; ) { |
| 68 | + int count, span, pos = -1; |
| 69 | + const char *base, *slash; |
| 70 | + struct cache_entry *ce = istate->cache[i]; |
| 71 | + |
| 72 | + /* |
| 73 | + * Detect if this is a normal entry outside of any subtree |
| 74 | + * entry. |
| 75 | + */ |
| 76 | + base = ce->name + ct_pathlen; |
| 77 | + slash = strchr(base, '/'); |
| 78 | + |
| 79 | + if (slash) |
| 80 | + pos = cache_tree_subtree_pos(ct, base, slash - base); |
| 81 | + |
| 82 | + if (pos < 0) { |
| 83 | + istate->cache[num_converted++] = ce; |
| 84 | + i++; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + strbuf_setlen(&child_path, 0); |
| 89 | + strbuf_add(&child_path, ce->name, slash - ce->name + 1); |
| 90 | + |
| 91 | + span = ct->down[pos]->cache_tree->entry_count; |
| 92 | + count = convert_to_sparse_rec(istate, |
| 93 | + num_converted, i, i + span, |
| 94 | + child_path.buf, child_path.len, |
| 95 | + ct->down[pos]->cache_tree); |
| 96 | + num_converted += count; |
| 97 | + i += span; |
| 98 | + } |
| 99 | + |
| 100 | + strbuf_release(&child_path); |
| 101 | + return num_converted - start_converted; |
| 102 | +} |
| 103 | + |
| 104 | +int convert_to_sparse(struct index_state *istate) |
| 105 | +{ |
| 106 | + if (istate->split_index || istate->sparse_index || |
| 107 | + !core_apply_sparse_checkout || !core_sparse_checkout_cone) |
| 108 | + return 0; |
| 109 | + |
| 110 | + /* |
| 111 | + * For now, only create a sparse index with the |
| 112 | + * GIT_TEST_SPARSE_INDEX environment variable. We will relax |
| 113 | + * this once we have a proper way to opt-in (and later still, |
| 114 | + * opt-out). |
| 115 | + */ |
| 116 | + if (!git_env_bool("GIT_TEST_SPARSE_INDEX", 0)) |
| 117 | + return 0; |
| 118 | + |
| 119 | + if (!istate->sparse_checkout_patterns) { |
| 120 | + istate->sparse_checkout_patterns = xcalloc(1, sizeof(struct pattern_list)); |
| 121 | + if (get_sparse_checkout_patterns(istate->sparse_checkout_patterns) < 0) |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + if (!istate->sparse_checkout_patterns->use_cone_patterns) { |
| 126 | + warning(_("attempting to use sparse-index without cone mode")); |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + if (cache_tree_update(istate, 0)) { |
| 131 | + warning(_("unable to update cache-tree, staying full")); |
| 132 | + return -1; |
| 133 | + } |
| 134 | + |
| 135 | + remove_fsmonitor(istate); |
| 136 | + |
| 137 | + trace2_region_enter("index", "convert_to_sparse", istate->repo); |
| 138 | + istate->cache_nr = convert_to_sparse_rec(istate, |
| 139 | + 0, 0, istate->cache_nr, |
| 140 | + "", 0, istate->cache_tree); |
| 141 | + istate->drop_cache_tree = 1; |
| 142 | + istate->sparse_index = 1; |
| 143 | + trace2_region_leave("index", "convert_to_sparse", istate->repo); |
| 144 | + return 0; |
| 145 | +} |
7 | 146 |
|
8 | 147 | static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce) |
9 | 148 | { |
|
0 commit comments