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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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