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
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

2108 wiersze
53 KiB

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