Skip to content

Commit c8aed5e

Browse files
pks-tgitster
authored andcommitted
repository: stop setting SHA1 as the default object hash
During the startup of Git, we call `initialize_the_repository()` to set up `the_repository` as well as `the_index`. Part of this setup is also to set the default object hash of the repository to SHA1. This has the effect that `the_hash_algo` is getting initialized to SHA1, as well. This default hash algorithm eventually gets overridden by most Git commands via `setup_git_directory()`, which also detects the actual hash algorithm used by the repository. There are some commands though that don't access a repository at all, or at a later point only, and thus retain the default hash function for some amount of time. As some of the the preceding commits demonstrate, this can lead to subtle issues when we access `the_hash_algo` when no repository has been set up. Address this issue by dropping the set up of the default hash algorithm completely. The effect of this is that `the_hash_algo` will map to a `NULL` pointer and thus cause Git to crash when something tries to access the hash algorithm without it being properly initialized. It thus forces all Git commands to explicitly set up the hash algorithm in case there is no repository. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 781ba69 commit c8aed5e

File tree

1 file changed

+0
-20
lines changed

1 file changed

+0
-20
lines changed

repository.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,6 @@ void initialize_repository(struct repository *repo)
2626
repo->parsed_objects = parsed_object_pool_new();
2727
ALLOC_ARRAY(repo->index, 1);
2828
index_state_init(repo->index, repo);
29-
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);
4929
}
5030

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

0 commit comments

Comments
 (0)