]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: drop `git_default_branch_name()`
authorPatrick Steinhardt <ps@pks.im>
Fri, 17 May 2024 08:19:09 +0000 (10:19 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 May 2024 17:33:39 +0000 (10:33 -0700)
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>
builtin/clone.c
builtin/var.c
refs.c
refs.h
remote.c
setup.c

index 554b29768c351eda6635936a2d29a96f8e921b72..bd3e8302edbf851d9fe1e0f18de9115c97c32852 100644 (file)
@@ -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,
index cf5567208a277a62b872a1846919060fb6e276c3..5dc384810c0969eb267707f675320e15e02a99b1 100644 (file)
@@ -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 5f1819b33ea6ee9bcb9a300e54bba2357b54e5a2..83cd965a264685c988b6f4f76860e5346a0613b6 100644 (file)
--- 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 e043b6cfa3ba5b60442d3d4f1b8cbcf130e53504..e906dbb44ac2a02a00b800e95dce83f849ea6a91 100644 (file)
--- 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);
 
 /*
index ec8c158e60b729119b50f44ad892bb913f2b7bc0..85c390b1990bbd548e43b7c34a750fe878a7251a 100644 (file)
--- 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 fec6bfd5faa16f3a4feb8382e0ee344fa29b43d5..481b57d523940d4a1247fcb22ae610af00c62709 100644 (file)
--- 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,