git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [BUG] git-stash does not handle branch name with slash correctly
@ 2021-11-10 18:55 Daniel Hahler
  2021-11-10 20:53 ` Jeff King
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Hahler @ 2021-11-10 18:55 UTC (permalink / raw)
  To: git


[-- Attachment #1.1: Type: text/plain, Size: 957 bytes --]

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.

(I have noticed this with a script/function that warns me when trying to
pop a stash to another branch than where it was stashed from initially,
which parses out the (original) branch name from this message.)


[System Info]
git version:
git version 2.33.1

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] git-stash does not handle branch name with slash correctly
  2021-11-10 18:55 [BUG] git-stash does not handle branch name with slash correctly Daniel Hahler
@ 2021-11-10 20:53 ` Jeff King
  2022-01-24 20:53   ` [PATCH] stash: strip "refs/heads/" with skip_prefix Glen Choo
  0 siblings, 1 reply; 5+ messages in thread
From: Jeff King @ 2021-11-10 20:53 UTC (permalink / raw)
  To: Daniel Hahler; +Cc: git

On Wed, Nov 10, 2021 at 07:55:06PM +0100, Daniel Hahler wrote:

> 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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] stash: strip "refs/heads/" with skip_prefix
  2021-11-10 20:53 ` Jeff King
@ 2022-01-24 20:53   ` Glen Choo
  2022-01-25  7:13     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Glen Choo @ 2022-01-24 20:53 UTC (permalink / raw)
  To: git; +Cc: Glen Choo, Kraymer, Daniel Hahler, Jeff King

When generating a message for a stash, "git stash" only records the
part of the branch name to the right of the last "/". e.g. if HEAD is at
"foo/bar/baz", "git stash" generates a message prefixed with "WIP on
baz:" instead of "WIP on foo/bar/baz:".

Fix this by using skip_prefix() to skip "refs/heads/" instead of looking
for the last instance of "/".

Reported-by: Kraymer <kraymer@gmail.com>
Reported-by: Daniel Hahler <git@thequod.de>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Glen Choo <chooglen@google.com>
---
I prepared this fix before checking the mailing list for any bug
reports; turns out that there are at least two existing reports.

My fix happens to be exactly the same as what Peff suggested, with the
additional test that he asked for.

 builtin/stash.c  |  2 +-
 t/t3903-stash.sh | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index 1ef2017c59..01f072a2fb 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1332,7 +1332,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);
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 686747e55a..bf83fb940e 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1042,6 +1042,17 @@ test_expect_success 'create stores correct message' '
 	test_cmp expect actual
 '
 
+test_expect_success 'create when branch name has /' '
+	test_when_finished "git checkout main" &&
+	git checkout -b some/topic &&
+	>foo &&
+	git add foo &&
+	STASH_ID=$(git stash create "create test message") &&
+	echo "On some/topic: create test message" >expect &&
+	git show --pretty=%s -s ${STASH_ID} >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'create with multiple arguments for the message' '
 	>foo &&
 	git add foo &&

base-commit: 89bece5c8c96f0b962cfc89e63f82d603fd60bed
-- 
2.33.GIT


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] stash: strip "refs/heads/" with skip_prefix
  2022-01-24 20:53   ` [PATCH] stash: strip "refs/heads/" with skip_prefix Glen Choo
@ 2022-01-25  7:13     ` Junio C Hamano
  2022-02-24  7:13       ` Glen Choo
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2022-01-25  7:13 UTC (permalink / raw)
  To: Glen Choo; +Cc: git, Kraymer, Daniel Hahler, Jeff King

Glen Choo <chooglen@google.com> writes:

>  	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);

The branch_name variable is initialized to a constant string "(no branch)",
so if HEAD is poihnting elsewhere (which you could do manually),
skip_prefix() would fail and leave branch_name intact, which would
give us the desirable outcome, too.

Looking good.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] stash: strip "refs/heads/" with skip_prefix
  2022-01-25  7:13     ` Junio C Hamano
@ 2022-02-24  7:13       ` Glen Choo
  0 siblings, 0 replies; 5+ messages in thread
From: Glen Choo @ 2022-02-24  7:13 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Kraymer, Daniel Hahler, Jeff King

Junio C Hamano <gitster@pobox.com> writes:

> Glen Choo <chooglen@google.com> writes:
>
>>  	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);
>
> The branch_name variable is initialized to a constant string "(no branch)",
> so if HEAD is poihnting elsewhere (which you could do manually),
> skip_prefix() would fail and leave branch_name intact, which would
> give us the desirable outcome, too.
>
> Looking good.

Hm, did we ever pick this up? I dug through the old "What's Cooking"
mails and didn't find any mention of this.

Admittedly, this dropped off my radar until performance review season
reminded me of this. Though now that I say this, it sounds like I want
this for the sake of performance review :p

(Which is not the case btw, I just want to scratch my own itch :))

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-02-24  7:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-10 18:55 [BUG] git-stash does not handle branch name with slash correctly Daniel Hahler
2021-11-10 20:53 ` Jeff King
2022-01-24 20:53   ` [PATCH] stash: strip "refs/heads/" with skip_prefix Glen Choo
2022-01-25  7:13     ` Junio C Hamano
2022-02-24  7:13       ` Glen Choo

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).