提交 8747ebb7 编写于 作者: D Don Goodman-Wilson 提交者: Junio C Hamano

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: NJohannes Schindelin <johannes.schindelin@gmx.de>
Helped-by: NDerrick Stolee <dstolee@microsoft.com>
Signed-off-by: NDon Goodman-Wilson <don@goodman-wilson.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 32ba12da
init.templateDir::
Specify the directory from which templates will be copied.
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
init.defaultBranch::
Allows overriding the default branch name when initializing
a new repository.
......@@ -269,7 +269,7 @@ static int create_default_files(const char *template_path,
char *ref;
if (!initial_branch)
initial_branch = "master";
initial_branch = git_default_branch_name();
ref = xstrfmt("refs/heads/%s", initial_branch);
if (check_refname_format(ref, 0) < 0)
......
......@@ -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
......
......@@ -154,6 +154,15 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
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.
*/
const char *git_default_branch_name(void);
char *repo_default_branch_name(struct repository *r);
/*
* A ref_transaction represents a collection of reference updates that
* should succeed or fail together.
......
......@@ -477,4 +477,17 @@ test_expect_success '--initial-branch' '
grep hello actual
'
test_expect_success 'overridden default initial branch name (config)' '
test_config_global init.defaultBranch nmb &&
git init initial-branch-config &&
git -C initial-branch-config symbolic-ref HEAD >actual &&
grep nmb actual
'
test_expect_success 'invalid default branch name' '
test_config_global init.defaultBranch "with space" &&
test_must_fail git init initial-branch-invalid 2>err &&
test_i18ngrep "invalid branch name" err
'
test_done
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册