Skip to content

Commit 97abaab

Browse files
pks-tgitster
authored andcommitted
refs: drop git_default_branch_name()
The `git_default_branch_name()` function is a thin wrapper around `repo_default_branch_name()` with two differences: - We implicitly rely on `the_repository`. - We cache the default branch name. None of the callsites of `git_default_branch_name()` are hot code paths though, so the caching of the branch name is not really required. Refactor the callsites to use `repo_default_branch_name()` instead and drop `git_default_branch_name()`, thus getting rid of one more case where we rely on `the_repository`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 30aaff4 commit 97abaab

File tree

6 files changed

+18
-20
lines changed

6 files changed

+18
-20
lines changed

builtin/clone.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
14681468
} else if (remote_head) {
14691469
our_head_points_at = NULL;
14701470
} else {
1471+
char *to_free = NULL;
14711472
const char *branch;
14721473

14731474
if (!mapped_refs) {
@@ -1480,7 +1481,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
14801481
"refs/heads/", &branch)) {
14811482
unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
14821483
} else {
1483-
branch = git_default_branch_name(0);
1484+
branch = to_free = repo_default_branch_name(the_repository, 0);
14841485
unborn_head = xstrfmt("refs/heads/%s", branch);
14851486
}
14861487

@@ -1496,6 +1497,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
14961497
* a match.
14971498
*/
14981499
our_head_points_at = find_remote_branch(mapped_refs, branch);
1500+
1501+
free(to_free);
14991502
}
15001503

15011504
write_refspec_config(src_ref_prefix, our_head_points_at,

builtin/var.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static char *pager(int ident_flag UNUSED)
4646

4747
static char *default_branch(int ident_flag UNUSED)
4848
{
49-
return xstrdup_or_null(git_default_branch_name(1));
49+
return repo_default_branch_name(the_repository, 1);
5050
}
5151

5252
static char *shell_path(int ident_flag UNUSED)

refs.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -664,16 +664,6 @@ char *repo_default_branch_name(struct repository *r, int quiet)
664664
return ret;
665665
}
666666

667-
const char *git_default_branch_name(int quiet)
668-
{
669-
static char *ret;
670-
671-
if (!ret)
672-
ret = repo_default_branch_name(the_repository, quiet);
673-
674-
return ret;
675-
}
676-
677667
/*
678668
* *string and *len will only be substituted, and *string returned (for
679669
* later free()ing) if the string passed in is a magic short-hand form

refs.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
169169
/*
170170
* Retrieves the default branch name for newly-initialized repositories.
171171
*
172-
* The return value of `repo_default_branch_name()` is an allocated string. The
173-
* return value of `git_default_branch_name()` is a singleton.
172+
* The return value is an allocated string.
174173
*/
175-
const char *git_default_branch_name(int quiet);
176174
char *repo_default_branch_name(struct repository *r, int quiet);
177175

178176
/*

remote.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static void read_remotes_file(struct remote_state *remote_state,
305305
static void read_branches_file(struct remote_state *remote_state,
306306
struct remote *remote)
307307
{
308-
char *frag;
308+
char *frag, *to_free = NULL;
309309
struct strbuf buf = STRBUF_INIT;
310310
FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
311311

@@ -333,7 +333,7 @@ static void read_branches_file(struct remote_state *remote_state,
333333
if (frag)
334334
*(frag++) = '\0';
335335
else
336-
frag = (char *)git_default_branch_name(0);
336+
frag = to_free = repo_default_branch_name(the_repository, 0);
337337

338338
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
339339
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -345,6 +345,8 @@ static void read_branches_file(struct remote_state *remote_state,
345345
*/
346346
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
347347
remote->fetch_tags = 1; /* always auto-follow */
348+
349+
free(to_free);
348350
}
349351

350352
static int handle_config(const char *key, const char *value,
@@ -2388,11 +2390,13 @@ struct ref *guess_remote_head(const struct ref *head,
23882390

23892391
/* If a remote branch exists with the default branch name, let's use it. */
23902392
if (!all) {
2391-
char *ref = xstrfmt("refs/heads/%s",
2392-
git_default_branch_name(0));
2393+
char *default_branch = repo_default_branch_name(the_repository, 0);
2394+
char *ref = xstrfmt("refs/heads/%s", default_branch);
23932395

23942396
r = find_ref_by_name(refs, ref);
23952397
free(ref);
2398+
free(default_branch);
2399+
23962400
if (r && oideq(&r->old_oid, &head->old_oid))
23972401
return copy_ref(r);
23982402

setup.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,7 @@ void create_reference_database(unsigned int ref_storage_format,
20462046
const char *initial_branch, int quiet)
20472047
{
20482048
struct strbuf err = STRBUF_INIT;
2049+
char *to_free = NULL;
20492050
int reinit = is_reinit();
20502051

20512052
repo_set_ref_storage_format(the_repository, ref_storage_format);
@@ -2060,7 +2061,8 @@ void create_reference_database(unsigned int ref_storage_format,
20602061
char *ref;
20612062

20622063
if (!initial_branch)
2063-
initial_branch = git_default_branch_name(quiet);
2064+
initial_branch = to_free =
2065+
repo_default_branch_name(the_repository, quiet);
20642066

20652067
ref = xstrfmt("refs/heads/%s", initial_branch);
20662068
if (check_refname_format(ref, 0) < 0)
@@ -2077,6 +2079,7 @@ void create_reference_database(unsigned int ref_storage_format,
20772079
initial_branch);
20782080

20792081
strbuf_release(&err);
2082+
free(to_free);
20802083
}
20812084

20822085
static int create_default_files(const char *template_path,

0 commit comments

Comments
 (0)