git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [BUG] git-stash does not handle branch name with slash correctly

From
Jeff King <peff@peff.net>
Date
Nov 10, 2021, 20:53 UTC
Message-ID
<YYwxVO7A29FcN+hN@coredump.intra.peff.net>
In-Reply-To
<693b55f9-3c0e-5a31-5a43-d6495d689779@hahler.de>
On Wed, Nov 10, 2021 at 07:55:06PM +0100, Daniel Hahler wrote:
Show 23 quoted lines
> The default (commit) message when creating a stash strips the beginning of
> branch names if they contain a slash,
> e.g. "WIP on 3.2.x: …" instead of "WIP on stable/3.2.x: …"
> 
> From builtin/stash.c (in do_create_stash):
> 
> 	branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
> 	if (flags & REF_ISSYMREF)
> 		branch_name = strrchr(branch_ref, '/') + 1;
> 
> 
> Whereas git-legacy-stash has this (in create_stash):
> 
> 	if branch=$(git symbolic-ref -q HEAD)
> 	then
> 		branch=${branch#refs/heads/}
> 	else
> 		branch='(no branch)'
> 	fi
> 	msg=$(printf '%s: %s' "$branch" "$head")
> 
> I think it should also strip only "refs/heads/" or use another method that
> keeps the branch name intact.

Yes, the C behavior just seems wrong (and came as part of the C rewrite, so doesn't seem intentional). Something like:

diff --git a/builtin/stash.c b/builtin/stash.c
index d441481d68..70dcb15cb7 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1334,7 +1334,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 
 	branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
 	if (flags & REF_ISSYMREF)
-		branch_name = strrchr(branch_ref, '/') + 1;
+		skip_prefix(branch_ref, "refs/heads/", &branch_name);
 	head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
 					     DEFAULT_ABBREV);
 	strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);

seems like the right fix. Do you want to try to work that into a patch
with a test?

-Peff
Previous: Daniel HahlerNext: Glen Choo
Message 2 of 5 in “[BUG] git-stash does not handle branch name with slash correctly”
  1. Daniel HahlerNov 10, 2021
  2. Jeff KingNov 10, 2021
  3. stash: strip "refs/heads/" with skip_prefixGlen Choo, Jan 24, 2022
  4. Junio C HamanoJan 25, 2022
  5. Glen ChooFeb 24, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.