Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 2 | #include "environment.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 3 | #include "hex.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 4 | #include "lockfile.h" |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 5 | #include "tree.h" |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 6 | #include "tree-walk.h" |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 7 | #include "cache-tree.h" |
Neeraj Singh | 4d33e2b | 2022-04-04 22:20:10 -0700 | [diff] [blame] | 8 | #include "bulk-checkin.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 9 | #include "object-file.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 10 | #include "object-store-ll.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 11 | #include "read-cache-ll.h" |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 12 | #include "replace-object.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 13 | #include "promisor-remote.h" |
Derrick Stolee | 6e77352 | 2021-03-30 13:10:55 +0000 | [diff] [blame] | 14 | #include "sparse-index.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 15 | #include "trace.h" |
| 16 | #include "trace2.h" |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 17 | |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 18 | #ifndef DEBUG_CACHE_TREE |
| 19 | #define DEBUG_CACHE_TREE 0 |
Junio C Hamano | 4903161 | 2006-10-30 15:29:53 -0800 | [diff] [blame] | 20 | #endif |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 21 | |
| 22 | struct cache_tree *cache_tree(void) |
| 23 | { |
| 24 | struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree)); |
| 25 | it->entry_count = -1; |
| 26 | return it; |
| 27 | } |
| 28 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 29 | void cache_tree_free(struct cache_tree **it_p) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 30 | { |
| 31 | int i; |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 32 | struct cache_tree *it = *it_p; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 33 | |
| 34 | if (!it) |
| 35 | return; |
| 36 | for (i = 0; i < it->subtree_nr; i++) |
Elijah Newren | e92fa51 | 2010-09-06 15:40:16 -0600 | [diff] [blame] | 37 | if (it->down[i]) { |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 38 | cache_tree_free(&it->down[i]->cache_tree); |
Elijah Newren | e92fa51 | 2010-09-06 15:40:16 -0600 | [diff] [blame] | 39 | free(it->down[i]); |
| 40 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 41 | free(it->down); |
| 42 | free(it); |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 43 | *it_p = NULL; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 46 | static int subtree_name_cmp(const char *one, int onelen, |
| 47 | const char *two, int twolen) |
| 48 | { |
| 49 | if (onelen < twolen) |
| 50 | return -1; |
| 51 | if (twolen < onelen) |
| 52 | return 1; |
| 53 | return memcmp(one, two, onelen); |
| 54 | } |
| 55 | |
Derrick Stolee | c80dd39 | 2021-01-23 19:58:13 +0000 | [diff] [blame] | 56 | int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen) |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 57 | { |
| 58 | struct cache_tree_sub **down = it->down; |
| 59 | int lo, hi; |
| 60 | lo = 0; |
| 61 | hi = it->subtree_nr; |
| 62 | while (lo < hi) { |
Derrick Stolee | 19716b2 | 2017-10-08 14:29:37 -0400 | [diff] [blame] | 63 | int mi = lo + (hi - lo) / 2; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 64 | struct cache_tree_sub *mdl = down[mi]; |
| 65 | int cmp = subtree_name_cmp(path, pathlen, |
| 66 | mdl->name, mdl->namelen); |
| 67 | if (!cmp) |
| 68 | return mi; |
| 69 | if (cmp < 0) |
| 70 | hi = mi; |
| 71 | else |
| 72 | lo = mi + 1; |
| 73 | } |
| 74 | return -lo-1; |
| 75 | } |
| 76 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 77 | static struct cache_tree_sub *find_subtree(struct cache_tree *it, |
| 78 | const char *path, |
| 79 | int pathlen, |
| 80 | int create) |
| 81 | { |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 82 | struct cache_tree_sub *down; |
Derrick Stolee | c80dd39 | 2021-01-23 19:58:13 +0000 | [diff] [blame] | 83 | int pos = cache_tree_subtree_pos(it, path, pathlen); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 84 | if (0 <= pos) |
| 85 | return it->down[pos]; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 86 | if (!create) |
| 87 | return NULL; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 88 | |
| 89 | pos = -pos-1; |
Dmitry S. Dolzhenko | bcc7a03 | 2014-03-04 02:31:51 +0400 | [diff] [blame] | 90 | ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 91 | it->subtree_nr++; |
| 92 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 93 | FLEX_ALLOC_MEM(down, name, path, pathlen); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 94 | down->cache_tree = NULL; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 95 | down->namelen = pathlen; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 96 | |
| 97 | if (pos < it->subtree_nr) |
SZEDER Gábor | f919ffe | 2018-01-22 18:50:09 +0100 | [diff] [blame] | 98 | MOVE_ARRAY(it->down + pos + 1, it->down + pos, |
| 99 | it->subtree_nr - pos - 1); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 100 | it->down[pos] = down; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 101 | return down; |
| 102 | } |
| 103 | |
Junio C Hamano | 7927a55 | 2006-04-27 01:33:07 -0700 | [diff] [blame] | 104 | struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path) |
| 105 | { |
| 106 | int pathlen = strlen(path); |
| 107 | return find_subtree(it, path, pathlen, 1); |
| 108 | } |
| 109 | |
Nguyễn Thái Ngọc Duy | a5400ef | 2014-06-13 19:19:31 +0700 | [diff] [blame] | 110 | static int do_invalidate_path(struct cache_tree *it, const char *path) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 111 | { |
| 112 | /* a/b/c |
| 113 | * ==> invalidate self |
| 114 | * ==> find "a", have it invalidate "b/c" |
| 115 | * a |
| 116 | * ==> invalidate self |
| 117 | * ==> if "a" exists as a subtree, remove it. |
| 118 | */ |
| 119 | const char *slash; |
| 120 | int namelen; |
| 121 | struct cache_tree_sub *down; |
| 122 | |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 123 | #if DEBUG_CACHE_TREE |
Junio C Hamano | 00703e6 | 2006-05-03 16:10:45 -0700 | [diff] [blame] | 124 | fprintf(stderr, "cache-tree invalidate <%s>\n", path); |
| 125 | #endif |
| 126 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 127 | if (!it) |
Nguyễn Thái Ngọc Duy | a5400ef | 2014-06-13 19:19:31 +0700 | [diff] [blame] | 128 | return 0; |
Rohit Mani | 2c5495f | 2014-03-07 22:48:31 -0800 | [diff] [blame] | 129 | slash = strchrnul(path, '/'); |
| 130 | namelen = slash - path; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 131 | it->entry_count = -1; |
Rohit Mani | 2c5495f | 2014-03-07 22:48:31 -0800 | [diff] [blame] | 132 | if (!*slash) { |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 133 | int pos; |
Derrick Stolee | c80dd39 | 2021-01-23 19:58:13 +0000 | [diff] [blame] | 134 | pos = cache_tree_subtree_pos(it, path, namelen); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 135 | if (0 <= pos) { |
| 136 | cache_tree_free(&it->down[pos]->cache_tree); |
| 137 | free(it->down[pos]); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 138 | /* 0 1 2 3 4 5 |
| 139 | * ^ ^subtree_nr = 6 |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 140 | * pos |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 141 | * move 4 and 5 up one place (2 entries) |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 142 | * 2 = 6 - 3 - 1 = subtree_nr - pos - 1 |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 143 | */ |
René Scharfe | f331ab9 | 2017-07-15 22:00:45 +0200 | [diff] [blame] | 144 | MOVE_ARRAY(it->down + pos, it->down + pos + 1, |
| 145 | it->subtree_nr - pos - 1); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 146 | it->subtree_nr--; |
| 147 | } |
Nguyễn Thái Ngọc Duy | a5400ef | 2014-06-13 19:19:31 +0700 | [diff] [blame] | 148 | return 1; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 149 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 150 | down = find_subtree(it, path, namelen, 0); |
| 151 | if (down) |
Nguyễn Thái Ngọc Duy | a5400ef | 2014-06-13 19:19:31 +0700 | [diff] [blame] | 152 | do_invalidate_path(down->cache_tree, slash + 1); |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | void cache_tree_invalidate_path(struct index_state *istate, const char *path) |
| 157 | { |
| 158 | if (do_invalidate_path(istate->cache_tree, path)) |
| 159 | istate->cache_changed |= CACHE_TREE_CHANGED; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 162 | static int verify_cache(struct index_state *istate, int flags) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 163 | { |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 164 | unsigned i, funny; |
Nguyễn Thái Ngọc Duy | e859c69 | 2012-01-16 09:36:46 +0700 | [diff] [blame] | 165 | int silent = flags & WRITE_TREE_SILENT; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 166 | |
| 167 | /* Verify that the tree is merged */ |
| 168 | funny = 0; |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 169 | for (i = 0; i < istate->cache_nr; i++) { |
| 170 | const struct cache_entry *ce = istate->cache[i]; |
Junio C Hamano | 3f6d56d | 2012-02-07 11:55:48 -0800 | [diff] [blame] | 171 | if (ce_stage(ce)) { |
Thomas Rast | 996277c | 2011-12-06 18:43:37 +0100 | [diff] [blame] | 172 | if (silent) |
| 173 | return -1; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 174 | if (10 < ++funny) { |
| 175 | fprintf(stderr, "...\n"); |
| 176 | break; |
| 177 | } |
Nguyễn Thái Ngọc Duy | dbc3904 | 2012-12-16 11:15:25 +0700 | [diff] [blame] | 178 | fprintf(stderr, "%s: unmerged (%s)\n", |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 179 | ce->name, oid_to_hex(&ce->oid)); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | if (funny) |
| 183 | return -1; |
| 184 | |
| 185 | /* Also verify that the cache does not have path and path/file |
| 186 | * at the same time. At this point we know the cache has only |
| 187 | * stage 0 entries. |
| 188 | */ |
| 189 | funny = 0; |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 190 | for (i = 0; i + 1 < istate->cache_nr; i++) { |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 191 | /* path/file always comes after path because of the way |
| 192 | * the cache is sorted. Also path can appear only once, |
| 193 | * which means conflicting one would immediately follow. |
| 194 | */ |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 195 | const struct cache_entry *this_ce = istate->cache[i]; |
| 196 | const struct cache_entry *next_ce = istate->cache[i + 1]; |
René Scharfe | 0b72536 | 2021-01-07 16:32:10 +0000 | [diff] [blame] | 197 | const char *this_name = this_ce->name; |
| 198 | const char *next_name = next_ce->name; |
| 199 | int this_len = ce_namelen(this_ce); |
| 200 | if (this_len < ce_namelen(next_ce) && |
Derrick Stolee | a4b6d20 | 2021-01-07 16:32:11 +0000 | [diff] [blame] | 201 | next_name[this_len] == '/' && |
| 202 | strncmp(this_name, next_name, this_len) == 0) { |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 203 | if (10 < ++funny) { |
| 204 | fprintf(stderr, "...\n"); |
| 205 | break; |
| 206 | } |
| 207 | fprintf(stderr, "You have both %s and %s\n", |
| 208 | this_name, next_name); |
| 209 | } |
| 210 | } |
| 211 | if (funny) |
| 212 | return -1; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static void discard_unused_subtrees(struct cache_tree *it) |
| 217 | { |
| 218 | struct cache_tree_sub **down = it->down; |
| 219 | int nr = it->subtree_nr; |
| 220 | int dst, src; |
| 221 | for (dst = src = 0; src < nr; src++) { |
| 222 | struct cache_tree_sub *s = down[src]; |
| 223 | if (s->used) |
| 224 | down[dst++] = s; |
| 225 | else { |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 226 | cache_tree_free(&s->cache_tree); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 227 | free(s); |
| 228 | it->subtree_nr--; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 233 | int cache_tree_fully_valid(struct cache_tree *it) |
| 234 | { |
| 235 | int i; |
| 236 | if (!it) |
| 237 | return 0; |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 238 | if (it->entry_count < 0 || !repo_has_object_file(the_repository, &it->oid)) |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | for (i = 0; i < it->subtree_nr; i++) { |
| 241 | if (!cache_tree_fully_valid(it->down[i]->cache_tree)) |
| 242 | return 0; |
| 243 | } |
| 244 | return 1; |
| 245 | } |
| 246 | |
Jonathan Tan | d3da223 | 2021-07-23 11:52:23 -0700 | [diff] [blame] | 247 | static int must_check_existence(const struct cache_entry *ce) |
| 248 | { |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 249 | return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce)); |
Jonathan Tan | d3da223 | 2021-07-23 11:52:23 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 252 | static int update_one(struct cache_tree *it, |
Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 19:19:32 +0700 | [diff] [blame] | 253 | struct cache_entry **cache, |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 254 | int entries, |
| 255 | const char *base, |
| 256 | int baselen, |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 257 | int *skip_count, |
Nguyễn Thái Ngọc Duy | e859c69 | 2012-01-16 09:36:46 +0700 | [diff] [blame] | 258 | int flags) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 259 | { |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 260 | struct strbuf buffer; |
Nguyễn Thái Ngọc Duy | e859c69 | 2012-01-16 09:36:46 +0700 | [diff] [blame] | 261 | int missing_ok = flags & WRITE_TREE_MISSING_OK; |
| 262 | int dryrun = flags & WRITE_TREE_DRY_RUN; |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 263 | int repair = flags & WRITE_TREE_REPAIR; |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 264 | int to_invalidate = 0; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 265 | int i; |
| 266 | |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 267 | assert(!(dryrun && repair)); |
| 268 | |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 269 | *skip_count = 0; |
| 270 | |
Derrick Stolee | 2de37c5 | 2021-03-30 13:11:02 +0000 | [diff] [blame] | 271 | /* |
| 272 | * If the first entry of this region is a sparse directory |
| 273 | * entry corresponding exactly to 'base', then this cache_tree |
| 274 | * struct is a "leaf" in the data structure, pointing to the |
| 275 | * tree OID specified in the entry. |
| 276 | */ |
| 277 | if (entries > 0) { |
| 278 | const struct cache_entry *ce = cache[0]; |
| 279 | |
| 280 | if (S_ISSPARSEDIR(ce->ce_mode) && |
| 281 | ce->ce_namelen == baselen && |
| 282 | !strncmp(ce->name, base, baselen)) { |
| 283 | it->entry_count = 1; |
| 284 | oidcpy(&it->oid, &ce->oid); |
| 285 | return 1; |
| 286 | } |
| 287 | } |
| 288 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 289 | if (0 <= it->entry_count && repo_has_object_file(the_repository, &it->oid)) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 290 | return it->entry_count; |
| 291 | |
| 292 | /* |
| 293 | * We first scan for subtrees and update them; we start by |
| 294 | * marking existing subtrees -- the ones that are unmarked |
| 295 | * should not be in the result. |
| 296 | */ |
| 297 | for (i = 0; i < it->subtree_nr; i++) |
| 298 | it->down[i]->used = 0; |
| 299 | |
| 300 | /* |
| 301 | * Find the subtrees and update them. |
| 302 | */ |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 303 | i = 0; |
| 304 | while (i < entries) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 305 | const struct cache_entry *ce = cache[i]; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 306 | struct cache_tree_sub *sub; |
| 307 | const char *path, *slash; |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 308 | int pathlen, sublen, subcnt, subskip; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 309 | |
| 310 | path = ce->name; |
| 311 | pathlen = ce_namelen(ce); |
| 312 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
| 313 | break; /* at the end of this level */ |
| 314 | |
| 315 | slash = strchr(path + baselen, '/'); |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 316 | if (!slash) { |
| 317 | i++; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 318 | continue; |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 319 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 320 | /* |
| 321 | * a/bbb/c (base = a/, slash = /c) |
| 322 | * ==> |
| 323 | * path+baselen = bbb/c, sublen = 3 |
| 324 | */ |
| 325 | sublen = slash - (path + baselen); |
| 326 | sub = find_subtree(it, path + baselen, sublen, 1); |
| 327 | if (!sub->cache_tree) |
| 328 | sub->cache_tree = cache_tree(); |
| 329 | subcnt = update_one(sub->cache_tree, |
| 330 | cache + i, entries - i, |
| 331 | path, |
| 332 | baselen + sublen + 1, |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 333 | &subskip, |
Nguyễn Thái Ngọc Duy | e859c69 | 2012-01-16 09:36:46 +0700 | [diff] [blame] | 334 | flags); |
Johannes Sixt | 3d12d0c | 2006-11-13 13:50:00 +0000 | [diff] [blame] | 335 | if (subcnt < 0) |
| 336 | return subcnt; |
Jeff King | 729dbbd | 2014-10-29 13:11:58 -0400 | [diff] [blame] | 337 | if (!subcnt) |
| 338 | die("index cache-tree records empty sub-tree"); |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 339 | i += subcnt; |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 340 | sub->count = subcnt; /* to be used in the next loop */ |
| 341 | *skip_count += subskip; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 342 | sub->used = 1; |
| 343 | } |
| 344 | |
| 345 | discard_unused_subtrees(it); |
| 346 | |
| 347 | /* |
| 348 | * Then write out the tree object for this level. |
| 349 | */ |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 350 | strbuf_init(&buffer, 8192); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 351 | |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 352 | i = 0; |
| 353 | while (i < entries) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 354 | const struct cache_entry *ce = cache[i]; |
Nguyễn Thái Ngọc Duy | c041d54 | 2016-07-16 07:06:26 +0200 | [diff] [blame] | 355 | struct cache_tree_sub *sub = NULL; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 356 | const char *path, *slash; |
| 357 | int pathlen, entlen; |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 358 | const struct object_id *oid; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 359 | unsigned mode; |
Junio C Hamano | 4ed115e | 2014-09-02 14:16:20 -0700 | [diff] [blame] | 360 | int expected_missing = 0; |
Nguyễn Thái Ngọc Duy | 6d6a782 | 2016-07-16 07:06:27 +0200 | [diff] [blame] | 361 | int contains_ita = 0; |
Jonathan Tan | 2f215ff | 2018-10-09 11:40:37 -0700 | [diff] [blame] | 362 | int ce_missing_ok; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 363 | |
| 364 | path = ce->name; |
| 365 | pathlen = ce_namelen(ce); |
| 366 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
| 367 | break; /* at the end of this level */ |
| 368 | |
| 369 | slash = strchr(path + baselen, '/'); |
| 370 | if (slash) { |
| 371 | entlen = slash - (path + baselen); |
| 372 | sub = find_subtree(it, path + baselen, entlen, 0); |
| 373 | if (!sub) |
| 374 | die("cache-tree.c: '%.*s' in '%s' not found", |
| 375 | entlen, path + baselen, path); |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 376 | i += sub->count; |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 377 | oid = &sub->cache_tree->oid; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 378 | mode = S_IFDIR; |
Nguyễn Thái Ngọc Duy | 6d6a782 | 2016-07-16 07:06:27 +0200 | [diff] [blame] | 379 | contains_ita = sub->cache_tree->entry_count < 0; |
| 380 | if (contains_ita) { |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 381 | to_invalidate = 1; |
Junio C Hamano | 4ed115e | 2014-09-02 14:16:20 -0700 | [diff] [blame] | 382 | expected_missing = 1; |
| 383 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 384 | } |
| 385 | else { |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 386 | oid = &ce->oid; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 387 | mode = ce->ce_mode; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 388 | entlen = pathlen - baselen; |
Nguyễn Thái Ngọc Duy | 386cc8b | 2012-12-16 11:15:26 +0700 | [diff] [blame] | 389 | i++; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 390 | } |
Jeff King | a96d3cc | 2017-04-21 14:46:17 -0400 | [diff] [blame] | 391 | |
Jonathan Tan | 2f215ff | 2018-10-09 11:40:37 -0700 | [diff] [blame] | 392 | ce_missing_ok = mode == S_IFGITLINK || missing_ok || |
Jonathan Tan | d3da223 | 2021-07-23 11:52:23 -0700 | [diff] [blame] | 393 | !must_check_existence(ce); |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 394 | if (is_null_oid(oid) || |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 395 | (!ce_missing_ok && !repo_has_object_file(the_repository, oid))) { |
Jonathan Nieder | b6b56ac | 2010-08-09 22:32:11 -0500 | [diff] [blame] | 396 | strbuf_release(&buffer); |
Junio C Hamano | 4ed115e | 2014-09-02 14:16:20 -0700 | [diff] [blame] | 397 | if (expected_missing) |
| 398 | return -1; |
Linus Torvalds | a388373 | 2009-07-14 11:25:17 -0700 | [diff] [blame] | 399 | return error("invalid object %06o %s for '%.*s'", |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 400 | mode, oid_to_hex(oid), entlen+baselen, path); |
Jonathan Nieder | b6b56ac | 2010-08-09 22:32:11 -0500 | [diff] [blame] | 401 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 402 | |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 403 | /* |
| 404 | * CE_REMOVE entries are removed before the index is |
| 405 | * written to disk. Skip them to remain consistent |
| 406 | * with the future on-disk index. |
| 407 | */ |
| 408 | if (ce->ce_flags & CE_REMOVE) { |
| 409 | *skip_count = *skip_count + 1; |
| 410 | continue; |
| 411 | } |
| 412 | |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 413 | /* |
Andrei Rybak | b39a841 | 2023-01-07 14:56:55 +0100 | [diff] [blame] | 414 | * CE_INTENT_TO_ADD entries exist in on-disk index but |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 415 | * they are not part of generated trees. Invalidate up |
| 416 | * to root to force cache-tree users to read elsewhere. |
| 417 | */ |
Nguyễn Thái Ngọc Duy | c041d54 | 2016-07-16 07:06:26 +0200 | [diff] [blame] | 418 | if (!sub && ce_intent_to_add(ce)) { |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 419 | to_invalidate = 1; |
Nguyễn Thái Ngọc Duy | 3cf773e | 2012-12-16 11:15:27 +0700 | [diff] [blame] | 420 | continue; |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 421 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 422 | |
Nguyễn Thái Ngọc Duy | 6d6a782 | 2016-07-16 07:06:27 +0200 | [diff] [blame] | 423 | /* |
| 424 | * "sub" can be an empty tree if all subentries are i-t-a. |
| 425 | */ |
brian m. carlson | a055493 | 2018-05-02 00:26:04 +0000 | [diff] [blame] | 426 | if (contains_ita && is_empty_tree_oid(oid)) |
Nguyễn Thái Ngọc Duy | 6d6a782 | 2016-07-16 07:06:27 +0200 | [diff] [blame] | 427 | continue; |
| 428 | |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 429 | strbuf_grow(&buffer, entlen + 100); |
| 430 | strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 431 | strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 432 | |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 433 | #if DEBUG_CACHE_TREE |
Junio C Hamano | 00703e6 | 2006-05-03 16:10:45 -0700 | [diff] [blame] | 434 | fprintf(stderr, "cache-tree update-one %o %.*s\n", |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 435 | mode, entlen, path + baselen); |
| 436 | #endif |
| 437 | } |
| 438 | |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 439 | if (repair) { |
Patryk Obara | f070fac | 2018-01-28 01:13:13 +0100 | [diff] [blame] | 440 | struct object_id oid; |
Matheus Tavares | 2dcde20 | 2020-01-30 17:32:22 -0300 | [diff] [blame] | 441 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 442 | OBJ_TREE, &oid); |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 443 | if (repo_has_object_file_with_flags(the_repository, &oid, OBJECT_INFO_SKIP_FETCH_OBJECT)) |
Patryk Obara | f070fac | 2018-01-28 01:13:13 +0100 | [diff] [blame] | 444 | oidcpy(&it->oid, &oid); |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 445 | else |
| 446 | to_invalidate = 1; |
Patryk Obara | a09c985 | 2018-01-28 01:13:19 +0100 | [diff] [blame] | 447 | } else if (dryrun) { |
Matheus Tavares | 2dcde20 | 2020-01-30 17:32:22 -0300 | [diff] [blame] | 448 | hash_object_file(the_hash_algo, buffer.buf, buffer.len, |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 449 | OBJ_TREE, &it->oid); |
Ævar Arnfjörð Bjarmason | c80d226 | 2022-02-05 00:48:26 +0100 | [diff] [blame] | 450 | } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, |
Ævar Arnfjörð Bjarmason | 4ef91a2 | 2021-10-12 16:30:49 +0200 | [diff] [blame] | 451 | &it->oid, flags & WRITE_TREE_SILENT |
| 452 | ? HASH_SILENT : 0)) { |
Junio C Hamano | edae5f0 | 2008-04-23 09:47:17 -0700 | [diff] [blame] | 453 | strbuf_release(&buffer); |
| 454 | return -1; |
| 455 | } |
| 456 | |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 457 | strbuf_release(&buffer); |
Nguyễn Thái Ngọc Duy | eec3e7e | 2012-12-16 11:15:28 +0700 | [diff] [blame] | 458 | it->entry_count = to_invalidate ? -1 : i - *skip_count; |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 459 | #if DEBUG_CACHE_TREE |
Junio C Hamano | 00703e6 | 2006-05-03 16:10:45 -0700 | [diff] [blame] | 460 | fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n", |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 461 | it->entry_count, it->subtree_nr, |
brian m. carlson | e0a9280 | 2017-05-01 02:28:56 +0000 | [diff] [blame] | 462 | oid_to_hex(&it->oid)); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 463 | #endif |
| 464 | return i; |
| 465 | } |
| 466 | |
Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 19:19:32 +0700 | [diff] [blame] | 467 | int cache_tree_update(struct index_state *istate, int flags) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 468 | { |
Derrick Stolee | fb08826 | 2021-01-23 19:58:11 +0000 | [diff] [blame] | 469 | int skip, i; |
| 470 | |
Derrick Stolee | 8d87e33 | 2021-01-23 19:58:12 +0000 | [diff] [blame] | 471 | i = verify_cache(istate, flags); |
Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 19:19:32 +0700 | [diff] [blame] | 472 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 473 | if (i) |
| 474 | return i; |
Derrick Stolee | fb08826 | 2021-01-23 19:58:11 +0000 | [diff] [blame] | 475 | |
| 476 | if (!istate->cache_tree) |
| 477 | istate->cache_tree = cache_tree(); |
| 478 | |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 479 | if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository)) |
Jonathan Tan | d3da223 | 2021-07-23 11:52:23 -0700 | [diff] [blame] | 480 | prefetch_cache_entries(istate, must_check_existence); |
| 481 | |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 482 | trace_performance_enter(); |
Derrick Stolee | fa7ca5d | 2021-01-04 03:09:12 +0000 | [diff] [blame] | 483 | trace2_region_enter("cache_tree", "update", the_repository); |
Neeraj Singh | 4d33e2b | 2022-04-04 22:20:10 -0700 | [diff] [blame] | 484 | begin_odb_transaction(); |
Derrick Stolee | fb08826 | 2021-01-23 19:58:11 +0000 | [diff] [blame] | 485 | i = update_one(istate->cache_tree, istate->cache, istate->cache_nr, |
| 486 | "", 0, &skip, flags); |
Neeraj Singh | 4d33e2b | 2022-04-04 22:20:10 -0700 | [diff] [blame] | 487 | end_odb_transaction(); |
Derrick Stolee | fa7ca5d | 2021-01-04 03:09:12 +0000 | [diff] [blame] | 488 | trace2_region_leave("cache_tree", "update", the_repository); |
Nguyễn Thái Ngọc Duy | 0d1ed59 | 2018-08-18 16:41:23 +0200 | [diff] [blame] | 489 | trace_performance_leave("cache_tree_update"); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 490 | if (i < 0) |
| 491 | return i; |
Nguyễn Thái Ngọc Duy | d0cfc3e | 2014-06-13 19:19:32 +0700 | [diff] [blame] | 492 | istate->cache_changed |= CACHE_TREE_CHANGED; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 496 | static void write_one(struct strbuf *buffer, struct cache_tree *it, |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 497 | const char *path, int pathlen) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 498 | { |
| 499 | int i; |
| 500 | |
| 501 | /* One "cache-tree" entry consists of the following: |
| 502 | * path (NUL terminated) |
| 503 | * entry_count, subtree_nr ("%d %d\n") |
| 504 | * tree-sha1 (missing if invalid) |
| 505 | * subtree_nr "cache-tree" entries for subtrees. |
| 506 | */ |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 507 | strbuf_grow(buffer, pathlen + 100); |
| 508 | strbuf_add(buffer, path, pathlen); |
| 509 | strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 510 | |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 511 | #if DEBUG_CACHE_TREE |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 512 | if (0 <= it->entry_count) |
| 513 | fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", |
| 514 | pathlen, path, it->entry_count, it->subtree_nr, |
brian m. carlson | e0a9280 | 2017-05-01 02:28:56 +0000 | [diff] [blame] | 515 | oid_to_hex(&it->oid)); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 516 | else |
| 517 | fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n", |
| 518 | pathlen, path, it->subtree_nr); |
| 519 | #endif |
| 520 | |
| 521 | if (0 <= it->entry_count) { |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 522 | strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 523 | } |
| 524 | for (i = 0; i < it->subtree_nr; i++) { |
| 525 | struct cache_tree_sub *down = it->down[i]; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 526 | if (i) { |
| 527 | struct cache_tree_sub *prev = it->down[i-1]; |
| 528 | if (subtree_name_cmp(down->name, down->namelen, |
| 529 | prev->name, prev->namelen) <= 0) |
| 530 | die("fatal - unsorted cache subtree"); |
| 531 | } |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 532 | write_one(buffer, down->cache_tree, down->name, down->namelen); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 533 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 536 | void cache_tree_write(struct strbuf *sb, struct cache_tree *root) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 537 | { |
Derrick Stolee | 4c3e187 | 2021-01-04 03:09:13 +0000 | [diff] [blame] | 538 | trace2_region_enter("cache_tree", "write", the_repository); |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 539 | write_one(sb, root, "", 0); |
Derrick Stolee | 4c3e187 | 2021-01-04 03:09:13 +0000 | [diff] [blame] | 540 | trace2_region_leave("cache_tree", "write", the_repository); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) |
| 544 | { |
| 545 | const char *buf = *buffer; |
| 546 | unsigned long size = *size_p; |
Johannes Schindelin | 0111ea3 | 2006-05-02 03:31:02 +0200 | [diff] [blame] | 547 | const char *cp; |
| 548 | char *ep; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 549 | struct cache_tree *it; |
| 550 | int i, subtree_nr; |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 551 | const unsigned rawsz = the_hash_algo->rawsz; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 552 | |
| 553 | it = NULL; |
| 554 | /* skip name, but make sure name exists */ |
| 555 | while (size && *buf) { |
| 556 | size--; |
| 557 | buf++; |
| 558 | } |
| 559 | if (!size) |
| 560 | goto free_return; |
| 561 | buf++; size--; |
| 562 | it = cache_tree(); |
Johannes Schindelin | 0111ea3 | 2006-05-02 03:31:02 +0200 | [diff] [blame] | 563 | |
| 564 | cp = buf; |
| 565 | it->entry_count = strtol(cp, &ep, 10); |
| 566 | if (cp == ep) |
| 567 | goto free_return; |
| 568 | cp = ep; |
| 569 | subtree_nr = strtol(cp, &ep, 10); |
| 570 | if (cp == ep) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 571 | goto free_return; |
| 572 | while (size && *buf && *buf != '\n') { |
| 573 | size--; |
| 574 | buf++; |
| 575 | } |
| 576 | if (!size) |
| 577 | goto free_return; |
| 578 | buf++; size--; |
| 579 | if (0 <= it->entry_count) { |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 580 | if (size < rawsz) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 581 | goto free_return; |
brian m. carlson | 69d1242 | 2018-05-02 00:25:29 +0000 | [diff] [blame] | 582 | oidread(&it->oid, (const unsigned char *)buf); |
brian m. carlson | 6dcb462 | 2018-03-12 02:27:24 +0000 | [diff] [blame] | 583 | buf += rawsz; |
| 584 | size -= rawsz; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jeff Hostetler | e9b9cc5 | 2019-06-19 14:05:58 -0700 | [diff] [blame] | 587 | #if DEBUG_CACHE_TREE |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 588 | if (0 <= it->entry_count) |
| 589 | fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", |
| 590 | *buffer, it->entry_count, subtree_nr, |
brian m. carlson | e0a9280 | 2017-05-01 02:28:56 +0000 | [diff] [blame] | 591 | oid_to_hex(&it->oid)); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 592 | else |
| 593 | fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n", |
| 594 | *buffer, subtree_nr); |
| 595 | #endif |
| 596 | |
| 597 | /* |
| 598 | * Just a heuristic -- we do not add directories that often but |
| 599 | * we do not want to have to extend it immediately when we do, |
| 600 | * hence +2. |
| 601 | */ |
| 602 | it->subtree_alloc = subtree_nr + 2; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 603 | CALLOC_ARRAY(it->down, it->subtree_alloc); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 604 | for (i = 0; i < subtree_nr; i++) { |
| 605 | /* read each subtree */ |
| 606 | struct cache_tree *sub; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 607 | struct cache_tree_sub *subtree; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 608 | const char *name = buf; |
Junio C Hamano | 7927a55 | 2006-04-27 01:33:07 -0700 | [diff] [blame] | 609 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 610 | sub = read_one(&buf, &size); |
| 611 | if (!sub) |
| 612 | goto free_return; |
Junio C Hamano | 7927a55 | 2006-04-27 01:33:07 -0700 | [diff] [blame] | 613 | subtree = cache_tree_sub(it, name); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 614 | subtree->cache_tree = sub; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 615 | } |
| 616 | if (subtree_nr != it->subtree_nr) |
| 617 | die("cache-tree: internal error"); |
| 618 | *buffer = buf; |
| 619 | *size_p = size; |
| 620 | return it; |
| 621 | |
| 622 | free_return: |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 623 | cache_tree_free(&it); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 624 | return NULL; |
| 625 | } |
| 626 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 627 | struct cache_tree *cache_tree_read(const char *buffer, unsigned long size) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 628 | { |
Derrick Stolee | 4c3e187 | 2021-01-04 03:09:13 +0000 | [diff] [blame] | 629 | struct cache_tree *result; |
| 630 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 631 | if (buffer[0]) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 632 | return NULL; /* not the whole tree */ |
Derrick Stolee | 4c3e187 | 2021-01-04 03:09:13 +0000 | [diff] [blame] | 633 | |
| 634 | trace2_region_enter("cache_tree", "read", the_repository); |
| 635 | result = read_one(&buffer, &size); |
| 636 | trace2_region_leave("cache_tree", "read", the_repository); |
| 637 | |
| 638 | return result; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 639 | } |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 640 | |
Nanako Shiraishi | 7ba04d9 | 2008-07-16 19:42:10 +0900 | [diff] [blame] | 641 | static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path) |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 642 | { |
Junio C Hamano | b87fc96 | 2009-05-20 15:53:57 -0700 | [diff] [blame] | 643 | if (!it) |
| 644 | return NULL; |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 645 | while (*path) { |
| 646 | const char *slash; |
| 647 | struct cache_tree_sub *sub; |
| 648 | |
Michael Haggerty | 17e22dd | 2014-03-05 18:26:26 +0100 | [diff] [blame] | 649 | slash = strchrnul(path, '/'); |
Michael Haggerty | 79192b8 | 2014-03-05 18:26:27 +0100 | [diff] [blame] | 650 | /* |
| 651 | * Between path and slash is the name of the subtree |
| 652 | * to look for. |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 653 | */ |
| 654 | sub = find_subtree(it, path, slash - path, 0); |
| 655 | if (!sub) |
| 656 | return NULL; |
| 657 | it = sub->cache_tree; |
Michael Haggerty | 3491047 | 2014-03-05 18:26:30 +0100 | [diff] [blame] | 658 | |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 659 | path = slash; |
Michael Haggerty | 3491047 | 2014-03-05 18:26:30 +0100 | [diff] [blame] | 660 | while (*path == '/') |
| 661 | path++; |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 662 | } |
| 663 | return it; |
| 664 | } |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 665 | |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 666 | static int write_index_as_tree_internal(struct object_id *oid, |
| 667 | struct index_state *index_state, |
| 668 | int cache_tree_valid, |
| 669 | int flags, |
| 670 | const char *prefix) |
| 671 | { |
| 672 | if (flags & WRITE_TREE_IGNORE_CACHE_TREE) { |
| 673 | cache_tree_free(&index_state->cache_tree); |
| 674 | cache_tree_valid = 0; |
| 675 | } |
| 676 | |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 677 | if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0) |
| 678 | return WRITE_TREE_UNMERGED_INDEX; |
| 679 | |
| 680 | if (prefix) { |
| 681 | struct cache_tree *subtree; |
| 682 | subtree = cache_tree_find(index_state->cache_tree, prefix); |
| 683 | if (!subtree) |
| 684 | return WRITE_TREE_PREFIX_ERROR; |
| 685 | oidcpy(oid, &subtree->oid); |
| 686 | } |
| 687 | else |
| 688 | oidcpy(oid, &index_state->cache_tree->oid); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | struct tree* write_in_core_index_as_tree(struct repository *repo) { |
| 694 | struct object_id o; |
| 695 | int was_valid, ret; |
| 696 | |
| 697 | struct index_state *index_state = repo->index; |
| 698 | was_valid = index_state->cache_tree && |
| 699 | cache_tree_fully_valid(index_state->cache_tree); |
| 700 | |
| 701 | ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL); |
| 702 | if (ret == WRITE_TREE_UNMERGED_INDEX) { |
| 703 | int i; |
Ævar Arnfjörð Bjarmason | 6d40f0a | 2022-06-02 14:25:37 +0200 | [diff] [blame] | 704 | bug("there are unmerged index entries:"); |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 705 | for (i = 0; i < index_state->cache_nr; i++) { |
| 706 | const struct cache_entry *ce = index_state->cache[i]; |
| 707 | if (ce_stage(ce)) |
Ævar Arnfjörð Bjarmason | 6d40f0a | 2022-06-02 14:25:37 +0200 | [diff] [blame] | 708 | bug("%d %.*s", ce_stage(ce), |
| 709 | (int)ce_namelen(ce), ce->name); |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 710 | } |
Ævar Arnfjörð Bjarmason | 6d40f0a | 2022-06-02 14:25:37 +0200 | [diff] [blame] | 711 | BUG("unmerged index entries when writing in-core index"); |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return lookup_tree(repo, &index_state->cache_tree->oid); |
| 715 | } |
| 716 | |
| 717 | |
brian m. carlson | fc5cb99 | 2018-03-12 02:27:23 +0000 | [diff] [blame] | 718 | int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix) |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 719 | { |
Martin Ågren | 2954e5e | 2017-10-05 22:32:08 +0200 | [diff] [blame] | 720 | int entries, was_valid; |
Jeff King | bfffb48 | 2017-09-05 08:15:21 -0400 | [diff] [blame] | 721 | struct lock_file lock_file = LOCK_INIT; |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 722 | int ret; |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 723 | |
Martin Ågren | 2954e5e | 2017-10-05 22:32:08 +0200 | [diff] [blame] | 724 | hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR); |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 725 | |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 726 | entries = read_index_from(index_state, index_path, get_git_dir()); |
Jeff King | c82c75b | 2017-09-05 08:14:07 -0400 | [diff] [blame] | 727 | if (entries < 0) { |
| 728 | ret = WRITE_TREE_UNREADABLE_INDEX; |
| 729 | goto out; |
| 730 | } |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 731 | |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 732 | was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) && |
| 733 | index_state->cache_tree && |
| 734 | cache_tree_fully_valid(index_state->cache_tree); |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 735 | |
Elijah Newren | 724dd76 | 2019-08-17 11:41:32 -0700 | [diff] [blame] | 736 | ret = write_index_as_tree_internal(oid, index_state, was_valid, flags, |
| 737 | prefix); |
| 738 | if (!ret && !was_valid) { |
Martin Ågren | 2954e5e | 2017-10-05 22:32:08 +0200 | [diff] [blame] | 739 | write_locked_index(index_state, &lock_file, COMMIT_LOCK); |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 740 | /* Not being able to write is fine -- we are only interested |
| 741 | * in updating the cache-tree part, and if the next caller |
| 742 | * ends up using the old index with unupdated cache-tree part |
| 743 | * it misses the work we did here, but that is just a |
| 744 | * performance penalty and not a big deal. |
| 745 | */ |
| 746 | } |
| 747 | |
Jeff King | c82c75b | 2017-09-05 08:14:07 -0400 | [diff] [blame] | 748 | out: |
Martin Ågren | 2954e5e | 2017-10-05 22:32:08 +0200 | [diff] [blame] | 749 | rollback_lock_file(&lock_file); |
Jeff King | c82c75b | 2017-09-05 08:14:07 -0400 | [diff] [blame] | 750 | return ret; |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 751 | } |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 752 | |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 753 | static void prime_cache_tree_sparse_dir(struct cache_tree *it, |
| 754 | struct tree *tree) |
| 755 | { |
| 756 | |
| 757 | oidcpy(&it->oid, &tree->object.oid); |
| 758 | it->entry_count = 1; |
| 759 | } |
| 760 | |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 761 | static void prime_cache_tree_rec(struct repository *r, |
| 762 | struct cache_tree *it, |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 763 | struct tree *tree, |
| 764 | struct strbuf *tree_path) |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 765 | { |
| 766 | struct tree_desc desc; |
| 767 | struct name_entry entry; |
| 768 | int cnt; |
René Scharfe | 93ea118 | 2023-02-10 21:20:30 +0100 | [diff] [blame] | 769 | size_t base_path_len = tree_path->len; |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 770 | |
brian m. carlson | e0a9280 | 2017-05-01 02:28:56 +0000 | [diff] [blame] | 771 | oidcpy(&it->oid, &tree->object.oid); |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 772 | |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 773 | init_tree_desc(&desc, tree->buffer, tree->size); |
| 774 | cnt = 0; |
| 775 | while (tree_entry(&desc, &entry)) { |
| 776 | if (!S_ISDIR(entry.mode)) |
| 777 | cnt++; |
| 778 | else { |
| 779 | struct cache_tree_sub *sub; |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 780 | struct tree *subtree = lookup_tree(r, &entry.oid); |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 781 | |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 782 | if (!subtree->object.parsed) |
| 783 | parse_tree(subtree); |
| 784 | sub = cache_tree_sub(it, entry.path); |
| 785 | sub->cache_tree = cache_tree(); |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * Recursively-constructed subtree path is only needed when working |
| 789 | * in a sparse index (where it's used to determine whether the |
| 790 | * subtree is a sparse directory in the index). |
| 791 | */ |
| 792 | if (r->index->sparse_index) { |
| 793 | strbuf_setlen(tree_path, base_path_len); |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 794 | strbuf_add(tree_path, entry.path, entry.pathlen); |
| 795 | strbuf_addch(tree_path, '/'); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * If a sparse index is in use, the directory being processed may be |
| 800 | * sparse. To confirm that, we can check whether an entry with that |
| 801 | * exact name exists in the index. If it does, the created subtree |
| 802 | * should be sparse. Otherwise, cache tree expansion should continue |
| 803 | * as normal. |
| 804 | */ |
| 805 | if (r->index->sparse_index && |
| 806 | index_entry_exists(r->index, tree_path->buf, tree_path->len)) |
| 807 | prime_cache_tree_sparse_dir(sub->cache_tree, subtree); |
| 808 | else |
| 809 | prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path); |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 810 | cnt += sub->cache_tree->entry_count; |
| 811 | } |
| 812 | } |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 813 | |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 814 | it->entry_count = cnt; |
| 815 | } |
| 816 | |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 817 | void prime_cache_tree(struct repository *r, |
| 818 | struct index_state *istate, |
| 819 | struct tree *tree) |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 820 | { |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 821 | struct strbuf tree_path = STRBUF_INIT; |
| 822 | |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 823 | trace2_region_enter("cache-tree", "prime_cache_tree", r); |
Nguyễn Thái Ngọc Duy | e6c286e | 2014-06-13 19:19:33 +0700 | [diff] [blame] | 824 | cache_tree_free(&istate->cache_tree); |
| 825 | istate->cache_tree = cache_tree(); |
Derrick Stolee | 0e5c950 | 2021-01-04 03:09:14 +0000 | [diff] [blame] | 826 | |
Victoria Dye | 20ec2d0 | 2021-11-29 15:52:41 +0000 | [diff] [blame] | 827 | prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path); |
| 828 | strbuf_release(&tree_path); |
Nguyễn Thái Ngọc Duy | e6c286e | 2014-06-13 19:19:33 +0700 | [diff] [blame] | 829 | istate->cache_changed |= CACHE_TREE_CHANGED; |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 830 | trace2_region_leave("cache-tree", "prime_cache_tree", r); |
Junio C Hamano | b9d37a5 | 2009-04-20 03:58:18 -0700 | [diff] [blame] | 831 | } |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 832 | |
| 833 | /* |
| 834 | * find the cache_tree that corresponds to the current level without |
| 835 | * exploding the full path into textual form. The root of the |
| 836 | * cache tree is given as "root", and our current level is "info". |
| 837 | * (1) When at root level, info->prev is NULL, so it is "root" itself. |
| 838 | * (2) Otherwise, find the cache_tree that corresponds to one level |
| 839 | * above us, and find ourselves in there. |
| 840 | */ |
| 841 | static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root, |
| 842 | struct traverse_info *info) |
| 843 | { |
| 844 | struct cache_tree *our_parent; |
| 845 | |
| 846 | if (!info->prev) |
| 847 | return root; |
| 848 | our_parent = find_cache_tree_from_traversal(root, info->prev); |
Jeff King | 9055384 | 2019-07-31 00:38:15 -0400 | [diff] [blame] | 849 | return cache_tree_find(our_parent, info->name); |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | int cache_tree_matches_traversal(struct cache_tree *root, |
| 853 | struct name_entry *ent, |
| 854 | struct traverse_info *info) |
| 855 | { |
| 856 | struct cache_tree *it; |
| 857 | |
| 858 | it = find_cache_tree_from_traversal(root, info); |
| 859 | it = cache_tree_find(it, ent->path); |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 860 | if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid)) |
Junio C Hamano | b65982b | 2009-05-20 15:57:22 -0700 | [diff] [blame] | 861 | return it->entry_count; |
| 862 | return 0; |
| 863 | } |
Thomas Rast | 996277c | 2011-12-06 18:43:37 +0100 | [diff] [blame] | 864 | |
Jeff King | 5db8e59 | 2022-08-20 05:02:48 -0400 | [diff] [blame] | 865 | static void verify_one_sparse(struct index_state *istate, |
Derrick Stolee | 9ad2d5e | 2021-03-30 13:11:03 +0000 | [diff] [blame] | 866 | struct strbuf *path, |
| 867 | int pos) |
| 868 | { |
| 869 | struct cache_entry *ce = istate->cache[pos]; |
| 870 | |
| 871 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 872 | BUG("directory '%s' is present in index, but not sparse", |
| 873 | path->buf); |
| 874 | } |
| 875 | |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 876 | /* |
| 877 | * Returns: |
| 878 | * 0 - Verification completed. |
| 879 | * 1 - Restart verification - a call to ensure_full_index() freed the cache |
| 880 | * tree that is being verified and verification needs to be restarted from |
| 881 | * the new toplevel cache tree. |
| 882 | */ |
| 883 | static int verify_one(struct repository *r, |
| 884 | struct index_state *istate, |
| 885 | struct cache_tree *it, |
| 886 | struct strbuf *path) |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 887 | { |
| 888 | int i, pos, len = path->len; |
| 889 | struct strbuf tree_buf = STRBUF_INIT; |
| 890 | struct object_id new_oid; |
| 891 | |
| 892 | for (i = 0; i < it->subtree_nr; i++) { |
| 893 | strbuf_addf(path, "%s/", it->down[i]->name); |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 894 | if (verify_one(r, istate, it->down[i]->cache_tree, path)) |
| 895 | return 1; |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 896 | strbuf_setlen(path, len); |
| 897 | } |
| 898 | |
| 899 | if (it->entry_count < 0 || |
| 900 | /* no verification on tests (t7003) that replace trees */ |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 901 | lookup_replace_object(r, &it->oid) != &it->oid) |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 902 | return 0; |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 903 | |
| 904 | if (path->len) { |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 905 | /* |
| 906 | * If the index is sparse and the cache tree is not |
| 907 | * index_name_pos() may trigger ensure_full_index() which will |
| 908 | * free the tree that is being verified. |
| 909 | */ |
| 910 | int is_sparse = istate->sparse_index; |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 911 | pos = index_name_pos(istate, path->buf, path->len); |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 912 | if (is_sparse && !istate->sparse_index) |
| 913 | return 1; |
Derrick Stolee | 9ad2d5e | 2021-03-30 13:11:03 +0000 | [diff] [blame] | 914 | |
| 915 | if (pos >= 0) { |
Jeff King | 5db8e59 | 2022-08-20 05:02:48 -0400 | [diff] [blame] | 916 | verify_one_sparse(istate, path, pos); |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 917 | return 0; |
Derrick Stolee | 9ad2d5e | 2021-03-30 13:11:03 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 920 | pos = -pos - 1; |
| 921 | } else { |
| 922 | pos = 0; |
| 923 | } |
| 924 | |
| 925 | i = 0; |
| 926 | while (i < it->entry_count) { |
| 927 | struct cache_entry *ce = istate->cache[pos + i]; |
| 928 | const char *slash; |
| 929 | struct cache_tree_sub *sub = NULL; |
| 930 | const struct object_id *oid; |
| 931 | const char *name; |
| 932 | unsigned mode; |
| 933 | int entlen; |
| 934 | |
| 935 | if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) |
| 936 | BUG("%s with flags 0x%x should not be in cache-tree", |
| 937 | ce->name, ce->ce_flags); |
| 938 | name = ce->name + path->len; |
| 939 | slash = strchr(name, '/'); |
| 940 | if (slash) { |
| 941 | entlen = slash - name; |
| 942 | sub = find_subtree(it, ce->name + path->len, entlen, 0); |
| 943 | if (!sub || sub->cache_tree->entry_count < 0) |
| 944 | BUG("bad subtree '%.*s'", entlen, name); |
| 945 | oid = &sub->cache_tree->oid; |
| 946 | mode = S_IFDIR; |
| 947 | i += sub->cache_tree->entry_count; |
| 948 | } else { |
| 949 | oid = &ce->oid; |
| 950 | mode = ce->ce_mode; |
| 951 | entlen = ce_namelen(ce) - path->len; |
| 952 | i++; |
| 953 | } |
| 954 | strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); |
Matheus Tavares | a651946 | 2020-01-30 17:32:18 -0300 | [diff] [blame] | 955 | strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 956 | } |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 957 | hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE, |
Matheus Tavares | 2dcde20 | 2020-01-30 17:32:22 -0300 | [diff] [blame] | 958 | &new_oid); |
Jeff King | e43d2dc | 2018-10-02 17:19:21 -0400 | [diff] [blame] | 959 | if (!oideq(&new_oid, &it->oid)) |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 960 | BUG("cache-tree for path %.*s does not match. " |
| 961 | "Expected %s got %s", len, path->buf, |
| 962 | oid_to_hex(&new_oid), oid_to_hex(&it->oid)); |
| 963 | strbuf_setlen(path, len); |
| 964 | strbuf_release(&tree_buf); |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 965 | return 0; |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Nguyễn Thái Ngọc Duy | c207e9e | 2018-11-10 06:49:02 +0100 | [diff] [blame] | 968 | void cache_tree_verify(struct repository *r, struct index_state *istate) |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 969 | { |
| 970 | struct strbuf path = STRBUF_INIT; |
| 971 | |
| 972 | if (!istate->cache_tree) |
| 973 | return; |
Phillip Wood | f751097 | 2021-10-07 18:07:21 +0000 | [diff] [blame] | 974 | if (verify_one(r, istate, istate->cache_tree, &path)) { |
| 975 | strbuf_reset(&path); |
| 976 | if (verify_one(r, istate, istate->cache_tree, &path)) |
| 977 | BUG("ensure_full_index() called twice while verifying cache tree"); |
| 978 | } |
Nguyễn Thái Ngọc Duy | 4592e60 | 2018-08-18 16:41:28 +0200 | [diff] [blame] | 979 | strbuf_release(&path); |
| 980 | } |