aboutsummaryrefslogtreecommitdiffstats
path: root/refs.c
diff options
context:
space:
mode:
authorDon Goodman-Wilson <don@goodman-wilson.com>2020-06-24 14:46:33 +0000
committerJunio C Hamano <gitster@pobox.com>2020-06-24 09:14:21 -0700
commit8747ebb7cde9e90d20794c06e6806f75cd540142 (patch)
tree2dedd0e043802202071b7490c1ba1a97ac03ad88 /refs.c
parent32ba12dab2acf1ad11836a627956d1473f6b851a (diff)
downloadgit-8747ebb7cde9e90d20794c06e6806f75cd540142.tar.gz
init: allow setting the default for the initial branch name via the config
We just introduced the command-line option `--initial-branch=<branch-name>` to allow initializing a new repository with a different initial branch than the hard-coded one. To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each and every `git init` invocation), let's introduce the `init.defaultBranch` config setting. Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Helped-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Don Goodman-Wilson <don@goodman-wilson.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/refs.c b/refs.c
index 224ff66c7b..b98dea5217 100644
--- a/refs.c
+++ b/refs.c
@@ -560,6 +560,36 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
argv_array_pushf(prefixes, *p, len, prefix);
}
+char *repo_default_branch_name(struct repository *r)
+{
+ const char *config_key = "init.defaultbranch";
+ const char *config_display_key = "init.defaultBranch";
+ char *ret = NULL, *full_ref;
+
+ if (repo_config_get_string(r, config_key, &ret) < 0)
+ die(_("could not retrieve `%s`"), config_display_key);
+
+ if (!ret)
+ ret = xstrdup("master");
+
+ full_ref = xstrfmt("refs/heads/%s", ret);
+ if (check_refname_format(full_ref, 0))
+ die(_("invalid branch name: %s = %s"), config_display_key, ret);
+ free(full_ref);
+
+ return ret;
+}
+
+const char *git_default_branch_name(void)
+{
+ static char *ret;
+
+ if (!ret)
+ ret = repo_default_branch_name(the_repository);
+
+ 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