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.

2121 lines
54KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "dir.h"
  4. #include "git-compat-util.h"
  5. #include "lockfile.h"
  6. #include "pack.h"
  7. #include "packfile.h"
  8. #include "commit.h"
  9. #include "object.h"
  10. #include "refs.h"
  11. #include "revision.h"
  12. #include "sha1-lookup.h"
  13. #include "commit-graph.h"
  14. #include "object-store.h"
  15. #include "alloc.h"
  16. #include "hashmap.h"
  17. #include "replace-object.h"
  18. #include "progress.h"
  19. #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
  20. #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
  21. #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
  22. #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
  23. #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
  24. #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
  25. #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
  26. #define GRAPH_VERSION_1 0x1
  27. #define GRAPH_VERSION GRAPH_VERSION_1
  28. #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
  29. #define GRAPH_EDGE_LAST_MASK 0x7fffffff
  30. #define GRAPH_PARENT_NONE 0x70000000
  31. #define GRAPH_LAST_EDGE 0x80000000
  32. #define GRAPH_HEADER_SIZE 8
  33. #define GRAPH_FANOUT_SIZE (4 * 256)
  34. #define GRAPH_CHUNKLOOKUP_WIDTH 12
  35. #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
  36. + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
  37. char *get_commit_graph_filename(const char *obj_dir)
  38. {
  39. char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
  40. char *normalized = xmalloc(strlen(filename) + 1);
  41. normalize_path_copy(normalized, filename);
  42. free(filename);
  43. return normalized;
  44. }
  45. static char *get_split_graph_filename(const char *obj_dir,
  46. const char *oid_hex)
  47. {
  48. char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
  49. obj_dir,
  50. oid_hex);
  51. char *normalized = xmalloc(strlen(filename) + 1);
  52. normalize_path_copy(normalized, filename);
  53. free(filename);
  54. return normalized;
  55. }
  56. static char *get_chain_filename(const char *obj_dir)
  57. {
  58. return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
  59. }
  60. static uint8_t oid_version(void)
  61. {
  62. return 1;
  63. }
  64. static struct commit_graph *alloc_commit_graph(void)
  65. {
  66. struct commit_graph *g = xcalloc(1, sizeof(*g));
  67. g->graph_fd = -1;
  68. return g;
  69. }
  70. extern int read_replace_refs;
  71. static int commit_graph_compatible(struct repository *r)
  72. {
  73. if (!r->gitdir)
  74. return 0;
  75. if (read_replace_refs) {
  76. prepare_replace_object(r);
  77. if (hashmap_get_size(&r->objects->replace_map->map))
  78. return 0;
  79. }
  80. prepare_commit_graft(r);
  81. if (r->parsed_objects && r->parsed_objects->grafts_nr)
  82. return 0;
  83. if (is_repository_shallow(r))
  84. return 0;
  85. return 1;
  86. }
  87. int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
  88. {
  89. *fd = git_open(graph_file);
  90. if (*fd < 0)
  91. return 0;
  92. if (fstat(*fd, st)) {
  93. close(*fd);
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
  99. {
  100. void *graph_map;
  101. size_t graph_size;
  102. struct commit_graph *ret;
  103. graph_size = xsize_t(st->st_size);
  104. if (graph_size < GRAPH_MIN_SIZE) {
  105. close(fd);
  106. error(_("commit-graph file is too small"));
  107. return NULL;
  108. }
  109. graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
  110. ret = parse_commit_graph(graph_map, fd, graph_size);
  111. if (!ret) {
  112. munmap(graph_map, graph_size);
  113. close(fd);
  114. }
  115. return ret;
  116. }
  117. static int verify_commit_graph_lite(struct commit_graph *g)
  118. {
  119. /*
  120. * Basic validation shared between parse_commit_graph()
  121. * which'll be called every time the graph is used, and the
  122. * much more expensive verify_commit_graph() used by
  123. * "commit-graph verify".
  124. *
  125. * There should only be very basic checks here to ensure that
  126. * we don't e.g. segfault in fill_commit_in_graph(), but
  127. * because this is a very hot codepath nothing that e.g. loops
  128. * over g->num_commits, or runs a checksum on the commit-graph
  129. * itself.
  130. */
  131. if (!g->chunk_oid_fanout) {
  132. error("commit-graph is missing the OID Fanout chunk");
  133. return 1;
  134. }
  135. if (!g->chunk_oid_lookup) {
  136. error("commit-graph is missing the OID Lookup chunk");
  137. return 1;
  138. }
  139. if (!g->chunk_commit_data) {
  140. error("commit-graph is missing the Commit Data chunk");
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. struct commit_graph *parse_commit_graph(void *graph_map, int fd,
  146. size_t graph_size)
  147. {
  148. const unsigned char *data, *chunk_lookup;
  149. uint32_t i;
  150. struct commit_graph *graph;
  151. uint64_t last_chunk_offset;
  152. uint32_t last_chunk_id;
  153. uint32_t graph_signature;
  154. unsigned char graph_version, hash_version;
  155. if (!graph_map)
  156. return NULL;
  157. if (graph_size < GRAPH_MIN_SIZE)
  158. return NULL;
  159. data = (const unsigned char *)graph_map;
  160. graph_signature = get_be32(data);
  161. if (graph_signature != GRAPH_SIGNATURE) {
  162. error(_("commit-graph signature %X does not match signature %X"),
  163. graph_signature, GRAPH_SIGNATURE);
  164. return NULL;
  165. }
  166. graph_version = *(unsigned char*)(data + 4);
  167. if (graph_version != GRAPH_VERSION) {
  168. error(_("commit-graph version %X does not match version %X"),
  169. graph_version, GRAPH_VERSION);
  170. return NULL;
  171. }
  172. hash_version = *(unsigned char*)(data + 5);
  173. if (hash_version != oid_version()) {
  174. error(_("commit-graph hash version %X does not match version %X"),
  175. hash_version, oid_version());
  176. return NULL;
  177. }
  178. graph = alloc_commit_graph();
  179. graph->hash_len = the_hash_algo->rawsz;
  180. graph->num_chunks = *(unsigned char*)(data + 6);
  181. graph->graph_fd = fd;
  182. graph->data = graph_map;
  183. graph->data_len = graph_size;
  184. last_chunk_id = 0;
  185. last_chunk_offset = 8;
  186. chunk_lookup = data + 8;
  187. for (i = 0; i < graph->num_chunks; i++) {
  188. uint32_t chunk_id;
  189. uint64_t chunk_offset;
  190. int chunk_repeated = 0;
  191. if (data + graph_size - chunk_lookup <
  192. GRAPH_CHUNKLOOKUP_WIDTH) {
  193. error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
  194. free(graph);
  195. return NULL;
  196. }
  197. chunk_id = get_be32(chunk_lookup + 0);
  198. chunk_offset = get_be64(chunk_lookup + 4);
  199. chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
  200. if (chunk_offset > graph_size - the_hash_algo->rawsz) {
  201. error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
  202. (uint32_t)chunk_offset);
  203. free(graph);
  204. return NULL;
  205. }
  206. switch (chunk_id) {
  207. case GRAPH_CHUNKID_OIDFANOUT:
  208. if (graph->chunk_oid_fanout)
  209. chunk_repeated = 1;
  210. else
  211. graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
  212. break;
  213. case GRAPH_CHUNKID_OIDLOOKUP:
  214. if (graph->chunk_oid_lookup)
  215. chunk_repeated = 1;
  216. else
  217. graph->chunk_oid_lookup = data + chunk_offset;
  218. break;
  219. case GRAPH_CHUNKID_DATA:
  220. if (graph->chunk_commit_data)
  221. chunk_repeated = 1;
  222. else
  223. graph->chunk_commit_data = data + chunk_offset;
  224. break;
  225. case GRAPH_CHUNKID_EXTRAEDGES:
  226. if (graph->chunk_extra_edges)
  227. chunk_repeated = 1;
  228. else
  229. graph->chunk_extra_edges = data + chunk_offset;
  230. break;
  231. case GRAPH_CHUNKID_BASE:
  232. if (graph->chunk_base_graphs)
  233. chunk_repeated = 1;
  234. else
  235. graph->chunk_base_graphs = data + chunk_offset;
  236. }
  237. if (chunk_repeated) {
  238. error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
  239. free(graph);
  240. return NULL;
  241. }
  242. if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
  243. {
  244. graph->num_commits = (chunk_offset - last_chunk_offset)
  245. / graph->hash_len;
  246. }
  247. last_chunk_id = chunk_id;
  248. last_chunk_offset = chunk_offset;
  249. }
  250. hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
  251. if (verify_commit_graph_lite(graph)) {
  252. free(graph);
  253. return NULL;
  254. }
  255. return graph;
  256. }
  257. static struct commit_graph *load_commit_graph_one(const char *graph_file)
  258. {
  259. struct stat st;
  260. int fd;
  261. struct commit_graph *g;
  262. int open_ok = open_commit_graph(graph_file, &fd, &st);
  263. if (!open_ok)
  264. return NULL;
  265. g = load_commit_graph_one_fd_st(fd, &st);
  266. if (g)
  267. g->filename = xstrdup(graph_file);
  268. return g;
  269. }
  270. static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
  271. {
  272. char *graph_name = get_commit_graph_filename(obj_dir);
  273. struct commit_graph *g = load_commit_graph_one(graph_name);
  274. free(graph_name);
  275. if (g)
  276. g->obj_dir = obj_dir;
  277. return g;
  278. }
  279. static int add_graph_to_chain(struct commit_graph *g,
  280. struct commit_graph *chain,
  281. struct object_id *oids,
  282. int n)
  283. {
  284. struct commit_graph *cur_g = chain;
  285. if (n && !g->chunk_base_graphs) {
  286. warning(_("commit-graph has no base graphs chunk"));
  287. return 0;
  288. }
  289. while (n) {
  290. n--;
  291. if (!cur_g ||
  292. !oideq(&oids[n], &cur_g->oid) ||
  293. !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
  294. warning(_("commit-graph chain does not match"));
  295. return 0;
  296. }
  297. cur_g = cur_g->base_graph;
  298. }
  299. g->base_graph = chain;
  300. if (chain)
  301. g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
  302. return 1;
  303. }
  304. static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
  305. {
  306. struct commit_graph *graph_chain = NULL;
  307. struct strbuf line = STRBUF_INIT;
  308. struct stat st;
  309. struct object_id *oids;
  310. int i = 0, valid = 1, count;
  311. char *chain_name = get_chain_filename(obj_dir);
  312. FILE *fp;
  313. int stat_res;
  314. fp = fopen(chain_name, "r");
  315. stat_res = stat(chain_name, &st);
  316. free(chain_name);
  317. if (!fp ||
  318. stat_res ||
  319. st.st_size <= the_hash_algo->hexsz)
  320. return NULL;
  321. count = st.st_size / (the_hash_algo->hexsz + 1);
  322. oids = xcalloc(count, sizeof(struct object_id));
  323. prepare_alt_odb(r);
  324. for (i = 0; i < count; i++) {
  325. struct object_directory *odb;
  326. if (strbuf_getline_lf(&line, fp) == EOF)
  327. break;
  328. if (get_oid_hex(line.buf, &oids[i])) {
  329. warning(_("invalid commit-graph chain: line '%s' not a hash"),
  330. line.buf);
  331. valid = 0;
  332. break;
  333. }
  334. valid = 0;
  335. for (odb = r->objects->odb; odb; odb = odb->next) {
  336. char *graph_name = get_split_graph_filename(odb->path, line.buf);
  337. struct commit_graph *g = load_commit_graph_one(graph_name);
  338. free(graph_name);
  339. if (g) {
  340. g->obj_dir = odb->path;
  341. if (add_graph_to_chain(g, graph_chain, oids, i)) {
  342. graph_chain = g;
  343. valid = 1;
  344. }
  345. break;
  346. }
  347. }
  348. if (!valid) {
  349. warning(_("unable to find all commit-graph files"));
  350. break;
  351. }
  352. }
  353. free(oids);
  354. fclose(fp);
  355. strbuf_release(&line);
  356. return graph_chain;
  357. }
  358. struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
  359. {
  360. struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
  361. if (!g)
  362. g = load_commit_graph_chain(r, obj_dir);
  363. return g;
  364. }
  365. static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
  366. {
  367. if (r->objects->commit_graph)
  368. return;
  369. r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
  370. }
  371. /*
  372. * Return 1 if commit_graph is non-NULL, and 0 otherwise.
  373. *
  374. * On the first invocation, this function attemps to load the commit
  375. * graph if the_repository is configured to have one.
  376. */
  377. static int prepare_commit_graph(struct repository *r)
  378. {
  379. struct object_directory *odb;
  380. /*
  381. * This must come before the "already attempted?" check below, because
  382. * we want to disable even an already-loaded graph file.
  383. */
  384. if (r->commit_graph_disabled)
  385. return 0;
  386. if (r->objects->commit_graph_attempted)
  387. return !!r->objects->commit_graph;
  388. r->objects->commit_graph_attempted = 1;
  389. if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
  390. die("dying as requested by the '%s' variable on commit-graph load!",
  391. GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
  392. prepare_repo_settings(r);
  393. if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
  394. r->settings.core_commit_graph != 1)
  395. /*
  396. * This repository is not configured to use commit graphs, so
  397. * do not load one. (But report commit_graph_attempted anyway
  398. * so that commit graph loading is not attempted again for this
  399. * repository.)
  400. */
  401. return 0;
  402. if (!commit_graph_compatible(r))
  403. return 0;
  404. prepare_alt_odb(r);
  405. for (odb = r->objects->odb;
  406. !r->objects->commit_graph && odb;
  407. odb = odb->next)
  408. prepare_commit_graph_one(r, odb->path);
  409. return !!r->objects->commit_graph;
  410. }
  411. int generation_numbers_enabled(struct repository *r)
  412. {
  413. uint32_t first_generation;
  414. struct commit_graph *g;
  415. if (!prepare_commit_graph(r))
  416. return 0;
  417. g = r->objects->commit_graph;
  418. if (!g->num_commits)
  419. return 0;
  420. first_generation = get_be32(g->chunk_commit_data +
  421. g->hash_len + 8) >> 2;
  422. return !!first_generation;
  423. }
  424. static void close_commit_graph_one(struct commit_graph *g)
  425. {
  426. if (!g)
  427. return;
  428. close_commit_graph_one(g->base_graph);
  429. free_commit_graph(g);
  430. }
  431. void close_commit_graph(struct raw_object_store *o)
  432. {
  433. close_commit_graph_one(o->commit_graph);
  434. o->commit_graph = NULL;
  435. }
  436. static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
  437. {
  438. return bsearch_hash(oid->hash, g->chunk_oid_fanout,
  439. g->chunk_oid_lookup, g->hash_len, pos);
  440. }
  441. static void load_oid_from_graph(struct commit_graph *g,
  442. uint32_t pos,
  443. struct object_id *oid)
  444. {
  445. uint32_t lex_index;
  446. while (g && pos < g->num_commits_in_base)
  447. g = g->base_graph;
  448. if (!g)
  449. BUG("NULL commit-graph");
  450. if (pos >= g->num_commits + g->num_commits_in_base)
  451. die(_("invalid commit position. commit-graph is likely corrupt"));
  452. lex_index = pos - g->num_commits_in_base;
  453. hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
  454. }
  455. static struct commit_list **insert_parent_or_die(struct repository *r,
  456. struct commit_graph *g,
  457. uint32_t pos,
  458. struct commit_list **pptr)
  459. {
  460. struct commit *c;
  461. struct object_id oid;
  462. if (pos >= g->num_commits + g->num_commits_in_base)
  463. die("invalid parent position %"PRIu32, pos);
  464. load_oid_from_graph(g, pos, &oid);
  465. c = lookup_commit(r, &oid);
  466. if (!c)
  467. die(_("could not find commit %s"), oid_to_hex(&oid));
  468. c->graph_pos = pos;
  469. return &commit_list_insert(c, pptr)->next;
  470. }
  471. static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
  472. {
  473. const unsigned char *commit_data;
  474. uint32_t lex_index;
  475. while (pos < g->num_commits_in_base)
  476. g = g->base_graph;
  477. lex_index = pos - g->num_commits_in_base;
  478. commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
  479. item->graph_pos = pos;
  480. item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
  481. }
  482. static inline void set_commit_tree(struct commit *c, struct tree *t)
  483. {
  484. c->maybe_tree = t;
  485. }
  486. static int fill_commit_in_graph(struct repository *r,
  487. struct commit *item,
  488. struct commit_graph *g, uint32_t pos)
  489. {
  490. uint32_t edge_value;
  491. uint32_t *parent_data_ptr;
  492. uint64_t date_low, date_high;
  493. struct commit_list **pptr;
  494. const unsigned char *commit_data;
  495. uint32_t lex_index;
  496. while (pos < g->num_commits_in_base)
  497. g = g->base_graph;
  498. if (pos >= g->num_commits + g->num_commits_in_base)
  499. die(_("invalid commit position. commit-graph is likely corrupt"));
  500. /*
  501. * Store the "full" position, but then use the
  502. * "local" position for the rest of the calculation.
  503. */
  504. item->graph_pos = pos;
  505. lex_index = pos - g->num_commits_in_base;
  506. commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
  507. item->object.parsed = 1;
  508. set_commit_tree(item, NULL);
  509. date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
  510. date_low = get_be32(commit_data + g->hash_len + 12);
  511. item->date = (timestamp_t)((date_high << 32) | date_low);
  512. item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
  513. pptr = &item->parents;
  514. edge_value = get_be32(commit_data + g->hash_len);
  515. if (edge_value == GRAPH_PARENT_NONE)
  516. return 1;
  517. pptr = insert_parent_or_die(r, g, edge_value, pptr);
  518. edge_value = get_be32(commit_data + g->hash_len + 4);
  519. if (edge_value == GRAPH_PARENT_NONE)
  520. return 1;
  521. if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
  522. pptr = insert_parent_or_die(r, g, edge_value, pptr);
  523. return 1;
  524. }
  525. parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
  526. 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
  527. do {
  528. edge_value = get_be32(parent_data_ptr);
  529. pptr = insert_parent_or_die(r, g,
  530. edge_value & GRAPH_EDGE_LAST_MASK,
  531. pptr);
  532. parent_data_ptr++;
  533. } while (!(edge_value & GRAPH_LAST_EDGE));
  534. return 1;
  535. }
  536. static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
  537. {
  538. if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
  539. *pos = item->graph_pos;
  540. return 1;
  541. } else {
  542. struct commit_graph *cur_g = g;
  543. uint32_t lex_index;
  544. while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
  545. cur_g = cur_g->base_graph;
  546. if (cur_g) {
  547. *pos = lex_index + cur_g->num_commits_in_base;
  548. return 1;
  549. }
  550. return 0;
  551. }
  552. }
  553. static int parse_commit_in_graph_one(struct repository *r,
  554. struct commit_graph *g,
  555. struct commit *item)
  556. {
  557. uint32_t pos;
  558. if (item->object.parsed)
  559. return 1;
  560. if (find_commit_in_graph(item, g, &pos))
  561. return fill_commit_in_graph(r, item, g, pos);
  562. return 0;
  563. }
  564. int parse_commit_in_graph(struct repository *r, struct commit *item)
  565. {
  566. if (!prepare_commit_graph(r))
  567. return 0;
  568. return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
  569. }
  570. void load_commit_graph_info(struct repository *r, struct commit *item)
  571. {
  572. uint32_t pos;
  573. if (!prepare_commit_graph(r))
  574. return;
  575. if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
  576. fill_commit_graph_info(item, r->objects->commit_graph, pos);
  577. }
  578. static struct tree *load_tree_for_commit(struct repository *r,
  579. struct commit_graph *g,
  580. struct commit *c)
  581. {
  582. struct object_id oid;
  583. const unsigned char *commit_data;
  584. while (c->graph_pos < g->num_commits_in_base)
  585. g = g->base_graph;
  586. commit_data = g->chunk_commit_data +
  587. GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
  588. hashcpy(oid.hash, commit_data);
  589. set_commit_tree(c, lookup_tree(r, &oid));
  590. return c->maybe_tree;
  591. }
  592. static struct tree *get_commit_tree_in_graph_one(struct repository *r,
  593. struct commit_graph *g,
  594. const struct commit *c)
  595. {
  596. if (c->maybe_tree)
  597. return c->maybe_tree;
  598. if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
  599. BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
  600. return load_tree_for_commit(r, g, (struct commit *)c);
  601. }
  602. struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
  603. {
  604. return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
  605. }
  606. struct packed_commit_list {
  607. struct commit **list;
  608. int nr;
  609. int alloc;
  610. };
  611. struct packed_oid_list {
  612. struct object_id *list;
  613. int nr;
  614. int alloc;
  615. };
  616. struct write_commit_graph_context {
  617. struct repository *r;
  618. char *obj_dir;
  619. char *graph_name;
  620. struct packed_oid_list oids;
  621. struct packed_commit_list commits;
  622. int num_extra_edges;
  623. unsigned long approx_nr_objects;
  624. struct progress *progress;
  625. int progress_done;
  626. uint64_t progress_cnt;
  627. char *base_graph_name;
  628. int num_commit_graphs_before;
  629. int num_commit_graphs_after;
  630. char **commit_graph_filenames_before;
  631. char **commit_graph_filenames_after;
  632. char **commit_graph_hash_after;
  633. uint32_t new_num_commits_in_base;
  634. struct commit_graph *new_base_graph;
  635. unsigned append:1,
  636. report_progress:1,
  637. split:1,
  638. check_oids:1;
  639. const struct split_commit_graph_opts *split_opts;
  640. };
  641. static void write_graph_chunk_fanout(struct hashfile *f,
  642. struct write_commit_graph_context *ctx)
  643. {
  644. int i, count = 0;
  645. struct commit **list = ctx->commits.list;
  646. /*
  647. * Write the first-level table (the list is sorted,
  648. * but we use a 256-entry lookup to be able to avoid
  649. * having to do eight extra binary search iterations).
  650. */
  651. for (i = 0; i < 256; i++) {
  652. while (count < ctx->commits.nr) {
  653. if ((*list)->object.oid.hash[0] != i)
  654. break;
  655. display_progress(ctx->progress, ++ctx->progress_cnt);
  656. count++;
  657. list++;
  658. }
  659. hashwrite_be32(f, count);
  660. }
  661. }
  662. static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
  663. struct write_commit_graph_context *ctx)
  664. {
  665. struct commit **list = ctx->commits.list;
  666. int count;
  667. for (count = 0; count < ctx->commits.nr; count++, list++) {
  668. display_progress(ctx->progress, ++ctx->progress_cnt);
  669. hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
  670. }
  671. }
  672. static const unsigned char *commit_to_sha1(size_t index, void *table)
  673. {
  674. struct commit **commits = table;
  675. return commits[index]->object.oid.hash;
  676. }
  677. static void write_graph_chunk_data(struct hashfile *f, int hash_len,
  678. struct write_commit_graph_context *ctx)
  679. {
  680. struct commit **list = ctx->commits.list;
  681. struct commit **last = ctx->commits.list + ctx->commits.nr;
  682. uint32_t num_extra_edges = 0;
  683. while (list < last) {
  684. struct commit_list *parent;
  685. struct object_id *tree;
  686. int edge_value;
  687. uint32_t packedDate[2];
  688. display_progress(ctx->progress, ++ctx->progress_cnt);
  689. if (parse_commit_no_graph(*list))
  690. die(_("unable to parse commit %s"),
  691. oid_to_hex(&(*list)->object.oid));
  692. tree = get_commit_tree_oid(*list);
  693. if (!tree)
  694. die(_("unable to get tree for %s"),
  695. oid_to_hex(&(*list)->object.oid));
  696. hashwrite(f, tree->hash, hash_len);
  697. parent = (*list)->parents;
  698. if (!parent)
  699. edge_value = GRAPH_PARENT_NONE;
  700. else {
  701. edge_value = sha1_pos(parent->item->object.oid.hash,
  702. ctx->commits.list,
  703. ctx->commits.nr,
  704. commit_to_sha1);
  705. if (edge_value >= 0)
  706. edge_value += ctx->new_num_commits_in_base;
  707. else {
  708. uint32_t pos;
  709. if (find_commit_in_graph(parent->item,
  710. ctx->new_base_graph,
  711. &pos))
  712. edge_value = pos;
  713. }
  714. if (edge_value < 0)
  715. BUG("missing parent %s for commit %s",
  716. oid_to_hex(&parent->item->object.oid),
  717. oid_to_hex(&(*list)->object.oid));
  718. }
  719. hashwrite_be32(f, edge_value);
  720. if (parent)
  721. parent = parent->next;
  722. if (!parent)
  723. edge_value = GRAPH_PARENT_NONE;
  724. else if (parent->next)
  725. edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
  726. else {
  727. edge_value = sha1_pos(parent->item->object.oid.hash,
  728. ctx->commits.list,
  729. ctx->commits.nr,
  730. commit_to_sha1);
  731. if (edge_value >= 0)
  732. edge_value += ctx->new_num_commits_in_base;
  733. else {
  734. uint32_t pos;
  735. if (find_commit_in_graph(parent->item,
  736. ctx->new_base_graph,
  737. &pos))
  738. edge_value = pos;
  739. }
  740. if (edge_value < 0)
  741. BUG("missing parent %s for commit %s",
  742. oid_to_hex(&parent->item->object.oid),
  743. oid_to_hex(&(*list)->object.oid));
  744. }
  745. hashwrite_be32(f, edge_value);
  746. if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
  747. do {
  748. num_extra_edges++;
  749. parent = parent->next;
  750. } while (parent);
  751. }
  752. if (sizeof((*list)->date) > 4)
  753. packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
  754. else
  755. packedDate[0] = 0;
  756. packedDate[0] |= htonl((*list)->generation << 2);
  757. packedDate[1] = htonl((*list)->date);
  758. hashwrite(f, packedDate, 8);
  759. list++;
  760. }
  761. }
  762. static void write_graph_chunk_extra_edges(struct hashfile *f,
  763. struct write_commit_graph_context *ctx)
  764. {
  765. struct commit **list = ctx->commits.list;
  766. struct commit **last = ctx->commits.list + ctx->commits.nr;
  767. struct commit_list *parent;
  768. while (list < last) {
  769. int num_parents = 0;
  770. display_progress(ctx->progress, ++ctx->progress_cnt);
  771. for (parent = (*list)->parents; num_parents < 3 && parent;
  772. parent = parent->next)
  773. num_parents++;
  774. if (num_parents <= 2) {
  775. list++;
  776. continue;
  777. }
  778. /* Since num_parents > 2, this initializer is safe. */
  779. for (parent = (*list)->parents->next; parent; parent = parent->next) {
  780. int edge_value = sha1_pos(parent->item->object.oid.hash,
  781. ctx->commits.list,
  782. ctx->commits.nr,
  783. commit_to_sha1);
  784. if (edge_value >= 0)
  785. edge_value += ctx->new_num_commits_in_base;
  786. else {
  787. uint32_t pos;
  788. if (find_commit_in_graph(parent->item,
  789. ctx->new_base_graph,
  790. &pos))
  791. edge_value = pos;
  792. }
  793. if (edge_value < 0)
  794. BUG("missing parent %s for commit %s",
  795. oid_to_hex(&parent->item->object.oid),
  796. oid_to_hex(&(*list)->object.oid));
  797. else if (!parent->next)
  798. edge_value |= GRAPH_LAST_EDGE;
  799. hashwrite_be32(f, edge_value);
  800. }
  801. list++;
  802. }
  803. }
  804. static int oid_compare(const void *_a, const void *_b)
  805. {
  806. const struct object_id *a = (const struct object_id *)_a;
  807. const struct object_id *b = (const struct object_id *)_b;
  808. return oidcmp(a, b);
  809. }
  810. static int add_packed_commits(const struct object_id *oid,
  811. struct packed_git *pack,
  812. uint32_t pos,
  813. void *data)
  814. {
  815. struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
  816. enum object_type type;
  817. off_t offset = nth_packed_object_offset(pack, pos);
  818. struct object_info oi = OBJECT_INFO_INIT;
  819. if (ctx->progress)
  820. display_progress(ctx->progress, ++ctx->progress_done);
  821. oi.typep = &type;
  822. if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
  823. die(_("unable to get type of object %s"), oid_to_hex(oid));
  824. if (type != OBJ_COMMIT)
  825. return 0;
  826. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  827. oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
  828. ctx->oids.nr++;
  829. return 0;
  830. }
  831. static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
  832. {
  833. struct commit_list *parent;
  834. for (parent = commit->parents; parent; parent = parent->next) {
  835. if (!(parent->item->object.flags & UNINTERESTING)) {
  836. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  837. oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
  838. ctx->oids.nr++;
  839. parent->item->object.flags |= UNINTERESTING;
  840. }
  841. }
  842. }
  843. static void close_reachable(struct write_commit_graph_context *ctx)
  844. {
  845. int i;
  846. struct commit *commit;
  847. if (ctx->report_progress)
  848. ctx->progress = start_delayed_progress(
  849. _("Loading known commits in commit graph"),
  850. ctx->oids.nr);
  851. for (i = 0; i < ctx->oids.nr; i++) {
  852. display_progress(ctx->progress, i + 1);
  853. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  854. if (commit)
  855. commit->object.flags |= UNINTERESTING;
  856. }
  857. stop_progress(&ctx->progress);
  858. /*
  859. * As this loop runs, ctx->oids.nr may grow, but not more
  860. * than the number of missing commits in the reachable
  861. * closure.
  862. */
  863. if (ctx->report_progress)
  864. ctx->progress = start_delayed_progress(
  865. _("Expanding reachable commits in commit graph"),
  866. 0);
  867. for (i = 0; i < ctx->oids.nr; i++) {
  868. display_progress(ctx->progress, i + 1);
  869. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  870. if (!commit)
  871. continue;
  872. if (ctx->split) {
  873. if (!parse_commit(commit) &&
  874. commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
  875. add_missing_parents(ctx, commit);
  876. } else if (!parse_commit_no_graph(commit))
  877. add_missing_parents(ctx, commit);
  878. }
  879. stop_progress(&ctx->progress);
  880. if (ctx->report_progress)
  881. ctx->progress = start_delayed_progress(
  882. _("Clearing commit marks in commit graph"),
  883. ctx->oids.nr);
  884. for (i = 0; i < ctx->oids.nr; i++) {
  885. display_progress(ctx->progress, i + 1);
  886. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  887. if (commit)
  888. commit->object.flags &= ~UNINTERESTING;
  889. }
  890. stop_progress(&ctx->progress);
  891. }
  892. static void compute_generation_numbers(struct write_commit_graph_context *ctx)
  893. {
  894. int i;
  895. struct commit_list *list = NULL;
  896. if (ctx->report_progress)
  897. ctx->progress = start_progress(
  898. _("Computing commit graph generation numbers"),
  899. ctx->commits.nr);
  900. for (i = 0; i < ctx->commits.nr; i++) {
  901. display_progress(ctx->progress, i + 1);
  902. if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
  903. ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
  904. continue;
  905. commit_list_insert(ctx->commits.list[i], &list);
  906. while (list) {
  907. struct commit *current = list->item;
  908. struct commit_list *parent;
  909. int all_parents_computed = 1;
  910. uint32_t max_generation = 0;
  911. for (parent = current->parents; parent; parent = parent->next) {
  912. if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
  913. parent->item->generation == GENERATION_NUMBER_ZERO) {
  914. all_parents_computed = 0;
  915. commit_list_insert(parent->item, &list);
  916. break;
  917. } else if (parent->item->generation > max_generation) {
  918. max_generation = parent->item->generation;
  919. }
  920. }
  921. if (all_parents_computed) {
  922. current->generation = max_generation + 1;
  923. pop_commit(&list);
  924. if (current->generation > GENERATION_NUMBER_MAX)
  925. current->generation = GENERATION_NUMBER_MAX;
  926. }
  927. }
  928. }
  929. stop_progress(&ctx->progress);
  930. }
  931. static int add_ref_to_list(const char *refname,
  932. const struct object_id *oid,
  933. int flags, void *cb_data)
  934. {
  935. struct string_list *list = (struct string_list *)cb_data;
  936. string_list_append(list, oid_to_hex(oid));
  937. return 0;
  938. }
  939. int write_commit_graph_reachable(const char *obj_dir,
  940. enum commit_graph_write_flags flags,
  941. const struct split_commit_graph_opts *split_opts)
  942. {
  943. struct string_list list = STRING_LIST_INIT_DUP;
  944. int result;
  945. for_each_ref(add_ref_to_list, &list);
  946. result = write_commit_graph(obj_dir, NULL, &list,
  947. flags, split_opts);
  948. string_list_clear(&list, 0);
  949. return result;
  950. }
  951. static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
  952. struct string_list *pack_indexes)
  953. {
  954. uint32_t i;
  955. struct strbuf progress_title = STRBUF_INIT;
  956. struct strbuf packname = STRBUF_INIT;
  957. int dirlen;
  958. strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
  959. dirlen = packname.len;
  960. if (ctx->report_progress) {
  961. strbuf_addf(&progress_title,
  962. Q_("Finding commits for commit graph in %d pack",
  963. "Finding commits for commit graph in %d packs",
  964. pack_indexes->nr),
  965. pack_indexes->nr);
  966. ctx->progress = start_delayed_progress(progress_title.buf, 0);
  967. ctx->progress_done = 0;
  968. }
  969. for (i = 0; i < pack_indexes->nr; i++) {
  970. struct packed_git *p;
  971. strbuf_setlen(&packname, dirlen);
  972. strbuf_addstr(&packname, pack_indexes->items[i].string);
  973. p = add_packed_git(packname.buf, packname.len, 1);
  974. if (!p) {
  975. error(_("error adding pack %s"), packname.buf);
  976. return -1;
  977. }
  978. if (open_pack_index(p)) {
  979. error(_("error opening index for %s"), packname.buf);
  980. return -1;
  981. }
  982. for_each_object_in_pack(p, add_packed_commits, ctx,
  983. FOR_EACH_OBJECT_PACK_ORDER);
  984. close_pack(p);
  985. free(p);
  986. }
  987. stop_progress(&ctx->progress);
  988. strbuf_release(&progress_title);
  989. strbuf_release(&packname);
  990. return 0;
  991. }
  992. static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
  993. struct string_list *commit_hex)
  994. {
  995. uint32_t i;
  996. struct strbuf progress_title = STRBUF_INIT;
  997. if (ctx->report_progress) {
  998. strbuf_addf(&progress_title,
  999. Q_("Finding commits for commit graph from %d ref",
  1000. "Finding commits for commit graph from %d refs",
  1001. commit_hex->nr),
  1002. commit_hex->nr);
  1003. ctx->progress = start_delayed_progress(
  1004. progress_title.buf,
  1005. commit_hex->nr);
  1006. }
  1007. for (i = 0; i < commit_hex->nr; i++) {
  1008. const char *end;
  1009. struct object_id oid;
  1010. struct commit *result;
  1011. display_progress(ctx->progress, i + 1);
  1012. if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
  1013. (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
  1014. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  1015. oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
  1016. ctx->oids.nr++;
  1017. } else if (ctx->check_oids) {
  1018. error(_("invalid commit object id: %s"),
  1019. commit_hex->items[i].string);
  1020. return -1;
  1021. }
  1022. }
  1023. stop_progress(&ctx->progress);
  1024. strbuf_release(&progress_title);
  1025. return 0;
  1026. }
  1027. static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
  1028. {
  1029. if (ctx->report_progress)
  1030. ctx->progress = start_delayed_progress(
  1031. _("Finding commits for commit graph among packed objects"),
  1032. ctx->approx_nr_objects);
  1033. for_each_packed_object(add_packed_commits, ctx,
  1034. FOR_EACH_OBJECT_PACK_ORDER);
  1035. if (ctx->progress_done < ctx->approx_nr_objects)
  1036. display_progress(ctx->progress, ctx->approx_nr_objects);
  1037. stop_progress(&ctx->progress);
  1038. }
  1039. static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
  1040. {
  1041. uint32_t i, count_distinct = 1;
  1042. if (ctx->report_progress)
  1043. ctx->progress = start_delayed_progress(
  1044. _("Counting distinct commits in commit graph"),
  1045. ctx->oids.nr);
  1046. display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
  1047. QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
  1048. for (i = 1; i < ctx->oids.nr; i++) {
  1049. display_progress(ctx->progress, i + 1);
  1050. if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
  1051. if (ctx->split) {
  1052. struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
  1053. if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
  1054. continue;
  1055. }
  1056. count_distinct++;
  1057. }
  1058. }
  1059. stop_progress(&ctx->progress);
  1060. return count_distinct;
  1061. }
  1062. static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
  1063. {
  1064. uint32_t i;
  1065. ctx->num_extra_edges = 0;
  1066. if (ctx->report_progress)
  1067. ctx->progress = start_delayed_progress(
  1068. _("Finding extra edges in commit graph"),
  1069. ctx->oids.nr);
  1070. for (i = 0; i < ctx->oids.nr; i++) {
  1071. unsigned int num_parents;
  1072. display_progress(ctx->progress, i + 1);
  1073. if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
  1074. continue;
  1075. ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
  1076. ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
  1077. if (ctx->split &&
  1078. ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
  1079. continue;
  1080. parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
  1081. num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
  1082. if (num_parents > 2)
  1083. ctx->num_extra_edges += num_parents - 1;
  1084. ctx->commits.nr++;
  1085. }
  1086. stop_progress(&ctx->progress);
  1087. }
  1088. static int write_graph_chunk_base_1(struct hashfile *f,
  1089. struct commit_graph *g)
  1090. {
  1091. int num = 0;
  1092. if (!g)
  1093. return 0;
  1094. num = write_graph_chunk_base_1(f, g->base_graph);
  1095. hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
  1096. return num + 1;
  1097. }
  1098. static int write_graph_chunk_base(struct hashfile *f,
  1099. struct write_commit_graph_context *ctx)
  1100. {
  1101. int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
  1102. if (num != ctx->num_commit_graphs_after - 1) {
  1103. error(_("failed to write correct number of base graph ids"));
  1104. return -1;
  1105. }
  1106. return 0;
  1107. }
  1108. static int write_commit_graph_file(struct write_commit_graph_context *ctx)
  1109. {
  1110. uint32_t i;
  1111. int fd;
  1112. struct hashfile *f;
  1113. struct lock_file lk = LOCK_INIT;
  1114. uint32_t chunk_ids[6];
  1115. uint64_t chunk_offsets[6];
  1116. const unsigned hashsz = the_hash_algo->rawsz;
  1117. struct strbuf progress_title = STRBUF_INIT;
  1118. int num_chunks = 3;
  1119. struct object_id file_hash;
  1120. if (ctx->split) {
  1121. struct strbuf tmp_file = STRBUF_INIT;
  1122. strbuf_addf(&tmp_file,
  1123. "%s/info/commit-graphs/tmp_graph_XXXXXX",
  1124. ctx->obj_dir);
  1125. ctx->graph_name = strbuf_detach(&tmp_file, NULL);
  1126. } else {
  1127. ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
  1128. }
  1129. if (safe_create_leading_directories(ctx->graph_name)) {
  1130. UNLEAK(ctx->graph_name);
  1131. error(_("unable to create leading directories of %s"),
  1132. ctx->graph_name);
  1133. return -1;
  1134. }
  1135. if (ctx->split) {
  1136. char *lock_name = get_chain_filename(ctx->obj_dir);
  1137. hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
  1138. fd = git_mkstemp_mode(ctx->graph_name, 0444);
  1139. if (fd < 0) {
  1140. error(_("unable to create '%s'"), ctx->graph_name);
  1141. return -1;
  1142. }
  1143. f = hashfd(fd, ctx->graph_name);
  1144. } else {
  1145. hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
  1146. fd = lk.tempfile->fd;
  1147. f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
  1148. }
  1149. chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
  1150. chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
  1151. chunk_ids[2] = GRAPH_CHUNKID_DATA;
  1152. if (ctx->num_extra_edges) {
  1153. chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
  1154. num_chunks++;
  1155. }
  1156. if (ctx->num_commit_graphs_after > 1) {
  1157. chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
  1158. num_chunks++;
  1159. }
  1160. chunk_ids[num_chunks] = 0;
  1161. chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
  1162. chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
  1163. chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
  1164. chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
  1165. num_chunks = 3;
  1166. if (ctx->num_extra_edges) {
  1167. chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
  1168. 4 * ctx->num_extra_edges;
  1169. num_chunks++;
  1170. }
  1171. if (ctx->num_commit_graphs_after > 1) {
  1172. chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
  1173. hashsz * (ctx->num_commit_graphs_after - 1);
  1174. num_chunks++;
  1175. }
  1176. hashwrite_be32(f, GRAPH_SIGNATURE);
  1177. hashwrite_u8(f, GRAPH_VERSION);
  1178. hashwrite_u8(f, oid_version());
  1179. hashwrite_u8(f, num_chunks);
  1180. hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
  1181. for (i = 0; i <= num_chunks; i++) {
  1182. uint32_t chunk_write[3];
  1183. chunk_write[0] = htonl(chunk_ids[i]);
  1184. chunk_write[1] = htonl(chunk_offsets[i] >> 32);
  1185. chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
  1186. hashwrite(f, chunk_write, 12);
  1187. }
  1188. if (ctx->report_progress) {
  1189. strbuf_addf(&progress_title,
  1190. Q_("Writing out commit graph in %d pass",
  1191. "Writing out commit graph in %d passes",
  1192. num_chunks),
  1193. num_chunks);
  1194. ctx->progress = start_delayed_progress(
  1195. progress_title.buf,
  1196. num_chunks * ctx->commits.nr);
  1197. }
  1198. write_graph_chunk_fanout(f, ctx);
  1199. write_graph_chunk_oids(f, hashsz, ctx);
  1200. write_graph_chunk_data(f, hashsz, ctx);
  1201. if (ctx->num_extra_edges)
  1202. write_graph_chunk_extra_edges(f, ctx);
  1203. if (ctx->num_commit_graphs_after > 1 &&
  1204. write_graph_chunk_base(f, ctx)) {
  1205. return -1;
  1206. }
  1207. stop_progress(&ctx->progress);
  1208. strbuf_release(&progress_title);
  1209. if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
  1210. char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
  1211. char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
  1212. free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
  1213. free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
  1214. ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
  1215. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
  1216. }
  1217. close_commit_graph(ctx->r->objects);
  1218. finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
  1219. if (ctx->split) {
  1220. FILE *chainf = fdopen_lock_file(&lk, "w");
  1221. char *final_graph_name;
  1222. int result;
  1223. close(fd);
  1224. if (!chainf) {
  1225. error(_("unable to open commit-graph chain file"));
  1226. return -1;
  1227. }
  1228. if (ctx->base_graph_name) {
  1229. const char *dest = ctx->commit_graph_filenames_after[
  1230. ctx->num_commit_graphs_after - 2];
  1231. if (strcmp(ctx->base_graph_name, dest)) {
  1232. result = rename(ctx->base_graph_name, dest);
  1233. if (result) {
  1234. error(_("failed to rename base commit-graph file"));
  1235. return -1;
  1236. }
  1237. }
  1238. } else {
  1239. char *graph_name = get_commit_graph_filename(ctx->obj_dir);
  1240. unlink(graph_name);
  1241. }
  1242. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
  1243. final_graph_name = get_split_graph_filename(ctx->obj_dir,
  1244. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
  1245. ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
  1246. result = rename(ctx->graph_name, final_graph_name);
  1247. for (i = 0; i < ctx->num_commit_graphs_after; i++)
  1248. fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
  1249. if (result) {
  1250. error(_("failed to rename temporary commit-graph file"));
  1251. return -1;
  1252. }
  1253. }
  1254. commit_lock_file(&lk);
  1255. return 0;
  1256. }
  1257. static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
  1258. {
  1259. struct commit_graph *g;
  1260. uint32_t num_commits;
  1261. uint32_t i;
  1262. int max_commits = 0;
  1263. int size_mult = 2;
  1264. if (ctx->split_opts) {
  1265. max_commits = ctx->split_opts->max_commits;
  1266. size_mult = ctx->split_opts->size_multiple;
  1267. }
  1268. g = ctx->r->objects->commit_graph;
  1269. num_commits = ctx->commits.nr;
  1270. ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
  1271. while (g && (g->num_commits <= size_mult * num_commits ||
  1272. (max_commits && num_commits > max_commits))) {
  1273. if (strcmp(g->obj_dir, ctx->obj_dir))
  1274. break;
  1275. num_commits += g->num_commits;
  1276. g = g->base_graph;
  1277. ctx->num_commit_graphs_after--;
  1278. }
  1279. ctx->new_base_graph = g;
  1280. if (ctx->num_commit_graphs_after == 2) {
  1281. char *old_graph_name = get_commit_graph_filename(g->obj_dir);
  1282. if (!strcmp(g->filename, old_graph_name) &&
  1283. strcmp(g->obj_dir, ctx->obj_dir)) {
  1284. ctx->num_commit_graphs_after = 1;
  1285. ctx->new_base_graph = NULL;
  1286. }
  1287. free(old_graph_name);
  1288. }
  1289. ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
  1290. ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
  1291. for (i = 0; i < ctx->num_commit_graphs_after &&
  1292. i < ctx->num_commit_graphs_before; i++)
  1293. ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
  1294. i = ctx->num_commit_graphs_before - 1;
  1295. g = ctx->r->objects->commit_graph;
  1296. while (g) {
  1297. if (i < ctx->num_commit_graphs_after)
  1298. ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
  1299. i--;
  1300. g = g->base_graph;
  1301. }
  1302. }
  1303. static void merge_commit_graph(struct write_commit_graph_context *ctx,
  1304. struct commit_graph *g)
  1305. {
  1306. uint32_t i;
  1307. uint32_t offset = g->num_commits_in_base;
  1308. ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
  1309. for (i = 0; i < g->num_commits; i++) {
  1310. struct object_id oid;
  1311. struct commit *result;
  1312. display_progress(ctx->progress, i + 1);
  1313. load_oid_from_graph(g, i + offset, &oid);
  1314. /* only add commits if they still exist in the repo */
  1315. result = lookup_commit_reference_gently(ctx->r, &oid, 1);
  1316. if (result) {
  1317. ctx->commits.list[ctx->commits.nr] = result;
  1318. ctx->commits.nr++;
  1319. }
  1320. }
  1321. }
  1322. static int commit_compare(const void *_a, const void *_b)
  1323. {
  1324. const struct commit *a = *(const struct commit **)_a;
  1325. const struct commit *b = *(const struct commit **)_b;
  1326. return oidcmp(&a->object.oid, &b->object.oid);
  1327. }
  1328. static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
  1329. {
  1330. uint32_t i;
  1331. if (ctx->report_progress)
  1332. ctx->progress = start_delayed_progress(
  1333. _("Scanning merged commits"),
  1334. ctx->commits.nr);
  1335. QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
  1336. ctx->num_extra_edges = 0;
  1337. for (i = 0; i < ctx->commits.nr; i++) {
  1338. display_progress(ctx->progress, i);
  1339. if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
  1340. &ctx->commits.list[i]->object.oid)) {
  1341. die(_("unexpected duplicate commit id %s"),
  1342. oid_to_hex(&ctx->commits.list[i]->object.oid));
  1343. } else {
  1344. unsigned int num_parents;
  1345. num_parents = commit_list_count(ctx->commits.list[i]->parents);
  1346. if (num_parents > 2)
  1347. ctx->num_extra_edges += num_parents - 1;
  1348. }
  1349. }
  1350. stop_progress(&ctx->progress);
  1351. }
  1352. static void merge_commit_graphs(struct write_commit_graph_context *ctx)
  1353. {
  1354. struct commit_graph *g = ctx->r->objects->commit_graph;
  1355. uint32_t current_graph_number = ctx->num_commit_graphs_before;
  1356. struct strbuf progress_title = STRBUF_INIT;
  1357. while (g && current_graph_number >= ctx->num_commit_graphs_after) {
  1358. current_graph_number--;
  1359. if (ctx->report_progress) {
  1360. strbuf_addstr(&progress_title, _("Merging commit-graph"));
  1361. ctx->progress = start_delayed_progress(progress_title.buf, 0);
  1362. }
  1363. merge_commit_graph(ctx, g);
  1364. stop_progress(&ctx->progress);
  1365. strbuf_release(&progress_title);
  1366. g = g->base_graph;
  1367. }
  1368. if (g) {
  1369. ctx->new_base_graph = g;
  1370. ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
  1371. }
  1372. if (ctx->new_base_graph)
  1373. ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
  1374. sort_and_scan_merged_commits(ctx);
  1375. }
  1376. static void mark_commit_graphs(struct write_commit_graph_context *ctx)
  1377. {
  1378. uint32_t i;
  1379. time_t now = time(NULL);
  1380. for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
  1381. struct stat st;
  1382. struct utimbuf updated_time;
  1383. stat(ctx->commit_graph_filenames_before[i], &st);
  1384. updated_time.actime = st.st_atime;
  1385. updated_time.modtime = now;
  1386. utime(ctx->commit_graph_filenames_before[i], &updated_time);
  1387. }
  1388. }
  1389. static void expire_commit_graphs(struct write_commit_graph_context *ctx)
  1390. {
  1391. struct strbuf path = STRBUF_INIT;
  1392. DIR *dir;
  1393. struct dirent *de;
  1394. size_t dirnamelen;
  1395. timestamp_t expire_time = time(NULL);
  1396. if (ctx->split_opts && ctx->split_opts->expire_time)
  1397. expire_time -= ctx->split_opts->expire_time;
  1398. if (!ctx->split) {
  1399. char *chain_file_name = get_chain_filename(ctx->obj_dir);
  1400. unlink(chain_file_name);
  1401. free(chain_file_name);
  1402. ctx->num_commit_graphs_after = 0;
  1403. }
  1404. strbuf_addstr(&path, ctx->obj_dir);
  1405. strbuf_addstr(&path, "/info/commit-graphs");
  1406. dir = opendir(path.buf);
  1407. if (!dir)
  1408. goto out;
  1409. strbuf_addch(&path, '/');
  1410. dirnamelen = path.len;
  1411. while ((de = readdir(dir)) != NULL) {
  1412. struct stat st;
  1413. uint32_t i, found = 0;
  1414. strbuf_setlen(&path, dirnamelen);
  1415. strbuf_addstr(&path, de->d_name);
  1416. stat(path.buf, &st);
  1417. if (st.st_mtime > expire_time)
  1418. continue;
  1419. if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
  1420. continue;
  1421. for (i = 0; i < ctx->num_commit_graphs_after; i++) {
  1422. if (!strcmp(ctx->commit_graph_filenames_after[i],
  1423. path.buf)) {
  1424. found = 1;
  1425. break;
  1426. }
  1427. }
  1428. if (!found)
  1429. unlink(path.buf);
  1430. }
  1431. out:
  1432. strbuf_release(&path);
  1433. }
  1434. int write_commit_graph(const char *obj_dir,
  1435. struct string_list *pack_indexes,
  1436. struct string_list *commit_hex,
  1437. enum commit_graph_write_flags flags,
  1438. const struct split_commit_graph_opts *split_opts)
  1439. {
  1440. struct write_commit_graph_context *ctx;
  1441. uint32_t i, count_distinct = 0;
  1442. size_t len;
  1443. int res = 0;
  1444. if (!commit_graph_compatible(the_repository))
  1445. return 0;
  1446. ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
  1447. ctx->r = the_repository;
  1448. /* normalize object dir with no trailing slash */
  1449. ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
  1450. normalize_path_copy(ctx->obj_dir, obj_dir);
  1451. len = strlen(ctx->obj_dir);
  1452. if (len && ctx->obj_dir[len - 1] == '/')
  1453. ctx->obj_dir[len - 1] = 0;
  1454. ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
  1455. ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
  1456. ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
  1457. ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
  1458. ctx->split_opts = split_opts;
  1459. if (ctx->split) {
  1460. struct commit_graph *g;
  1461. prepare_commit_graph(ctx->r);
  1462. g = ctx->r->objects->commit_graph;
  1463. while (g) {
  1464. ctx->num_commit_graphs_before++;
  1465. g = g->base_graph;
  1466. }
  1467. if (ctx->num_commit_graphs_before) {
  1468. ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
  1469. i = ctx->num_commit_graphs_before;
  1470. g = ctx->r->objects->commit_graph;
  1471. while (g) {
  1472. ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
  1473. g = g->base_graph;
  1474. }
  1475. }
  1476. }
  1477. ctx->approx_nr_objects = approximate_object_count();
  1478. ctx->oids.alloc = ctx->approx_nr_objects / 32;
  1479. if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
  1480. ctx->oids.alloc = split_opts->max_commits;
  1481. if (ctx->append) {
  1482. prepare_commit_graph_one(ctx->r, ctx->obj_dir);
  1483. if (ctx->r->objects->commit_graph)
  1484. ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
  1485. }
  1486. if (ctx->oids.alloc < 1024)
  1487. ctx->oids.alloc = 1024;
  1488. ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
  1489. if (ctx->append && ctx->r->objects->commit_graph) {
  1490. struct commit_graph *g = ctx->r->objects->commit_graph;
  1491. for (i = 0; i < g->num_commits; i++) {
  1492. const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
  1493. hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
  1494. }
  1495. }
  1496. if (pack_indexes) {
  1497. if ((res = fill_oids_from_packs(ctx, pack_indexes)))
  1498. goto cleanup;
  1499. }
  1500. if (commit_hex) {
  1501. if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
  1502. goto cleanup;
  1503. }
  1504. if (!pack_indexes && !commit_hex)
  1505. fill_oids_from_all_packs(ctx);
  1506. close_reachable(ctx);
  1507. count_distinct = count_distinct_commits(ctx);
  1508. if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
  1509. error(_("the commit graph format cannot write %d commits"), count_distinct);
  1510. res = -1;
  1511. goto cleanup;
  1512. }
  1513. ctx->commits.alloc = count_distinct;
  1514. ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
  1515. copy_oids_to_commits(ctx);
  1516. if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
  1517. error(_("too many commits to write graph"));
  1518. res = -1;
  1519. goto cleanup;
  1520. }
  1521. if (!ctx->commits.nr)
  1522. goto cleanup;
  1523. if (ctx->split) {
  1524. split_graph_merge_strategy(ctx);
  1525. merge_commit_graphs(ctx);
  1526. } else
  1527. ctx->num_commit_graphs_after = 1;
  1528. compute_generation_numbers(ctx);
  1529. res = write_commit_graph_file(ctx);
  1530. if (ctx->split)
  1531. mark_commit_graphs(ctx);
  1532. expire_commit_graphs(ctx);
  1533. cleanup:
  1534. free(ctx->graph_name);
  1535. free(ctx->commits.list);
  1536. free(ctx->oids.list);
  1537. free(ctx->obj_dir);
  1538. if (ctx->commit_graph_filenames_after) {
  1539. for (i = 0; i < ctx->num_commit_graphs_after; i++) {
  1540. free(ctx->commit_graph_filenames_after[i]);
  1541. free(ctx->commit_graph_hash_after[i]);
  1542. }
  1543. for (i = 0; i < ctx->num_commit_graphs_before; i++)
  1544. free(ctx->commit_graph_filenames_before[i]);
  1545. free(ctx->commit_graph_filenames_after);
  1546. free(ctx->commit_graph_filenames_before);
  1547. free(ctx->commit_graph_hash_after);
  1548. }
  1549. free(ctx);
  1550. return res;
  1551. }
  1552. #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
  1553. static int verify_commit_graph_error;
  1554. static void graph_report(const char *fmt, ...)
  1555. {
  1556. va_list ap;
  1557. verify_commit_graph_error = 1;
  1558. va_start(ap, fmt);
  1559. vfprintf(stderr, fmt, ap);
  1560. fprintf(stderr, "\n");
  1561. va_end(ap);
  1562. }
  1563. #define GENERATION_ZERO_EXISTS 1
  1564. #define GENERATION_NUMBER_EXISTS 2
  1565. int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
  1566. {
  1567. uint32_t i, cur_fanout_pos = 0;
  1568. struct object_id prev_oid, cur_oid, checksum;
  1569. int generation_zero = 0;
  1570. struct hashfile *f;
  1571. int devnull;
  1572. struct progress *progress = NULL;
  1573. int local_error = 0;
  1574. if (!g) {
  1575. graph_report("no commit-graph file loaded");
  1576. return 1;
  1577. }
  1578. verify_commit_graph_error = verify_commit_graph_lite(g);
  1579. if (verify_commit_graph_error)
  1580. return verify_commit_graph_error;
  1581. devnull = open("/dev/null", O_WRONLY);
  1582. f = hashfd(devnull, NULL);
  1583. hashwrite(f, g->data, g->data_len - g->hash_len);
  1584. finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
  1585. if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
  1586. graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
  1587. verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
  1588. }
  1589. for (i = 0; i < g->num_commits; i++) {
  1590. struct commit *graph_commit;
  1591. hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
  1592. if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
  1593. graph_report(_("commit-graph has incorrect OID order: %s then %s"),
  1594. oid_to_hex(&prev_oid),
  1595. oid_to_hex(&cur_oid));
  1596. oidcpy(&prev_oid, &cur_oid);
  1597. while (cur_oid.hash[0] > cur_fanout_pos) {
  1598. uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
  1599. if (i != fanout_value)
  1600. graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
  1601. cur_fanout_pos, fanout_value, i);
  1602. cur_fanout_pos++;
  1603. }
  1604. graph_commit = lookup_commit(r, &cur_oid);
  1605. if (!parse_commit_in_graph_one(r, g, graph_commit))
  1606. graph_report(_("failed to parse commit %s from commit-graph"),
  1607. oid_to_hex(&cur_oid));
  1608. }
  1609. while (cur_fanout_pos < 256) {
  1610. uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
  1611. if (g->num_commits != fanout_value)
  1612. graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
  1613. cur_fanout_pos, fanout_value, i);
  1614. cur_fanout_pos++;
  1615. }
  1616. if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
  1617. return verify_commit_graph_error;
  1618. if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
  1619. progress = start_progress(_("Verifying commits in commit graph"),
  1620. g->num_commits);
  1621. for (i = 0; i < g->num_commits; i++) {
  1622. struct commit *graph_commit, *odb_commit;
  1623. struct commit_list *graph_parents, *odb_parents;
  1624. uint32_t max_generation = 0;
  1625. display_progress(progress, i + 1);
  1626. hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
  1627. graph_commit = lookup_commit(r, &cur_oid);
  1628. odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
  1629. if (parse_commit_internal(odb_commit, 0, 0)) {
  1630. graph_report(_("failed to parse commit %s from object database for commit-graph"),
  1631. oid_to_hex(&cur_oid));
  1632. continue;
  1633. }
  1634. if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
  1635. get_commit_tree_oid(odb_commit)))
  1636. graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
  1637. oid_to_hex(&cur_oid),
  1638. oid_to_hex(get_commit_tree_oid(graph_commit)),
  1639. oid_to_hex(get_commit_tree_oid(odb_commit)));
  1640. graph_parents = graph_commit->parents;
  1641. odb_parents = odb_commit->parents;
  1642. while (graph_parents) {
  1643. if (odb_parents == NULL) {
  1644. graph_report(_("commit-graph parent list for commit %s is too long"),
  1645. oid_to_hex(&cur_oid));
  1646. break;
  1647. }
  1648. /* parse parent in case it is in a base graph */
  1649. parse_commit_in_graph_one(r, g, graph_parents->item);
  1650. if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
  1651. graph_report(_("commit-graph parent for %s is %s != %s"),
  1652. oid_to_hex(&cur_oid),
  1653. oid_to_hex(&graph_parents->item->object.oid),
  1654. oid_to_hex(&odb_parents->item->object.oid));
  1655. if (graph_parents->item->generation > max_generation)
  1656. max_generation = graph_parents->item->generation;
  1657. graph_parents = graph_parents->next;
  1658. odb_parents = odb_parents->next;
  1659. }
  1660. if (odb_parents != NULL)
  1661. graph_report(_("commit-graph parent list for commit %s terminates early"),
  1662. oid_to_hex(&cur_oid));
  1663. if (!graph_commit->generation) {
  1664. if (generation_zero == GENERATION_NUMBER_EXISTS)
  1665. graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
  1666. oid_to_hex(&cur_oid));
  1667. generation_zero = GENERATION_ZERO_EXISTS;
  1668. } else if (generation_zero == GENERATION_ZERO_EXISTS)
  1669. graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
  1670. oid_to_hex(&cur_oid));
  1671. if (generation_zero == GENERATION_ZERO_EXISTS)
  1672. continue;
  1673. /*
  1674. * If one of our parents has generation GENERATION_NUMBER_MAX, then
  1675. * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
  1676. * extra logic in the following condition.
  1677. */
  1678. if (max_generation == GENERATION_NUMBER_MAX)
  1679. max_generation--;
  1680. if (graph_commit->generation != max_generation + 1)
  1681. graph_report(_("commit-graph generation for commit %s is %u != %u"),
  1682. oid_to_hex(&cur_oid),
  1683. graph_commit->generation,
  1684. max_generation + 1);
  1685. if (graph_commit->date != odb_commit->date)
  1686. graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
  1687. oid_to_hex(&cur_oid),
  1688. graph_commit->date,
  1689. odb_commit->date);
  1690. }
  1691. stop_progress(&progress);
  1692. local_error = verify_commit_graph_error;
  1693. if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
  1694. local_error |= verify_commit_graph(r, g->base_graph, flags);
  1695. return local_error;
  1696. }
  1697. void free_commit_graph(struct commit_graph *g)
  1698. {
  1699. if (!g)
  1700. return;
  1701. if (g->graph_fd >= 0) {
  1702. munmap((void *)g->data, g->data_len);
  1703. g->data = NULL;
  1704. close(g->graph_fd);
  1705. }
  1706. free(g->filename);
  1707. free(g);
  1708. }
  1709. void disable_commit_graph(struct repository *r)
  1710. {
  1711. r->commit_graph_disabled = 1;
  1712. }