repository: drop `initialize_the_repository()`
Now that we have dropped `the_index`, `initialize_the_repository()` doesn't really do a lot anymore except for setting up the pointer for `the_repository` and then calling `initialize_repository()`. The former can be replaced by statically initializing the pointer though, which basically makes this function moot. Convert callers to instead call `initialize_repository(the_repository)` and drop `initialize_thee_repository()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Apr 18, 2024 at 14:14 UTC
0c473557908ec626e9e8abf66d2f2308624cdd14
5 files changed
+26
-12
common-main.c
+1
-1
@@ -48,7 +48,7 @@ int main(int argc, const char **argv)
48
setlocale(LC_CTYPE, "");
49
git_setup_gettext();
50
51
- initialize_the_repository();
51
+ initialize_repository(the_repository);
52
53
attr_start();
54
oss-fuzz/fuzz-commit-graph.c
+2
-1
@@ -11,7 +11,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
11
{
12
struct commit_graph *g;
13
14
- initialize_the_repository();
14
+ initialize_repository(the_repository);
15
+
16
/*
17
* Initialize the_repository with commit-graph settings that would
18
* normally be read from the repository's gitdir. We want to avoid
repository.c
+21
-8
@@ -17,22 +17,35 @@
17
18
/* The main repository */
19
static struct repository the_repo;
20
-struct repository *the_repository;
20
+struct repository *the_repository = &the_repo;
21
22
-static void initialize_repository(struct repository *repo)
22
+void initialize_repository(struct repository *repo)
23
{
24
repo->objects = raw_object_store_new();
25
repo->remote_state = remote_state_new();
26
repo->parsed_objects = parsed_object_pool_new();
27
ALLOC_ARRAY(repo->index, 1);
28
index_state_init(repo->index, repo);
29
-}
29
31
-void initialize_the_repository(void)
32
-{
33
- the_repository = &the_repo;
34
- initialize_repository(the_repository);
35
- repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
30
+ /*
31
+ * Unfortunately, we need to keep this hack around for the time being:
32
+ *
33
+ * - Not setting up the hash algorithm for `the_repository` leads to
34
+ * crashes because `the_hash_algo` is a macro that expands to
35
+ * `the_repository->hash_algo`. So if Git commands try to access
36
+ * `the_hash_algo` without a Git directory we crash.
37
+ *
38
+ * - Setting up the hash algorithm to be SHA1 by default breaks other
39
+ * commands when running with SHA256.
40
+ *
41
+ * This is another point in case why having global state is a bad idea.
42
+ * Eventually, we should remove this hack and stop setting the hash
43
+ * algorithm in this function altogether. Instead, it should only ever
44
+ * be set via our repository setup procedures. But that requires more
45
+ * work.
46
+ */
47
+ if (repo == the_repository)
48
+ repo_set_hash_algo(repo, GIT_HASH_SHA1);
49
}
50
51
static void expand_base_dir(char **out, const char *in,
repository.h
+1
-1
@@ -207,7 +207,7 @@ void repo_set_worktree(struct repository *repo, const char *path);
207
void repo_set_hash_algo(struct repository *repo, int algo);
208
void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
209
void repo_set_ref_storage_format(struct repository *repo, unsigned int format);
210
-void initialize_the_repository(void);
210
+void initialize_repository(struct repository *repo);
211
RESULT_MUST_BE_USED
212
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
213
t/helper/test-read-cache.c
+1
-1
@@ -9,7 +9,7 @@ int cmd__read_cache(int argc, const char **argv)
9
int i, cnt = 1;
10
const char *name = NULL;
11
12
- initialize_the_repository();
12
+ initialize_repository(the_repository);
13
14
if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) {
15
argc--;