Skip to content

Commit 18c9cb7

Browse files
pks-tgitster
authored andcommitted
builtin/clone: create the refdb with the correct object format
We're currently creating the reference database with a potentially incorrect object format when the remote repository's object format is different from the local default object format. This works just fine for now because the files backend never records the object format anywhere. But this logic will fail with any new reference backend that encodes this information in some form either on-disk or in-memory. The preceding commits have reshuffled code in git-clone(1) so that there is no code path that will access the reference database before we have detected the remote's object format. With these refactorings we can now defer initialization of the reference database until after we have learned the remote's object format and thus initialize it with the correct format from the get-go. These refactorings are required to make git-clone(1) work with the upcoming reftable backend when cloning repositories with the SHA256 object format. This change breaks a test in "t5550-http-fetch-dumb.sh" when cloning an empty repository with `GIT_TEST_DEFAULT_HASH=sha256`. The test expects the resulting hash format of the empty cloned repository to match the default hash, but now we always end up with a sha1 repository. The problem is that for dumb HTTP fetches, we have no easy way to figure out the remote's hash function except for deriving it based on the hash length of refs in `info/refs`. But as the remote repository is empty we cannot rely on this detection mechanism. Before the change in this commit we already initialized the repository with the default hash function and then left it as-is. With this patch we always use the hash function detected via the remote, where we fall back to "sha1" in case we cannot detect it. Neither the old nor the new behaviour are correct as we second-guess the remote hash function in both cases. But given that this is a rather unlikely edge case (we use the dumb HTTP protocol, the remote repository uses SHA256 and the remote repository is empty), let's simply adapt the test to assert the new behaviour. If we want to properly address this edge case in the future we will have to extend the dumb HTTP protocol so that we can properly detect the hash function for empty repositories. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c8f60c commit 18c9cb7

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

builtin/clone.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10971097
}
10981098
}
10991099

1100+
/*
1101+
* Initialize the repository, but skip initializing the reference
1102+
* database. We do not yet know about the object format of the
1103+
* repository, and reference backends may persist that information into
1104+
* their on-disk data structures.
1105+
*/
11001106
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1101-
do_not_override_repo_unix_permissions, INIT_DB_QUIET);
1107+
do_not_override_repo_unix_permissions, INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
11021108

11031109
if (real_git_dir) {
11041110
free((char *)git_dir);
@@ -1282,6 +1288,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12821288
hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
12831289
initialize_repository_version(hash_algo, 1);
12841290
repo_set_hash_algo(the_repository, hash_algo);
1291+
create_reference_database(NULL, 1);
12851292

12861293
/*
12871294
* Before fetching from the remote, download and install bundle

setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ static int is_reinit(void)
18971897
return ret;
18981898
}
18991899

1900-
static void create_reference_database(const char *initial_branch, int quiet)
1900+
void create_reference_database(const char *initial_branch, int quiet)
19011901
{
19021902
struct strbuf err = STRBUF_INIT;
19031903
int reinit = is_reinit();

setup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
178178
const char *initial_branch, int init_shared_repository,
179179
unsigned int flags);
180180
void initialize_repository_version(int hash_algo, int reinit);
181+
void create_reference_database(const char *initial_branch, int quiet);
181182

182183
/*
183184
* NOTE NOTE NOTE!!

t/t5550-http-fetch-dumb.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ test_expect_success 'create empty remote repository' '
6666
setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
6767
'
6868

69-
test_expect_success 'empty dumb HTTP repository has default hash algorithm' '
69+
test_expect_success 'empty dumb HTTP repository falls back to SHA1' '
7070
test_when_finished "rm -fr clone-empty" &&
7171
git clone $HTTPD_URL/dumb/empty.git clone-empty &&
7272
git -C clone-empty rev-parse --show-object-format >empty-format &&
73-
test "$(cat empty-format)" = "$(test_oid algo)"
73+
test "$(cat empty-format)" = sha1
7474
'
7575

7676
setup_askpass_helper

0 commit comments

Comments
 (0)