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.

2150 lines
54KB

  1. #include "cache.h"
  2. #include "list.h"
  3. #include "pack.h"
  4. #include "repository.h"
  5. #include "dir.h"
  6. #include "mergesort.h"
  7. #include "packfile.h"
  8. #include "delta.h"
  9. #include "streaming.h"
  10. #include "sha1-lookup.h"
  11. #include "commit.h"
  12. #include "object.h"
  13. #include "tag.h"
  14. #include "tree-walk.h"
  15. #include "tree.h"
  16. #include "object-store.h"
  17. #include "midx.h"
  18. #include "commit-graph.h"
  19. #include "promisor-remote.h"
  20. char *odb_pack_name(struct strbuf *buf,
  21. const unsigned char *hash,
  22. const char *ext)
  23. {
  24. strbuf_reset(buf);
  25. strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
  26. hash_to_hex(hash), ext);
  27. return buf->buf;
  28. }
  29. char *sha1_pack_name(const unsigned char *sha1)
  30. {
  31. static struct strbuf buf = STRBUF_INIT;
  32. return odb_pack_name(&buf, sha1, "pack");
  33. }
  34. char *sha1_pack_index_name(const unsigned char *sha1)
  35. {
  36. static struct strbuf buf = STRBUF_INIT;
  37. return odb_pack_name(&buf, sha1, "idx");
  38. }
  39. static unsigned int pack_used_ctr;
  40. static unsigned int pack_mmap_calls;
  41. static unsigned int peak_pack_open_windows;
  42. static unsigned int pack_open_windows;
  43. static unsigned int pack_open_fds;
  44. static unsigned int pack_max_fds;
  45. static size_t peak_pack_mapped;
  46. static size_t pack_mapped;
  47. #define SZ_FMT PRIuMAX
  48. static inline uintmax_t sz_fmt(size_t s) { return s; }
  49. void pack_report(void)
  50. {
  51. fprintf(stderr,
  52. "pack_report: getpagesize() = %10" SZ_FMT "\n"
  53. "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
  54. "pack_report: core.packedGitLimit = %10" SZ_FMT "\n",
  55. sz_fmt(getpagesize()),
  56. sz_fmt(packed_git_window_size),
  57. sz_fmt(packed_git_limit));
  58. fprintf(stderr,
  59. "pack_report: pack_used_ctr = %10u\n"
  60. "pack_report: pack_mmap_calls = %10u\n"
  61. "pack_report: pack_open_windows = %10u / %10u\n"
  62. "pack_report: pack_mapped = "
  63. "%10" SZ_FMT " / %10" SZ_FMT "\n",
  64. pack_used_ctr,
  65. pack_mmap_calls,
  66. pack_open_windows, peak_pack_open_windows,
  67. sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
  68. }
  69. /*
  70. * Open and mmap the index file at path, perform a couple of
  71. * consistency checks, then record its information to p. Return 0 on
  72. * success.
  73. */
  74. static int check_packed_git_idx(const char *path, struct packed_git *p)
  75. {
  76. void *idx_map;
  77. size_t idx_size;
  78. int fd = git_open(path), ret;
  79. struct stat st;
  80. const unsigned int hashsz = the_hash_algo->rawsz;
  81. if (fd < 0)
  82. return -1;
  83. if (fstat(fd, &st)) {
  84. close(fd);
  85. return -1;
  86. }
  87. idx_size = xsize_t(st.st_size);
  88. if (idx_size < 4 * 256 + hashsz + hashsz) {
  89. close(fd);
  90. return error("index file %s is too small", path);
  91. }
  92. idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
  93. close(fd);
  94. ret = load_idx(path, hashsz, idx_map, idx_size, p);
  95. if (ret)
  96. munmap(idx_map, idx_size);
  97. return ret;
  98. }
  99. int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
  100. size_t idx_size, struct packed_git *p)
  101. {
  102. struct pack_idx_header *hdr = idx_map;
  103. uint32_t version, nr, i, *index;
  104. if (idx_size < 4 * 256 + hashsz + hashsz)
  105. return error("index file %s is too small", path);
  106. if (idx_map == NULL)
  107. return error("empty data");
  108. if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
  109. version = ntohl(hdr->idx_version);
  110. if (version < 2 || version > 2)
  111. return error("index file %s is version %"PRIu32
  112. " and is not supported by this binary"
  113. " (try upgrading GIT to a newer version)",
  114. path, version);
  115. } else
  116. version = 1;
  117. nr = 0;
  118. index = idx_map;
  119. if (version > 1)
  120. index += 2; /* skip index header */
  121. for (i = 0; i < 256; i++) {
  122. uint32_t n = ntohl(index[i]);
  123. if (n < nr)
  124. return error("non-monotonic index %s", path);
  125. nr = n;
  126. }
  127. if (version == 1) {
  128. /*
  129. * Total size:
  130. * - 256 index entries 4 bytes each
  131. * - 24-byte entries * nr (object ID + 4-byte offset)
  132. * - hash of the packfile
  133. * - file checksum
  134. */
  135. if (idx_size != 4 * 256 + nr * (hashsz + 4) + hashsz + hashsz)
  136. return error("wrong index v1 file size in %s", path);
  137. } else if (version == 2) {
  138. /*
  139. * Minimum size:
  140. * - 8 bytes of header
  141. * - 256 index entries 4 bytes each
  142. * - object ID entry * nr
  143. * - 4-byte crc entry * nr
  144. * - 4-byte offset entry * nr
  145. * - hash of the packfile
  146. * - file checksum
  147. * And after the 4-byte offset table might be a
  148. * variable sized table containing 8-byte entries
  149. * for offsets larger than 2^31.
  150. */
  151. unsigned long min_size = 8 + 4*256 + nr*(hashsz + 4 + 4) + hashsz + hashsz;
  152. unsigned long max_size = min_size;
  153. if (nr)
  154. max_size += (nr - 1)*8;
  155. if (idx_size < min_size || idx_size > max_size)
  156. return error("wrong index v2 file size in %s", path);
  157. if (idx_size != min_size &&
  158. /*
  159. * make sure we can deal with large pack offsets.
  160. * 31-bit signed offset won't be enough, neither
  161. * 32-bit unsigned one will be.
  162. */
  163. (sizeof(off_t) <= 4))
  164. return error("pack too large for current definition of off_t in %s", path);
  165. }
  166. p->index_version = version;
  167. p->index_data = idx_map;
  168. p->index_size = idx_size;
  169. p->num_objects = nr;
  170. return 0;
  171. }
  172. int open_pack_index(struct packed_git *p)
  173. {
  174. char *idx_name;
  175. size_t len;
  176. int ret;
  177. if (p->index_data)
  178. return 0;
  179. if (!strip_suffix(p->pack_name, ".pack", &len))
  180. BUG("pack_name does not end in .pack");
  181. idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
  182. ret = check_packed_git_idx(idx_name, p);
  183. free(idx_name);
  184. return ret;
  185. }
  186. uint32_t get_pack_fanout(struct packed_git *p, uint32_t value)
  187. {
  188. const uint32_t *level1_ofs = p->index_data;
  189. if (!level1_ofs) {
  190. if (open_pack_index(p))
  191. return 0;
  192. level1_ofs = p->index_data;
  193. }
  194. if (p->index_version > 1) {
  195. level1_ofs += 2;
  196. }
  197. return ntohl(level1_ofs[value]);
  198. }
  199. static struct packed_git *alloc_packed_git(int extra)
  200. {
  201. struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
  202. memset(p, 0, sizeof(*p));
  203. p->pack_fd = -1;
  204. return p;
  205. }
  206. struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
  207. {
  208. const char *path = sha1_pack_name(sha1);
  209. size_t alloc = st_add(strlen(path), 1);
  210. struct packed_git *p = alloc_packed_git(alloc);
  211. memcpy(p->pack_name, path, alloc); /* includes NUL */
  212. hashcpy(p->hash, sha1);
  213. if (check_packed_git_idx(idx_path, p)) {
  214. free(p);
  215. return NULL;
  216. }
  217. return p;
  218. }
  219. static void scan_windows(struct packed_git *p,
  220. struct packed_git **lru_p,
  221. struct pack_window **lru_w,
  222. struct pack_window **lru_l)
  223. {
  224. struct pack_window *w, *w_l;
  225. for (w_l = NULL, w = p->windows; w; w = w->next) {
  226. if (!w->inuse_cnt) {
  227. if (!*lru_w || w->last_used < (*lru_w)->last_used) {
  228. *lru_p = p;
  229. *lru_w = w;
  230. *lru_l = w_l;
  231. }
  232. }
  233. w_l = w;
  234. }
  235. }
  236. static int unuse_one_window(struct packed_git *current)
  237. {
  238. struct packed_git *p, *lru_p = NULL;
  239. struct pack_window *lru_w = NULL, *lru_l = NULL;
  240. if (current)
  241. scan_windows(current, &lru_p, &lru_w, &lru_l);
  242. for (p = the_repository->objects->packed_git; p; p = p->next)
  243. scan_windows(p, &lru_p, &lru_w, &lru_l);
  244. if (lru_p) {
  245. munmap(lru_w->base, lru_w->len);
  246. pack_mapped -= lru_w->len;
  247. if (lru_l)
  248. lru_l->next = lru_w->next;
  249. else
  250. lru_p->windows = lru_w->next;
  251. free(lru_w);
  252. pack_open_windows--;
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. void close_pack_windows(struct packed_git *p)
  258. {
  259. while (p->windows) {
  260. struct pack_window *w = p->windows;
  261. if (w->inuse_cnt)
  262. die("pack '%s' still has open windows to it",
  263. p->pack_name);
  264. munmap(w->base, w->len);
  265. pack_mapped -= w->len;
  266. pack_open_windows--;
  267. p->windows = w->next;
  268. free(w);
  269. }
  270. }
  271. int close_pack_fd(struct packed_git *p)
  272. {
  273. if (p->pack_fd < 0)
  274. return 0;
  275. close(p->pack_fd);
  276. pack_open_fds--;
  277. p->pack_fd = -1;
  278. return 1;
  279. }
  280. void close_pack_index(struct packed_git *p)
  281. {
  282. if (p->index_data) {
  283. munmap((void *)p->index_data, p->index_size);
  284. p->index_data = NULL;
  285. }
  286. }
  287. void close_pack(struct packed_git *p)
  288. {
  289. close_pack_windows(p);
  290. close_pack_fd(p);
  291. close_pack_index(p);
  292. }
  293. void close_object_store(struct raw_object_store *o)
  294. {
  295. struct packed_git *p;
  296. for (p = o->packed_git; p; p = p->next)
  297. if (p->do_not_close)
  298. BUG("want to close pack marked 'do-not-close'");
  299. else
  300. close_pack(p);
  301. if (o->multi_pack_index) {
  302. close_midx(o->multi_pack_index);
  303. o->multi_pack_index = NULL;
  304. }
  305. close_commit_graph(o);
  306. }
  307. void unlink_pack_path(const char *pack_name, int force_delete)
  308. {
  309. static const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
  310. int i;
  311. struct strbuf buf = STRBUF_INIT;
  312. size_t plen;
  313. strbuf_addstr(&buf, pack_name);
  314. strip_suffix_mem(buf.buf, &buf.len, ".pack");
  315. plen = buf.len;
  316. if (!force_delete) {
  317. strbuf_addstr(&buf, ".keep");
  318. if (!access(buf.buf, F_OK)) {
  319. strbuf_release(&buf);
  320. return;
  321. }
  322. }
  323. for (i = 0; i < ARRAY_SIZE(exts); i++) {
  324. strbuf_setlen(&buf, plen);
  325. strbuf_addstr(&buf, exts[i]);
  326. unlink(buf.buf);
  327. }
  328. strbuf_release(&buf);
  329. }
  330. /*
  331. * The LRU pack is the one with the oldest MRU window, preferring packs
  332. * with no used windows, or the oldest mtime if it has no windows allocated.
  333. */
  334. static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse)
  335. {
  336. struct pack_window *w, *this_mru_w;
  337. int has_windows_inuse = 0;
  338. /*
  339. * Reject this pack if it has windows and the previously selected
  340. * one does not. If this pack does not have windows, reject
  341. * it if the pack file is newer than the previously selected one.
  342. */
  343. if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime))
  344. return;
  345. for (w = this_mru_w = p->windows; w; w = w->next) {
  346. /*
  347. * Reject this pack if any of its windows are in use,
  348. * but the previously selected pack did not have any
  349. * inuse windows. Otherwise, record that this pack
  350. * has windows in use.
  351. */
  352. if (w->inuse_cnt) {
  353. if (*accept_windows_inuse)
  354. has_windows_inuse = 1;
  355. else
  356. return;
  357. }
  358. if (w->last_used > this_mru_w->last_used)
  359. this_mru_w = w;
  360. /*
  361. * Reject this pack if it has windows that have been
  362. * used more recently than the previously selected pack.
  363. * If the previously selected pack had windows inuse and
  364. * we have not encountered a window in this pack that is
  365. * inuse, skip this check since we prefer a pack with no
  366. * inuse windows to one that has inuse windows.
  367. */
  368. if (*mru_w && *accept_windows_inuse == has_windows_inuse &&
  369. this_mru_w->last_used > (*mru_w)->last_used)
  370. return;
  371. }
  372. /*
  373. * Select this pack.
  374. */
  375. *mru_w = this_mru_w;
  376. *lru_p = p;
  377. *accept_windows_inuse = has_windows_inuse;
  378. }
  379. static int close_one_pack(void)
  380. {
  381. struct packed_git *p, *lru_p = NULL;
  382. struct pack_window *mru_w = NULL;
  383. int accept_windows_inuse = 1;
  384. for (p = the_repository->objects->packed_git; p; p = p->next) {
  385. if (p->pack_fd == -1)
  386. continue;
  387. find_lru_pack(p, &lru_p, &mru_w, &accept_windows_inuse);
  388. }
  389. if (lru_p)
  390. return close_pack_fd(lru_p);
  391. return 0;
  392. }
  393. static unsigned int get_max_fd_limit(void)
  394. {
  395. #ifdef RLIMIT_NOFILE
  396. {
  397. struct rlimit lim;
  398. if (!getrlimit(RLIMIT_NOFILE, &lim))
  399. return lim.rlim_cur;
  400. }
  401. #endif
  402. #ifdef _SC_OPEN_MAX
  403. {
  404. long open_max = sysconf(_SC_OPEN_MAX);
  405. if (0 < open_max)
  406. return open_max;
  407. /*
  408. * Otherwise, we got -1 for one of the two
  409. * reasons:
  410. *
  411. * (1) sysconf() did not understand _SC_OPEN_MAX
  412. * and signaled an error with -1; or
  413. * (2) sysconf() said there is no limit.
  414. *
  415. * We _could_ clear errno before calling sysconf() to
  416. * tell these two cases apart and return a huge number
  417. * in the latter case to let the caller cap it to a
  418. * value that is not so selfish, but letting the
  419. * fallback OPEN_MAX codepath take care of these cases
  420. * is a lot simpler.
  421. */
  422. }
  423. #endif
  424. #ifdef OPEN_MAX
  425. return OPEN_MAX;
  426. #else
  427. return 1; /* see the caller ;-) */
  428. #endif
  429. }
  430. const char *pack_basename(struct packed_git *p)
  431. {
  432. const char *ret = strrchr(p->pack_name, '/');
  433. if (ret)
  434. ret = ret + 1; /* skip past slash */
  435. else
  436. ret = p->pack_name; /* we only have a base */
  437. return ret;
  438. }
  439. /*
  440. * Do not call this directly as this leaks p->pack_fd on error return;
  441. * call open_packed_git() instead.
  442. */
  443. static int open_packed_git_1(struct packed_git *p)
  444. {
  445. struct stat st;
  446. struct pack_header hdr;
  447. unsigned char hash[GIT_MAX_RAWSZ];
  448. unsigned char *idx_hash;
  449. long fd_flag;
  450. ssize_t read_result;
  451. const unsigned hashsz = the_hash_algo->rawsz;
  452. if (!p->index_data) {
  453. struct multi_pack_index *m;
  454. const char *pack_name = pack_basename(p);
  455. for (m = the_repository->objects->multi_pack_index;
  456. m; m = m->next) {
  457. if (midx_contains_pack(m, pack_name))
  458. break;
  459. }
  460. if (!m && open_pack_index(p))
  461. return error("packfile %s index unavailable", p->pack_name);
  462. }
  463. if (!pack_max_fds) {
  464. unsigned int max_fds = get_max_fd_limit();
  465. /* Save 3 for stdin/stdout/stderr, 22 for work */
  466. if (25 < max_fds)
  467. pack_max_fds = max_fds - 25;
  468. else
  469. pack_max_fds = 1;
  470. }
  471. while (pack_max_fds <= pack_open_fds && close_one_pack())
  472. ; /* nothing */
  473. p->pack_fd = git_open(p->pack_name);
  474. if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
  475. return -1;
  476. pack_open_fds++;
  477. /* If we created the struct before we had the pack we lack size. */
  478. if (!p->pack_size) {
  479. if (!S_ISREG(st.st_mode))
  480. return error("packfile %s not a regular file", p->pack_name);
  481. p->pack_size = st.st_size;
  482. } else if (p->pack_size != st.st_size)
  483. return error("packfile %s size changed", p->pack_name);
  484. /* We leave these file descriptors open with sliding mmap;
  485. * there is no point keeping them open across exec(), though.
  486. */
  487. fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
  488. if (fd_flag < 0)
  489. return error("cannot determine file descriptor flags");
  490. fd_flag |= FD_CLOEXEC;
  491. if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
  492. return error("cannot set FD_CLOEXEC");
  493. /* Verify we recognize this pack file format. */
  494. read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr));
  495. if (read_result < 0)
  496. return error_errno("error reading from %s", p->pack_name);
  497. if (read_result != sizeof(hdr))
  498. return error("file %s is far too short to be a packfile", p->pack_name);
  499. if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
  500. return error("file %s is not a GIT packfile", p->pack_name);
  501. if (!pack_version_ok(hdr.hdr_version))
  502. return error("packfile %s is version %"PRIu32" and not"
  503. " supported (try upgrading GIT to a newer version)",
  504. p->pack_name, ntohl(hdr.hdr_version));
  505. /* Skip index checking if in multi-pack-index */
  506. if (!p->index_data)
  507. return 0;
  508. /* Verify the pack matches its index. */
  509. if (p->num_objects != ntohl(hdr.hdr_entries))
  510. return error("packfile %s claims to have %"PRIu32" objects"
  511. " while index indicates %"PRIu32" objects",
  512. p->pack_name, ntohl(hdr.hdr_entries),
  513. p->num_objects);
  514. if (lseek(p->pack_fd, p->pack_size - hashsz, SEEK_SET) == -1)
  515. return error("end of packfile %s is unavailable", p->pack_name);
  516. read_result = read_in_full(p->pack_fd, hash, hashsz);
  517. if (read_result < 0)
  518. return error_errno("error reading from %s", p->pack_name);
  519. if (read_result != hashsz)
  520. return error("packfile %s signature is unavailable", p->pack_name);
  521. idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
  522. if (!hasheq(hash, idx_hash))
  523. return error("packfile %s does not match index", p->pack_name);
  524. return 0;
  525. }
  526. static int open_packed_git(struct packed_git *p)
  527. {
  528. if (!open_packed_git_1(p))
  529. return 0;
  530. close_pack_fd(p);
  531. return -1;
  532. }
  533. static int in_window(struct pack_window *win, off_t offset)
  534. {
  535. /* We must promise at least one full hash after the
  536. * offset is available from this window, otherwise the offset
  537. * is not actually in this window and a different window (which
  538. * has that one hash excess) must be used. This is to support
  539. * the object header and delta base parsing routines below.
  540. */
  541. off_t win_off = win->offset;
  542. return win_off <= offset
  543. && (offset + the_hash_algo->rawsz) <= (win_off + win->len);
  544. }
  545. unsigned char *use_pack(struct packed_git *p,
  546. struct pack_window **w_cursor,
  547. off_t offset,
  548. unsigned long *left)
  549. {
  550. struct pack_window *win = *w_cursor;
  551. /* Since packfiles end in a hash of their content and it's
  552. * pointless to ask for an offset into the middle of that
  553. * hash, and the in_window function above wouldn't match
  554. * don't allow an offset too close to the end of the file.
  555. */
  556. if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
  557. die("packfile %s cannot be accessed", p->pack_name);
  558. if (offset > (p->pack_size - the_hash_algo->rawsz))
  559. die("offset beyond end of packfile (truncated pack?)");
  560. if (offset < 0)
  561. die(_("offset before end of packfile (broken .idx?)"));
  562. if (!win || !in_window(win, offset)) {
  563. if (win)
  564. win->inuse_cnt--;
  565. for (win = p->windows; win; win = win->next) {
  566. if (in_window(win, offset))
  567. break;
  568. }
  569. if (!win) {
  570. size_t window_align = packed_git_window_size / 2;
  571. off_t len;
  572. if (p->pack_fd == -1 && open_packed_git(p))
  573. die("packfile %s cannot be accessed", p->pack_name);
  574. win = xcalloc(1, sizeof(*win));
  575. win->offset = (offset / window_align) * window_align;
  576. len = p->pack_size - win->offset;
  577. if (len > packed_git_window_size)
  578. len = packed_git_window_size;
  579. win->len = (size_t)len;
  580. pack_mapped += win->len;
  581. while (packed_git_limit < pack_mapped
  582. && unuse_one_window(p))
  583. ; /* nothing */
  584. win->base = xmmap_gently(NULL, win->len,
  585. PROT_READ, MAP_PRIVATE,
  586. p->pack_fd, win->offset);
  587. if (win->base == MAP_FAILED)
  588. die_errno("packfile %s cannot be mapped",
  589. p->pack_name);
  590. if (!win->offset && win->len == p->pack_size
  591. && !p->do_not_close)
  592. close_pack_fd(p);
  593. pack_mmap_calls++;
  594. pack_open_windows++;
  595. if (pack_mapped > peak_pack_mapped)
  596. peak_pack_mapped = pack_mapped;
  597. if (pack_open_windows > peak_pack_open_windows)
  598. peak_pack_open_windows = pack_open_windows;
  599. win->next = p->windows;
  600. p->windows = win;
  601. }
  602. }
  603. if (win != *w_cursor) {
  604. win->last_used = pack_used_ctr++;
  605. win->inuse_cnt++;
  606. *w_cursor = win;
  607. }
  608. offset -= win->offset;
  609. if (left)
  610. *left = win->len - xsize_t(offset);
  611. return win->base + offset;
  612. }
  613. void unuse_pack(struct pack_window **w_cursor)
  614. {
  615. struct pack_window *w = *w_cursor;
  616. if (w) {
  617. w->inuse_cnt--;
  618. *w_cursor = NULL;
  619. }
  620. }
  621. struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
  622. {
  623. struct stat st;
  624. size_t alloc;
  625. struct packed_git *p;
  626. /*
  627. * Make sure a corresponding .pack file exists and that
  628. * the index looks sane.
  629. */
  630. if (!strip_suffix_mem(path, &path_len, ".idx"))
  631. return NULL;
  632. /*
  633. * ".promisor" is long enough to hold any suffix we're adding (and
  634. * the use xsnprintf double-checks that)
  635. */
  636. alloc = st_add3(path_len, strlen(".promisor"), 1);
  637. p = alloc_packed_git(alloc);
  638. memcpy(p->pack_name, path, path_len);
  639. xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
  640. if (!access(p->pack_name, F_OK))
  641. p->pack_keep = 1;
  642. xsnprintf(p->pack_name + path_len, alloc - path_len, ".promisor");
  643. if (!access(p->pack_name, F_OK))
  644. p->pack_promisor = 1;
  645. xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack");
  646. if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) {
  647. free(p);
  648. return NULL;
  649. }
  650. /* ok, it looks sane as far as we can check without
  651. * actually mapping the pack file.
  652. */
  653. p->pack_size = st.st_size;
  654. p->pack_local = local;
  655. p->mtime = st.st_mtime;
  656. if (path_len < the_hash_algo->hexsz ||
  657. get_sha1_hex(path + path_len - the_hash_algo->hexsz, p->hash))
  658. hashclr(p->hash);
  659. return p;
  660. }
  661. void install_packed_git(struct repository *r, struct packed_git *pack)
  662. {
  663. if (pack->pack_fd != -1)
  664. pack_open_fds++;
  665. pack->next = r->objects->packed_git;
  666. r->objects->packed_git = pack;
  667. }
  668. void (*report_garbage)(unsigned seen_bits, const char *path);
  669. static void report_helper(const struct string_list *list,
  670. int seen_bits, int first, int last)
  671. {
  672. if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
  673. return;
  674. for (; first < last; first++)
  675. report_garbage(seen_bits, list->items[first].string);
  676. }
  677. static void report_pack_garbage(struct string_list *list)
  678. {
  679. int i, baselen = -1, first = 0, seen_bits = 0;
  680. if (!report_garbage)
  681. return;
  682. string_list_sort(list);
  683. for (i = 0; i < list->nr; i++) {
  684. const char *path = list->items[i].string;
  685. if (baselen != -1 &&
  686. strncmp(path, list->items[first].string, baselen)) {
  687. report_helper(list, seen_bits, first, i);
  688. baselen = -1;
  689. seen_bits = 0;
  690. }
  691. if (baselen == -1) {
  692. const char *dot = strrchr(path, '.');
  693. if (!dot) {
  694. report_garbage(PACKDIR_FILE_GARBAGE, path);
  695. continue;
  696. }
  697. baselen = dot - path + 1;
  698. first = i;
  699. }
  700. if (!strcmp(path + baselen, "pack"))
  701. seen_bits |= 1;
  702. else if (!strcmp(path + baselen, "idx"))
  703. seen_bits |= 2;
  704. }
  705. report_helper(list, seen_bits, first, list->nr);
  706. }
  707. void for_each_file_in_pack_dir(const char *objdir,
  708. each_file_in_pack_dir_fn fn,
  709. void *data)
  710. {
  711. struct strbuf path = STRBUF_INIT;
  712. size_t dirnamelen;
  713. DIR *dir;
  714. struct dirent *de;
  715. strbuf_addstr(&path, objdir);
  716. strbuf_addstr(&path, "/pack");
  717. dir = opendir(path.buf);
  718. if (!dir) {
  719. if (errno != ENOENT)
  720. error_errno("unable to open object pack directory: %s",
  721. path.buf);
  722. strbuf_release(&path);
  723. return;
  724. }
  725. strbuf_addch(&path, '/');
  726. dirnamelen = path.len;
  727. while ((de = readdir(dir)) != NULL) {
  728. if (is_dot_or_dotdot(de->d_name))
  729. continue;
  730. strbuf_setlen(&path, dirnamelen);
  731. strbuf_addstr(&path, de->d_name);
  732. fn(path.buf, path.len, de->d_name, data);
  733. }
  734. closedir(dir);
  735. strbuf_release(&path);
  736. }
  737. struct prepare_pack_data {
  738. struct repository *r;
  739. struct string_list *garbage;
  740. int local;
  741. struct multi_pack_index *m;
  742. };
  743. static void prepare_pack(const char *full_name, size_t full_name_len,
  744. const char *file_name, void *_data)
  745. {
  746. struct prepare_pack_data *data = (struct prepare_pack_data *)_data;
  747. struct packed_git *p;
  748. size_t base_len = full_name_len;
  749. if (strip_suffix_mem(full_name, &base_len, ".idx") &&
  750. !(data->m && midx_contains_pack(data->m, file_name))) {
  751. /* Don't reopen a pack we already have. */
  752. for (p = data->r->objects->packed_git; p; p = p->next) {
  753. size_t len;
  754. if (strip_suffix(p->pack_name, ".pack", &len) &&
  755. len == base_len &&
  756. !memcmp(p->pack_name, full_name, len))
  757. break;
  758. }
  759. if (!p) {
  760. p = add_packed_git(full_name, full_name_len, data->local);
  761. if (p)
  762. install_packed_git(data->r, p);
  763. }
  764. }
  765. if (!report_garbage)
  766. return;
  767. if (!strcmp(file_name, "multi-pack-index"))
  768. return;
  769. if (ends_with(file_name, ".idx") ||
  770. ends_with(file_name, ".pack") ||
  771. ends_with(file_name, ".bitmap") ||
  772. ends_with(file_name, ".keep") ||
  773. ends_with(file_name, ".promisor"))
  774. string_list_append(data->garbage, full_name);
  775. else
  776. report_garbage(PACKDIR_FILE_GARBAGE, full_name);
  777. }
  778. static void prepare_packed_git_one(struct repository *r, char *objdir, int local)
  779. {
  780. struct prepare_pack_data data;
  781. struct string_list garbage = STRING_LIST_INIT_DUP;
  782. data.m = r->objects->multi_pack_index;
  783. /* look for the multi-pack-index for this object directory */
  784. while (data.m && strcmp(data.m->object_dir, objdir))
  785. data.m = data.m->next;
  786. data.r = r;
  787. data.garbage = &garbage;
  788. data.local = local;
  789. for_each_file_in_pack_dir(objdir, prepare_pack, &data);
  790. report_pack_garbage(data.garbage);
  791. string_list_clear(data.garbage, 0);
  792. }
  793. static void prepare_packed_git(struct repository *r);
  794. /*
  795. * Give a fast, rough count of the number of objects in the repository. This
  796. * ignores loose objects completely. If you have a lot of them, then either
  797. * you should repack because your performance will be awful, or they are
  798. * all unreachable objects about to be pruned, in which case they're not really
  799. * interesting as a measure of repo size in the first place.
  800. */
  801. unsigned long repo_approximate_object_count(struct repository *r)
  802. {
  803. if (!r->objects->approximate_object_count_valid) {
  804. unsigned long count;
  805. struct multi_pack_index *m;
  806. struct packed_git *p;
  807. prepare_packed_git(r);
  808. count = 0;
  809. for (m = get_multi_pack_index(r); m; m = m->next)
  810. count += m->num_objects;
  811. for (p = r->objects->packed_git; p; p = p->next) {
  812. if (open_pack_index(p))
  813. continue;
  814. count += p->num_objects;
  815. }
  816. r->objects->approximate_object_count = count;
  817. }
  818. return r->objects->approximate_object_count;
  819. }
  820. static void *get_next_packed_git(const void *p)
  821. {
  822. return ((const struct packed_git *)p)->next;
  823. }
  824. static void set_next_packed_git(void *p, void *next)
  825. {
  826. ((struct packed_git *)p)->next = next;
  827. }
  828. static int sort_pack(const void *a_, const void *b_)
  829. {
  830. const struct packed_git *a = a_;
  831. const struct packed_git *b = b_;
  832. int st;
  833. /*
  834. * Local packs tend to contain objects specific to our
  835. * variant of the project than remote ones. In addition,
  836. * remote ones could be on a network mounted filesystem.
  837. * Favor local ones for these reasons.
  838. */
  839. st = a->pack_local - b->pack_local;
  840. if (st)
  841. return -st;
  842. /*
  843. * Younger packs tend to contain more recent objects,
  844. * and more recent objects tend to get accessed more
  845. * often.
  846. */
  847. if (a->mtime < b->mtime)
  848. return 1;
  849. else if (a->mtime == b->mtime)
  850. return 0;
  851. return -1;
  852. }
  853. static void rearrange_packed_git(struct repository *r)
  854. {
  855. r->objects->packed_git = llist_mergesort(
  856. r->objects->packed_git, get_next_packed_git,
  857. set_next_packed_git, sort_pack);
  858. }
  859. static void prepare_packed_git_mru(struct repository *r)
  860. {
  861. struct packed_git *p;
  862. INIT_LIST_HEAD(&r->objects->packed_git_mru);
  863. for (p = r->objects->packed_git; p; p = p->next)
  864. list_add_tail(&p->mru, &r->objects->packed_git_mru);
  865. }
  866. static void prepare_packed_git(struct repository *r)
  867. {
  868. struct object_directory *odb;
  869. if (r->objects->packed_git_initialized)
  870. return;
  871. prepare_alt_odb(r);
  872. for (odb = r->objects->odb; odb; odb = odb->next) {
  873. int local = (odb == r->objects->odb);
  874. prepare_multi_pack_index_one(r, odb->path, local);
  875. prepare_packed_git_one(r, odb->path, local);
  876. }
  877. rearrange_packed_git(r);
  878. prepare_packed_git_mru(r);
  879. r->objects->packed_git_initialized = 1;
  880. }
  881. void reprepare_packed_git(struct repository *r)
  882. {
  883. struct object_directory *odb;
  884. for (odb = r->objects->odb; odb; odb = odb->next)
  885. odb_clear_loose_cache(odb);
  886. r->objects->approximate_object_count_valid = 0;
  887. r->objects->packed_git_initialized = 0;
  888. prepare_packed_git(r);
  889. }
  890. struct packed_git *get_packed_git(struct repository *r)
  891. {
  892. prepare_packed_git(r);
  893. return r->objects->packed_git;
  894. }
  895. struct multi_pack_index *get_multi_pack_index(struct repository *r)
  896. {
  897. prepare_packed_git(r);
  898. return r->objects->multi_pack_index;
  899. }
  900. struct packed_git *get_all_packs(struct repository *r)
  901. {
  902. struct multi_pack_index *m;
  903. prepare_packed_git(r);
  904. for (m = r->objects->multi_pack_index; m; m = m->next) {
  905. uint32_t i;
  906. for (i = 0; i < m->num_packs; i++)
  907. prepare_midx_pack(r, m, i);
  908. }
  909. return r->objects->packed_git;
  910. }
  911. struct list_head *get_packed_git_mru(struct repository *r)
  912. {
  913. prepare_packed_git(r);
  914. return &r->objects->packed_git_mru;
  915. }
  916. unsigned long unpack_object_header_buffer(const unsigned char *buf,
  917. unsigned long len, enum object_type *type, unsigned long *sizep)
  918. {
  919. unsigned shift;
  920. unsigned long size, c;
  921. unsigned long used = 0;
  922. c = buf[used++];
  923. *type = (c >> 4) & 7;
  924. size = c & 15;
  925. shift = 4;
  926. while (c & 0x80) {
  927. if (len <= used || bitsizeof(long) <= shift) {
  928. error("bad object header");
  929. size = used = 0;
  930. break;
  931. }
  932. c = buf[used++];
  933. size += (c & 0x7f) << shift;
  934. shift += 7;
  935. }
  936. *sizep = size;
  937. return used;
  938. }
  939. unsigned long get_size_from_delta(struct packed_git *p,
  940. struct pack_window **w_curs,
  941. off_t curpos)
  942. {
  943. const unsigned char *data;
  944. unsigned char delta_head[20], *in;
  945. git_zstream stream;
  946. int st;
  947. memset(&stream, 0, sizeof(stream));
  948. stream.next_out = delta_head;
  949. stream.avail_out = sizeof(delta_head);
  950. git_inflate_init(&stream);
  951. do {
  952. in = use_pack(p, w_curs, curpos, &stream.avail_in);
  953. stream.next_in = in;
  954. st = git_inflate(&stream, Z_FINISH);
  955. curpos += stream.next_in - in;
  956. } while ((st == Z_OK || st == Z_BUF_ERROR) &&
  957. stream.total_out < sizeof(delta_head));
  958. git_inflate_end(&stream);
  959. if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
  960. error("delta data unpack-initial failed");
  961. return 0;
  962. }
  963. /* Examine the initial part of the delta to figure out
  964. * the result size.
  965. */
  966. data = delta_head;
  967. /* ignore base size */
  968. get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
  969. /* Read the result size */
  970. return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
  971. }
  972. int unpack_object_header(struct packed_git *p,
  973. struct pack_window **w_curs,
  974. off_t *curpos,
  975. unsigned long *sizep)
  976. {
  977. unsigned char *base;
  978. unsigned long left;
  979. unsigned long used;
  980. enum object_type type;
  981. /* use_pack() assures us we have [base, base + 20) available
  982. * as a range that we can look at. (Its actually the hash
  983. * size that is assured.) With our object header encoding
  984. * the maximum deflated object size is 2^137, which is just
  985. * insane, so we know won't exceed what we have been given.
  986. */
  987. base = use_pack(p, w_curs, *curpos, &left);
  988. used = unpack_object_header_buffer(base, left, &type, sizep);
  989. if (!used) {
  990. type = OBJ_BAD;
  991. } else
  992. *curpos += used;
  993. return type;
  994. }
  995. void mark_bad_packed_object(struct packed_git *p, const unsigned char *sha1)
  996. {
  997. unsigned i;
  998. const unsigned hashsz = the_hash_algo->rawsz;
  999. for (i = 0; i < p->num_bad_objects; i++)
  1000. if (hasheq(sha1, p->bad_object_sha1 + hashsz * i))
  1001. return;
  1002. p->bad_object_sha1 = xrealloc(p->bad_object_sha1,
  1003. st_mult(GIT_MAX_RAWSZ,
  1004. st_add(p->num_bad_objects, 1)));
  1005. hashcpy(p->bad_object_sha1 + hashsz * p->num_bad_objects, sha1);
  1006. p->num_bad_objects++;
  1007. }
  1008. const struct packed_git *has_packed_and_bad(struct repository *r,
  1009. const unsigned char *sha1)
  1010. {
  1011. struct packed_git *p;
  1012. unsigned i;
  1013. for (p = r->objects->packed_git; p; p = p->next)
  1014. for (i = 0; i < p->num_bad_objects; i++)
  1015. if (hasheq(sha1,
  1016. p->bad_object_sha1 + the_hash_algo->rawsz * i))
  1017. return p;
  1018. return NULL;
  1019. }
  1020. static off_t get_delta_base(struct packed_git *p,
  1021. struct pack_window **w_curs,
  1022. off_t *curpos,
  1023. enum object_type type,
  1024. off_t delta_obj_offset)
  1025. {
  1026. unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
  1027. off_t base_offset;
  1028. /* use_pack() assured us we have [base_info, base_info + 20)
  1029. * as a range that we can look at without walking off the
  1030. * end of the mapped window. Its actually the hash size
  1031. * that is assured. An OFS_DELTA longer than the hash size
  1032. * is stupid, as then a REF_DELTA would be smaller to store.
  1033. */
  1034. if (type == OBJ_OFS_DELTA) {
  1035. unsigned used = 0;
  1036. unsigned char c = base_info[used++];
  1037. base_offset = c & 127;
  1038. while (c & 128) {
  1039. base_offset += 1;
  1040. if (!base_offset || MSB(base_offset, 7))
  1041. return 0; /* overflow */
  1042. c = base_info[used++];
  1043. base_offset = (base_offset << 7) + (c & 127);
  1044. }
  1045. base_offset = delta_obj_offset - base_offset;
  1046. if (base_offset <= 0 || base_offset >= delta_obj_offset)
  1047. return 0; /* out of bound */
  1048. *curpos += used;
  1049. } else if (type == OBJ_REF_DELTA) {
  1050. /* The base entry _must_ be in the same pack */
  1051. base_offset = find_pack_entry_one(base_info, p);
  1052. *curpos += the_hash_algo->rawsz;
  1053. } else
  1054. die("I am totally screwed");
  1055. return base_offset;
  1056. }
  1057. /*
  1058. * Like get_delta_base above, but we return the sha1 instead of the pack
  1059. * offset. This means it is cheaper for REF deltas (we do not have to do
  1060. * the final object lookup), but more expensive for OFS deltas (we
  1061. * have to load the revidx to convert the offset back into a sha1).
  1062. */
  1063. static const unsigned char *get_delta_base_sha1(struct packed_git *p,
  1064. struct pack_window **w_curs,
  1065. off_t curpos,
  1066. enum object_type type,
  1067. off_t delta_obj_offset)
  1068. {
  1069. if (type == OBJ_REF_DELTA) {
  1070. unsigned char *base = use_pack(p, w_curs, curpos, NULL);
  1071. return base;
  1072. } else if (type == OBJ_OFS_DELTA) {
  1073. struct revindex_entry *revidx;
  1074. off_t base_offset = get_delta_base(p, w_curs, &curpos,
  1075. type, delta_obj_offset);
  1076. if (!base_offset)
  1077. return NULL;
  1078. revidx = find_pack_revindex(p, base_offset);
  1079. if (!revidx)
  1080. return NULL;
  1081. return nth_packed_object_sha1(p, revidx->nr);
  1082. } else
  1083. return NULL;
  1084. }
  1085. static int retry_bad_packed_offset(struct repository *r,
  1086. struct packed_git *p,
  1087. off_t obj_offset)
  1088. {
  1089. int type;
  1090. struct revindex_entry *revidx;
  1091. struct object_id oid;
  1092. revidx = find_pack_revindex(p, obj_offset);
  1093. if (!revidx)
  1094. return OBJ_BAD;
  1095. nth_packed_object_oid(&oid, p, revidx->nr);
  1096. mark_bad_packed_object(p, oid.hash);
  1097. type = oid_object_info(r, &oid, NULL);
  1098. if (type <= OBJ_NONE)
  1099. return OBJ_BAD;
  1100. return type;
  1101. }
  1102. #define POI_STACK_PREALLOC 64
  1103. static enum object_type packed_to_object_type(struct repository *r,
  1104. struct packed_git *p,
  1105. off_t obj_offset,
  1106. enum object_type type,
  1107. struct pack_window **w_curs,
  1108. off_t curpos)
  1109. {
  1110. off_t small_poi_stack[POI_STACK_PREALLOC];
  1111. off_t *poi_stack = small_poi_stack;
  1112. int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
  1113. while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1114. off_t base_offset;
  1115. unsigned long size;
  1116. /* Push the object we're going to leave behind */
  1117. if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
  1118. poi_stack_alloc = alloc_nr(poi_stack_nr);
  1119. ALLOC_ARRAY(poi_stack, poi_stack_alloc);
  1120. COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr);
  1121. } else {
  1122. ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
  1123. }
  1124. poi_stack[poi_stack_nr++] = obj_offset;
  1125. /* If parsing the base offset fails, just unwind */
  1126. base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
  1127. if (!base_offset)
  1128. goto unwind;
  1129. curpos = obj_offset = base_offset;
  1130. type = unpack_object_header(p, w_curs, &curpos, &size);
  1131. if (type <= OBJ_NONE) {
  1132. /* If getting the base itself fails, we first
  1133. * retry the base, otherwise unwind */
  1134. type = retry_bad_packed_offset(r, p, base_offset);
  1135. if (type > OBJ_NONE)
  1136. goto out;
  1137. goto unwind;
  1138. }
  1139. }
  1140. switch (type) {
  1141. case OBJ_BAD:
  1142. case OBJ_COMMIT:
  1143. case OBJ_TREE:
  1144. case OBJ_BLOB:
  1145. case OBJ_TAG:
  1146. break;
  1147. default:
  1148. error("unknown object type %i at offset %"PRIuMAX" in %s",
  1149. type, (uintmax_t)obj_offset, p->pack_name);
  1150. type = OBJ_BAD;
  1151. }
  1152. out:
  1153. if (poi_stack != small_poi_stack)
  1154. free(poi_stack);
  1155. return type;
  1156. unwind:
  1157. while (poi_stack_nr) {
  1158. obj_offset = poi_stack[--poi_stack_nr];
  1159. type = retry_bad_packed_offset(r, p, obj_offset);
  1160. if (type > OBJ_NONE)
  1161. goto out;
  1162. }
  1163. type = OBJ_BAD;
  1164. goto out;
  1165. }
  1166. static struct hashmap delta_base_cache;
  1167. static size_t delta_base_cached;
  1168. static LIST_HEAD(delta_base_cache_lru);
  1169. struct delta_base_cache_key {
  1170. struct packed_git *p;
  1171. off_t base_offset;
  1172. };
  1173. struct delta_base_cache_entry {
  1174. struct hashmap_entry ent;
  1175. struct delta_base_cache_key key;
  1176. struct list_head lru;
  1177. void *data;
  1178. unsigned long size;
  1179. enum object_type type;
  1180. };
  1181. static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
  1182. {
  1183. unsigned int hash;
  1184. hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
  1185. hash += (hash >> 8) + (hash >> 16);
  1186. return hash;
  1187. }
  1188. static struct delta_base_cache_entry *
  1189. get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
  1190. {
  1191. struct hashmap_entry entry, *e;
  1192. struct delta_base_cache_key key;
  1193. if (!delta_base_cache.cmpfn)
  1194. return NULL;
  1195. hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
  1196. key.p = p;
  1197. key.base_offset = base_offset;
  1198. e = hashmap_get(&delta_base_cache, &entry, &key);
  1199. return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL;
  1200. }
  1201. static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
  1202. const struct delta_base_cache_key *b)
  1203. {
  1204. return a->p == b->p && a->base_offset == b->base_offset;
  1205. }
  1206. static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
  1207. const struct hashmap_entry *va,
  1208. const struct hashmap_entry *vb,
  1209. const void *vkey)
  1210. {
  1211. const struct delta_base_cache_entry *a, *b;
  1212. const struct delta_base_cache_key *key = vkey;
  1213. a = container_of(va, const struct delta_base_cache_entry, ent);
  1214. b = container_of(vb, const struct delta_base_cache_entry, ent);
  1215. if (key)
  1216. return !delta_base_cache_key_eq(&a->key, key);
  1217. else
  1218. return !delta_base_cache_key_eq(&a->key, &b->key);
  1219. }
  1220. static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
  1221. {
  1222. return !!get_delta_base_cache_entry(p, base_offset);
  1223. }
  1224. /*
  1225. * Remove the entry from the cache, but do _not_ free the associated
  1226. * entry data. The caller takes ownership of the "data" buffer, and
  1227. * should copy out any fields it wants before detaching.
  1228. */
  1229. static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
  1230. {
  1231. hashmap_remove(&delta_base_cache, &ent->ent, &ent->key);
  1232. list_del(&ent->lru);
  1233. delta_base_cached -= ent->size;
  1234. free(ent);
  1235. }
  1236. static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p,
  1237. off_t base_offset, unsigned long *base_size,
  1238. enum object_type *type)
  1239. {
  1240. struct delta_base_cache_entry *ent;
  1241. ent = get_delta_base_cache_entry(p, base_offset);
  1242. if (!ent)
  1243. return unpack_entry(r, p, base_offset, type, base_size);
  1244. if (type)
  1245. *type = ent->type;
  1246. if (base_size)
  1247. *base_size = ent->size;
  1248. return xmemdupz(ent->data, ent->size);
  1249. }
  1250. static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
  1251. {
  1252. free(ent->data);
  1253. detach_delta_base_cache_entry(ent);
  1254. }
  1255. void clear_delta_base_cache(void)
  1256. {
  1257. struct list_head *lru, *tmp;
  1258. list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
  1259. struct delta_base_cache_entry *entry =
  1260. list_entry(lru, struct delta_base_cache_entry, lru);
  1261. release_delta_base_cache(entry);
  1262. }
  1263. }
  1264. static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
  1265. void *base, unsigned long base_size, enum object_type type)
  1266. {
  1267. struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
  1268. struct list_head *lru, *tmp;
  1269. delta_base_cached += base_size;
  1270. list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
  1271. struct delta_base_cache_entry *f =
  1272. list_entry(lru, struct delta_base_cache_entry, lru);
  1273. if (delta_base_cached <= delta_base_cache_limit)
  1274. break;
  1275. release_delta_base_cache(f);
  1276. }
  1277. ent->key.p = p;
  1278. ent->key.base_offset = base_offset;
  1279. ent->type = type;
  1280. ent->data = base;
  1281. ent->size = base_size;
  1282. list_add_tail(&ent->lru, &delta_base_cache_lru);
  1283. if (!delta_base_cache.cmpfn)
  1284. hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
  1285. hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset));
  1286. hashmap_add(&delta_base_cache, &ent->ent);
  1287. }
  1288. int packed_object_info(struct repository *r, struct packed_git *p,
  1289. off_t obj_offset, struct object_info *oi)
  1290. {
  1291. struct pack_window *w_curs = NULL;
  1292. unsigned long size;
  1293. off_t curpos = obj_offset;
  1294. enum object_type type;
  1295. /*
  1296. * We always get the representation type, but only convert it to
  1297. * a "real" type later if the caller is interested.
  1298. */
  1299. if (oi->contentp) {
  1300. *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep,
  1301. &type);
  1302. if (!*oi->contentp)
  1303. type = OBJ_BAD;
  1304. } else {
  1305. type = unpack_object_header(p, &w_curs, &curpos, &size);
  1306. }
  1307. if (!oi->contentp && oi->sizep) {
  1308. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1309. off_t tmp_pos = curpos;
  1310. off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
  1311. type, obj_offset);
  1312. if (!base_offset) {
  1313. type = OBJ_BAD;
  1314. goto out;
  1315. }
  1316. *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
  1317. if (*oi->sizep == 0) {
  1318. type = OBJ_BAD;
  1319. goto out;
  1320. }
  1321. } else {
  1322. *oi->sizep = size;
  1323. }
  1324. }
  1325. if (oi->disk_sizep) {
  1326. struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
  1327. *oi->disk_sizep = revidx[1].offset - obj_offset;
  1328. }
  1329. if (oi->typep || oi->type_name) {
  1330. enum object_type ptot;
  1331. ptot = packed_to_object_type(r, p, obj_offset,
  1332. type, &w_curs, curpos);
  1333. if (oi->typep)
  1334. *oi->typep = ptot;
  1335. if (oi->type_name) {
  1336. const char *tn = type_name(ptot);
  1337. if (tn)
  1338. strbuf_addstr(oi->type_name, tn);
  1339. }
  1340. if (ptot < 0) {
  1341. type = OBJ_BAD;
  1342. goto out;
  1343. }
  1344. }
  1345. if (oi->delta_base_sha1) {
  1346. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
  1347. const unsigned char *base;
  1348. base = get_delta_base_sha1(p, &w_curs, curpos,
  1349. type, obj_offset);
  1350. if (!base) {
  1351. type = OBJ_BAD;
  1352. goto out;
  1353. }
  1354. hashcpy(oi->delta_base_sha1, base);
  1355. } else
  1356. hashclr(oi->delta_base_sha1);
  1357. }
  1358. oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED :
  1359. OI_PACKED;
  1360. out:
  1361. unuse_pack(&w_curs);
  1362. return type;
  1363. }
  1364. static void *unpack_compressed_entry(struct packed_git *p,
  1365. struct pack_window **w_curs,
  1366. off_t curpos,
  1367. unsigned long size)
  1368. {
  1369. int st;
  1370. git_zstream stream;
  1371. unsigned char *buffer, *in;
  1372. buffer = xmallocz_gently(size);
  1373. if (!buffer)
  1374. return NULL;
  1375. memset(&stream, 0, sizeof(stream));
  1376. stream.next_out = buffer;
  1377. stream.avail_out = size + 1;
  1378. git_inflate_init(&stream);
  1379. do {
  1380. in = use_pack(p, w_curs, curpos, &stream.avail_in);
  1381. stream.next_in = in;
  1382. st = git_inflate(&stream, Z_FINISH);
  1383. if (!stream.avail_out)
  1384. break; /* the payload is larger than it should be */
  1385. curpos += stream.next_in - in;
  1386. } while (st == Z_OK || st == Z_BUF_ERROR);
  1387. git_inflate_end(&stream);
  1388. if ((st != Z_STREAM_END) || stream.total_out != size) {
  1389. free(buffer);
  1390. return NULL;
  1391. }
  1392. /* versions of zlib can clobber unconsumed portion of outbuf */
  1393. buffer[size] = '\0';
  1394. return buffer;
  1395. }
  1396. static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
  1397. {
  1398. static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
  1399. trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
  1400. p->pack_name, (uintmax_t)obj_offset);
  1401. }
  1402. int do_check_packed_object_crc;
  1403. #define UNPACK_ENTRY_STACK_PREALLOC 64
  1404. struct unpack_entry_stack_ent {
  1405. off_t obj_offset;
  1406. off_t curpos;
  1407. unsigned long size;
  1408. };
  1409. static void *read_object(struct repository *r,
  1410. const struct object_id *oid,
  1411. enum object_type *type,
  1412. unsigned long *size)
  1413. {
  1414. struct object_info oi = OBJECT_INFO_INIT;
  1415. void *content;
  1416. oi.typep = type;
  1417. oi.sizep = size;
  1418. oi.contentp = &content;
  1419. if (oid_object_info_extended(r, oid, &oi, 0) < 0)
  1420. return NULL;
  1421. return content;
  1422. }
  1423. void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
  1424. enum object_type *final_type, unsigned long *final_size)
  1425. {
  1426. struct pack_window *w_curs = NULL;
  1427. off_t curpos = obj_offset;
  1428. void *data = NULL;
  1429. unsigned long size;
  1430. enum object_type type;
  1431. struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
  1432. struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
  1433. int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
  1434. int base_from_cache = 0;
  1435. write_pack_access_log(p, obj_offset);
  1436. /* PHASE 1: drill down to the innermost base object */
  1437. for (;;) {
  1438. off_t base_offset;
  1439. int i;
  1440. struct delta_base_cache_entry *ent;
  1441. ent = get_delta_base_cache_entry(p, curpos);
  1442. if (ent) {
  1443. type = ent->type;
  1444. data = ent->data;
  1445. size = ent->size;
  1446. detach_delta_base_cache_entry(ent);
  1447. base_from_cache = 1;
  1448. break;
  1449. }
  1450. if (do_check_packed_object_crc && p->index_version > 1) {
  1451. struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
  1452. off_t len = revidx[1].offset - obj_offset;
  1453. if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
  1454. struct object_id oid;
  1455. nth_packed_object_oid(&oid, p, revidx->nr);
  1456. error("bad packed object CRC for %s",
  1457. oid_to_hex(&oid));
  1458. mark_bad_packed_object(p, oid.hash);
  1459. data = NULL;
  1460. goto out;
  1461. }
  1462. }
  1463. type = unpack_object_header(p, &w_curs, &curpos, &size);
  1464. if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
  1465. break;
  1466. base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
  1467. if (!base_offset) {
  1468. error("failed to validate delta base reference "
  1469. "at offset %"PRIuMAX" from %s",
  1470. (uintmax_t)curpos, p->pack_name);
  1471. /* bail to phase 2, in hopes of recovery */
  1472. data = NULL;
  1473. break;
  1474. }
  1475. /* push object, proceed to base */
  1476. if (delta_stack_nr >= delta_stack_alloc
  1477. && delta_stack == small_delta_stack) {
  1478. delta_stack_alloc = alloc_nr(delta_stack_nr);
  1479. ALLOC_ARRAY(delta_stack, delta_stack_alloc);
  1480. COPY_ARRAY(delta_stack, small_delta_stack,
  1481. delta_stack_nr);
  1482. } else {
  1483. ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
  1484. }
  1485. i = delta_stack_nr++;
  1486. delta_stack[i].obj_offset = obj_offset;
  1487. delta_stack[i].curpos = curpos;
  1488. delta_stack[i].size = size;
  1489. curpos = obj_offset = base_offset;
  1490. }
  1491. /* PHASE 2: handle the base */
  1492. switch (type) {
  1493. case OBJ_OFS_DELTA:
  1494. case OBJ_REF_DELTA:
  1495. if (data)
  1496. BUG("unpack_entry: left loop at a valid delta");
  1497. break;
  1498. case OBJ_COMMIT:
  1499. case OBJ_TREE:
  1500. case OBJ_BLOB:
  1501. case OBJ_TAG:
  1502. if (!base_from_cache)
  1503. data = unpack_compressed_entry(p, &w_curs, curpos, size);
  1504. break;
  1505. default:
  1506. data = NULL;
  1507. error("unknown object type %i at offset %"PRIuMAX" in %s",
  1508. type, (uintmax_t)obj_offset, p->pack_name);
  1509. }
  1510. /* PHASE 3: apply deltas in order */
  1511. /* invariants:
  1512. * 'data' holds the base data, or NULL if there was corruption
  1513. */
  1514. while (delta_stack_nr) {
  1515. void *delta_data;
  1516. void *base = data;
  1517. void *external_base = NULL;
  1518. unsigned long delta_size, base_size = size;
  1519. int i;
  1520. data = NULL;
  1521. if (base)
  1522. add_delta_base_cache(p, obj_offset, base, base_size, type);
  1523. if (!base) {
  1524. /*
  1525. * We're probably in deep shit, but let's try to fetch
  1526. * the required base anyway from another pack or loose.
  1527. * This is costly but should happen only in the presence
  1528. * of a corrupted pack, and is better than failing outright.
  1529. */
  1530. struct revindex_entry *revidx;
  1531. struct object_id base_oid;
  1532. revidx = find_pack_revindex(p, obj_offset);
  1533. if (revidx) {
  1534. nth_packed_object_oid(&base_oid, p, revidx->nr);
  1535. error("failed to read delta base object %s"
  1536. " at offset %"PRIuMAX" from %s",
  1537. oid_to_hex(&base_oid), (uintmax_t)obj_offset,
  1538. p->pack_name);
  1539. mark_bad_packed_object(p, base_oid.hash);
  1540. base = read_object(r, &base_oid, &type, &base_size);
  1541. external_base = base;
  1542. }
  1543. }
  1544. i = --delta_stack_nr;
  1545. obj_offset = delta_stack[i].obj_offset;
  1546. curpos = delta_stack[i].curpos;
  1547. delta_size = delta_stack[i].size;
  1548. if (!base)
  1549. continue;
  1550. delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
  1551. if (!delta_data) {
  1552. error("failed to unpack compressed delta "
  1553. "at offset %"PRIuMAX" from %s",
  1554. (uintmax_t)curpos, p->pack_name);
  1555. data = NULL;
  1556. free(external_base);
  1557. continue;
  1558. }
  1559. data = patch_delta(base, base_size,
  1560. delta_data, delta_size,
  1561. &size);
  1562. /*
  1563. * We could not apply the delta; warn the user, but keep going.
  1564. * Our failure will be noticed either in the next iteration of
  1565. * the loop, or if this is the final delta, in the caller when
  1566. * we return NULL. Those code paths will take care of making
  1567. * a more explicit warning and retrying with another copy of
  1568. * the object.
  1569. */
  1570. if (!data)
  1571. error("failed to apply delta");
  1572. free(delta_data);
  1573. free(external_base);
  1574. }
  1575. if (final_type)
  1576. *final_type = type;
  1577. if (final_size)
  1578. *final_size = size;
  1579. out:
  1580. unuse_pack(&w_curs);
  1581. if (delta_stack != small_delta_stack)
  1582. free(delta_stack);
  1583. return data;
  1584. }
  1585. int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
  1586. {
  1587. const unsigned char *index_fanout = p->index_data;
  1588. const unsigned char *index_lookup;
  1589. const unsigned int hashsz = the_hash_algo->rawsz;
  1590. int index_lookup_width;
  1591. if (!index_fanout)
  1592. BUG("bsearch_pack called without a valid pack-index");
  1593. index_lookup = index_fanout + 4 * 256;
  1594. if (p->index_version == 1) {
  1595. index_lookup_width = hashsz + 4;
  1596. index_lookup += 4;
  1597. } else {
  1598. index_lookup_width = hashsz;
  1599. index_fanout += 8;
  1600. index_lookup += 8;
  1601. }
  1602. return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
  1603. index_lookup, index_lookup_width, result);
  1604. }
  1605. const unsigned char *nth_packed_object_sha1(struct packed_git *p,
  1606. uint32_t n)
  1607. {
  1608. const unsigned char *index = p->index_data;
  1609. const unsigned int hashsz = the_hash_algo->rawsz;
  1610. if (!index) {
  1611. if (open_pack_index(p))
  1612. return NULL;
  1613. index = p->index_data;
  1614. }
  1615. if (n >= p->num_objects)
  1616. return NULL;
  1617. index += 4 * 256;
  1618. if (p->index_version == 1) {
  1619. return index + (hashsz + 4) * n + 4;
  1620. } else {
  1621. index += 8;
  1622. return index + hashsz * n;
  1623. }
  1624. }
  1625. const struct object_id *nth_packed_object_oid(struct object_id *oid,
  1626. struct packed_git *p,
  1627. uint32_t n)
  1628. {
  1629. const unsigned char *hash = nth_packed_object_sha1(p, n);
  1630. if (!hash)
  1631. return NULL;
  1632. hashcpy(oid->hash, hash);
  1633. return oid;
  1634. }
  1635. void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
  1636. {
  1637. const unsigned char *ptr = vptr;
  1638. const unsigned char *start = p->index_data;
  1639. const unsigned char *end = start + p->index_size;
  1640. if (ptr < start)
  1641. die(_("offset before start of pack index for %s (corrupt index?)"),
  1642. p->pack_name);
  1643. /* No need to check for underflow; .idx files must be at least 8 bytes */
  1644. if (ptr >= end - 8)
  1645. die(_("offset beyond end of pack index for %s (truncated index?)"),
  1646. p->pack_name);
  1647. }
  1648. off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
  1649. {
  1650. const unsigned char *index = p->index_data;
  1651. const unsigned int hashsz = the_hash_algo->rawsz;
  1652. index += 4 * 256;
  1653. if (p->index_version == 1) {
  1654. return ntohl(*((uint32_t *)(index + (hashsz + 4) * n)));
  1655. } else {
  1656. uint32_t off;
  1657. index += 8 + p->num_objects * (hashsz + 4);
  1658. off = ntohl(*((uint32_t *)(index + 4 * n)));
  1659. if (!(off & 0x80000000))
  1660. return off;
  1661. index += p->num_objects * 4 + (off & 0x7fffffff) * 8;
  1662. check_pack_index_ptr(p, index);
  1663. return get_be64(index);
  1664. }
  1665. }
  1666. off_t find_pack_entry_one(const unsigned char *sha1,
  1667. struct packed_git *p)
  1668. {
  1669. const unsigned char *index = p->index_data;
  1670. struct object_id oid;
  1671. uint32_t result;
  1672. if (!index) {
  1673. if (open_pack_index(p))
  1674. return 0;
  1675. }
  1676. hashcpy(oid.hash, sha1);
  1677. if (bsearch_pack(&oid, p, &result))
  1678. return nth_packed_object_offset(p, result);
  1679. return 0;
  1680. }
  1681. int is_pack_valid(struct packed_git *p)
  1682. {
  1683. /* An already open pack is known to be valid. */
  1684. if (p->pack_fd != -1)
  1685. return 1;
  1686. /* If the pack has one window completely covering the
  1687. * file size, the pack is known to be valid even if
  1688. * the descriptor is not currently open.
  1689. */
  1690. if (p->windows) {
  1691. struct pack_window *w = p->windows;
  1692. if (!w->offset && w->len == p->pack_size)
  1693. return 1;
  1694. }
  1695. /* Force the pack to open to prove its valid. */
  1696. return !open_packed_git(p);
  1697. }
  1698. struct packed_git *find_sha1_pack(const unsigned char *sha1,
  1699. struct packed_git *packs)
  1700. {
  1701. struct packed_git *p;
  1702. for (p = packs; p; p = p->next) {
  1703. if (find_pack_entry_one(sha1, p))
  1704. return p;
  1705. }
  1706. return NULL;
  1707. }
  1708. static int fill_pack_entry(const struct object_id *oid,
  1709. struct pack_entry *e,
  1710. struct packed_git *p)
  1711. {
  1712. off_t offset;
  1713. if (p->num_bad_objects) {
  1714. unsigned i;
  1715. for (i = 0; i < p->num_bad_objects; i++)
  1716. if (hasheq(oid->hash,
  1717. p->bad_object_sha1 + the_hash_algo->rawsz * i))
  1718. return 0;
  1719. }
  1720. offset = find_pack_entry_one(oid->hash, p);
  1721. if (!offset)
  1722. return 0;
  1723. /*
  1724. * We are about to tell the caller where they can locate the
  1725. * requested object. We better make sure the packfile is
  1726. * still here and can be accessed before supplying that
  1727. * answer, as it may have been deleted since the index was
  1728. * loaded!
  1729. */
  1730. if (!is_pack_valid(p))
  1731. return 0;
  1732. e->offset = offset;
  1733. e->p = p;
  1734. return 1;
  1735. }
  1736. int find_pack_entry(struct repository *r, const struct object_id *oid, struct pack_entry *e)
  1737. {
  1738. struct list_head *pos;
  1739. struct multi_pack_index *m;
  1740. prepare_packed_git(r);
  1741. if (!r->objects->packed_git && !r->objects->multi_pack_index)
  1742. return 0;
  1743. for (m = r->objects->multi_pack_index; m; m = m->next) {
  1744. if (fill_midx_entry(r, oid, e, m))
  1745. return 1;
  1746. }
  1747. list_for_each(pos, &r->objects->packed_git_mru) {
  1748. struct packed_git *p = list_entry(pos, struct packed_git, mru);
  1749. if (!p->multi_pack_index && fill_pack_entry(oid, e, p)) {
  1750. list_move(&p->mru, &r->objects->packed_git_mru);
  1751. return 1;
  1752. }
  1753. }
  1754. return 0;
  1755. }
  1756. int has_object_pack(const struct object_id *oid)
  1757. {
  1758. struct pack_entry e;
  1759. return find_pack_entry(the_repository, oid, &e);
  1760. }
  1761. int has_pack_index(const unsigned char *sha1)
  1762. {
  1763. struct stat st;
  1764. if (stat(sha1_pack_index_name(sha1), &st))
  1765. return 0;
  1766. return 1;
  1767. }
  1768. int for_each_object_in_pack(struct packed_git *p,
  1769. each_packed_object_fn cb, void *data,
  1770. enum for_each_object_flags flags)
  1771. {
  1772. uint32_t i;
  1773. int r = 0;
  1774. if (flags & FOR_EACH_OBJECT_PACK_ORDER) {
  1775. if (load_pack_revindex(p))
  1776. return -1;
  1777. }
  1778. for (i = 0; i < p->num_objects; i++) {
  1779. uint32_t pos;
  1780. struct object_id oid;
  1781. if (flags & FOR_EACH_OBJECT_PACK_ORDER)
  1782. pos = p->revindex[i].nr;
  1783. else
  1784. pos = i;
  1785. if (!nth_packed_object_oid(&oid, p, pos))
  1786. return error("unable to get sha1 of object %u in %s",
  1787. pos, p->pack_name);
  1788. r = cb(&oid, p, pos, data);
  1789. if (r)
  1790. break;
  1791. }
  1792. return r;
  1793. }
  1794. int for_each_packed_object(each_packed_object_fn cb, void *data,
  1795. enum for_each_object_flags flags)
  1796. {
  1797. struct packed_git *p;
  1798. int r = 0;
  1799. int pack_errors = 0;
  1800. prepare_packed_git(the_repository);
  1801. for (p = get_all_packs(the_repository); p; p = p->next) {
  1802. if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
  1803. continue;
  1804. if ((flags & FOR_EACH_OBJECT_PROMISOR_ONLY) &&
  1805. !p->pack_promisor)
  1806. continue;
  1807. if (open_pack_index(p)) {
  1808. pack_errors = 1;
  1809. continue;
  1810. }
  1811. r = for_each_object_in_pack(p, cb, data, flags);
  1812. if (r)
  1813. break;
  1814. }
  1815. return r ? r : pack_errors;
  1816. }
  1817. static int add_promisor_object(const struct object_id *oid,
  1818. struct packed_git *pack,
  1819. uint32_t pos,
  1820. void *set_)
  1821. {
  1822. struct oidset *set = set_;
  1823. struct object *obj = parse_object(the_repository, oid);
  1824. if (!obj)
  1825. return 1;
  1826. oidset_insert(set, oid);
  1827. /*
  1828. * If this is a tree, commit, or tag, the objects it refers
  1829. * to are also promisor objects. (Blobs refer to no objects->)
  1830. */
  1831. if (obj->type == OBJ_TREE) {
  1832. struct tree *tree = (struct tree *)obj;
  1833. struct tree_desc desc;
  1834. struct name_entry entry;
  1835. if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
  1836. /*
  1837. * Error messages are given when packs are
  1838. * verified, so do not print any here.
  1839. */
  1840. return 0;
  1841. while (tree_entry_gently(&desc, &entry))
  1842. oidset_insert(set, &entry.oid);
  1843. } else if (obj->type == OBJ_COMMIT) {
  1844. struct commit *commit = (struct commit *) obj;
  1845. struct commit_list *parents = commit->parents;
  1846. oidset_insert(set, get_commit_tree_oid(commit));
  1847. for (; parents; parents = parents->next)
  1848. oidset_insert(set, &parents->item->object.oid);
  1849. } else if (obj->type == OBJ_TAG) {
  1850. struct tag *tag = (struct tag *) obj;
  1851. oidset_insert(set, get_tagged_oid(tag));
  1852. }
  1853. return 0;
  1854. }
  1855. int is_promisor_object(const struct object_id *oid)
  1856. {
  1857. static struct oidset promisor_objects;
  1858. static int promisor_objects_prepared;
  1859. if (!promisor_objects_prepared) {
  1860. if (has_promisor_remote()) {
  1861. for_each_packed_object(add_promisor_object,
  1862. &promisor_objects,
  1863. FOR_EACH_OBJECT_PROMISOR_ONLY);
  1864. }
  1865. promisor_objects_prepared = 1;
  1866. }
  1867. return oidset_contains(&promisor_objects, oid);
  1868. }