]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: use the default algorithm to initialize repo format
authorbrian m. carlson <sandals@crustytoothpaste.net>
Tue, 1 Jul 2025 21:22:31 +0000 (21:22 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Jul 2025 21:58:24 +0000 (14:58 -0700)
When we define a new repository format with REPOSITORY_FORMAT_INIT, we
always use GIT_HASH_SHA1, and this value ends up getting used as the
default value to initialize a repository if none of the command line,
environment, or config tell us to do otherwise.

Because we might not always want to use SHA-1 as the default, let's
instead specify the default hash algorithm constant so that we will use
whatever the specified default is.

However, we also need to continue to read older repositories.  If we're
in a v0 repository or extensions.objectformat is not set, then we must
continue to default to the original hash algorithm: SHA-1.  If an
algorithm is set explicitly, however, it will override the hash_algo
member of the repository_format struct and we'll get the right value.

Similarly, if the repository was initialized before Git 0.99.3, then it
may lack a core.repositoryformatversion key, and some repositories lack
a config file altogether.  In both cases, format->version is -1 and we
need to assume that SHA-1 is in use.

Because clear_repository_format reinitializes the struct
repository_format and therefore sets the hash_algo member to the default
(which could in the future not be SHA-1), we need to reset this member
explicitly.  We know, however, that at the point we call
read_repository_format, we are actually reading an existing repository
and not initializing a new one or operating outside of a repository, so
we are not changing the default behavior back to SHA-1 if the default
algorithm is different.

It is potentially questionable that we ignore all repository
configuration if there is a config file but it doesn't have
core.repositoryformatversion set, in which case we reset all of the
configuration to the default.  However, it is unclear what the right
thing to do instead with such an old repository is and a simple git init
will add the missing entry, so for now, we simply honor what the
existing code does and reset the value to the default, simply adding our
initialization to SHA-1.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c
setup.h

diff --git a/setup.c b/setup.c
index 3d2b3e745b579e1b64181e19edff4673f973afbb..03a61bd06afb778ef6260ec5c31c67f888153570 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -835,9 +835,12 @@ static void init_repository_format(struct repository_format *format)
 int read_repository_format(struct repository_format *format, const char *path)
 {
        clear_repository_format(format);
+       format->hash_algo = GIT_HASH_SHA1_LEGACY;
        git_config_from_file(check_repo_format, path, format);
-       if (format->version == -1)
+       if (format->version == -1) {
                clear_repository_format(format);
+               format->hash_algo = GIT_HASH_SHA1_LEGACY;
+       }
        return format->version;
 }
 
diff --git a/setup.h b/setup.h
index 18dc3b73686ce28fac2fe04282ce95f8bf3e6b74..8522fa8575da7123d0a9dc57dbabf08677a58eaa 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -149,7 +149,7 @@ struct repository_format {
 { \
        .version = -1, \
        .is_bare = -1, \
-       .hash_algo = GIT_HASH_SHA1, \
+       .hash_algo = GIT_HASH_DEFAULT, \
        .ref_storage_format = REF_STORAGE_FORMAT_FILES, \
        .unknown_extensions = STRING_LIST_INIT_DUP, \
        .v1_only_extensions = STRING_LIST_INIT_DUP, \