Re: [PATCH v3 1/3] branch: add '--name-prefix' option
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Mar 7, 2026, 07:06 UTC
- Message-ID
- <CAPig+cRQcA+0yWqbYMfjFXxhTsQ-tjJEqsVFudu1thuYi3AXUw@mail.gmail.com>
- In-Reply-To
- <6cbb950d8bc3b647d7fff72a72f938ee369b552c.1772802872.git.gitgitgadget@gmail.com>
On Fri, Mar 6, 2026 at 8:15 AM VALERI Yoann via GitGitGadget <gitgitgadget@gmail.com> wrote:
Show 12 quoted lines
> This patch adds a '--name-prefix' option to add a prefix to a newly
> created branch. It can use a regular string or a token as prefix. The
> only token currently handled is '@{current}', which is substituted for
> the current branch's name.
>
> Signed-off-by: VALERI Yoann <yoann.valeri@cea.fr>
> ---
> diff --git a/Documentation/git-branch.adoc b/Documentation/git-branch.adoc
> @@ -64,6 +65,10 @@ Note that this will create the new branch, but it will not switch the
> +With a `--name-prefix` option, you can add a prefix to the branch to create.
> +This can either a simple name, or a token. Currently, only '@{current}' is
> +managed as token, and will use the current branch name as prefix.s/This can/& be/
Show 6 quoted lines
> diff --git a/branch.c b/branch.c
> @@ -365,6 +365,23 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
> +void add_branch_prefix(const char *name_prefix,
> + const char *current_branch, struct strbuf *buf)
> +{
> + int value = 0;What is `value`? It doesn't seem to be used at all in this function.
Show 11 quoted lines
> + if (!name_prefix)
> + return;
> +
> + if (name_prefix[0] != '@') {
> + strbuf_addstr(buf, name_prefix);
> + return;
> + }
> +
> + if (strcmp(name_prefix, "@{current}") == 0)
> + strbuf_addstr(buf, current_branch);
> +}I would expect this function to produce some sort of diagnostic warning when the user has given it a "@{token}" it doesn't recognize.
Show 12 quoted lines
> diff --git a/branch.h b/branch.h
> @@ -148,6 +148,18 @@ int install_branch_config(int flag, const char *local, const char *origin, const
> +/*
> + * Store in 'buf' a prefix to the name of a branch to create by using the given
> + * string 'name_prefix'. It can either be a simple string to a shorthand
> + * starting with '@'.
> + *
> + * Currently, only '@{current}' is managed, and will use 'current_branch' as
> + * prefix.
> + */
> +void add_branch_prefix(const char *name_prefix, const char *current_branch,
> + struct strbuf *buf);It feels unnecessarily burdensome to force the caller to compute and pass in `current_branch`. Intuitively, one would expect add_branch_prefix() to compute the current branch itself if it discovers that "@{current}" has been requested.
Moveover, this approach will not scale well when support for additional "@{tokens}" is added down the road since it burdens *all* callers with providing the values for *all* possible tokens.
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh > @@ -1716,4 +1716,22 @@ test_expect_success 'errors if given a bad branch name' ' > +test_expect_success 'create branch with --name-prefix' ' > + git config branch.autosetupmerge false &&
Let's use `test_config` which will ensure that this setting is reverted at the end of the test.
Show 15 quoted lines
> + git branch branch-with-prefix &&
> + git branch --name-prefix "blob" -- -with-prefix &&
> + test_must_fail git branch --name-prefix "blob" -- -with-prefix &&
> + git branch --name-prefix "@{current}" -- -with-prefix &&
> + git switch blob-with-prefix &&
> + git branch --name-prefix "@{current}" -- -with-prefix &&
> + test_must_fail git branch --name-prefix "@{current}" -- -with-prefix &&
> + test_ref_exists refs/heads/branch-with-prefix &&
> + test_ref_exists refs/heads/main-with-prefix &&
> + test_ref_exists refs/heads/blob-with-prefix &&
> + test_ref_exists refs/heads/blob-with-prefix-with-prefix &&
> + git checkout main &&
> + git branch -D branch-with-prefix main-with-prefix blob-with-prefix &&
> + git branch -D blob-with-prefix-with-prefix
> +'