Skip to content

Commit d51f717

Browse files
committed
branch: add 'branch.namePrefix' config param
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>
1 parent 6cbb950 commit d51f717

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

Documentation/config/branch.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ This option defaults to `never`.
3535
value of this variable will be used as the default.
3636
See linkgit:git-for-each-ref[1] field names for valid values.
3737

38+
`branch.namePrefix`::
39+
When a new branch is created with `git branch`, use the provided value as
40+
prefix for its name. Can be '@{current}' to use the current branch's name
41+
as prefix.
42+
3843
`branch.<name>.remote`::
3944
When on branch _<name>_, it tells `git fetch` and `git push`
4045
which remote to fetch from or push to. The remote to push to

branch.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,22 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
368368
void add_branch_prefix(const char *name_prefix,
369369
const char *current_branch, struct strbuf *buf)
370370
{
371-
int value = 0;
371+
char *config_prefix = NULL;
372372

373-
if (!name_prefix)
374-
return;
373+
if (!name_prefix) {
374+
if (repo_config_get_string(the_repository, "branch.namePrefix",
375+
&config_prefix))
376+
return;
375377

376-
if (name_prefix[0] != '@') {
377-
strbuf_addstr(buf, name_prefix);
378-
return;
378+
name_prefix = config_prefix;
379379
}
380380

381-
if (strcmp(name_prefix, "@{current}") == 0)
381+
if (name_prefix[0] != '@')
382+
strbuf_addstr(buf, name_prefix);
383+
else if (strcmp(name_prefix, "@{current}") == 0)
382384
strbuf_addstr(buf, current_branch);
385+
386+
free(config_prefix);
383387
}
384388

385389
/*

t/t3200-branch.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,4 +1734,16 @@ test_expect_success 'create branch with --name-prefix' '
17341734
git branch -D blob-with-prefix-with-prefix
17351735
'
17361736

1737+
test_expect_success 'create branch with config prefix' '
1738+
test_config branch.namePrefix blob &&
1739+
git branch -- -with-prefix &&
1740+
test_must_fail git branch -- -with-prefix &&
1741+
test_config branch.namePrefix "@{current}" &&
1742+
git checkout main &&
1743+
git branch -- -with-prefix &&
1744+
test_ref_exists refs/heads/blob-with-prefix &&
1745+
test_ref_exists refs/heads/main-with-prefix &&
1746+
git branch -D blob-with-prefix main-with-prefix
1747+
'
1748+
17371749
test_done

0 commit comments

Comments
 (0)