commit-graph: always load commit-graph information

Most code paths load commits using lookup_commit() and then parse_commit(). In some cases, including some branch lookups, the commit is parsed using parse_object_buffer() which side-steps parse_commit() in favor of parse_commit_buffer(). With generation numbers in the commit-graph, we need to ensure that any commit that exists in the commit-graph file has its generation number loaded. Create new load_commit_graph_info() method to fill in the information for a commit that exists only in the commit-graph file. Call it from parse_commit_buffer() after loading the other commit information from the given buffer. Only fill this information when specified by the 'check_graph' parameter. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 1, 2018 at 12:47 UTC e2838d85b6d35592ff5851d67f0232a78083ada7
6 files changed +47 -20
commit-graph.c
+31 -15
@@ -245,6 +245,13 @@ static struct commit_list **insert_parent_or_die(struct commit_graph *g,
245 return &commit_list_insert(c, pptr)->next;
246 }
247
248 +static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
249 +{
250 + const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
251 + item->graph_pos = pos;
252 + item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
253 +}
254 +
255 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
256 {
257 uint32_t edge_value;
@@ -292,31 +299,40 @@ static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uin
299 return 1;
300 }
301
302 +static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
303 +{
304 + if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
305 + *pos = item->graph_pos;
306 + return 1;
307 + } else {
308 + return bsearch_graph(g, &(item->object.oid), pos);
309 + }
310 +}
311 +
312 int parse_commit_in_graph(struct commit *item)
313 {
314 + uint32_t pos;
315 +
316 if (!core_commit_graph)
317 return 0;
318 if (item->object.parsed)
319 return 1;
301 -
320 prepare_commit_graph();
303 - if (commit_graph) {
304 - uint32_t pos;
305 - int found;
306 - if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
307 - pos = item->graph_pos;
308 - found = 1;
309 - } else {
310 - found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
311 - }
312 -
313 - if (found)
314 - return fill_commit_in_graph(item, commit_graph, pos);
315 - }
316 -
321 + if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
322 + return fill_commit_in_graph(item, commit_graph, pos);
323 return 0;
324 }
325
326 +void load_commit_graph_info(struct commit *item)
327 +{
328 + uint32_t pos;
329 + if (!core_commit_graph)
330 + return;
331 + prepare_commit_graph();
332 + if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
333 + fill_commit_graph_info(item, commit_graph, pos);
334 +}
335 +
336 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
337 {
338 struct object_id oid;
commit-graph.h
+8
@@ -17,6 +17,14 @@ char *get_commit_graph_filename(const char *obj_dir);
17 */
18 int parse_commit_in_graph(struct commit *item);
19
20 +/*
21 + * It is possible that we loaded commit contents from the commit buffer,
22 + * but we also want to ensure the commit-graph content is correctly
23 + * checked and filled. Fill the graph_pos and generation members of
24 + * the given commit.
25 + */
26 +void load_commit_graph_info(struct commit *item);
27 +
28 struct tree *get_commit_tree_in_graph(const struct commit *c);
29
30 struct commit_graph {
commit.c
+5 -2
@@ -331,7 +331,7 @@ const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
331 return ret;
332 }
333
334 -int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
334 +int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
335 {
336 const char *tail = buffer;
337 const char *bufptr = buffer;
@@ -386,6 +386,9 @@ int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long s
386 }
387 item->date = parse_commit_date(bufptr, tail);
388
389 + if (check_graph)
390 + load_commit_graph_info(item);
391 +
392 return 0;
393 }
394
@@ -412,7 +415,7 @@ int parse_commit_gently(struct commit *item, int quiet_on_missing)
415 return error("Object %s not a commit",
416 oid_to_hex(&item->object.oid));
417 }
415 - ret = parse_commit_buffer(item, buffer, size);
418 + ret = parse_commit_buffer(item, buffer, size, 0);
419 if (save_commit_buffer && !ret) {
420 set_commit_buffer(item, buffer, size);
421 return 0;
commit.h
+1 -1
@@ -72,7 +72,7 @@ struct commit *lookup_commit_reference_by_name(const char *name);
72 */
73 struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name);
74
75 -int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
75 +int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph);
76 int parse_commit_gently(struct commit *item, int quiet_on_missing);
77 static inline int parse_commit(struct commit *item)
78 {
object.c
+1 -1
@@ -207,7 +207,7 @@ struct object *parse_object_buffer(const struct object_id *oid, enum object_type
207 } else if (type == OBJ_COMMIT) {
208 struct commit *commit = lookup_commit(oid);
209 if (commit) {
210 - if (parse_commit_buffer(commit, buffer, size))
210 + if (parse_commit_buffer(commit, buffer, size, 1))
211 return NULL;
212 if (!get_cached_commit_buffer(commit, NULL)) {
213 set_commit_buffer(commit, buffer, size);
sha1_file.c
+1 -1
@@ -1755,7 +1755,7 @@ static void check_commit(const void *buf, size_t size)
1755 {
1756 struct commit c;
1757 memset(&c, 0, sizeof(c));
1758 - if (parse_commit_buffer(&c, buf, size))
1758 + if (parse_commit_buffer(&c, buf, size, 0))
1759 die("corrupt commit");
1760 }
1761