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