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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2121 lignes
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 attempts 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. hashwrite(f, tree->hash, hash_len);
  696. parent = (*list)->parents;
  697. if (!parent)
  698. edge_value = GRAPH_PARENT_NONE;
  699. else {
  700. edge_value = sha1_pos(parent->item->object.oid.hash,
  701. ctx->commits.list,
  702. ctx->commits.nr,
  703. commit_to_sha1);
  704. if (edge_value >= 0)
  705. edge_value += ctx->new_num_commits_in_base;
  706. else {
  707. uint32_t pos;
  708. if (find_commit_in_graph(parent->item,
  709. ctx->new_base_graph,
  710. &pos))
  711. edge_value = pos;
  712. }
  713. if (edge_value < 0)
  714. BUG("missing parent %s for commit %s",
  715. oid_to_hex(&parent->item->object.oid),
  716. oid_to_hex(&(*list)->object.oid));
  717. }
  718. hashwrite_be32(f, edge_value);
  719. if (parent)
  720. parent = parent->next;
  721. if (!parent)
  722. edge_value = GRAPH_PARENT_NONE;
  723. else if (parent->next)
  724. edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
  725. else {
  726. edge_value = sha1_pos(parent->item->object.oid.hash,
  727. ctx->commits.list,
  728. ctx->commits.nr,
  729. commit_to_sha1);
  730. if (edge_value >= 0)
  731. edge_value += ctx->new_num_commits_in_base;
  732. else {
  733. uint32_t pos;
  734. if (find_commit_in_graph(parent->item,
  735. ctx->new_base_graph,
  736. &pos))
  737. edge_value = pos;
  738. }
  739. if (edge_value < 0)
  740. BUG("missing parent %s for commit %s",
  741. oid_to_hex(&parent->item->object.oid),
  742. oid_to_hex(&(*list)->object.oid));
  743. }
  744. hashwrite_be32(f, edge_value);
  745. if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
  746. do {
  747. num_extra_edges++;
  748. parent = parent->next;
  749. } while (parent);
  750. }
  751. if (sizeof((*list)->date) > 4)
  752. packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
  753. else
  754. packedDate[0] = 0;
  755. packedDate[0] |= htonl((*list)->generation << 2);
  756. packedDate[1] = htonl((*list)->date);
  757. hashwrite(f, packedDate, 8);
  758. list++;
  759. }
  760. }
  761. static void write_graph_chunk_extra_edges(struct hashfile *f,
  762. struct write_commit_graph_context *ctx)
  763. {
  764. struct commit **list = ctx->commits.list;
  765. struct commit **last = ctx->commits.list + ctx->commits.nr;
  766. struct commit_list *parent;
  767. while (list < last) {
  768. int num_parents = 0;
  769. display_progress(ctx->progress, ++ctx->progress_cnt);
  770. for (parent = (*list)->parents; num_parents < 3 && parent;
  771. parent = parent->next)
  772. num_parents++;
  773. if (num_parents <= 2) {
  774. list++;
  775. continue;
  776. }
  777. /* Since num_parents > 2, this initializer is safe. */
  778. for (parent = (*list)->parents->next; parent; parent = parent->next) {
  779. int edge_value = sha1_pos(parent->item->object.oid.hash,
  780. ctx->commits.list,
  781. ctx->commits.nr,
  782. commit_to_sha1);
  783. if (edge_value >= 0)
  784. edge_value += ctx->new_num_commits_in_base;
  785. else {
  786. uint32_t pos;
  787. if (find_commit_in_graph(parent->item,
  788. ctx->new_base_graph,
  789. &pos))
  790. edge_value = pos;
  791. }
  792. if (edge_value < 0)
  793. BUG("missing parent %s for commit %s",
  794. oid_to_hex(&parent->item->object.oid),
  795. oid_to_hex(&(*list)->object.oid));
  796. else if (!parent->next)
  797. edge_value |= GRAPH_LAST_EDGE;
  798. hashwrite_be32(f, edge_value);
  799. }
  800. list++;
  801. }
  802. }
  803. static int oid_compare(const void *_a, const void *_b)
  804. {
  805. const struct object_id *a = (const struct object_id *)_a;
  806. const struct object_id *b = (const struct object_id *)_b;
  807. return oidcmp(a, b);
  808. }
  809. static int add_packed_commits(const struct object_id *oid,
  810. struct packed_git *pack,
  811. uint32_t pos,
  812. void *data)
  813. {
  814. struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
  815. enum object_type type;
  816. off_t offset = nth_packed_object_offset(pack, pos);
  817. struct object_info oi = OBJECT_INFO_INIT;
  818. if (ctx->progress)
  819. display_progress(ctx->progress, ++ctx->progress_done);
  820. oi.typep = &type;
  821. if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
  822. die(_("unable to get type of object %s"), oid_to_hex(oid));
  823. if (type != OBJ_COMMIT)
  824. return 0;
  825. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  826. oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
  827. ctx->oids.nr++;
  828. return 0;
  829. }
  830. static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
  831. {
  832. struct commit_list *parent;
  833. for (parent = commit->parents; parent; parent = parent->next) {
  834. if (!(parent->item->object.flags & REACHABLE)) {
  835. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  836. oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
  837. ctx->oids.nr++;
  838. parent->item->object.flags |= REACHABLE;
  839. }
  840. }
  841. }
  842. static void close_reachable(struct write_commit_graph_context *ctx)
  843. {
  844. int i;
  845. struct commit *commit;
  846. if (ctx->report_progress)
  847. ctx->progress = start_delayed_progress(
  848. _("Loading known commits in commit graph"),
  849. ctx->oids.nr);
  850. for (i = 0; i < ctx->oids.nr; i++) {
  851. display_progress(ctx->progress, i + 1);
  852. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  853. if (commit)
  854. commit->object.flags |= REACHABLE;
  855. }
  856. stop_progress(&ctx->progress);
  857. /*
  858. * As this loop runs, ctx->oids.nr may grow, but not more
  859. * than the number of missing commits in the reachable
  860. * closure.
  861. */
  862. if (ctx->report_progress)
  863. ctx->progress = start_delayed_progress(
  864. _("Expanding reachable commits in commit graph"),
  865. 0);
  866. for (i = 0; i < ctx->oids.nr; i++) {
  867. display_progress(ctx->progress, i + 1);
  868. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  869. if (!commit)
  870. continue;
  871. if (ctx->split) {
  872. if (!parse_commit(commit) &&
  873. commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
  874. add_missing_parents(ctx, commit);
  875. } else if (!parse_commit_no_graph(commit))
  876. add_missing_parents(ctx, commit);
  877. }
  878. stop_progress(&ctx->progress);
  879. if (ctx->report_progress)
  880. ctx->progress = start_delayed_progress(
  881. _("Clearing commit marks in commit graph"),
  882. ctx->oids.nr);
  883. for (i = 0; i < ctx->oids.nr; i++) {
  884. display_progress(ctx->progress, i + 1);
  885. commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
  886. if (commit)
  887. commit->object.flags &= ~REACHABLE;
  888. }
  889. stop_progress(&ctx->progress);
  890. }
  891. static void compute_generation_numbers(struct write_commit_graph_context *ctx)
  892. {
  893. int i;
  894. struct commit_list *list = NULL;
  895. if (ctx->report_progress)
  896. ctx->progress = start_delayed_progress(
  897. _("Computing commit graph generation numbers"),
  898. ctx->commits.nr);
  899. for (i = 0; i < ctx->commits.nr; i++) {
  900. display_progress(ctx->progress, i + 1);
  901. if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
  902. ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
  903. continue;
  904. commit_list_insert(ctx->commits.list[i], &list);
  905. while (list) {
  906. struct commit *current = list->item;
  907. struct commit_list *parent;
  908. int all_parents_computed = 1;
  909. uint32_t max_generation = 0;
  910. for (parent = current->parents; parent; parent = parent->next) {
  911. if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
  912. parent->item->generation == GENERATION_NUMBER_ZERO) {
  913. all_parents_computed = 0;
  914. commit_list_insert(parent->item, &list);
  915. break;
  916. } else if (parent->item->generation > max_generation) {
  917. max_generation = parent->item->generation;
  918. }
  919. }
  920. if (all_parents_computed) {
  921. current->generation = max_generation + 1;
  922. pop_commit(&list);
  923. if (current->generation > GENERATION_NUMBER_MAX)
  924. current->generation = GENERATION_NUMBER_MAX;
  925. }
  926. }
  927. }
  928. stop_progress(&ctx->progress);
  929. }
  930. static int add_ref_to_list(const char *refname,
  931. const struct object_id *oid,
  932. int flags, void *cb_data)
  933. {
  934. struct string_list *list = (struct string_list *)cb_data;
  935. string_list_append(list, oid_to_hex(oid));
  936. return 0;
  937. }
  938. int write_commit_graph_reachable(const char *obj_dir,
  939. enum commit_graph_write_flags flags,
  940. const struct split_commit_graph_opts *split_opts)
  941. {
  942. struct string_list list = STRING_LIST_INIT_DUP;
  943. int result;
  944. for_each_ref(add_ref_to_list, &list);
  945. result = write_commit_graph(obj_dir, NULL, &list,
  946. flags, split_opts);
  947. string_list_clear(&list, 0);
  948. return result;
  949. }
  950. static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
  951. struct string_list *pack_indexes)
  952. {
  953. uint32_t i;
  954. struct strbuf progress_title = STRBUF_INIT;
  955. struct strbuf packname = STRBUF_INIT;
  956. int dirlen;
  957. strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
  958. dirlen = packname.len;
  959. if (ctx->report_progress) {
  960. strbuf_addf(&progress_title,
  961. Q_("Finding commits for commit graph in %d pack",
  962. "Finding commits for commit graph in %d packs",
  963. pack_indexes->nr),
  964. pack_indexes->nr);
  965. ctx->progress = start_delayed_progress(progress_title.buf, 0);
  966. ctx->progress_done = 0;
  967. }
  968. for (i = 0; i < pack_indexes->nr; i++) {
  969. struct packed_git *p;
  970. strbuf_setlen(&packname, dirlen);
  971. strbuf_addstr(&packname, pack_indexes->items[i].string);
  972. p = add_packed_git(packname.buf, packname.len, 1);
  973. if (!p) {
  974. error(_("error adding pack %s"), packname.buf);
  975. return -1;
  976. }
  977. if (open_pack_index(p)) {
  978. error(_("error opening index for %s"), packname.buf);
  979. return -1;
  980. }
  981. for_each_object_in_pack(p, add_packed_commits, ctx,
  982. FOR_EACH_OBJECT_PACK_ORDER);
  983. close_pack(p);
  984. free(p);
  985. }
  986. stop_progress(&ctx->progress);
  987. strbuf_release(&progress_title);
  988. strbuf_release(&packname);
  989. return 0;
  990. }
  991. static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
  992. struct string_list *commit_hex)
  993. {
  994. uint32_t i;
  995. struct strbuf progress_title = STRBUF_INIT;
  996. if (ctx->report_progress) {
  997. strbuf_addf(&progress_title,
  998. Q_("Finding commits for commit graph from %d ref",
  999. "Finding commits for commit graph from %d refs",
  1000. commit_hex->nr),
  1001. commit_hex->nr);
  1002. ctx->progress = start_delayed_progress(
  1003. progress_title.buf,
  1004. commit_hex->nr);
  1005. }
  1006. for (i = 0; i < commit_hex->nr; i++) {
  1007. const char *end;
  1008. struct object_id oid;
  1009. struct commit *result;
  1010. display_progress(ctx->progress, i + 1);
  1011. if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
  1012. (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
  1013. ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
  1014. oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
  1015. ctx->oids.nr++;
  1016. } else if (ctx->check_oids) {
  1017. error(_("invalid commit object id: %s"),
  1018. commit_hex->items[i].string);
  1019. return -1;
  1020. }
  1021. }
  1022. stop_progress(&ctx->progress);
  1023. strbuf_release(&progress_title);
  1024. return 0;
  1025. }
  1026. static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
  1027. {
  1028. if (ctx->report_progress)
  1029. ctx->progress = start_delayed_progress(
  1030. _("Finding commits for commit graph among packed objects"),
  1031. ctx->approx_nr_objects);
  1032. for_each_packed_object(add_packed_commits, ctx,
  1033. FOR_EACH_OBJECT_PACK_ORDER);
  1034. if (ctx->progress_done < ctx->approx_nr_objects)
  1035. display_progress(ctx->progress, ctx->approx_nr_objects);
  1036. stop_progress(&ctx->progress);
  1037. }
  1038. static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
  1039. {
  1040. uint32_t i, count_distinct = 1;
  1041. if (ctx->report_progress)
  1042. ctx->progress = start_delayed_progress(
  1043. _("Counting distinct commits in commit graph"),
  1044. ctx->oids.nr);
  1045. display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
  1046. QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
  1047. for (i = 1; i < ctx->oids.nr; i++) {
  1048. display_progress(ctx->progress, i + 1);
  1049. if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
  1050. if (ctx->split) {
  1051. struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
  1052. if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
  1053. continue;
  1054. }
  1055. count_distinct++;
  1056. }
  1057. }
  1058. stop_progress(&ctx->progress);
  1059. return count_distinct;
  1060. }
  1061. static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
  1062. {
  1063. uint32_t i;
  1064. ctx->num_extra_edges = 0;
  1065. if (ctx->report_progress)
  1066. ctx->progress = start_delayed_progress(
  1067. _("Finding extra edges in commit graph"),
  1068. ctx->oids.nr);
  1069. for (i = 0; i < ctx->oids.nr; i++) {
  1070. unsigned int num_parents;
  1071. display_progress(ctx->progress, i + 1);
  1072. if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
  1073. continue;
  1074. ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
  1075. ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
  1076. if (ctx->split &&
  1077. ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
  1078. continue;
  1079. parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
  1080. num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
  1081. if (num_parents > 2)
  1082. ctx->num_extra_edges += num_parents - 1;
  1083. ctx->commits.nr++;
  1084. }
  1085. stop_progress(&ctx->progress);
  1086. }
  1087. static int write_graph_chunk_base_1(struct hashfile *f,
  1088. struct commit_graph *g)
  1089. {
  1090. int num = 0;
  1091. if (!g)
  1092. return 0;
  1093. num = write_graph_chunk_base_1(f, g->base_graph);
  1094. hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
  1095. return num + 1;
  1096. }
  1097. static int write_graph_chunk_base(struct hashfile *f,
  1098. struct write_commit_graph_context *ctx)
  1099. {
  1100. int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
  1101. if (num != ctx->num_commit_graphs_after - 1) {
  1102. error(_("failed to write correct number of base graph ids"));
  1103. return -1;
  1104. }
  1105. return 0;
  1106. }
  1107. static int write_commit_graph_file(struct write_commit_graph_context *ctx)
  1108. {
  1109. uint32_t i;
  1110. int fd;
  1111. struct hashfile *f;
  1112. struct lock_file lk = LOCK_INIT;
  1113. uint32_t chunk_ids[6];
  1114. uint64_t chunk_offsets[6];
  1115. const unsigned hashsz = the_hash_algo->rawsz;
  1116. struct strbuf progress_title = STRBUF_INIT;
  1117. int num_chunks = 3;
  1118. struct object_id file_hash;
  1119. if (ctx->split) {
  1120. struct strbuf tmp_file = STRBUF_INIT;
  1121. strbuf_addf(&tmp_file,
  1122. "%s/info/commit-graphs/tmp_graph_XXXXXX",
  1123. ctx->obj_dir);
  1124. ctx->graph_name = strbuf_detach(&tmp_file, NULL);
  1125. } else {
  1126. ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
  1127. }
  1128. if (safe_create_leading_directories(ctx->graph_name)) {
  1129. UNLEAK(ctx->graph_name);
  1130. error(_("unable to create leading directories of %s"),
  1131. ctx->graph_name);
  1132. return -1;
  1133. }
  1134. if (ctx->split) {
  1135. char *lock_name = get_chain_filename(ctx->obj_dir);
  1136. hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
  1137. fd = git_mkstemp_mode(ctx->graph_name, 0444);
  1138. if (fd < 0) {
  1139. error(_("unable to create '%s'"), ctx->graph_name);
  1140. return -1;
  1141. }
  1142. f = hashfd(fd, ctx->graph_name);
  1143. } else {
  1144. hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
  1145. fd = lk.tempfile->fd;
  1146. f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
  1147. }
  1148. chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
  1149. chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
  1150. chunk_ids[2] = GRAPH_CHUNKID_DATA;
  1151. if (ctx->num_extra_edges) {
  1152. chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
  1153. num_chunks++;
  1154. }
  1155. if (ctx->num_commit_graphs_after > 1) {
  1156. chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
  1157. num_chunks++;
  1158. }
  1159. chunk_ids[num_chunks] = 0;
  1160. chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
  1161. chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
  1162. chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
  1163. chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
  1164. num_chunks = 3;
  1165. if (ctx->num_extra_edges) {
  1166. chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
  1167. 4 * ctx->num_extra_edges;
  1168. num_chunks++;
  1169. }
  1170. if (ctx->num_commit_graphs_after > 1) {
  1171. chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
  1172. hashsz * (ctx->num_commit_graphs_after - 1);
  1173. num_chunks++;
  1174. }
  1175. hashwrite_be32(f, GRAPH_SIGNATURE);
  1176. hashwrite_u8(f, GRAPH_VERSION);
  1177. hashwrite_u8(f, oid_version());
  1178. hashwrite_u8(f, num_chunks);
  1179. hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
  1180. for (i = 0; i <= num_chunks; i++) {
  1181. uint32_t chunk_write[3];
  1182. chunk_write[0] = htonl(chunk_ids[i]);
  1183. chunk_write[1] = htonl(chunk_offsets[i] >> 32);
  1184. chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
  1185. hashwrite(f, chunk_write, 12);
  1186. }
  1187. if (ctx->report_progress) {
  1188. strbuf_addf(&progress_title,
  1189. Q_("Writing out commit graph in %d pass",
  1190. "Writing out commit graph in %d passes",
  1191. num_chunks),
  1192. num_chunks);
  1193. ctx->progress = start_delayed_progress(
  1194. progress_title.buf,
  1195. num_chunks * ctx->commits.nr);
  1196. }
  1197. write_graph_chunk_fanout(f, ctx);
  1198. write_graph_chunk_oids(f, hashsz, ctx);
  1199. write_graph_chunk_data(f, hashsz, ctx);
  1200. if (ctx->num_extra_edges)
  1201. write_graph_chunk_extra_edges(f, ctx);
  1202. if (ctx->num_commit_graphs_after > 1 &&
  1203. write_graph_chunk_base(f, ctx)) {
  1204. return -1;
  1205. }
  1206. stop_progress(&ctx->progress);
  1207. strbuf_release(&progress_title);
  1208. if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
  1209. char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
  1210. char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
  1211. free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
  1212. free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
  1213. ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
  1214. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
  1215. }
  1216. close_commit_graph(ctx->r->objects);
  1217. finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
  1218. if (ctx->split) {
  1219. FILE *chainf = fdopen_lock_file(&lk, "w");
  1220. char *final_graph_name;
  1221. int result;
  1222. close(fd);
  1223. if (!chainf) {
  1224. error(_("unable to open commit-graph chain file"));
  1225. return -1;
  1226. }
  1227. if (ctx->base_graph_name) {
  1228. const char *dest = ctx->commit_graph_filenames_after[
  1229. ctx->num_commit_graphs_after - 2];
  1230. if (strcmp(ctx->base_graph_name, dest)) {
  1231. result = rename(ctx->base_graph_name, dest);
  1232. if (result) {
  1233. error(_("failed to rename base commit-graph file"));
  1234. return -1;
  1235. }
  1236. }
  1237. } else {
  1238. char *graph_name = get_commit_graph_filename(ctx->obj_dir);
  1239. unlink(graph_name);
  1240. }
  1241. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
  1242. final_graph_name = get_split_graph_filename(ctx->obj_dir,
  1243. ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
  1244. ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
  1245. result = rename(ctx->graph_name, final_graph_name);
  1246. for (i = 0; i < ctx->num_commit_graphs_after; i++)
  1247. fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
  1248. if (result) {
  1249. error(_("failed to rename temporary commit-graph file"));
  1250. return -1;
  1251. }
  1252. }
  1253. commit_lock_file(&lk);
  1254. return 0;
  1255. }
  1256. static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
  1257. {
  1258. struct commit_graph *g;
  1259. uint32_t num_commits;
  1260. uint32_t i;
  1261. int max_commits = 0;
  1262. int size_mult = 2;
  1263. if (ctx->split_opts) {
  1264. max_commits = ctx->split_opts->max_commits;
  1265. size_mult = ctx->split_opts->size_multiple;
  1266. }
  1267. g = ctx->r->objects->commit_graph;
  1268. num_commits = ctx->commits.nr;
  1269. ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
  1270. while (g && (g->num_commits <= size_mult * num_commits ||
  1271. (max_commits && num_commits > max_commits))) {
  1272. if (strcmp(g->obj_dir, ctx->obj_dir))
  1273. break;
  1274. num_commits += g->num_commits;
  1275. g = g->base_graph;
  1276. ctx->num_commit_graphs_after--;
  1277. }
  1278. ctx->new_base_graph = g;
  1279. if (ctx->num_commit_graphs_after == 2) {
  1280. char *old_graph_name = get_commit_graph_filename(g->obj_dir);
  1281. if (!strcmp(g->filename, old_graph_name) &&
  1282. strcmp(g->obj_dir, ctx->obj_dir)) {
  1283. ctx->num_commit_graphs_after = 1;
  1284. ctx->new_base_graph = NULL;
  1285. }
  1286. free(old_graph_name);
  1287. }
  1288. ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
  1289. ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
  1290. for (i = 0; i < ctx->num_commit_graphs_after &&
  1291. i < ctx->num_commit_graphs_before; i++)
  1292. ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
  1293. i = ctx->num_commit_graphs_before - 1;
  1294. g = ctx->r->objects->commit_graph;
  1295. while (g) {
  1296. if (i < ctx->num_commit_graphs_after)
  1297. ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
  1298. i--;
  1299. g = g->base_graph;
  1300. }
  1301. }
  1302. static void merge_commit_graph(struct write_commit_graph_context *ctx,
  1303. struct commit_graph *g)
  1304. {
  1305. uint32_t i;
  1306. uint32_t offset = g->num_commits_in_base;
  1307. ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
  1308. for (i = 0; i < g->num_commits; i++) {
  1309. struct object_id oid;
  1310. struct commit *result;
  1311. display_progress(ctx->progress, i + 1);
  1312. load_oid_from_graph(g, i + offset, &oid);
  1313. /* only add commits if they still exist in the repo */
  1314. result = lookup_commit_reference_gently(ctx->r, &oid, 1);
  1315. if (result) {
  1316. ctx->commits.list[ctx->commits.nr] = result;
  1317. ctx->commits.nr++;
  1318. }
  1319. }
  1320. }
  1321. static int commit_compare(const void *_a, const void *_b)
  1322. {
  1323. const struct commit *a = *(const struct commit **)_a;
  1324. const struct commit *b = *(const struct commit **)_b;
  1325. return oidcmp(&a->object.oid, &b->object.oid);
  1326. }
  1327. static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
  1328. {
  1329. uint32_t i;
  1330. if (ctx->report_progress)
  1331. ctx->progress = start_delayed_progress(
  1332. _("Scanning merged commits"),
  1333. ctx->commits.nr);
  1334. QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
  1335. ctx->num_extra_edges = 0;
  1336. for (i = 0; i < ctx->commits.nr; i++) {
  1337. display_progress(ctx->progress, i);
  1338. if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
  1339. &ctx->commits.list[i]->object.oid)) {
  1340. die(_("unexpected duplicate commit id %s"),
  1341. oid_to_hex(&ctx->commits.list[i]->object.oid));
  1342. } else {
  1343. unsigned int num_parents;
  1344. num_parents = commit_list_count(ctx->commits.list[i]->parents);
  1345. if (num_parents > 2)
  1346. ctx->num_extra_edges += num_parents - 1;
  1347. }
  1348. }
  1349. stop_progress(&ctx->progress);
  1350. }
  1351. static void merge_commit_graphs(struct write_commit_graph_context *ctx)
  1352. {
  1353. struct commit_graph *g = ctx->r->objects->commit_graph;
  1354. uint32_t current_graph_number = ctx->num_commit_graphs_before;
  1355. struct strbuf progress_title = STRBUF_INIT;
  1356. while (g && current_graph_number >= ctx->num_commit_graphs_after) {
  1357. current_graph_number--;
  1358. if (ctx->report_progress) {
  1359. strbuf_addstr(&progress_title, _("Merging commit-graph"));
  1360. ctx->progress = start_delayed_progress(progress_title.buf, 0);
  1361. }
  1362. merge_commit_graph(ctx, g);
  1363. stop_progress(&ctx->progress);
  1364. strbuf_release(&progress_title);
  1365. g = g->base_graph;
  1366. }
  1367. if (g) {
  1368. ctx->new_base_graph = g;
  1369. ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
  1370. }
  1371. if (ctx->new_base_graph)
  1372. ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
  1373. sort_and_scan_merged_commits(ctx);
  1374. }
  1375. static void mark_commit_graphs(struct write_commit_graph_context *ctx)
  1376. {
  1377. uint32_t i;
  1378. time_t now = time(NULL);
  1379. for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
  1380. struct stat st;
  1381. struct utimbuf updated_time;
  1382. stat(ctx->commit_graph_filenames_before[i], &st);
  1383. updated_time.actime = st.st_atime;
  1384. updated_time.modtime = now;
  1385. utime(ctx->commit_graph_filenames_before[i], &updated_time);
  1386. }
  1387. }
  1388. static void expire_commit_graphs(struct write_commit_graph_context *ctx)
  1389. {
  1390. struct strbuf path = STRBUF_INIT;
  1391. DIR *dir;
  1392. struct dirent *de;
  1393. size_t dirnamelen;
  1394. timestamp_t expire_time = time(NULL);
  1395. if (ctx->split_opts && ctx->split_opts->expire_time)
  1396. expire_time -= ctx->split_opts->expire_time;
  1397. if (!ctx->split) {
  1398. char *chain_file_name = get_chain_filename(ctx->obj_dir);
  1399. unlink(chain_file_name);
  1400. free(chain_file_name);
  1401. ctx->num_commit_graphs_after = 0;
  1402. }
  1403. strbuf_addstr(&path, ctx->obj_dir);
  1404. strbuf_addstr(&path, "/info/commit-graphs");
  1405. dir = opendir(path.buf);
  1406. if (!dir)
  1407. goto out;
  1408. strbuf_addch(&path, '/');
  1409. dirnamelen = path.len;
  1410. while ((de = readdir(dir)) != NULL) {
  1411. struct stat st;
  1412. uint32_t i, found = 0;
  1413. strbuf_setlen(&path, dirnamelen);
  1414. strbuf_addstr(&path, de->d_name);
  1415. stat(path.buf, &st);
  1416. if (st.st_mtime > expire_time)
  1417. continue;
  1418. if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
  1419. continue;
  1420. for (i = 0; i < ctx->num_commit_graphs_after; i++) {
  1421. if (!strcmp(ctx->commit_graph_filenames_after[i],
  1422. path.buf)) {
  1423. found = 1;
  1424. break;
  1425. }
  1426. }
  1427. if (!found)
  1428. unlink(path.buf);
  1429. }
  1430. out:
  1431. strbuf_release(&path);
  1432. }
  1433. int write_commit_graph(const char *obj_dir,
  1434. struct string_list *pack_indexes,
  1435. struct string_list *commit_hex,
  1436. enum commit_graph_write_flags flags,
  1437. const struct split_commit_graph_opts *split_opts)
  1438. {
  1439. struct write_commit_graph_context *ctx;
  1440. uint32_t i, count_distinct = 0;
  1441. size_t len;
  1442. int res = 0;
  1443. if (!commit_graph_compatible(the_repository))
  1444. return 0;
  1445. ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
  1446. ctx->r = the_repository;
  1447. /* normalize object dir with no trailing slash */
  1448. ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
  1449. normalize_path_copy(ctx->obj_dir, obj_dir);
  1450. len = strlen(ctx->obj_dir);
  1451. if (len && ctx->obj_dir[len - 1] == '/')
  1452. ctx->obj_dir[len - 1] = 0;
  1453. ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
  1454. ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
  1455. ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
  1456. ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
  1457. ctx->split_opts = split_opts;
  1458. if (ctx->split) {
  1459. struct commit_graph *g;
  1460. prepare_commit_graph(ctx->r);
  1461. g = ctx->r->objects->commit_graph;
  1462. while (g) {
  1463. ctx->num_commit_graphs_before++;
  1464. g = g->base_graph;
  1465. }
  1466. if (ctx->num_commit_graphs_before) {
  1467. ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
  1468. i = ctx->num_commit_graphs_before;
  1469. g = ctx->r->objects->commit_graph;
  1470. while (g) {
  1471. ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
  1472. g = g->base_graph;
  1473. }
  1474. }
  1475. }
  1476. ctx->approx_nr_objects = approximate_object_count();
  1477. ctx->oids.alloc = ctx->approx_nr_objects / 32;
  1478. if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
  1479. ctx->oids.alloc = split_opts->max_commits;
  1480. if (ctx->append) {
  1481. prepare_commit_graph_one(ctx->r, ctx->obj_dir);
  1482. if (ctx->r->objects->commit_graph)
  1483. ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
  1484. }
  1485. if (ctx->oids.alloc < 1024)
  1486. ctx->oids.alloc = 1024;
  1487. ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
  1488. if (ctx->append && ctx->r->objects->commit_graph) {
  1489. struct commit_graph *g = ctx->r->objects->commit_graph;
  1490. for (i = 0; i < g->num_commits; i++) {
  1491. const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
  1492. hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
  1493. }
  1494. }
  1495. if (pack_indexes) {
  1496. if ((res = fill_oids_from_packs(ctx, pack_indexes)))
  1497. goto cleanup;
  1498. }
  1499. if (commit_hex) {
  1500. if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
  1501. goto cleanup;
  1502. }
  1503. if (!pack_indexes && !commit_hex)
  1504. fill_oids_from_all_packs(ctx);
  1505. close_reachable(ctx);
  1506. count_distinct = count_distinct_commits(ctx);
  1507. if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
  1508. error(_("the commit graph format cannot write %d commits"), count_distinct);
  1509. res = -1;
  1510. goto cleanup;
  1511. }
  1512. ctx->commits.alloc = count_distinct;
  1513. ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
  1514. copy_oids_to_commits(ctx);
  1515. if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
  1516. error(_("too many commits to write graph"));
  1517. res = -1;
  1518. goto cleanup;
  1519. }
  1520. if (!ctx->commits.nr)
  1521. goto cleanup;
  1522. if (ctx->split) {
  1523. split_graph_merge_strategy(ctx);
  1524. merge_commit_graphs(ctx);
  1525. } else
  1526. ctx->num_commit_graphs_after = 1;
  1527. compute_generation_numbers(ctx);
  1528. res = write_commit_graph_file(ctx);
  1529. if (ctx->split)
  1530. mark_commit_graphs(ctx);
  1531. expire_commit_graphs(ctx);
  1532. cleanup:
  1533. free(ctx->graph_name);
  1534. free(ctx->commits.list);
  1535. free(ctx->oids.list);
  1536. free(ctx->obj_dir);
  1537. if (ctx->commit_graph_filenames_after) {
  1538. for (i = 0; i < ctx->num_commit_graphs_after; i++) {
  1539. free(ctx->commit_graph_filenames_after[i]);
  1540. free(ctx->commit_graph_hash_after[i]);
  1541. }
  1542. for (i = 0; i < ctx->num_commit_graphs_before; i++)
  1543. free(ctx->commit_graph_filenames_before[i]);
  1544. free(ctx->commit_graph_filenames_after);
  1545. free(ctx->commit_graph_filenames_before);
  1546. free(ctx->commit_graph_hash_after);
  1547. }
  1548. free(ctx);
  1549. return res;
  1550. }
  1551. #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
  1552. static int verify_commit_graph_error;
  1553. static void graph_report(const char *fmt, ...)
  1554. {
  1555. va_list ap;
  1556. verify_commit_graph_error = 1;
  1557. va_start(ap, fmt);
  1558. vfprintf(stderr, fmt, ap);
  1559. fprintf(stderr, "\n");
  1560. va_end(ap);
  1561. }
  1562. #define GENERATION_ZERO_EXISTS 1
  1563. #define GENERATION_NUMBER_EXISTS 2
  1564. int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
  1565. {
  1566. uint32_t i, cur_fanout_pos = 0;
  1567. struct object_id prev_oid, cur_oid, checksum;
  1568. int generation_zero = 0;
  1569. struct hashfile *f;
  1570. int devnull;
  1571. struct progress *progress = NULL;
  1572. int local_error = 0;
  1573. if (!g) {
  1574. graph_report("no commit-graph file loaded");
  1575. return 1;
  1576. }
  1577. verify_commit_graph_error = verify_commit_graph_lite(g);
  1578. if (verify_commit_graph_error)
  1579. return verify_commit_graph_error;
  1580. devnull = open("/dev/null", O_WRONLY);
  1581. f = hashfd(devnull, NULL);
  1582. hashwrite(f, g->data, g->data_len - g->hash_len);
  1583. finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
  1584. if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
  1585. graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
  1586. verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
  1587. }
  1588. for (i = 0; i < g->num_commits; i++) {
  1589. struct commit *graph_commit;
  1590. hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
  1591. if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
  1592. graph_report(_("commit-graph has incorrect OID order: %s then %s"),
  1593. oid_to_hex(&prev_oid),
  1594. oid_to_hex(&cur_oid));
  1595. oidcpy(&prev_oid, &cur_oid);
  1596. while (cur_oid.hash[0] > cur_fanout_pos) {
  1597. uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
  1598. if (i != fanout_value)
  1599. graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
  1600. cur_fanout_pos, fanout_value, i);
  1601. cur_fanout_pos++;
  1602. }
  1603. graph_commit = lookup_commit(r, &cur_oid);
  1604. if (!parse_commit_in_graph_one(r, g, graph_commit))
  1605. graph_report(_("failed to parse commit %s from commit-graph"),
  1606. oid_to_hex(&cur_oid));
  1607. }
  1608. while (cur_fanout_pos < 256) {
  1609. uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
  1610. if (g->num_commits != fanout_value)
  1611. graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
  1612. cur_fanout_pos, fanout_value, i);
  1613. cur_fanout_pos++;
  1614. }
  1615. if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
  1616. return verify_commit_graph_error;
  1617. if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
  1618. progress = start_progress(_("Verifying commits in commit graph"),
  1619. g->num_commits);
  1620. for (i = 0; i < g->num_commits; i++) {
  1621. struct commit *graph_commit, *odb_commit;
  1622. struct commit_list *graph_parents, *odb_parents;
  1623. uint32_t max_generation = 0;
  1624. display_progress(progress, i + 1);
  1625. hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
  1626. graph_commit = lookup_commit(r, &cur_oid);
  1627. odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
  1628. if (parse_commit_internal(odb_commit, 0, 0)) {
  1629. graph_report(_("failed to parse commit %s from object database for commit-graph"),
  1630. oid_to_hex(&cur_oid));
  1631. continue;
  1632. }
  1633. if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
  1634. get_commit_tree_oid(odb_commit)))
  1635. graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
  1636. oid_to_hex(&cur_oid),
  1637. oid_to_hex(get_commit_tree_oid(graph_commit)),
  1638. oid_to_hex(get_commit_tree_oid(odb_commit)));
  1639. graph_parents = graph_commit->parents;
  1640. odb_parents = odb_commit->parents;
  1641. while (graph_parents) {
  1642. if (odb_parents == NULL) {
  1643. graph_report(_("commit-graph parent list for commit %s is too long"),
  1644. oid_to_hex(&cur_oid));
  1645. break;
  1646. }
  1647. /* parse parent in case it is in a base graph */
  1648. parse_commit_in_graph_one(r, g, graph_parents->item);
  1649. if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
  1650. graph_report(_("commit-graph parent for %s is %s != %s"),
  1651. oid_to_hex(&cur_oid),
  1652. oid_to_hex(&graph_parents->item->object.oid),
  1653. oid_to_hex(&odb_parents->item->object.oid));
  1654. if (graph_parents->item->generation > max_generation)
  1655. max_generation = graph_parents->item->generation;
  1656. graph_parents = graph_parents->next;
  1657. odb_parents = odb_parents->next;
  1658. }
  1659. if (odb_parents != NULL)
  1660. graph_report(_("commit-graph parent list for commit %s terminates early"),
  1661. oid_to_hex(&cur_oid));
  1662. if (!graph_commit->generation) {
  1663. if (generation_zero == GENERATION_NUMBER_EXISTS)
  1664. graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
  1665. oid_to_hex(&cur_oid));
  1666. generation_zero = GENERATION_ZERO_EXISTS;
  1667. } else if (generation_zero == GENERATION_ZERO_EXISTS)
  1668. graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
  1669. oid_to_hex(&cur_oid));
  1670. if (generation_zero == GENERATION_ZERO_EXISTS)
  1671. continue;
  1672. /*
  1673. * If one of our parents has generation GENERATION_NUMBER_MAX, then
  1674. * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
  1675. * extra logic in the following condition.
  1676. */
  1677. if (max_generation == GENERATION_NUMBER_MAX)
  1678. max_generation--;
  1679. if (graph_commit->generation != max_generation + 1)
  1680. graph_report(_("commit-graph generation for commit %s is %u != %u"),
  1681. oid_to_hex(&cur_oid),
  1682. graph_commit->generation,
  1683. max_generation + 1);
  1684. if (graph_commit->date != odb_commit->date)
  1685. graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
  1686. oid_to_hex(&cur_oid),
  1687. graph_commit->date,
  1688. odb_commit->date);
  1689. }
  1690. stop_progress(&progress);
  1691. local_error = verify_commit_graph_error;
  1692. if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
  1693. local_error |= verify_commit_graph(r, g->base_graph, flags);
  1694. return local_error;
  1695. }
  1696. void free_commit_graph(struct commit_graph *g)
  1697. {
  1698. if (!g)
  1699. return;
  1700. if (g->graph_fd >= 0) {
  1701. munmap((void *)g->data, g->data_len);
  1702. g->data = NULL;
  1703. close(g->graph_fd);
  1704. }
  1705. free(g->filename);
  1706. free(g);
  1707. }
  1708. void disable_commit_graph(struct repository *r)
  1709. {
  1710. r->commit_graph_disabled = 1;
  1711. }