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