There are many ways for any command to access a git repository: - most of them will try to discover the .git dir via setup_git_directory() and friends - the server side programs already know where the repo is and prepare with enter_repo() - special commands that deal with repo creation (init/clone) use init_db() once the new repo is ready for access. - somebody accesses $GIT_DIR before any of above functions are called and accidentally sets up a git repository by set_git_dir() alone "the_repository" is partially set up via set_git_dir() at some point in all four cases. The hash algorithm though is configured later after .git/config is read. So far proper repo initialization is done only for the first case [1]. The second case is not covered (but that's fine [3]). The third case was found and worked around in [2]. The fourth case is a buggy one, which should be fixed already by jk/no-looking-at-dotgit-outside-repo and never happens again. This patch makes sure all cases initialize the hash algorithm in the_repository correctly. Both second and third cases must run check_repo_format() before "entering" it. Eventually we probably just rename this function to init_repo() or something. [1] 78a6766802 (Integrate hash algorithm support with repo setup - 2017-11-12) [2] e26f7f19b6 (repository: pre-initialize hash algo pointer - 2018-01-19) [3] the reason server side is still running ok with no hash algo before [2] is because the programs that use enter_repo() do very little (and unlikely to hash anything) then spawn a new program (like pack-objects or upload-archive) to do the heavy lifting. These programs already use setup_git_directory() or the gently version Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- builtin/init-db.c | 3 ++- cache.h | 3 ++- path.c | 2 +- setup.c | 5 ++++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/builtin/init-db.c b/builtin/init-db.c index c9b7946bad..d119d9906b 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -9,6 +9,7 @@ #include "builtin.h" #include "exec_cmd.h" #include "parse-options.h" +#include "repository.h" #ifndef DEFAULT_GIT_TEMPLATE_DIR #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" @@ -369,7 +370,7 @@ int init_db(const char *git_dir, const char *real_git_dir, * config file, so this will not fail. What we are catching * is an attempt to reinitialize new repository with an old tool. */ - check_repository_format(); + check_repository_format(the_repository); reinit = create_default_files(template_dir, original_git_dir); diff --git a/cache.h b/cache.h index 21fbcc2414..6b97138264 100644 --- a/cache.h +++ b/cache.h @@ -894,6 +894,7 @@ extern int repository_format_precious_objects; extern char *repository_format_partial_clone; extern const char *core_partial_clone_filter_default; +struct repository; struct repository_format { int version; int precious_objects; @@ -926,7 +927,7 @@ int verify_repository_format(const struct repository_format *format, * set_git_dir() before calling this, and use it only for "are we in a valid * repo?". */ -extern void check_repository_format(void); +extern void check_repository_format(struct repository *); #define MTIME_CHANGED 0x0001 #define CTIME_CHANGED 0x0002 diff --git a/path.c b/path.c index da8b655730..a544252198 100644 --- a/path.c +++ b/path.c @@ -827,7 +827,7 @@ const char *enter_repo(const char *path, int strict) if (is_git_directory(".")) { set_git_dir("."); - check_repository_format(); + check_repository_format(the_repository); return path; } diff --git a/setup.c b/setup.c index c5d55dcee4..a82103832e 100644 --- a/setup.c +++ b/setup.c @@ -1180,11 +1180,14 @@ int git_config_perm(const char *var, const char *value) return -(i & 0666); } -void check_repository_format(void) +/* optionally configure "repo" to the correct format */ +void check_repository_format(struct repository *repo) { struct repository_format repo_fmt; check_repository_format_gently(get_git_dir(), &repo_fmt, NULL); startup_info->have_repository = 1; + if (repo) + repo_set_hash_algo(repo, repo_fmt.hash_algo); } /* -- 2.16.1.435.g8f24da2e1a