pack-bitmap.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. #include "cache.h"
  2. #include "commit.h"
  3. #include "tag.h"
  4. #include "diff.h"
  5. #include "revision.h"
  6. #include "progress.h"
  7. #include "list-objects.h"
  8. #include "pack.h"
  9. #include "pack-bitmap.h"
  10. #include "pack-revindex.h"
  11. #include "pack-objects.h"
  12. #include "packfile.h"
  13. #include "repository.h"
  14. #include "object-store.h"
  15. #include "list-objects-filter-options.h"
  16. /*
  17. * An entry on the bitmap index, representing the bitmap for a given
  18. * commit.
  19. */
  20. struct stored_bitmap {
  21. struct object_id oid;
  22. struct ewah_bitmap *root;
  23. struct stored_bitmap *xor;
  24. int flags;
  25. };
  26. /*
  27. * The active bitmap index for a repository. By design, repositories only have
  28. * a single bitmap index available (the index for the biggest packfile in
  29. * the repository), since bitmap indexes need full closure.
  30. *
  31. * If there is more than one bitmap index available (e.g. because of alternates),
  32. * the active bitmap index is the largest one.
  33. */
  34. struct bitmap_index {
  35. /* Packfile to which this bitmap index belongs to */
  36. struct packed_git *pack;
  37. /*
  38. * Mark the first `reuse_objects` in the packfile as reused:
  39. * they will be sent as-is without using them for repacking
  40. * calculations
  41. */
  42. uint32_t reuse_objects;
  43. /* mmapped buffer of the whole bitmap index */
  44. unsigned char *map;
  45. size_t map_size; /* size of the mmaped buffer */
  46. size_t map_pos; /* current position when loading the index */
  47. /*
  48. * Type indexes.
  49. *
  50. * Each bitmap marks which objects in the packfile are of the given
  51. * type. This provides type information when yielding the objects from
  52. * the packfile during a walk, which allows for better delta bases.
  53. */
  54. struct ewah_bitmap *commits;
  55. struct ewah_bitmap *trees;
  56. struct ewah_bitmap *blobs;
  57. struct ewah_bitmap *tags;
  58. /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
  59. kh_oid_map_t *bitmaps;
  60. /* Number of bitmapped commits */
  61. uint32_t entry_count;
  62. /* If not NULL, this is a name-hash cache pointing into map. */
  63. uint32_t *hashes;
  64. /*
  65. * Extended index.
  66. *
  67. * When trying to perform bitmap operations with objects that are not
  68. * packed in `pack`, these objects are added to this "fake index" and
  69. * are assumed to appear at the end of the packfile for all operations
  70. */
  71. struct eindex {
  72. struct object **objects;
  73. uint32_t *hashes;
  74. uint32_t count, alloc;
  75. kh_oid_pos_t *positions;
  76. } ext_index;
  77. /* Bitmap result of the last performed walk */
  78. struct bitmap *result;
  79. /* "have" bitmap from the last performed walk */
  80. struct bitmap *haves;
  81. /* Version of the bitmap index */
  82. unsigned int version;
  83. };
  84. static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
  85. {
  86. struct ewah_bitmap *parent;
  87. struct ewah_bitmap *composed;
  88. if (st->xor == NULL)
  89. return st->root;
  90. composed = ewah_pool_new();
  91. parent = lookup_stored_bitmap(st->xor);
  92. ewah_xor(st->root, parent, composed);
  93. ewah_pool_free(st->root);
  94. st->root = composed;
  95. st->xor = NULL;
  96. return composed;
  97. }
  98. /*
  99. * Read a bitmap from the current read position on the mmaped
  100. * index, and increase the read position accordingly
  101. */
  102. static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
  103. {
  104. struct ewah_bitmap *b = ewah_pool_new();
  105. ssize_t bitmap_size = ewah_read_mmap(b,
  106. index->map + index->map_pos,
  107. index->map_size - index->map_pos);
  108. if (bitmap_size < 0) {
  109. error("Failed to load bitmap index (corrupted?)");
  110. ewah_pool_free(b);
  111. return NULL;
  112. }
  113. index->map_pos += bitmap_size;
  114. return b;
  115. }
  116. static int load_bitmap_header(struct bitmap_index *index)
  117. {
  118. struct bitmap_disk_header *header = (void *)index->map;
  119. if (index->map_size < sizeof(*header) + the_hash_algo->rawsz)
  120. return error("Corrupted bitmap index (missing header data)");
  121. if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
  122. return error("Corrupted bitmap index file (wrong header)");
  123. index->version = ntohs(header->version);
  124. if (index->version != 1)
  125. return error("Unsupported version for bitmap index file (%d)", index->version);
  126. /* Parse known bitmap format options */
  127. {
  128. uint32_t flags = ntohs(header->options);
  129. if ((flags & BITMAP_OPT_FULL_DAG) == 0)
  130. return error("Unsupported options for bitmap index file "
  131. "(Git requires BITMAP_OPT_FULL_DAG)");
  132. if (flags & BITMAP_OPT_HASH_CACHE) {
  133. unsigned char *end = index->map + index->map_size - the_hash_algo->rawsz;
  134. index->hashes = ((uint32_t *)end) - index->pack->num_objects;
  135. }
  136. }
  137. index->entry_count = ntohl(header->entry_count);
  138. index->map_pos += sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
  139. return 0;
  140. }
  141. static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
  142. struct ewah_bitmap *root,
  143. const struct object_id *oid,
  144. struct stored_bitmap *xor_with,
  145. int flags)
  146. {
  147. struct stored_bitmap *stored;
  148. khiter_t hash_pos;
  149. int ret;
  150. stored = xmalloc(sizeof(struct stored_bitmap));
  151. stored->root = root;
  152. stored->xor = xor_with;
  153. stored->flags = flags;
  154. oidcpy(&stored->oid, oid);
  155. hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
  156. /* a 0 return code means the insertion succeeded with no changes,
  157. * because the SHA1 already existed on the map. this is bad, there
  158. * shouldn't be duplicated commits in the index */
  159. if (ret == 0) {
  160. error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
  161. return NULL;
  162. }
  163. kh_value(index->bitmaps, hash_pos) = stored;
  164. return stored;
  165. }
  166. static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
  167. {
  168. uint32_t result = get_be32(buffer + *pos);
  169. (*pos) += sizeof(result);
  170. return result;
  171. }
  172. static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
  173. {
  174. return buffer[(*pos)++];
  175. }
  176. #define MAX_XOR_OFFSET 160
  177. static int load_bitmap_entries_v1(struct bitmap_index *index)
  178. {
  179. uint32_t i;
  180. struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
  181. for (i = 0; i < index->entry_count; ++i) {
  182. int xor_offset, flags;
  183. struct ewah_bitmap *bitmap = NULL;
  184. struct stored_bitmap *xor_bitmap = NULL;
  185. uint32_t commit_idx_pos;
  186. struct object_id oid;
  187. commit_idx_pos = read_be32(index->map, &index->map_pos);
  188. xor_offset = read_u8(index->map, &index->map_pos);
  189. flags = read_u8(index->map, &index->map_pos);
  190. nth_packed_object_id(&oid, index->pack, commit_idx_pos);
  191. bitmap = read_bitmap_1(index);
  192. if (!bitmap)
  193. return -1;
  194. if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
  195. return error("Corrupted bitmap pack index");
  196. if (xor_offset > 0) {
  197. xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
  198. if (xor_bitmap == NULL)
  199. return error("Invalid XOR offset in bitmap pack index");
  200. }
  201. recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
  202. index, bitmap, &oid, xor_bitmap, flags);
  203. }
  204. return 0;
  205. }
  206. static char *pack_bitmap_filename(struct packed_git *p)
  207. {
  208. size_t len;
  209. if (!strip_suffix(p->pack_name, ".pack", &len))
  210. BUG("pack_name does not end in .pack");
  211. return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
  212. }
  213. static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
  214. {
  215. int fd;
  216. struct stat st;
  217. char *idx_name;
  218. if (open_pack_index(packfile))
  219. return -1;
  220. idx_name = pack_bitmap_filename(packfile);
  221. fd = git_open(idx_name);
  222. free(idx_name);
  223. if (fd < 0)
  224. return -1;
  225. if (fstat(fd, &st)) {
  226. close(fd);
  227. return -1;
  228. }
  229. if (bitmap_git->pack) {
  230. warning("ignoring extra bitmap file: %s", packfile->pack_name);
  231. close(fd);
  232. return -1;
  233. }
  234. bitmap_git->pack = packfile;
  235. bitmap_git->map_size = xsize_t(st.st_size);
  236. bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
  237. bitmap_git->map_pos = 0;
  238. close(fd);
  239. if (load_bitmap_header(bitmap_git) < 0) {
  240. munmap(bitmap_git->map, bitmap_git->map_size);
  241. bitmap_git->map = NULL;
  242. bitmap_git->map_size = 0;
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. static int load_pack_bitmap(struct bitmap_index *bitmap_git)
  248. {
  249. assert(bitmap_git->map);
  250. bitmap_git->bitmaps = kh_init_oid_map();
  251. bitmap_git->ext_index.positions = kh_init_oid_pos();
  252. if (load_pack_revindex(bitmap_git->pack))
  253. goto failed;
  254. if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
  255. !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
  256. !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
  257. !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
  258. goto failed;
  259. if (load_bitmap_entries_v1(bitmap_git) < 0)
  260. goto failed;
  261. return 0;
  262. failed:
  263. munmap(bitmap_git->map, bitmap_git->map_size);
  264. bitmap_git->map = NULL;
  265. bitmap_git->map_size = 0;
  266. kh_destroy_oid_map(bitmap_git->bitmaps);
  267. bitmap_git->bitmaps = NULL;
  268. kh_destroy_oid_pos(bitmap_git->ext_index.positions);
  269. bitmap_git->ext_index.positions = NULL;
  270. return -1;
  271. }
  272. static int open_pack_bitmap(struct repository *r,
  273. struct bitmap_index *bitmap_git)
  274. {
  275. struct packed_git *p;
  276. int ret = -1;
  277. assert(!bitmap_git->map);
  278. for (p = get_all_packs(r); p; p = p->next) {
  279. if (open_pack_bitmap_1(bitmap_git, p) == 0)
  280. ret = 0;
  281. }
  282. return ret;
  283. }
  284. struct bitmap_index *prepare_bitmap_git(struct repository *r)
  285. {
  286. struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
  287. if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
  288. return bitmap_git;
  289. free_bitmap_index(bitmap_git);
  290. return NULL;
  291. }
  292. struct include_data {
  293. struct bitmap_index *bitmap_git;
  294. struct bitmap *base;
  295. struct bitmap *seen;
  296. };
  297. static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
  298. const struct object_id *oid)
  299. {
  300. kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
  301. khiter_t pos = kh_get_oid_pos(positions, *oid);
  302. if (pos < kh_end(positions)) {
  303. int bitmap_pos = kh_value(positions, pos);
  304. return bitmap_pos + bitmap_git->pack->num_objects;
  305. }
  306. return -1;
  307. }
  308. static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
  309. const struct object_id *oid)
  310. {
  311. off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
  312. if (!offset)
  313. return -1;
  314. return find_revindex_position(bitmap_git->pack, offset);
  315. }
  316. static int bitmap_position(struct bitmap_index *bitmap_git,
  317. const struct object_id *oid)
  318. {
  319. int pos = bitmap_position_packfile(bitmap_git, oid);
  320. return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
  321. }
  322. static int ext_index_add_object(struct bitmap_index *bitmap_git,
  323. struct object *object, const char *name)
  324. {
  325. struct eindex *eindex = &bitmap_git->ext_index;
  326. khiter_t hash_pos;
  327. int hash_ret;
  328. int bitmap_pos;
  329. hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
  330. if (hash_ret > 0) {
  331. if (eindex->count >= eindex->alloc) {
  332. eindex->alloc = (eindex->alloc + 16) * 3 / 2;
  333. REALLOC_ARRAY(eindex->objects, eindex->alloc);
  334. REALLOC_ARRAY(eindex->hashes, eindex->alloc);
  335. }
  336. bitmap_pos = eindex->count;
  337. eindex->objects[eindex->count] = object;
  338. eindex->hashes[eindex->count] = pack_name_hash(name);
  339. kh_value(eindex->positions, hash_pos) = bitmap_pos;
  340. eindex->count++;
  341. } else {
  342. bitmap_pos = kh_value(eindex->positions, hash_pos);
  343. }
  344. return bitmap_pos + bitmap_git->pack->num_objects;
  345. }
  346. struct bitmap_show_data {
  347. struct bitmap_index *bitmap_git;
  348. struct bitmap *base;
  349. };
  350. static void show_object(struct object *object, const char *name, void *data_)
  351. {
  352. struct bitmap_show_data *data = data_;
  353. int bitmap_pos;
  354. bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
  355. if (bitmap_pos < 0)
  356. bitmap_pos = ext_index_add_object(data->bitmap_git, object,
  357. name);
  358. bitmap_set(data->base, bitmap_pos);
  359. }
  360. static void show_commit(struct commit *commit, void *data)
  361. {
  362. }
  363. static int add_to_include_set(struct bitmap_index *bitmap_git,
  364. struct include_data *data,
  365. const struct object_id *oid,
  366. int bitmap_pos)
  367. {
  368. khiter_t hash_pos;
  369. if (data->seen && bitmap_get(data->seen, bitmap_pos))
  370. return 0;
  371. if (bitmap_get(data->base, bitmap_pos))
  372. return 0;
  373. hash_pos = kh_get_oid_map(bitmap_git->bitmaps, *oid);
  374. if (hash_pos < kh_end(bitmap_git->bitmaps)) {
  375. struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, hash_pos);
  376. bitmap_or_ewah(data->base, lookup_stored_bitmap(st));
  377. return 0;
  378. }
  379. bitmap_set(data->base, bitmap_pos);
  380. return 1;
  381. }
  382. static int should_include(struct commit *commit, void *_data)
  383. {
  384. struct include_data *data = _data;
  385. int bitmap_pos;
  386. bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
  387. if (bitmap_pos < 0)
  388. bitmap_pos = ext_index_add_object(data->bitmap_git,
  389. (struct object *)commit,
  390. NULL);
  391. if (!add_to_include_set(data->bitmap_git, data, &commit->object.oid,
  392. bitmap_pos)) {
  393. struct commit_list *parent = commit->parents;
  394. while (parent) {
  395. parent->item->object.flags |= SEEN;
  396. parent = parent->next;
  397. }
  398. return 0;
  399. }
  400. return 1;
  401. }
  402. static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
  403. struct rev_info *revs,
  404. struct object_list *roots,
  405. struct bitmap *seen,
  406. struct list_objects_filter_options *filter)
  407. {
  408. struct bitmap *base = NULL;
  409. int needs_walk = 0;
  410. struct object_list *not_mapped = NULL;
  411. /*
  412. * Go through all the roots for the walk. The ones that have bitmaps
  413. * on the bitmap index will be `or`ed together to form an initial
  414. * global reachability analysis.
  415. *
  416. * The ones without bitmaps in the index will be stored in the
  417. * `not_mapped_list` for further processing.
  418. */
  419. while (roots) {
  420. struct object *object = roots->item;
  421. roots = roots->next;
  422. if (object->type == OBJ_COMMIT) {
  423. khiter_t pos = kh_get_oid_map(bitmap_git->bitmaps, object->oid);
  424. if (pos < kh_end(bitmap_git->bitmaps)) {
  425. struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos);
  426. struct ewah_bitmap *or_with = lookup_stored_bitmap(st);
  427. if (base == NULL)
  428. base = ewah_to_bitmap(or_with);
  429. else
  430. bitmap_or_ewah(base, or_with);
  431. object->flags |= SEEN;
  432. continue;
  433. }
  434. }
  435. object_list_insert(object, &not_mapped);
  436. }
  437. /*
  438. * Best case scenario: We found bitmaps for all the roots,
  439. * so the resulting `or` bitmap has the full reachability analysis
  440. */
  441. if (not_mapped == NULL)
  442. return base;
  443. roots = not_mapped;
  444. /*
  445. * Let's iterate through all the roots that don't have bitmaps to
  446. * check if we can determine them to be reachable from the existing
  447. * global bitmap.
  448. *
  449. * If we cannot find them in the existing global bitmap, we'll need
  450. * to push them to an actual walk and run it until we can confirm
  451. * they are reachable
  452. */
  453. while (roots) {
  454. struct object *object = roots->item;
  455. int pos;
  456. roots = roots->next;
  457. pos = bitmap_position(bitmap_git, &object->oid);
  458. if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
  459. object->flags &= ~UNINTERESTING;
  460. add_pending_object(revs, object, "");
  461. needs_walk = 1;
  462. } else {
  463. object->flags |= SEEN;
  464. }
  465. }
  466. if (needs_walk) {
  467. struct include_data incdata;
  468. struct bitmap_show_data show_data;
  469. if (base == NULL)
  470. base = bitmap_new();
  471. incdata.bitmap_git = bitmap_git;
  472. incdata.base = base;
  473. incdata.seen = seen;
  474. revs->include_check = should_include;
  475. revs->include_check_data = &incdata;
  476. if (prepare_revision_walk(revs))
  477. die("revision walk setup failed");
  478. show_data.bitmap_git = bitmap_git;
  479. show_data.base = base;
  480. traverse_commit_list_filtered(filter, revs,
  481. show_commit, show_object,
  482. &show_data, NULL);
  483. }
  484. return base;
  485. }
  486. static void show_extended_objects(struct bitmap_index *bitmap_git,
  487. struct rev_info *revs,
  488. show_reachable_fn show_reach)
  489. {
  490. struct bitmap *objects = bitmap_git->result;
  491. struct eindex *eindex = &bitmap_git->ext_index;
  492. uint32_t i;
  493. for (i = 0; i < eindex->count; ++i) {
  494. struct object *obj;
  495. if (!bitmap_get(objects, bitmap_git->pack->num_objects + i))
  496. continue;
  497. obj = eindex->objects[i];
  498. if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
  499. (obj->type == OBJ_TREE && !revs->tree_objects) ||
  500. (obj->type == OBJ_TAG && !revs->tag_objects))
  501. continue;
  502. show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
  503. }
  504. }
  505. static void init_type_iterator(struct ewah_iterator *it,
  506. struct bitmap_index *bitmap_git,
  507. enum object_type type)
  508. {
  509. switch (type) {
  510. case OBJ_COMMIT:
  511. ewah_iterator_init(it, bitmap_git->commits);
  512. break;
  513. case OBJ_TREE:
  514. ewah_iterator_init(it, bitmap_git->trees);
  515. break;
  516. case OBJ_BLOB:
  517. ewah_iterator_init(it, bitmap_git->blobs);
  518. break;
  519. case OBJ_TAG:
  520. ewah_iterator_init(it, bitmap_git->tags);
  521. break;
  522. default:
  523. BUG("object type %d not stored by bitmap type index", type);
  524. break;
  525. }
  526. }
  527. static void show_objects_for_type(
  528. struct bitmap_index *bitmap_git,
  529. enum object_type object_type,
  530. show_reachable_fn show_reach)
  531. {
  532. size_t i = 0;
  533. uint32_t offset;
  534. struct ewah_iterator it;
  535. eword_t filter;
  536. struct bitmap *objects = bitmap_git->result;
  537. init_type_iterator(&it, bitmap_git, object_type);
  538. for (i = 0; i < objects->word_alloc &&
  539. ewah_iterator_next(&filter, &it); i++) {
  540. eword_t word = objects->words[i] & filter;
  541. size_t pos = (i * BITS_IN_EWORD);
  542. if (!word)
  543. continue;
  544. for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
  545. struct object_id oid;
  546. struct revindex_entry *entry;
  547. uint32_t hash = 0;
  548. if ((word >> offset) == 0)
  549. break;
  550. offset += ewah_bit_ctz64(word >> offset);
  551. entry = &bitmap_git->pack->revindex[pos + offset];
  552. nth_packed_object_id(&oid, bitmap_git->pack, entry->nr);
  553. if (bitmap_git->hashes)
  554. hash = get_be32(bitmap_git->hashes + entry->nr);
  555. show_reach(&oid, object_type, 0, hash, bitmap_git->pack, entry->offset);
  556. }
  557. }
  558. }
  559. static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
  560. struct object_list *roots)
  561. {
  562. while (roots) {
  563. struct object *object = roots->item;
  564. roots = roots->next;
  565. if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
  566. return 1;
  567. }
  568. return 0;
  569. }
  570. static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
  571. struct object_list *tip_objects,
  572. enum object_type type)
  573. {
  574. struct bitmap *result = bitmap_new();
  575. struct object_list *p;
  576. for (p = tip_objects; p; p = p->next) {
  577. int pos;
  578. if (p->item->type != type)
  579. continue;
  580. pos = bitmap_position(bitmap_git, &p->item->oid);
  581. if (pos < 0)
  582. continue;
  583. bitmap_set(result, pos);
  584. }
  585. return result;
  586. }
  587. static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
  588. struct object_list *tip_objects,
  589. struct bitmap *to_filter,
  590. enum object_type type)
  591. {
  592. struct eindex *eindex = &bitmap_git->ext_index;
  593. struct bitmap *tips;
  594. struct ewah_iterator it;
  595. eword_t mask;
  596. uint32_t i;
  597. if (type != OBJ_BLOB && type != OBJ_TREE)
  598. BUG("filter_bitmap_exclude_type: unsupported type '%d'", type);
  599. /*
  600. * The non-bitmap version of this filter never removes
  601. * objects which the other side specifically asked for,
  602. * so we must match that behavior.
  603. */
  604. tips = find_tip_objects(bitmap_git, tip_objects, type);
  605. /*
  606. * We can use the blob type-bitmap to work in whole words
  607. * for the objects that are actually in the bitmapped packfile.
  608. */
  609. for (i = 0, init_type_iterator(&it, bitmap_git, type);
  610. i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
  611. i++) {
  612. if (i < tips->word_alloc)
  613. mask &= ~tips->words[i];
  614. to_filter->words[i] &= ~mask;
  615. }
  616. /*
  617. * Clear any blobs that weren't in the packfile (and so would not have
  618. * been caught by the loop above. We'll have to check them
  619. * individually.
  620. */
  621. for (i = 0; i < eindex->count; i++) {
  622. uint32_t pos = i + bitmap_git->pack->num_objects;
  623. if (eindex->objects[i]->type == type &&
  624. bitmap_get(to_filter, pos) &&
  625. !bitmap_get(tips, pos))
  626. bitmap_unset(to_filter, pos);
  627. }
  628. bitmap_free(tips);
  629. }
  630. static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
  631. struct object_list *tip_objects,
  632. struct bitmap *to_filter)
  633. {
  634. filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
  635. OBJ_BLOB);
  636. }
  637. static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
  638. uint32_t pos)
  639. {
  640. struct packed_git *pack = bitmap_git->pack;
  641. unsigned long size;
  642. struct object_info oi = OBJECT_INFO_INIT;
  643. oi.sizep = &size;
  644. if (pos < pack->num_objects) {
  645. struct revindex_entry *entry = &pack->revindex[pos];
  646. if (packed_object_info(the_repository, pack,
  647. entry->offset, &oi) < 0) {
  648. struct object_id oid;
  649. nth_packed_object_id(&oid, pack, entry->nr);
  650. die(_("unable to get size of %s"), oid_to_hex(&oid));
  651. }
  652. } else {
  653. struct eindex *eindex = &bitmap_git->ext_index;
  654. struct object *obj = eindex->objects[pos - pack->num_objects];
  655. if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
  656. die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
  657. }
  658. return size;
  659. }
  660. static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
  661. struct object_list *tip_objects,
  662. struct bitmap *to_filter,
  663. unsigned long limit)
  664. {
  665. struct eindex *eindex = &bitmap_git->ext_index;
  666. struct bitmap *tips;
  667. struct ewah_iterator it;
  668. eword_t mask;
  669. uint32_t i;
  670. tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
  671. for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
  672. i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
  673. i++) {
  674. eword_t word = to_filter->words[i] & mask;
  675. unsigned offset;
  676. for (offset = 0; offset < BITS_IN_EWORD; offset++) {
  677. uint32_t pos;
  678. if ((word >> offset) == 0)
  679. break;
  680. offset += ewah_bit_ctz64(word >> offset);
  681. pos = i * BITS_IN_EWORD + offset;
  682. if (!bitmap_get(tips, pos) &&
  683. get_size_by_pos(bitmap_git, pos) >= limit)
  684. bitmap_unset(to_filter, pos);
  685. }
  686. }
  687. for (i = 0; i < eindex->count; i++) {
  688. uint32_t pos = i + bitmap_git->pack->num_objects;
  689. if (eindex->objects[i]->type == OBJ_BLOB &&
  690. bitmap_get(to_filter, pos) &&
  691. !bitmap_get(tips, pos) &&
  692. get_size_by_pos(bitmap_git, pos) >= limit)
  693. bitmap_unset(to_filter, pos);
  694. }
  695. bitmap_free(tips);
  696. }
  697. static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
  698. struct object_list *tip_objects,
  699. struct bitmap *to_filter,
  700. unsigned long limit)
  701. {
  702. if (limit)
  703. BUG("filter_bitmap_tree_depth given non-zero limit");
  704. filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
  705. OBJ_TREE);
  706. filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
  707. OBJ_BLOB);
  708. }
  709. static int filter_bitmap(struct bitmap_index *bitmap_git,
  710. struct object_list *tip_objects,
  711. struct bitmap *to_filter,
  712. struct list_objects_filter_options *filter)
  713. {
  714. if (!filter || filter->choice == LOFC_DISABLED)
  715. return 0;
  716. if (filter->choice == LOFC_BLOB_NONE) {
  717. if (bitmap_git)
  718. filter_bitmap_blob_none(bitmap_git, tip_objects,
  719. to_filter);
  720. return 0;
  721. }
  722. if (filter->choice == LOFC_BLOB_LIMIT) {
  723. if (bitmap_git)
  724. filter_bitmap_blob_limit(bitmap_git, tip_objects,
  725. to_filter,
  726. filter->blob_limit_value);
  727. return 0;
  728. }
  729. if (filter->choice == LOFC_TREE_DEPTH &&
  730. filter->tree_exclude_depth == 0) {
  731. if (bitmap_git)
  732. filter_bitmap_tree_depth(bitmap_git, tip_objects,
  733. to_filter,
  734. filter->tree_exclude_depth);
  735. return 0;
  736. }
  737. /* filter choice not handled */
  738. return -1;
  739. }
  740. static int can_filter_bitmap(struct list_objects_filter_options *filter)
  741. {
  742. return !filter_bitmap(NULL, NULL, NULL, filter);
  743. }
  744. struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
  745. struct list_objects_filter_options *filter)
  746. {
  747. unsigned int i;
  748. struct object_list *wants = NULL;
  749. struct object_list *haves = NULL;
  750. struct bitmap *wants_bitmap = NULL;
  751. struct bitmap *haves_bitmap = NULL;
  752. struct bitmap_index *bitmap_git;
  753. /*
  754. * We can't do pathspec limiting with bitmaps, because we don't know
  755. * which commits are associated with which object changes (let alone
  756. * even which objects are associated with which paths).
  757. */
  758. if (revs->prune)
  759. return NULL;
  760. if (!can_filter_bitmap(filter))
  761. return NULL;
  762. /* try to open a bitmapped pack, but don't parse it yet
  763. * because we may not need to use it */
  764. bitmap_git = xcalloc(1, sizeof(*bitmap_git));
  765. if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
  766. goto cleanup;
  767. for (i = 0; i < revs->pending.nr; ++i) {
  768. struct object *object = revs->pending.objects[i].item;
  769. if (object->type == OBJ_NONE)
  770. parse_object_or_die(&object->oid, NULL);
  771. while (object->type == OBJ_TAG) {
  772. struct tag *tag = (struct tag *) object;
  773. if (object->flags & UNINTERESTING)
  774. object_list_insert(object, &haves);
  775. else
  776. object_list_insert(object, &wants);
  777. object = parse_object_or_die(get_tagged_oid(tag), NULL);
  778. }
  779. if (object->flags & UNINTERESTING)
  780. object_list_insert(object, &haves);
  781. else
  782. object_list_insert(object, &wants);
  783. }
  784. /*
  785. * if we have a HAVES list, but none of those haves is contained
  786. * in the packfile that has a bitmap, we don't have anything to
  787. * optimize here
  788. */
  789. if (haves && !in_bitmapped_pack(bitmap_git, haves))
  790. goto cleanup;
  791. /* if we don't want anything, we're done here */
  792. if (!wants)
  793. goto cleanup;
  794. /*
  795. * now we're going to use bitmaps, so load the actual bitmap entries
  796. * from disk. this is the point of no return; after this the rev_list
  797. * becomes invalidated and we must perform the revwalk through bitmaps
  798. */
  799. if (load_pack_bitmap(bitmap_git) < 0)
  800. goto cleanup;
  801. object_array_clear(&revs->pending);
  802. if (haves) {
  803. revs->ignore_missing_links = 1;
  804. haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
  805. filter);
  806. reset_revision_walk();
  807. revs->ignore_missing_links = 0;
  808. if (haves_bitmap == NULL)
  809. BUG("failed to perform bitmap walk");
  810. }
  811. wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
  812. filter);
  813. if (!wants_bitmap)
  814. BUG("failed to perform bitmap walk");
  815. if (haves_bitmap)
  816. bitmap_and_not(wants_bitmap, haves_bitmap);
  817. filter_bitmap(bitmap_git, wants, wants_bitmap, filter);
  818. bitmap_git->result = wants_bitmap;
  819. bitmap_git->haves = haves_bitmap;
  820. object_list_free(&wants);
  821. object_list_free(&haves);
  822. return bitmap_git;
  823. cleanup:
  824. free_bitmap_index(bitmap_git);
  825. object_list_free(&wants);
  826. object_list_free(&haves);
  827. return NULL;
  828. }
  829. static void try_partial_reuse(struct bitmap_index *bitmap_git,
  830. size_t pos,
  831. struct bitmap *reuse,
  832. struct pack_window **w_curs)
  833. {
  834. struct revindex_entry *revidx;
  835. off_t offset;
  836. enum object_type type;
  837. unsigned long size;
  838. if (pos >= bitmap_git->pack->num_objects)
  839. return; /* not actually in the pack */
  840. revidx = &bitmap_git->pack->revindex[pos];
  841. offset = revidx->offset;
  842. type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
  843. if (type < 0)
  844. return; /* broken packfile, punt */
  845. if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
  846. off_t base_offset;
  847. int base_pos;
  848. /*
  849. * Find the position of the base object so we can look it up
  850. * in our bitmaps. If we can't come up with an offset, or if
  851. * that offset is not in the revidx, the pack is corrupt.
  852. * There's nothing we can do, so just punt on this object,
  853. * and the normal slow path will complain about it in
  854. * more detail.
  855. */
  856. base_offset = get_delta_base(bitmap_git->pack, w_curs,
  857. &offset, type, revidx->offset);
  858. if (!base_offset)
  859. return;
  860. base_pos = find_revindex_position(bitmap_git->pack, base_offset);
  861. if (base_pos < 0)
  862. return;
  863. /*
  864. * We assume delta dependencies always point backwards. This
  865. * lets us do a single pass, and is basically always true
  866. * due to the way OFS_DELTAs work. You would not typically
  867. * find REF_DELTA in a bitmapped pack, since we only bitmap
  868. * packs we write fresh, and OFS_DELTA is the default). But
  869. * let's double check to make sure the pack wasn't written with
  870. * odd parameters.
  871. */
  872. if (base_pos >= pos)
  873. return;
  874. /*
  875. * And finally, if we're not sending the base as part of our
  876. * reuse chunk, then don't send this object either. The base
  877. * would come after us, along with other objects not
  878. * necessarily in the pack, which means we'd need to convert
  879. * to REF_DELTA on the fly. Better to just let the normal
  880. * object_entry code path handle it.
  881. */
  882. if (!bitmap_get(reuse, base_pos))
  883. return;
  884. }
  885. /*
  886. * If we got here, then the object is OK to reuse. Mark it.
  887. */
  888. bitmap_set(reuse, pos);
  889. }
  890. int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
  891. struct packed_git **packfile_out,
  892. uint32_t *entries,
  893. struct bitmap **reuse_out)
  894. {
  895. struct bitmap *result = bitmap_git->result;
  896. struct bitmap *reuse;
  897. struct pack_window *w_curs = NULL;
  898. size_t i = 0;
  899. uint32_t offset;
  900. assert(result);
  901. while (i < result->word_alloc && result->words[i] == (eword_t)~0)
  902. i++;
  903. /* Don't mark objects not in the packfile */
  904. if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
  905. i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
  906. reuse = bitmap_word_alloc(i);
  907. memset(reuse->words, 0xFF, i * sizeof(eword_t));
  908. for (; i < result->word_alloc; ++i) {
  909. eword_t word = result->words[i];
  910. size_t pos = (i * BITS_IN_EWORD);
  911. for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
  912. if ((word >> offset) == 0)
  913. break;
  914. offset += ewah_bit_ctz64(word >> offset);
  915. try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
  916. }
  917. }
  918. unuse_pack(&w_curs);
  919. *entries = bitmap_popcount(reuse);
  920. if (!*entries) {
  921. bitmap_free(reuse);
  922. return -1;
  923. }
  924. /*
  925. * Drop any reused objects from the result, since they will not
  926. * need to be handled separately.
  927. */
  928. bitmap_and_not(result, reuse);
  929. *packfile_out = bitmap_git->pack;
  930. *reuse_out = reuse;
  931. return 0;
  932. }
  933. int bitmap_walk_contains(struct bitmap_index *bitmap_git,
  934. struct bitmap *bitmap, const struct object_id *oid)
  935. {
  936. int idx;
  937. if (!bitmap)
  938. return 0;
  939. idx = bitmap_position(bitmap_git, oid);
  940. return idx >= 0 && bitmap_get(bitmap, idx);
  941. }
  942. void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
  943. struct rev_info *revs,
  944. show_reachable_fn show_reachable)
  945. {
  946. assert(bitmap_git->result);
  947. show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
  948. if (revs->tree_objects)
  949. show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
  950. if (revs->blob_objects)
  951. show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
  952. if (revs->tag_objects)
  953. show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
  954. show_extended_objects(bitmap_git, revs, show_reachable);
  955. }
  956. static uint32_t count_object_type(struct bitmap_index *bitmap_git,
  957. enum object_type type)
  958. {
  959. struct bitmap *objects = bitmap_git->result;
  960. struct eindex *eindex = &bitmap_git->ext_index;
  961. uint32_t i = 0, count = 0;
  962. struct ewah_iterator it;
  963. eword_t filter;
  964. init_type_iterator(&it, bitmap_git, type);
  965. while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
  966. eword_t word = objects->words[i++] & filter;
  967. count += ewah_bit_popcount64(word);
  968. }
  969. for (i = 0; i < eindex->count; ++i) {
  970. if (eindex->objects[i]->type == type &&
  971. bitmap_get(objects, bitmap_git->pack->num_objects + i))
  972. count++;
  973. }
  974. return count;
  975. }
  976. void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
  977. uint32_t *commits, uint32_t *trees,
  978. uint32_t *blobs, uint32_t *tags)
  979. {
  980. assert(bitmap_git->result);
  981. if (commits)
  982. *commits = count_object_type(bitmap_git, OBJ_COMMIT);
  983. if (trees)
  984. *trees = count_object_type(bitmap_git, OBJ_TREE);
  985. if (blobs)
  986. *blobs = count_object_type(bitmap_git, OBJ_BLOB);
  987. if (tags)
  988. *tags = count_object_type(bitmap_git, OBJ_TAG);
  989. }
  990. struct bitmap_test_data {
  991. struct bitmap_index *bitmap_git;
  992. struct bitmap *base;
  993. struct progress *prg;
  994. size_t seen;
  995. };
  996. static void test_show_object(struct object *object, const char *name,
  997. void *data)
  998. {
  999. struct bitmap_test_data *tdata = data;
  1000. int bitmap_pos;
  1001. bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
  1002. if (bitmap_pos < 0)
  1003. die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
  1004. bitmap_set(tdata->base, bitmap_pos);
  1005. display_progress(tdata->prg, ++tdata->seen);
  1006. }
  1007. static void test_show_commit(struct commit *commit, void *data)
  1008. {
  1009. struct bitmap_test_data *tdata = data;
  1010. int bitmap_pos;
  1011. bitmap_pos = bitmap_position(tdata->bitmap_git,
  1012. &commit->object.oid);
  1013. if (bitmap_pos < 0)
  1014. die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
  1015. bitmap_set(tdata->base, bitmap_pos);
  1016. display_progress(tdata->prg, ++tdata->seen);
  1017. }
  1018. void test_bitmap_walk(struct rev_info *revs)
  1019. {
  1020. struct object *root;
  1021. struct bitmap *result = NULL;
  1022. khiter_t pos;
  1023. size_t result_popcnt;
  1024. struct bitmap_test_data tdata;
  1025. struct bitmap_index *bitmap_git;
  1026. if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
  1027. die("failed to load bitmap indexes");
  1028. if (revs->pending.nr != 1)
  1029. die("you must specify exactly one commit to test");
  1030. fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
  1031. bitmap_git->version, bitmap_git->entry_count);
  1032. root = revs->pending.objects[0].item;
  1033. pos = kh_get_oid_map(bitmap_git->bitmaps, root->oid);
  1034. if (pos < kh_end(bitmap_git->bitmaps)) {
  1035. struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos);
  1036. struct ewah_bitmap *bm = lookup_stored_bitmap(st);
  1037. fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
  1038. oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
  1039. result = ewah_to_bitmap(bm);
  1040. }
  1041. if (result == NULL)
  1042. die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
  1043. revs->tag_objects = 1;
  1044. revs->tree_objects = 1;
  1045. revs->blob_objects = 1;
  1046. result_popcnt = bitmap_popcount(result);
  1047. if (prepare_revision_walk(revs))
  1048. die("revision walk setup failed");
  1049. tdata.bitmap_git = bitmap_git;
  1050. tdata.base = bitmap_new();
  1051. tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
  1052. tdata.seen = 0;
  1053. traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
  1054. stop_progress(&tdata.prg);
  1055. if (bitmap_equals(result, tdata.base))
  1056. fprintf(stderr, "OK!\n");
  1057. else
  1058. fprintf(stderr, "Mismatch!\n");
  1059. free_bitmap_index(bitmap_git);
  1060. }
  1061. static int rebuild_bitmap(uint32_t *reposition,
  1062. struct ewah_bitmap *source,
  1063. struct bitmap *dest)
  1064. {
  1065. uint32_t pos = 0;
  1066. struct ewah_iterator it;
  1067. eword_t word;
  1068. ewah_iterator_init(&it, source);
  1069. while (ewah_iterator_next(&word, &it)) {
  1070. uint32_t offset, bit_pos;
  1071. for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
  1072. if ((word >> offset) == 0)
  1073. break;
  1074. offset += ewah_bit_ctz64(word >> offset);
  1075. bit_pos = reposition[pos + offset];
  1076. if (bit_pos > 0)
  1077. bitmap_set(dest, bit_pos - 1);
  1078. else /* can't reuse, we don't have the object */
  1079. return -1;
  1080. }
  1081. pos += BITS_IN_EWORD;
  1082. }
  1083. return 0;
  1084. }
  1085. int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git,
  1086. struct packing_data *mapping,
  1087. kh_oid_map_t *reused_bitmaps,
  1088. int show_progress)
  1089. {
  1090. uint32_t i, num_objects;
  1091. uint32_t *reposition;
  1092. struct bitmap *rebuild;
  1093. struct stored_bitmap *stored;
  1094. struct progress *progress = NULL;
  1095. khiter_t hash_pos;
  1096. int hash_ret;
  1097. num_objects = bitmap_git->pack->num_objects;
  1098. reposition = xcalloc(num_objects, sizeof(uint32_t));
  1099. for (i = 0; i < num_objects; ++i) {
  1100. struct object_id oid;
  1101. struct revindex_entry *entry;
  1102. struct object_entry *oe;
  1103. entry = &bitmap_git->pack->revindex[i];
  1104. nth_packed_object_id(&oid, bitmap_git->pack, entry->nr);
  1105. oe = packlist_find(mapping, &oid);
  1106. if (oe)
  1107. reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
  1108. }
  1109. rebuild = bitmap_new();
  1110. i = 0;
  1111. if (show_progress)
  1112. progress = start_progress("Reusing bitmaps", 0);
  1113. kh_foreach_value(bitmap_git->bitmaps, stored, {
  1114. if (stored->flags & BITMAP_FLAG_REUSE) {
  1115. if (!rebuild_bitmap(reposition,
  1116. lookup_stored_bitmap(stored),
  1117. rebuild)) {
  1118. hash_pos = kh_put_oid_map(reused_bitmaps,
  1119. stored->oid,
  1120. &hash_ret);
  1121. kh_value(reused_bitmaps, hash_pos) =
  1122. bitmap_to_ewah(rebuild);
  1123. }
  1124. bitmap_reset(rebuild);
  1125. display_progress(progress, ++i);
  1126. }
  1127. });
  1128. stop_progress(&progress);
  1129. free(reposition);
  1130. bitmap_free(rebuild);
  1131. return 0;
  1132. }
  1133. void free_bitmap_index(struct bitmap_index *b)
  1134. {
  1135. if (!b)
  1136. return;
  1137. if (b->map)
  1138. munmap(b->map, b->map_size);
  1139. ewah_pool_free(b->commits);
  1140. ewah_pool_free(b->trees);
  1141. ewah_pool_free(b->blobs);
  1142. ewah_pool_free(b->tags);
  1143. kh_destroy_oid_map(b->bitmaps);
  1144. free(b->ext_index.objects);
  1145. free(b->ext_index.hashes);
  1146. bitmap_free(b->result);
  1147. bitmap_free(b->haves);
  1148. free(b);
  1149. }
  1150. int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
  1151. const struct object_id *oid)
  1152. {
  1153. return bitmap_git &&
  1154. bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
  1155. }