[PATCH v4 2/2] branch: add 'branch.namePrefix' config param
- From
- VALERI Yoann via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Apr 9, 2026, 10:43 UTC
- Message-ID
- <80d1ffde9d3d55d0ff2b28219e2484fb12d543d9.1775731390.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2202.v4.git.git.1775731390.gitgitgadget@gmail.com>
From: VALERI Yoann <yoann.valeri@cea.fr>
This patch adds a new configuration parameter for the branch creation feature: 'branch.namePrefix'. It corresponds to the '--name-prefix' option of 'git branch' made as configuration parameter, and behaves exactly like it.
Signed-off-by: VALERI Yoann <yoann.valeri@cea.fr> --- Documentation/config/branch.adoc | 6 ++++++ builtin/branch.c | 5 +++++ t/t3200-branch.sh | 15 +++++++++++++++ 3 files changed, 26 insertions(+)
diff --git a/Documentation/config/branch.adoc b/Documentation/config/branch.adoc index a4db9fa5c8..392228c300 100644 --- a/Documentation/config/branch.adoc +++ b/Documentation/config/branch.adoc @@ -35,6 +35,12 @@ This option defaults to `never`. value of this variable will be used as the default. See linkgit:git-for-each-ref[1] field names for valid values. +`branch.namePrefix`:: + When a new branch is created with `git branch`, use the provided value as + prefix for its name. Can be '@{current}' to use the current branch's name + as prefix. This value can be overriden by using the '--[no-]name-prefix' + option of `git branch`. + `branch.<name>.remote`:: When on branch _<name>_, it tells `git fetch` and `git push` which remote to fetch from or push to. The remote to push to diff --git a/builtin/branch.c b/builtin/branch.c index cf7d095f22..2987ac4122 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -726,6 +726,7 @@ int cmd_branch(int argc, struct ref_format format = REF_FORMAT_INIT; struct repo_config_values *cfg = repo_config_values(the_repository); char *name_prefix = NULL; + char *safekeep_name_prefix; int ret; struct option options[] = { @@ -809,6 +810,9 @@ int cmd_branch(int argc, else if (!skip_prefix(head, "refs/heads/", &head)) die(_("HEAD not found below refs/heads!")); + repo_config_get_string(the_repository, "branch.namePrefix", &name_prefix); + safekeep_name_prefix = name_prefix; + argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, 0); @@ -1029,6 +1033,7 @@ int cmd_branch(int argc, ret = 0; out: + free(safekeep_name_prefix); string_list_clear(&sorting_options, 0); return ret; } diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index f81d4380a9..e75f886842 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -1741,4 +1741,19 @@ test_expect_success 'create branch with --name-prefix' ' git branch -D bn-with-no-prefix ' +test_expect_success 'create branch with config prefix' ' + test_config branch.autosetupmerge false && + test_config branch.namePrefix blob && + git branch -- -with-prefix && + test_ref_exists refs/heads/blob-with-prefix && + test_must_fail git branch -- -with-prefix && + test_config branch.namePrefix "@{current}" && + git checkout main && + git branch -- -with-prefix && + test_ref_exists refs/heads/main-with-prefix && + git branch --no-name-prefix branch-with-no-prefix && + test_ref_exists refs/heads/branch-with-no-prefix && + git branch -D blob-with-prefix main-with-prefix branch-with-no-prefix +' + test_done
-- gitgitgadget