]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/clone: avoid failure with GIT_DEFAULT_HASH
authorbrian m. carlson <sandals@crustytoothpaste.net>
Sun, 20 Sep 2020 22:35:41 +0000 (22:35 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 22 Sep 2020 16:22:32 +0000 (09:22 -0700)
If a user is cloning a SHA-1 repository with GIT_DEFAULT_HASH set to
"sha256", then we can end up with a repository where the repository
format version is 0 but the extensions.objectformat key is set to
"sha256".  This is both wrong (the user has a SHA-1 repository) and
nonfunctional (because the extension cannot be used in a v0 repository).

This happens because in a clone, we initially set up the repository, and
then change its algorithm based on what the remote side tells us it's
using.  We've initially set up the repository as SHA-256 in this case,
and then later on reset the repository version without clearing the
extension.

We could just always set the extension in this case, but that would mean
that our SHA-1 repositories weren't compatible with older Git versions,
even though there's no reason why they shouldn't be.  And we also don't
want to initialize the repository as SHA-1 initially, since that means
if we're cloning an empty repository, we'll have failed to honor the
GIT_DEFAULT_HASH variable and will end up with a SHA-1 repository, not a
SHA-256 repository.

Neither of those are appealing, so let's tell the repository
initialization code if we're doing a reinit like this, and if so, to
clear the extension if we're using SHA-1.  This makes sure we produce a
valid and functional repository and doesn't break any of our other use
cases.

Reported-by: Matheus Tavares <matheus.bernardino@usp.br>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
builtin/init-db.c
cache.h
t/t5601-clone.sh

index fbfd6568cd875b77a30c2ed10977afb975cfb028..391aa41075ee75fe20a7b9a4d79e73cd90e10375 100644 (file)
@@ -1233,7 +1233,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                 * Now that we know what algorithm the remote side is using,
                 * let's set ours to the same thing.
                 */
-               initialize_repository_version(hash_algo);
+               initialize_repository_version(hash_algo, 1);
                repo_set_hash_algo(the_repository, hash_algo);
 
                mapped_refs = wanted_peer_refs(refs, &remote->fetch);
index cd3e7605417dad82417294644b9f6de376792379..01bc648d416fa0167da43897f826209019948d61 100644 (file)
@@ -179,7 +179,7 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
        return 1;
 }
 
-void initialize_repository_version(int hash_algo)
+void initialize_repository_version(int hash_algo, int reinit)
 {
        char repo_version_string[10];
        int repo_version = GIT_REPO_VERSION;
@@ -195,6 +195,8 @@ void initialize_repository_version(int hash_algo)
        if (hash_algo != GIT_HASH_SHA1)
                git_config_set("extensions.objectformat",
                               hash_algos[hash_algo].name);
+       else if (reinit)
+               git_config_set_gently("extensions.objectformat", NULL);
 }
 
 static int create_default_files(const char *template_path,
@@ -277,7 +279,7 @@ static int create_default_files(const char *template_path,
                free(ref);
        }
 
-       initialize_repository_version(fmt->hash_algo);
+       initialize_repository_version(fmt->hash_algo, 0);
 
        /* Check filemode trustability */
        path = git_path_buf(&buf, "config");
diff --git a/cache.h b/cache.h
index cee8aa5dc325a422ac6ba7a7795fb05afd8ae642..c0072d43b1a78101f92df1283f9f34712f4c0265 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -629,7 +629,7 @@ int path_inside_repo(const char *prefix, const char *path);
 int init_db(const char *git_dir, const char *real_git_dir,
            const char *template_dir, int hash_algo,
            const char *initial_branch, unsigned int flags);
-void initialize_repository_version(int hash_algo);
+void initialize_repository_version(int hash_algo, int reinit);
 
 void sanitize_stdfds(void);
 int daemonize(void);
index 15fb64c18d860e582542c0ae225897b9b22e89d0..b6c8312da1444cf5f77c168f3df43564deca4d78 100755 (executable)
@@ -631,6 +631,20 @@ test_expect_success CASE_INSENSITIVE_FS 'colliding file detection' '
        test_i18ngrep "the following paths have collided" icasefs/warning
 '
 
+test_expect_success 'clone with GIT_DEFAULT_HASH' '
+       (
+               sane_unset GIT_DEFAULT_HASH &&
+               git init --object-format=sha1 test-sha1 &&
+               git init --object-format=sha256 test-sha256
+       ) &&
+       test_commit -C test-sha1 foo &&
+       test_commit -C test-sha256 foo &&
+       GIT_DEFAULT_HASH=sha1 git clone test-sha256 test-clone-sha256 &&
+       GIT_DEFAULT_HASH=sha256 git clone test-sha1 test-clone-sha1 &&
+       git -C test-clone-sha1 status &&
+       git -C test-clone-sha256 status
+'
+
 partial_clone_server () {
               SERVER="$1" &&