Skip to content

Commit ceaf037

Browse files
chooglengitster
authored andcommitted
stash: strip "refs/heads/" with skip_prefix
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 <[email protected]> Reported-by: Daniel Hahler <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dab1b79 commit ceaf037

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

builtin/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
13271327

13281328
branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
13291329
if (flags & REF_ISSYMREF)
1330-
branch_name = strrchr(branch_ref, '/') + 1;
1330+
skip_prefix(branch_ref, "refs/heads/", &branch_name);
13311331
head_short_sha1 = find_unique_abbrev(&head_commit->object.oid,
13321332
DEFAULT_ABBREV);
13331333
strbuf_addf(&msg, "%s: %s ", branch_name, head_short_sha1);

t/t3903-stash.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,17 @@ test_expect_success 'create stores correct message' '
10421042
test_cmp expect actual
10431043
'
10441044

1045+
test_expect_success 'create when branch name has /' '
1046+
test_when_finished "git checkout main" &&
1047+
git checkout -b some/topic &&
1048+
>foo &&
1049+
git add foo &&
1050+
STASH_ID=$(git stash create "create test message") &&
1051+
echo "On some/topic: create test message" >expect &&
1052+
git show --pretty=%s -s ${STASH_ID} >actual &&
1053+
test_cmp expect actual
1054+
'
1055+
10451056
test_expect_success 'create with multiple arguments for the message' '
10461057
>foo &&
10471058
git add foo &&

0 commit comments

Comments
 (0)