Skip to content

Commit 0c47355

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 19fa8cd commit 0c47355

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

common-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main(int argc, const char **argv)
4848
setlocale(LC_CTYPE, "");
4949
git_setup_gettext();
5050

51-
initialize_the_repository();
51+
initialize_repository(the_repository);
5252

5353
attr_start();
5454

oss-fuzz/fuzz-commit-graph.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1111
{
1212
struct commit_graph *g;
1313

14-
initialize_the_repository();
14+
initialize_repository(the_repository);
15+
1516
/*
1617
* Initialize the_repository with commit-graph settings that would
1718
* normally be read from the repository's gitdir. We want to avoid

repository.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,35 @@
1717

1818
/* The main repository */
1919
static struct repository the_repo;
20-
struct repository *the_repository;
20+
struct repository *the_repository = &the_repo;
2121

22-
static void initialize_repository(struct repository *repo)
22+
void initialize_repository(struct repository *repo)
2323
{
2424
repo->objects = raw_object_store_new();
2525
repo->remote_state = remote_state_new();
2626
repo->parsed_objects = parsed_object_pool_new();
2727
ALLOC_ARRAY(repo->index, 1);
2828
index_state_init(repo->index, repo);
29-
}
3029

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);
3649
}
3750

3851
static void expand_base_dir(char **out, const char *in,

repository.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void repo_set_worktree(struct repository *repo, const char *path);
207207
void repo_set_hash_algo(struct repository *repo, int algo);
208208
void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
209209
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);
211211
RESULT_MUST_BE_USED
212212
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
213213

t/helper/test-read-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int cmd__read_cache(int argc, const char **argv)
99
int i, cnt = 1;
1010
const char *name = NULL;
1111

12-
initialize_the_repository();
12+
initialize_repository(the_repository);
1313

1414
if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) {
1515
argc--;

0 commit comments

Comments
 (0)