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