From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Subject: [PATCH 12/16] refs: drop `git_default_branch_name()`
Date: Thu, 16 May 2024 10:05:10 +0200 [thread overview]
Message-ID: <09ca848e7d8a9c4f4d8927d8f01a1c3cb5d34e8c.1715836916.git.ps@pks.im> (raw)
In-Reply-To: <cover.1715836916.git.ps@pks.im>
[-- Attachment #1: Type: text/plain, Size: 6081 bytes --]
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>
---
builtin/clone.c | 5 ++++-
builtin/var.c | 2 +-
refs.c | 10 ----------
refs.h | 4 +---
remote.c | 12 ++++++++----
setup.c | 5 ++++-
6 files changed, 18 insertions(+), 20 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 554b29768c..bd3e8302ed 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -1468,6 +1468,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
} else if (remote_head) {
our_head_points_at = NULL;
} else {
+ char *to_free = NULL;
const char *branch;
if (!mapped_refs) {
@@ -1480,7 +1481,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
"refs/heads/", &branch)) {
unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
} else {
- branch = git_default_branch_name(0);
+ branch = to_free = repo_default_branch_name(the_repository, 0);
unborn_head = xstrfmt("refs/heads/%s", branch);
}
@@ -1496,6 +1497,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
* a match.
*/
our_head_points_at = find_remote_branch(mapped_refs, branch);
+
+ free(to_free);
}
write_refspec_config(src_ref_prefix, our_head_points_at,
diff --git a/builtin/var.c b/builtin/var.c
index cf5567208a..5dc384810c 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -46,7 +46,7 @@ static char *pager(int ident_flag UNUSED)
static char *default_branch(int ident_flag UNUSED)
{
- return xstrdup_or_null(git_default_branch_name(1));
+ return repo_default_branch_name(the_repository, 1);
}
static char *shell_path(int ident_flag UNUSED)
diff --git a/refs.c b/refs.c
index 7aecf11bf4..3618c8f7a4 100644
--- a/refs.c
+++ b/refs.c
@@ -664,16 +664,6 @@ char *repo_default_branch_name(struct repository *r, int quiet)
return ret;
}
-const char *git_default_branch_name(int quiet)
-{
- static char *ret;
-
- if (!ret)
- ret = repo_default_branch_name(the_repository, quiet);
-
- return ret;
-}
-
/*
* *string and *len will only be substituted, and *string returned (for
* later free()ing) if the string passed in is a magic short-hand form
diff --git a/refs.h b/refs.h
index 9769a25edd..95c3437443 100644
--- a/refs.h
+++ b/refs.h
@@ -169,10 +169,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
/*
* Retrieves the default branch name for newly-initialized repositories.
*
- * The return value of `repo_default_branch_name()` is an allocated string. The
- * return value of `git_default_branch_name()` is a singleton.
+ * The return value is an allocated string.
*/
-const char *git_default_branch_name(int quiet);
char *repo_default_branch_name(struct repository *r, int quiet);
/*
diff --git a/remote.c b/remote.c
index ec8c158e60..85c390b199 100644
--- a/remote.c
+++ b/remote.c
@@ -305,7 +305,7 @@ static void read_remotes_file(struct remote_state *remote_state,
static void read_branches_file(struct remote_state *remote_state,
struct remote *remote)
{
- char *frag;
+ char *frag, *to_free = NULL;
struct strbuf buf = STRBUF_INIT;
FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
@@ -333,7 +333,7 @@ static void read_branches_file(struct remote_state *remote_state,
if (frag)
*(frag++) = '\0';
else
- frag = (char *)git_default_branch_name(0);
+ frag = to_free = repo_default_branch_name(the_repository, 0);
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -345,6 +345,8 @@ static void read_branches_file(struct remote_state *remote_state,
*/
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
remote->fetch_tags = 1; /* always auto-follow */
+
+ free(to_free);
}
static int handle_config(const char *key, const char *value,
@@ -2388,11 +2390,13 @@ struct ref *guess_remote_head(const struct ref *head,
/* If a remote branch exists with the default branch name, let's use it. */
if (!all) {
- char *ref = xstrfmt("refs/heads/%s",
- git_default_branch_name(0));
+ char *default_branch = repo_default_branch_name(the_repository, 0);
+ char *ref = xstrfmt("refs/heads/%s", default_branch);
r = find_ref_by_name(refs, ref);
free(ref);
+ free(default_branch);
+
if (r && oideq(&r->old_oid, &head->old_oid))
return copy_ref(r);
diff --git a/setup.c b/setup.c
index 4a738f4c90..15ad84d3be 100644
--- a/setup.c
+++ b/setup.c
@@ -2046,6 +2046,7 @@ void create_reference_database(unsigned int ref_storage_format,
const char *initial_branch, int quiet)
{
struct strbuf err = STRBUF_INIT;
+ char *to_free = NULL;
int reinit = is_reinit();
repo_set_ref_storage_format(the_repository, ref_storage_format);
@@ -2060,7 +2061,8 @@ void create_reference_database(unsigned int ref_storage_format,
char *ref;
if (!initial_branch)
- initial_branch = git_default_branch_name(quiet);
+ initial_branch = to_free =
+ repo_default_branch_name(the_repository, quiet);
ref = xstrfmt("refs/heads/%s", initial_branch);
if (check_refname_format(ref, 0) < 0)
@@ -2077,6 +2079,7 @@ void create_reference_database(unsigned int ref_storage_format,
initial_branch);
strbuf_release(&err);
+ free(to_free);
}
static int create_default_files(const char *template_path,
--
2.45.1.190.g19fe900cfc.dirty
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-05-16 8:05 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-16 8:04 [PATCH 00/16] refs: drop all references to `the_repository` Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 01/16] refs: adjust names for `init` and `init_db` callbacks Patrick Steinhardt
2024-05-16 14:50 ` Karthik Nayak
2024-05-16 8:04 ` [PATCH 02/16] refs: rename `init_db` callback to avoid confusion Patrick Steinhardt
2024-05-16 15:00 ` Karthik Nayak
2024-05-16 18:24 ` Junio C Hamano
2024-05-17 7:08 ` Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 03/16] refs: implement releasing ref storages Patrick Steinhardt
2024-05-16 16:39 ` Karthik Nayak
2024-05-16 18:01 ` Junio C Hamano
2024-05-17 7:09 ` Patrick Steinhardt
2024-05-17 7:08 ` Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 04/16] refs: track ref stores via strmap Patrick Steinhardt
2024-05-16 18:34 ` Junio C Hamano
2024-05-16 8:04 ` [PATCH 05/16] refs: pass repo when retrieving submodule ref store Patrick Steinhardt
2024-05-16 18:44 ` Junio C Hamano
2024-05-17 7:09 ` Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 06/16] refs: refactor `resolve_gitlink_ref()` to accept a repository Patrick Steinhardt
2024-05-16 18:45 ` Junio C Hamano
2024-05-16 8:04 ` [PATCH 07/16] refs: retrieve worktree ref stores via associated repository Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 08/16] refs: convert iteration over replace refs to accept ref store Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 09/16] refs: pass ref store when detecting dangling symrefs Patrick Steinhardt
2024-05-16 8:04 ` [PATCH 10/16] refs: move object peeling into "object.c" Patrick Steinhardt
2024-05-16 20:55 ` Junio C Hamano
2024-05-16 8:05 ` [PATCH 11/16] refs: pass repo when peeling objects Patrick Steinhardt
2024-05-16 8:05 ` Patrick Steinhardt [this message]
2024-05-16 18:44 ` [PATCH 12/16] refs: drop `git_default_branch_name()` Karthik Nayak
2024-05-16 8:05 ` [PATCH 13/16] refs: remove `dwim_log()` Patrick Steinhardt
2024-05-16 8:05 ` [PATCH 14/16] refs/files: use correct repository Patrick Steinhardt
2024-05-16 8:05 ` [PATCH 15/16] refs/files: remove references to `the_hash_algo` Patrick Steinhardt
2024-05-16 8:05 ` [PATCH 16/16] refs/packed: " Patrick Steinhardt
2024-05-16 18:57 ` [PATCH 00/16] refs: drop all references to `the_repository` Karthik Nayak
2024-05-17 7:08 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=09ca848e7d8a9c4f4d8927d8f01a1c3cb5d34e8c.1715836916.git.ps@pks.im \
--to=ps@pks.im \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).