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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
May 17, 2024 at 10:19 UTC
97abaab5f6390ccb3e55c8b63c9087be7b1fc1d7
6 files changed
+18
-20
builtin/clone.c
+4
-1
@@ -1468,6 +1468,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1468
} else if (remote_head) {
1469
our_head_points_at = NULL;
1470
} else {
1471
+ char *to_free = NULL;
1472
const char *branch;
1473
1474
if (!mapped_refs) {
@@ -1480,7 +1481,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1481
"refs/heads/", &branch)) {
1482
unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
1483
} else {
1483
- branch = git_default_branch_name(0);
1484
+ branch = to_free = repo_default_branch_name(the_repository, 0);
1485
unborn_head = xstrfmt("refs/heads/%s", branch);
1486
}
1487
@@ -1496,6 +1497,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1497
* a match.
1498
*/
1499
our_head_points_at = find_remote_branch(mapped_refs, branch);
1500
+
1501
+ free(to_free);
1502
}
1503
1504
write_refspec_config(src_ref_prefix, our_head_points_at,
builtin/var.c
+1
-1
@@ -46,7 +46,7 @@ static char *pager(int ident_flag UNUSED)
46
47
static char *default_branch(int ident_flag UNUSED)
48
{
49
- return xstrdup_or_null(git_default_branch_name(1));
49
+ return repo_default_branch_name(the_repository, 1);
50
}
51
52
static char *shell_path(int ident_flag UNUSED)
refs.c
-10
@@ -664,16 +664,6 @@ char *repo_default_branch_name(struct repository *r, int quiet)
664
return ret;
665
}
666
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
-
667
/*
668
* *string and *len will only be substituted, and *string returned (for
669
* later free()ing) if the string passed in is a magic short-hand form
refs.h
+1
-3
@@ -169,10 +169,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
169
/*
170
* Retrieves the default branch name for newly-initialized repositories.
171
*
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.
173
*/
175
-const char *git_default_branch_name(int quiet);
174
char *repo_default_branch_name(struct repository *r, int quiet);
175
176
/*
remote.c
+8
-4
@@ -305,7 +305,7 @@ static void read_remotes_file(struct remote_state *remote_state,
305
static void read_branches_file(struct remote_state *remote_state,
306
struct remote *remote)
307
{
308
- char *frag;
308
+ char *frag, *to_free = NULL;
309
struct strbuf buf = STRBUF_INIT;
310
FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
311
@@ -333,7 +333,7 @@ static void read_branches_file(struct remote_state *remote_state,
333
if (frag)
334
*(frag++) = '\0';
335
else
336
- frag = (char *)git_default_branch_name(0);
336
+ frag = to_free = repo_default_branch_name(the_repository, 0);
337
338
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
339
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -345,6 +345,8 @@ static void read_branches_file(struct remote_state *remote_state,
345
*/
346
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
347
remote->fetch_tags = 1; /* always auto-follow */
348
+
349
+ free(to_free);
350
}
351
352
static int handle_config(const char *key, const char *value,
@@ -2388,11 +2390,13 @@ struct ref *guess_remote_head(const struct ref *head,
2390
2391
/* If a remote branch exists with the default branch name, let's use it. */
2392
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);
2395
2396
r = find_ref_by_name(refs, ref);
2397
free(ref);
2398
+ free(default_branch);
2399
+
2400
if (r && oideq(&r->old_oid, &head->old_oid))
2401
return copy_ref(r);
2402
setup.c
+4
-1
@@ -2046,6 +2046,7 @@ void create_reference_database(unsigned int ref_storage_format,
2046
const char *initial_branch, int quiet)
2047
{
2048
struct strbuf err = STRBUF_INIT;
2049
+ char *to_free = NULL;
2050
int reinit = is_reinit();
2051
2052
repo_set_ref_storage_format(the_repository, ref_storage_format);
@@ -2060,7 +2061,8 @@ void create_reference_database(unsigned int ref_storage_format,
2061
char *ref;
2062
2063
if (!initial_branch)
2063
- initial_branch = git_default_branch_name(quiet);
2064
+ initial_branch = to_free =
2065
+ repo_default_branch_name(the_repository, quiet);
2066
2067
ref = xstrfmt("refs/heads/%s", initial_branch);
2068
if (check_refname_format(ref, 0) < 0)
@@ -2077,6 +2079,7 @@ void create_reference_database(unsigned int ref_storage_format,
2079
initial_branch);
2080
2081
strbuf_release(&err);
2082
+ free(to_free);
2083
}
2084
2085
static int create_default_files(const char *template_path,