Skip to content

Commit 3c8f60c

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 360822a commit 3c8f60c

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11851185
if (option_required_reference.nr || option_optional_reference.nr)
11861186
setup_reference();
11871187

1188-
remote = remote_get(remote_name);
1188+
remote = remote_get_early(remote_name);
11891189

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

remote.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static void alias_all_urls(struct remote_state *remote_state)
509509
}
510510
}
511511

512-
static void read_config(struct repository *repo)
512+
static void read_config(struct repository *repo, int early)
513513
{
514514
int flag;
515515

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

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

562562
const char *remote_for_branch(struct branch *branch, int *explicit)
563563
{
564-
read_config(the_repository);
564+
read_config(the_repository, 0);
565565
die_on_missing_branch(the_repository, branch);
566566

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

588588
const char *pushremote_for_branch(struct branch *branch, int *explicit)
589589
{
590-
read_config(the_repository);
590+
read_config(the_repository, 0);
591591
die_on_missing_branch(the_repository, branch);
592592

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

600600
const char *remote_ref_for_branch(struct branch *branch, int for_push)
601601
{
602-
read_config(the_repository);
602+
read_config(the_repository, 0);
603603
die_on_missing_branch(the_repository, branch);
604604

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

710710
struct remote *remote_get(const char *name)
711711
{
712-
read_config(the_repository);
712+
read_config(the_repository, 0);
713+
return remotes_remote_get(the_repository->remote_state, name);
714+
}
715+
716+
struct remote *remote_get_early(const char *name)
717+
{
718+
read_config(the_repository, 1);
713719
return remotes_remote_get(the_repository->remote_state, name);
714720
}
715721

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

723729
struct remote *pushremote_get(const char *name)
724730
{
725-
read_config(the_repository);
731+
read_config(the_repository, 0);
726732
return remotes_pushremote_get(the_repository->remote_state, name);
727733
}
728734

@@ -738,7 +744,7 @@ int remote_is_configured(struct remote *remote, int in_repo)
738744
int for_each_remote(each_remote_fn fn, void *priv)
739745
{
740746
int i, result = 0;
741-
read_config(the_repository);
747+
read_config(the_repository, 0);
742748
for (i = 0; i < the_repository->remote_state->remotes_nr && !result;
743749
i++) {
744750
struct remote *remote =
@@ -1831,7 +1837,7 @@ struct branch *branch_get(const char *name)
18311837
{
18321838
struct branch *ret;
18331839

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

19741980
const char *branch_get_push(struct branch *branch, struct strbuf *err)
19751981
{
1976-
read_config(the_repository);
1982+
read_config(the_repository, 0);
19771983
die_on_missing_branch(the_repository, branch);
19781984

19791985
if (!branch)

remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct remote {
118118
* and configuration.
119119
*/
120120
struct remote *remote_get(const char *name);
121+
struct remote *remote_get_early(const char *name);
121122

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

0 commit comments

Comments
 (0)