@matheusbsbs / matheus-bagliano / commits / 8747ebb7cd
Fork 0

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>

Don Goodman-Wilson committed Jun 24, 2020 at 14:46 UTC 8747ebb7cde9e90d20794c06e6806f75cd540142
5 files changed +57 -1
Documentation/config/init.txt
+4
@@ -1,3 +1,7 @@
1 init.templateDir::
2 Specify the directory from which templates will be copied.
3 (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
4 +
5 +init.defaultBranch::
6 + Allows overriding the default branch name when initializing
7 + a new repository.
builtin/init-db.c
+1 -1
@@ -269,7 +269,7 @@ static int create_default_files(const char *template_path,
269 char *ref;
270
271 if (!initial_branch)
272 - initial_branch = "master";
272 + initial_branch = git_default_branch_name();
273
274 ref = xstrfmt("refs/heads/%s", initial_branch);
275 if (check_refname_format(ref, 0) < 0)
refs.c
+30
@@ -560,6 +560,36 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
560 argv_array_pushf(prefixes, *p, len, prefix);
561 }
562
563 +char *repo_default_branch_name(struct repository *r)
564 +{
565 + const char *config_key = "init.defaultbranch";
566 + const char *config_display_key = "init.defaultBranch";
567 + char *ret = NULL, *full_ref;
568 +
569 + if (repo_config_get_string(r, config_key, &ret) < 0)
570 + die(_("could not retrieve `%s`"), config_display_key);
571 +
572 + if (!ret)
573 + ret = xstrdup("master");
574 +
575 + full_ref = xstrfmt("refs/heads/%s", ret);
576 + if (check_refname_format(full_ref, 0))
577 + die(_("invalid branch name: %s = %s"), config_display_key, ret);
578 + free(full_ref);
579 +
580 + return ret;
581 +}
582 +
583 +const char *git_default_branch_name(void)
584 +{
585 + static char *ret;
586 +
587 + if (!ret)
588 + ret = repo_default_branch_name(the_repository);
589 +
590 + return ret;
591 +}
592 +
593 /*
594 * *string and *len will only be substituted, and *string returned (for
595 * later free()ing) if the string passed in is a magic short-hand form
refs.h
+9
@@ -154,6 +154,15 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
154 int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
155 int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
156
157 +/*
158 + * Retrieves the default branch name for newly-initialized repositories.
159 + *
160 + * The return value of `repo_default_branch_name()` is an allocated string. The
161 + * return value of `git_default_branch_name()` is a singleton.
162 + */
163 +const char *git_default_branch_name(void);
164 +char *repo_default_branch_name(struct repository *r);
165 +
166 /*
167 * A ref_transaction represents a collection of reference updates that
168 * should succeed or fail together.
t/t0001-init.sh
+13
@@ -477,4 +477,17 @@ test_expect_success '--initial-branch' '
477 grep hello actual
478 '
479
480 +test_expect_success 'overridden default initial branch name (config)' '
481 + test_config_global init.defaultBranch nmb &&
482 + git init initial-branch-config &&
483 + git -C initial-branch-config symbolic-ref HEAD >actual &&
484 + grep nmb actual
485 +'
486 +
487 +test_expect_success 'invalid default branch name' '
488 + test_config_global init.defaultBranch "with space" &&
489 + test_must_fail git init initial-branch-invalid 2>err &&
490 + test_i18ngrep "invalid branch name" err
491 +'
492 +
493 test_done