THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

724 lines
17 KiB

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