Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "tree.h" |
| 3 | #include "cache-tree.h" |
| 4 | |
Junio C Hamano | 4903161 | 2006-10-30 15:29:53 -0800 | [diff] [blame] | 5 | #ifndef DEBUG |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 6 | #define DEBUG 0 |
Junio C Hamano | 4903161 | 2006-10-30 15:29:53 -0800 | [diff] [blame] | 7 | #endif |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 8 | |
| 9 | struct cache_tree *cache_tree(void) |
| 10 | { |
| 11 | struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree)); |
| 12 | it->entry_count = -1; |
| 13 | return it; |
| 14 | } |
| 15 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 16 | void cache_tree_free(struct cache_tree **it_p) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 17 | { |
| 18 | int i; |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 19 | struct cache_tree *it = *it_p; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 20 | |
| 21 | if (!it) |
| 22 | return; |
| 23 | for (i = 0; i < it->subtree_nr; i++) |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 24 | if (it->down[i]) |
| 25 | cache_tree_free(&it->down[i]->cache_tree); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 26 | free(it->down); |
| 27 | free(it); |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 28 | *it_p = NULL; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 31 | static int subtree_name_cmp(const char *one, int onelen, |
| 32 | const char *two, int twolen) |
| 33 | { |
| 34 | if (onelen < twolen) |
| 35 | return -1; |
| 36 | if (twolen < onelen) |
| 37 | return 1; |
| 38 | return memcmp(one, two, onelen); |
| 39 | } |
| 40 | |
| 41 | static int subtree_pos(struct cache_tree *it, const char *path, int pathlen) |
| 42 | { |
| 43 | struct cache_tree_sub **down = it->down; |
| 44 | int lo, hi; |
| 45 | lo = 0; |
| 46 | hi = it->subtree_nr; |
| 47 | while (lo < hi) { |
| 48 | int mi = (lo + hi) / 2; |
| 49 | struct cache_tree_sub *mdl = down[mi]; |
| 50 | int cmp = subtree_name_cmp(path, pathlen, |
| 51 | mdl->name, mdl->namelen); |
| 52 | if (!cmp) |
| 53 | return mi; |
| 54 | if (cmp < 0) |
| 55 | hi = mi; |
| 56 | else |
| 57 | lo = mi + 1; |
| 58 | } |
| 59 | return -lo-1; |
| 60 | } |
| 61 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 62 | static struct cache_tree_sub *find_subtree(struct cache_tree *it, |
| 63 | const char *path, |
| 64 | int pathlen, |
| 65 | int create) |
| 66 | { |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 67 | struct cache_tree_sub *down; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 68 | int pos = subtree_pos(it, path, pathlen); |
| 69 | if (0 <= pos) |
| 70 | return it->down[pos]; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 71 | if (!create) |
| 72 | return NULL; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 73 | |
| 74 | pos = -pos-1; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 75 | if (it->subtree_alloc <= it->subtree_nr) { |
| 76 | it->subtree_alloc = alloc_nr(it->subtree_alloc); |
| 77 | it->down = xrealloc(it->down, it->subtree_alloc * |
| 78 | sizeof(*it->down)); |
| 79 | } |
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 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 102 | void cache_tree_invalidate_path(struct cache_tree *it, const char *path) |
| 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) |
| 120 | return; |
| 121 | slash = strchr(path, '/'); |
| 122 | it->entry_count = -1; |
| 123 | if (!slash) { |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 124 | int pos; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 125 | namelen = strlen(path); |
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 | } |
| 141 | return; |
| 142 | } |
| 143 | namelen = slash - path; |
| 144 | down = find_subtree(it, path, namelen, 0); |
| 145 | if (down) |
| 146 | cache_tree_invalidate_path(down->cache_tree, slash + 1); |
| 147 | } |
| 148 | |
| 149 | static int verify_cache(struct cache_entry **cache, |
| 150 | int entries) |
| 151 | { |
| 152 | int i, funny; |
| 153 | |
| 154 | /* Verify that the tree is merged */ |
| 155 | funny = 0; |
| 156 | for (i = 0; i < entries; i++) { |
| 157 | struct cache_entry *ce = cache[i]; |
| 158 | if (ce_stage(ce)) { |
| 159 | if (10 < ++funny) { |
| 160 | fprintf(stderr, "...\n"); |
| 161 | break; |
| 162 | } |
| 163 | fprintf(stderr, "%s: unmerged (%s)\n", |
| 164 | ce->name, sha1_to_hex(ce->sha1)); |
| 165 | } |
| 166 | } |
| 167 | if (funny) |
| 168 | return -1; |
| 169 | |
| 170 | /* Also verify that the cache does not have path and path/file |
| 171 | * at the same time. At this point we know the cache has only |
| 172 | * stage 0 entries. |
| 173 | */ |
| 174 | funny = 0; |
| 175 | for (i = 0; i < entries - 1; i++) { |
| 176 | /* path/file always comes after path because of the way |
| 177 | * the cache is sorted. Also path can appear only once, |
| 178 | * which means conflicting one would immediately follow. |
| 179 | */ |
| 180 | const char *this_name = cache[i]->name; |
| 181 | const char *next_name = cache[i+1]->name; |
| 182 | int this_len = strlen(this_name); |
| 183 | if (this_len < strlen(next_name) && |
| 184 | strncmp(this_name, next_name, this_len) == 0 && |
| 185 | next_name[this_len] == '/') { |
| 186 | if (10 < ++funny) { |
| 187 | fprintf(stderr, "...\n"); |
| 188 | break; |
| 189 | } |
| 190 | fprintf(stderr, "You have both %s and %s\n", |
| 191 | this_name, next_name); |
| 192 | } |
| 193 | } |
| 194 | if (funny) |
| 195 | return -1; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void discard_unused_subtrees(struct cache_tree *it) |
| 200 | { |
| 201 | struct cache_tree_sub **down = it->down; |
| 202 | int nr = it->subtree_nr; |
| 203 | int dst, src; |
| 204 | for (dst = src = 0; src < nr; src++) { |
| 205 | struct cache_tree_sub *s = down[src]; |
| 206 | if (s->used) |
| 207 | down[dst++] = s; |
| 208 | else { |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 209 | cache_tree_free(&s->cache_tree); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 210 | free(s); |
| 211 | it->subtree_nr--; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 216 | int cache_tree_fully_valid(struct cache_tree *it) |
| 217 | { |
| 218 | int i; |
| 219 | if (!it) |
| 220 | return 0; |
| 221 | if (it->entry_count < 0 || !has_sha1_file(it->sha1)) |
| 222 | return 0; |
| 223 | for (i = 0; i < it->subtree_nr; i++) { |
| 224 | if (!cache_tree_fully_valid(it->down[i]->cache_tree)) |
| 225 | return 0; |
| 226 | } |
| 227 | return 1; |
| 228 | } |
| 229 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 230 | static int update_one(struct cache_tree *it, |
| 231 | struct cache_entry **cache, |
| 232 | int entries, |
| 233 | const char *base, |
| 234 | int baselen, |
Junio C Hamano | 2956dd3 | 2006-04-27 16:21:54 -0700 | [diff] [blame] | 235 | int missing_ok, |
| 236 | int dryrun) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 237 | { |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 238 | struct strbuf buffer; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 239 | int i; |
| 240 | |
Junio C Hamano | dd0c34c | 2006-04-24 15:12:42 -0700 | [diff] [blame] | 241 | if (0 <= it->entry_count && has_sha1_file(it->sha1)) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 242 | return it->entry_count; |
| 243 | |
| 244 | /* |
| 245 | * We first scan for subtrees and update them; we start by |
| 246 | * marking existing subtrees -- the ones that are unmarked |
| 247 | * should not be in the result. |
| 248 | */ |
| 249 | for (i = 0; i < it->subtree_nr; i++) |
| 250 | it->down[i]->used = 0; |
| 251 | |
| 252 | /* |
| 253 | * Find the subtrees and update them. |
| 254 | */ |
| 255 | for (i = 0; i < entries; i++) { |
| 256 | struct cache_entry *ce = cache[i]; |
| 257 | struct cache_tree_sub *sub; |
| 258 | const char *path, *slash; |
| 259 | int pathlen, sublen, subcnt; |
| 260 | |
| 261 | path = ce->name; |
| 262 | pathlen = ce_namelen(ce); |
| 263 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
| 264 | break; /* at the end of this level */ |
| 265 | |
| 266 | slash = strchr(path + baselen, '/'); |
| 267 | if (!slash) |
| 268 | continue; |
| 269 | /* |
| 270 | * a/bbb/c (base = a/, slash = /c) |
| 271 | * ==> |
| 272 | * path+baselen = bbb/c, sublen = 3 |
| 273 | */ |
| 274 | sublen = slash - (path + baselen); |
| 275 | sub = find_subtree(it, path + baselen, sublen, 1); |
| 276 | if (!sub->cache_tree) |
| 277 | sub->cache_tree = cache_tree(); |
| 278 | subcnt = update_one(sub->cache_tree, |
| 279 | cache + i, entries - i, |
| 280 | path, |
| 281 | baselen + sublen + 1, |
Junio C Hamano | 2956dd3 | 2006-04-27 16:21:54 -0700 | [diff] [blame] | 282 | missing_ok, |
| 283 | dryrun); |
Johannes Sixt | 3d12d0c | 2006-11-13 13:50:00 +0000 | [diff] [blame] | 284 | if (subcnt < 0) |
| 285 | return subcnt; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 286 | i += subcnt - 1; |
| 287 | sub->used = 1; |
| 288 | } |
| 289 | |
| 290 | discard_unused_subtrees(it); |
| 291 | |
| 292 | /* |
| 293 | * Then write out the tree object for this level. |
| 294 | */ |
Pierre Habouzit | f1696ee | 2007-09-10 12:35:04 +0200 | [diff] [blame] | 295 | strbuf_init(&buffer, 8192); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 296 | |
| 297 | for (i = 0; i < entries; i++) { |
| 298 | struct cache_entry *ce = cache[i]; |
| 299 | struct cache_tree_sub *sub; |
| 300 | const char *path, *slash; |
| 301 | int pathlen, entlen; |
| 302 | const unsigned char *sha1; |
| 303 | unsigned mode; |
| 304 | |
| 305 | path = ce->name; |
| 306 | pathlen = ce_namelen(ce); |
| 307 | if (pathlen <= baselen || memcmp(base, path, baselen)) |
| 308 | break; /* at the end of this level */ |
| 309 | |
| 310 | slash = strchr(path + baselen, '/'); |
| 311 | if (slash) { |
| 312 | entlen = slash - (path + baselen); |
| 313 | sub = find_subtree(it, path + baselen, entlen, 0); |
| 314 | if (!sub) |
| 315 | die("cache-tree.c: '%.*s' in '%s' not found", |
| 316 | entlen, path + baselen, path); |
| 317 | i += sub->cache_tree->entry_count - 1; |
| 318 | sha1 = sub->cache_tree->sha1; |
| 319 | mode = S_IFDIR; |
| 320 | } |
| 321 | else { |
| 322 | sha1 = ce->sha1; |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 323 | mode = ce->ce_mode; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 324 | entlen = pathlen - baselen; |
| 325 | } |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 326 | if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 327 | return error("invalid object %s", sha1_to_hex(sha1)); |
| 328 | |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 329 | if (ce->ce_flags & CE_REMOVE) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 330 | continue; /* entry being removed */ |
| 331 | |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 332 | strbuf_grow(&buffer, entlen + 100); |
| 333 | strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0'); |
| 334 | strbuf_add(&buffer, sha1, 20); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 335 | |
| 336 | #if DEBUG |
Junio C Hamano | 00703e6 | 2006-05-03 16:10:45 -0700 | [diff] [blame] | 337 | fprintf(stderr, "cache-tree update-one %o %.*s\n", |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 338 | mode, entlen, path + baselen); |
| 339 | #endif |
| 340 | } |
| 341 | |
Rene Scharfe | abdc3fc | 2006-10-14 12:45:36 +0200 | [diff] [blame] | 342 | if (dryrun) |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 343 | hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1); |
Junio C Hamano | edae5f0 | 2008-04-23 09:47:17 -0700 | [diff] [blame] | 344 | else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) { |
| 345 | strbuf_release(&buffer); |
| 346 | return -1; |
| 347 | } |
| 348 | |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 349 | strbuf_release(&buffer); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 350 | it->entry_count = i; |
| 351 | #if DEBUG |
Junio C Hamano | 00703e6 | 2006-05-03 16:10:45 -0700 | [diff] [blame] | 352 | 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] | 353 | it->entry_count, it->subtree_nr, |
| 354 | sha1_to_hex(it->sha1)); |
| 355 | #endif |
| 356 | return i; |
| 357 | } |
| 358 | |
| 359 | int cache_tree_update(struct cache_tree *it, |
| 360 | struct cache_entry **cache, |
| 361 | int entries, |
Junio C Hamano | 2956dd3 | 2006-04-27 16:21:54 -0700 | [diff] [blame] | 362 | int missing_ok, |
| 363 | int dryrun) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 364 | { |
| 365 | int i; |
| 366 | i = verify_cache(cache, entries); |
| 367 | if (i) |
| 368 | return i; |
Junio C Hamano | 2956dd3 | 2006-04-27 16:21:54 -0700 | [diff] [blame] | 369 | i = update_one(it, cache, entries, "", 0, missing_ok, dryrun); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 370 | if (i < 0) |
| 371 | return i; |
| 372 | return 0; |
| 373 | } |
| 374 | |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 375 | static void write_one(struct strbuf *buffer, struct cache_tree *it, |
| 376 | const char *path, int pathlen) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 377 | { |
| 378 | int i; |
| 379 | |
| 380 | /* One "cache-tree" entry consists of the following: |
| 381 | * path (NUL terminated) |
| 382 | * entry_count, subtree_nr ("%d %d\n") |
| 383 | * tree-sha1 (missing if invalid) |
| 384 | * subtree_nr "cache-tree" entries for subtrees. |
| 385 | */ |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 386 | strbuf_grow(buffer, pathlen + 100); |
| 387 | strbuf_add(buffer, path, pathlen); |
| 388 | 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] | 389 | |
| 390 | #if DEBUG |
| 391 | if (0 <= it->entry_count) |
| 392 | fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n", |
| 393 | pathlen, path, it->entry_count, it->subtree_nr, |
| 394 | sha1_to_hex(it->sha1)); |
| 395 | else |
| 396 | fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n", |
| 397 | pathlen, path, it->subtree_nr); |
| 398 | #endif |
| 399 | |
| 400 | if (0 <= it->entry_count) { |
Pierre Habouzit | 5242bcb | 2007-09-06 13:20:11 +0200 | [diff] [blame] | 401 | strbuf_add(buffer, it->sha1, 20); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 402 | } |
| 403 | for (i = 0; i < it->subtree_nr; i++) { |
| 404 | struct cache_tree_sub *down = it->down[i]; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 405 | if (i) { |
| 406 | struct cache_tree_sub *prev = it->down[i-1]; |
| 407 | if (subtree_name_cmp(down->name, down->namelen, |
| 408 | prev->name, prev->namelen) <= 0) |
| 409 | die("fatal - unsorted cache subtree"); |
| 410 | } |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 411 | write_one(buffer, down->cache_tree, down->name, down->namelen); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 412 | } |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 415 | void cache_tree_write(struct strbuf *sb, struct cache_tree *root) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 416 | { |
Pierre Habouzit | 1dffb8f | 2007-09-25 10:22:44 +0200 | [diff] [blame] | 417 | write_one(sb, root, "", 0); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static struct cache_tree *read_one(const char **buffer, unsigned long *size_p) |
| 421 | { |
| 422 | const char *buf = *buffer; |
| 423 | unsigned long size = *size_p; |
Johannes Schindelin | 0111ea3 | 2006-05-02 03:31:02 +0200 | [diff] [blame] | 424 | const char *cp; |
| 425 | char *ep; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 426 | struct cache_tree *it; |
| 427 | int i, subtree_nr; |
| 428 | |
| 429 | it = NULL; |
| 430 | /* skip name, but make sure name exists */ |
| 431 | while (size && *buf) { |
| 432 | size--; |
| 433 | buf++; |
| 434 | } |
| 435 | if (!size) |
| 436 | goto free_return; |
| 437 | buf++; size--; |
| 438 | it = cache_tree(); |
Johannes Schindelin | 0111ea3 | 2006-05-02 03:31:02 +0200 | [diff] [blame] | 439 | |
| 440 | cp = buf; |
| 441 | it->entry_count = strtol(cp, &ep, 10); |
| 442 | if (cp == ep) |
| 443 | goto free_return; |
| 444 | cp = ep; |
| 445 | subtree_nr = strtol(cp, &ep, 10); |
| 446 | if (cp == ep) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 447 | goto free_return; |
| 448 | while (size && *buf && *buf != '\n') { |
| 449 | size--; |
| 450 | buf++; |
| 451 | } |
| 452 | if (!size) |
| 453 | goto free_return; |
| 454 | buf++; size--; |
| 455 | if (0 <= it->entry_count) { |
| 456 | if (size < 20) |
| 457 | goto free_return; |
Junio C Hamano | 63daae4 | 2007-06-22 23:19:43 -0700 | [diff] [blame] | 458 | hashcpy(it->sha1, (const unsigned char*)buf); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 459 | buf += 20; |
| 460 | size -= 20; |
| 461 | } |
| 462 | |
| 463 | #if DEBUG |
| 464 | if (0 <= it->entry_count) |
| 465 | fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n", |
| 466 | *buffer, it->entry_count, subtree_nr, |
| 467 | sha1_to_hex(it->sha1)); |
| 468 | else |
| 469 | fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n", |
| 470 | *buffer, subtree_nr); |
| 471 | #endif |
| 472 | |
| 473 | /* |
| 474 | * Just a heuristic -- we do not add directories that often but |
| 475 | * we do not want to have to extend it immediately when we do, |
| 476 | * hence +2. |
| 477 | */ |
| 478 | it->subtree_alloc = subtree_nr + 2; |
| 479 | it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *)); |
| 480 | for (i = 0; i < subtree_nr; i++) { |
| 481 | /* read each subtree */ |
| 482 | struct cache_tree *sub; |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 483 | struct cache_tree_sub *subtree; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 484 | const char *name = buf; |
Junio C Hamano | 7927a55 | 2006-04-27 01:33:07 -0700 | [diff] [blame] | 485 | |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 486 | sub = read_one(&buf, &size); |
| 487 | if (!sub) |
| 488 | goto free_return; |
Junio C Hamano | 7927a55 | 2006-04-27 01:33:07 -0700 | [diff] [blame] | 489 | subtree = cache_tree_sub(it, name); |
Junio C Hamano | 61fa309 | 2006-04-25 17:40:02 -0700 | [diff] [blame] | 490 | subtree->cache_tree = sub; |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 491 | } |
| 492 | if (subtree_nr != it->subtree_nr) |
| 493 | die("cache-tree: internal error"); |
| 494 | *buffer = buf; |
| 495 | *size_p = size; |
| 496 | return it; |
| 497 | |
| 498 | free_return: |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 499 | cache_tree_free(&it); |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 503 | 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] | 504 | { |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 505 | if (buffer[0]) |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 506 | return NULL; /* not the whole tree */ |
Junio C Hamano | 7498646 | 2006-04-23 16:52:20 -0700 | [diff] [blame] | 507 | return read_one(&buffer, &size); |
| 508 | } |
Junio C Hamano | 6bd2035 | 2006-04-26 01:20:50 -0700 | [diff] [blame] | 509 | |
Nanako Shiraishi | 7ba04d9 | 2008-07-16 19:42:10 +0900 | [diff] [blame] | 510 | 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] | 511 | { |
| 512 | while (*path) { |
| 513 | const char *slash; |
| 514 | struct cache_tree_sub *sub; |
| 515 | |
| 516 | slash = strchr(path, '/'); |
| 517 | if (!slash) |
| 518 | slash = path + strlen(path); |
| 519 | /* between path and slash is the name of the |
| 520 | * subtree to look for. |
| 521 | */ |
| 522 | sub = find_subtree(it, path, slash - path, 0); |
| 523 | if (!sub) |
| 524 | return NULL; |
| 525 | it = sub->cache_tree; |
| 526 | if (slash) |
| 527 | while (*slash && *slash == '/') |
| 528 | slash++; |
| 529 | if (!slash || !*slash) |
| 530 | return it; /* prefix ended with slashes */ |
| 531 | path = slash; |
| 532 | } |
| 533 | return it; |
| 534 | } |
Junio C Hamano | 45525bd | 2008-01-10 22:49:35 -0800 | [diff] [blame] | 535 | |
| 536 | int write_cache_as_tree(unsigned char *sha1, int missing_ok, const char *prefix) |
| 537 | { |
| 538 | int entries, was_valid, newfd; |
| 539 | |
| 540 | /* |
| 541 | * We can't free this memory, it becomes part of a linked list |
| 542 | * parsed atexit() |
| 543 | */ |
| 544 | struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); |
| 545 | |
| 546 | newfd = hold_locked_index(lock_file, 1); |
| 547 | |
| 548 | entries = read_cache(); |
| 549 | if (entries < 0) |
| 550 | return WRITE_TREE_UNREADABLE_INDEX; |
| 551 | |
| 552 | if (!active_cache_tree) |
| 553 | active_cache_tree = cache_tree(); |
| 554 | |
| 555 | was_valid = cache_tree_fully_valid(active_cache_tree); |
| 556 | |
| 557 | if (!was_valid) { |
| 558 | if (cache_tree_update(active_cache_tree, |
| 559 | active_cache, active_nr, |
| 560 | missing_ok, 0) < 0) |
| 561 | return WRITE_TREE_UNMERGED_INDEX; |
| 562 | if (0 <= newfd) { |
| 563 | if (!write_cache(newfd, active_cache, active_nr) && |
| 564 | !commit_lock_file(lock_file)) |
| 565 | newfd = -1; |
| 566 | } |
| 567 | /* Not being able to write is fine -- we are only interested |
| 568 | * in updating the cache-tree part, and if the next caller |
| 569 | * ends up using the old index with unupdated cache-tree part |
| 570 | * it misses the work we did here, but that is just a |
| 571 | * performance penalty and not a big deal. |
| 572 | */ |
| 573 | } |
| 574 | |
| 575 | if (prefix) { |
| 576 | struct cache_tree *subtree = |
| 577 | cache_tree_find(active_cache_tree, prefix); |
| 578 | if (!subtree) |
| 579 | return WRITE_TREE_PREFIX_ERROR; |
| 580 | hashcpy(sha1, subtree->sha1); |
| 581 | } |
| 582 | else |
| 583 | hashcpy(sha1, active_cache_tree->sha1); |
| 584 | |
| 585 | if (0 <= newfd) |
| 586 | rollback_lock_file(lock_file); |
| 587 | |
| 588 | return 0; |
| 589 | } |