builtin/clone: skip reading HEAD when retrieving remote

After we have set up the remote configuration in git-clone(1) we'll call
`remote_get()` to read the remote from the on-disk configuration. But
next to reading the on-disk configuration, `remote_get()` will also
cause us to try and read the repository's HEAD reference so that we can
figure out the current branch. Besides being pointless in git-clone(1)
because we're operating in an empty repository anyway, this will also
break once we move creation of the reference database to a later point
in time.

Refactor the code to introduce a new `remote_get_early()` function that
will skip reading the HEAD reference to address this issue.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Patrick Steinhardt 2023-12-12 08:01:03 +01:00 committed by Junio C Hamano
parent 360822a347
commit 3c8f60c641
3 changed files with 18 additions and 11 deletions

View File

@ -1185,7 +1185,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
if (option_required_reference.nr || option_optional_reference.nr) if (option_required_reference.nr || option_optional_reference.nr)
setup_reference(); setup_reference();


remote = remote_get(remote_name); remote = remote_get_early(remote_name);


refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix, refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
branch_top.buf); branch_top.buf);

View File

@ -509,7 +509,7 @@ static void alias_all_urls(struct remote_state *remote_state)
} }
} }


static void read_config(struct repository *repo) static void read_config(struct repository *repo, int early)
{ {
int flag; int flag;


@ -518,7 +518,7 @@ static void read_config(struct repository *repo)
repo->remote_state->initialized = 1; repo->remote_state->initialized = 1;


repo->remote_state->current_branch = NULL; repo->remote_state->current_branch = NULL;
if (startup_info->have_repository) { if (startup_info->have_repository && !early) {
const char *head_ref = refs_resolve_ref_unsafe( const char *head_ref = refs_resolve_ref_unsafe(
get_main_ref_store(repo), "HEAD", 0, NULL, &flag); get_main_ref_store(repo), "HEAD", 0, NULL, &flag);
if (head_ref && (flag & REF_ISSYMREF) && if (head_ref && (flag & REF_ISSYMREF) &&
@ -561,7 +561,7 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state,


const char *remote_for_branch(struct branch *branch, int *explicit) const char *remote_for_branch(struct branch *branch, int *explicit)
{ {
read_config(the_repository); read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch); die_on_missing_branch(the_repository, branch);


return remotes_remote_for_branch(the_repository->remote_state, branch, return remotes_remote_for_branch(the_repository->remote_state, branch,
@ -587,7 +587,7 @@ remotes_pushremote_for_branch(struct remote_state *remote_state,


const char *pushremote_for_branch(struct branch *branch, int *explicit) const char *pushremote_for_branch(struct branch *branch, int *explicit)
{ {
read_config(the_repository); read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch); die_on_missing_branch(the_repository, branch);


return remotes_pushremote_for_branch(the_repository->remote_state, return remotes_pushremote_for_branch(the_repository->remote_state,
@ -599,7 +599,7 @@ static struct remote *remotes_remote_get(struct remote_state *remote_state,


const char *remote_ref_for_branch(struct branch *branch, int for_push) const char *remote_ref_for_branch(struct branch *branch, int for_push)
{ {
read_config(the_repository); read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch); die_on_missing_branch(the_repository, branch);


if (branch) { if (branch) {
@ -709,7 +709,13 @@ remotes_remote_get(struct remote_state *remote_state, const char *name)


struct remote *remote_get(const char *name) struct remote *remote_get(const char *name)
{ {
read_config(the_repository); read_config(the_repository, 0);
return remotes_remote_get(the_repository->remote_state, name);
}

struct remote *remote_get_early(const char *name)
{
read_config(the_repository, 1);
return remotes_remote_get(the_repository->remote_state, name); return remotes_remote_get(the_repository->remote_state, name);
} }


@ -722,7 +728,7 @@ remotes_pushremote_get(struct remote_state *remote_state, const char *name)


struct remote *pushremote_get(const char *name) struct remote *pushremote_get(const char *name)
{ {
read_config(the_repository); read_config(the_repository, 0);
return remotes_pushremote_get(the_repository->remote_state, name); return remotes_pushremote_get(the_repository->remote_state, name);
} }


@ -738,7 +744,7 @@ int remote_is_configured(struct remote *remote, int in_repo)
int for_each_remote(each_remote_fn fn, void *priv) int for_each_remote(each_remote_fn fn, void *priv)
{ {
int i, result = 0; int i, result = 0;
read_config(the_repository); read_config(the_repository, 0);
for (i = 0; i < the_repository->remote_state->remotes_nr && !result; for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
i++) { i++) {
struct remote *remote = struct remote *remote =
@ -1831,7 +1837,7 @@ struct branch *branch_get(const char *name)
{ {
struct branch *ret; struct branch *ret;


read_config(the_repository); read_config(the_repository, 0);
if (!name || !*name || !strcmp(name, "HEAD")) if (!name || !*name || !strcmp(name, "HEAD"))
ret = the_repository->remote_state->current_branch; ret = the_repository->remote_state->current_branch;
else else
@ -1973,7 +1979,7 @@ static const char *branch_get_push_1(struct remote_state *remote_state,


const char *branch_get_push(struct branch *branch, struct strbuf *err) const char *branch_get_push(struct branch *branch, struct strbuf *err)
{ {
read_config(the_repository); read_config(the_repository, 0);
die_on_missing_branch(the_repository, branch); die_on_missing_branch(the_repository, branch);


if (!branch) if (!branch)

View File

@ -118,6 +118,7 @@ struct remote {
* and configuration. * and configuration.
*/ */
struct remote *remote_get(const char *name); struct remote *remote_get(const char *name);
struct remote *remote_get_early(const char *name);


struct remote *pushremote_get(const char *name); struct remote *pushremote_get(const char *name);
int remote_is_configured(struct remote *remote, int in_repo); int remote_is_configured(struct remote *remote, int in_repo);