Skip to content

Commit a92d852

Browse files
ttaylorrgitster
authored andcommitted
commit-graph: pass repo_settings instead of repository
The parse_commit_graph() function takes a 'struct repository *' pointer, but it only ever accesses config settings (either directly or through the .settings field of the repo struct). Move all relevant config settings into the repo_settings struct, and update parse_commit_graph() and its existing callers so that it takes 'struct repo_settings *' instead. Callers of parse_commit_graph() will now need to call prepare_repo_settings() themselves, or initialize a 'struct repo_settings' directly. Prior to ab14d06 (commit-graph: pass a 'struct repository *' in more places, 2020-09-09), parsing a commit-graph was a pure function depending only on the contents of the commit-graph itself. Commit ab14d06 introduced a dependency on a `struct repository` pointer, and later commits such as b66d847 (commit-graph: respect 'commitGraph.readChangedPaths', 2020-09-09) added dependencies on config settings, which were accessed through the `settings` field of the repository pointer. This field was initialized via a call to `prepare_repo_settings()`. Additionally, this fixes an issue in fuzz-commit-graph: In 44c7e62 (2021-12-06, repo-settings:prepare_repo_settings only in git repos), prepare_repo_settings was changed to issue a BUG() if it is called by a process whose CWD is not a Git repository. The combination of commits mentioned above broke fuzz-commit-graph, which attempts to parse arbitrary fuzzing-engine-provided bytes as a commit graph file. Prior to this change, parse_commit_graph() called prepare_repo_settings(), but since we run the fuzz tests without a valid repository, we are hitting the BUG() from 44c7e62 for every test case. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Josh Steadmon <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8168d5e commit a92d852

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

commit-graph.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
252252
}
253253
graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
254254
close(fd);
255-
ret = parse_commit_graph(r, graph_map, graph_size);
255+
prepare_repo_settings(r);
256+
ret = parse_commit_graph(&r->settings, graph_map, graph_size);
256257

257258
if (ret)
258259
ret->odb = odb;
@@ -321,7 +322,7 @@ static int graph_read_bloom_data(const unsigned char *chunk_start,
321322
return 0;
322323
}
323324

324-
struct commit_graph *parse_commit_graph(struct repository *r,
325+
struct commit_graph *parse_commit_graph(struct repo_settings *s,
325326
void *graph_map, size_t graph_size)
326327
{
327328
const unsigned char *data;
@@ -359,8 +360,6 @@ struct commit_graph *parse_commit_graph(struct repository *r,
359360
return NULL;
360361
}
361362

362-
prepare_repo_settings(r);
363-
364363
graph = alloc_commit_graph();
365364

366365
graph->hash_len = the_hash_algo->rawsz;
@@ -390,7 +389,7 @@ struct commit_graph *parse_commit_graph(struct repository *r,
390389
pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
391390
pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
392391

393-
if (get_configured_generation_version(r) >= 2) {
392+
if (s->commit_graph_generation_version >= 2) {
394393
pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
395394
&graph->chunk_generation_data);
396395
pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
@@ -400,7 +399,7 @@ struct commit_graph *parse_commit_graph(struct repository *r,
400399
graph->read_generation_data = 1;
401400
}
402401

403-
if (r->settings.commit_graph_read_changed_paths) {
402+
if (s->commit_graph_read_changed_paths) {
404403
pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
405404
&graph->chunk_bloom_indexes);
406405
read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,

commit-graph.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
9393
struct object_directory *odb);
9494
struct commit_graph *read_commit_graph_one(struct repository *r,
9595
struct object_directory *odb);
96-
struct commit_graph *parse_commit_graph(struct repository *r,
96+
97+
/*
98+
* Callers should initialize the repo_settings with prepare_repo_settings()
99+
* prior to calling parse_commit_graph().
100+
*/
101+
struct commit_graph *parse_commit_graph(struct repo_settings *s,
97102
void *graph_map, size_t graph_size);
98103

99104
/*

fuzz-commit-graph.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "commit-graph.h"
22
#include "repository.h"
33

4-
struct commit_graph *parse_commit_graph(struct repository *r,
4+
struct commit_graph *parse_commit_graph(struct repo_settings *s,
55
void *graph_map, size_t graph_size);
66

77
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
@@ -11,7 +11,15 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1111
struct commit_graph *g;
1212

1313
initialize_the_repository();
14-
g = parse_commit_graph(the_repository, (void *)data, size);
14+
/*
15+
* Initialize the_repository with commit-graph settings that would
16+
* normally be read from the repository's gitdir. We want to avoid
17+
* touching the disk to keep the individual fuzz-test cases as fast as
18+
* possible.
19+
*/
20+
the_repository->settings.commit_graph_generation_version = 2;
21+
the_repository->settings.commit_graph_read_changed_paths = 1;
22+
g = parse_commit_graph(&the_repository->settings, (void *)data, size);
1523
repo_clear(the_repository);
1624
free_commit_graph(g);
1725

repo-settings.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
1111
*dest = def;
1212
}
1313

14+
static void repo_cfg_int(struct repository *r, const char *key, int *dest,
15+
int def)
16+
{
17+
if (repo_config_get_int(r, key, dest))
18+
*dest = def;
19+
}
20+
1421
void prepare_repo_settings(struct repository *r)
1522
{
1623
int experimental;
@@ -42,11 +49,14 @@ void prepare_repo_settings(struct repository *r)
4249
r->settings.core_untracked_cache = UNTRACKED_CACHE_WRITE;
4350
}
4451

45-
/* Boolean config or default, does not cascade (simple) */
52+
/* Commit graph config or default, does not cascade (simple) */
4653
repo_cfg_bool(r, "core.commitgraph", &r->settings.core_commit_graph, 1);
54+
repo_cfg_int(r, "commitgraph.generationversion", &r->settings.commit_graph_generation_version, 2);
4755
repo_cfg_bool(r, "commitgraph.readchangedpaths", &r->settings.commit_graph_read_changed_paths, 1);
4856
repo_cfg_bool(r, "gc.writecommitgraph", &r->settings.gc_write_commit_graph, 1);
4957
repo_cfg_bool(r, "fetch.writecommitgraph", &r->settings.fetch_write_commit_graph, 0);
58+
59+
/* Boolean config or default, does not cascade (simple) */
5060
repo_cfg_bool(r, "pack.usesparse", &r->settings.pack_use_sparse, 1);
5161
repo_cfg_bool(r, "core.multipackindex", &r->settings.core_multi_pack_index, 1);
5262
repo_cfg_bool(r, "index.sparse", &r->settings.sparse_index, 0);

repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct repo_settings {
3030
int initialized;
3131

3232
int core_commit_graph;
33+
int commit_graph_generation_version;
3334
int commit_graph_read_changed_paths;
3435
int gc_write_commit_graph;
3536
int fetch_write_commit_graph;

0 commit comments

Comments
 (0)