]> git.ipfire.org Git - thirdparty/git.git/commitdiff
repository: drop `initialize_the_repository()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 18 Apr 2024 12:14:33 +0000 (14:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Apr 2024 19:30:43 +0000 (12:30 -0700)
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>
common-main.c
oss-fuzz/fuzz-commit-graph.c
repository.c
repository.h
t/helper/test-read-cache.c

index 033778b3c56a3557e191d3bb74592fd00497853b..b86f40600f7acbb1d552941a94217d63c2ab78a5 100644 (file)
@@ -48,7 +48,7 @@ int main(int argc, const char **argv)
        setlocale(LC_CTYPE, "");
        git_setup_gettext();
 
-       initialize_the_repository();
+       initialize_repository(the_repository);
 
        attr_start();
 
index 2992079dd97d75892f4f411cd4a979f459d68170..fe15e2c225d4b3c0d9b4196362404e2a2207ce9f 100644 (file)
@@ -11,7 +11,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
        struct commit_graph *g;
 
-       initialize_the_repository();
+       initialize_repository(the_repository);
+
        /*
         * Initialize the_repository with commit-graph settings that would
         * normally be read from the repository's gitdir. We want to avoid
index 089edbffa2d235a031ea4590e22c655d48477f90..2118f563e3b2d564472d537542d220b1961b7395 100644 (file)
 
 /* The main repository */
 static struct repository the_repo;
-struct repository *the_repository;
+struct repository *the_repository = &the_repo;
 
-static void initialize_repository(struct repository *repo)
+void initialize_repository(struct repository *repo)
 {
        repo->objects = raw_object_store_new();
        repo->remote_state = remote_state_new();
        repo->parsed_objects = parsed_object_pool_new();
        ALLOC_ARRAY(repo->index, 1);
        index_state_init(repo->index, repo);
-}
 
-void initialize_the_repository(void)
-{
-       the_repository = &the_repo;
-       initialize_repository(the_repository);
-       repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
+       /*
+        * Unfortunately, we need to keep this hack around for the time being:
+        *
+        *   - Not setting up the hash algorithm for `the_repository` leads to
+        *     crashes because `the_hash_algo` is a macro that expands to
+        *     `the_repository->hash_algo`. So if Git commands try to access
+        *     `the_hash_algo` without a Git directory we crash.
+        *
+        *   - Setting up the hash algorithm to be SHA1 by default breaks other
+        *     commands when running with SHA256.
+        *
+        * This is another point in case why having global state is a bad idea.
+        * Eventually, we should remove this hack and stop setting the hash
+        * algorithm in this function altogether. Instead, it should only ever
+        * be set via our repository setup procedures. But that requires more
+        * work.
+        */
+       if (repo == the_repository)
+               repo_set_hash_algo(repo, GIT_HASH_SHA1);
 }
 
 static void expand_base_dir(char **out, const char *in,
index 6f4af15417561e2fe39326c6be686f40d3d74325..41ed22543a815feb80d31e45e0a1295d6dc8d981 100644 (file)
@@ -207,7 +207,7 @@ void repo_set_worktree(struct repository *repo, const char *path);
 void repo_set_hash_algo(struct repository *repo, int algo);
 void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
 void repo_set_ref_storage_format(struct repository *repo, unsigned int format);
-void initialize_the_repository(void);
+void initialize_repository(struct repository *repo);
 RESULT_MUST_BE_USED
 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
 
index 458efa88a6359720f211b6d20a873b1c31837c53..e803c43ece92def7bc69c253a49bc5602129d22c 100644 (file)
@@ -9,7 +9,7 @@ int cmd__read_cache(int argc, const char **argv)
        int i, cnt = 1;
        const char *name = NULL;
 
-       initialize_the_repository();
+       initialize_repository(the_repository);
 
        if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) {
                argc--;