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