]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: strip "refs/heads/" with skip_prefix
authorGlen Choo <chooglen@google.com>
Mon, 24 Jan 2022 20:53:42 +0000 (12:53 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 24 Feb 2022 19:06:18 +0000 (11:06 -0800)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index 5897febfbec202d23fb0da2dfdc4c9cc1867127b..3e8af210fdee6e5901497e5c572d8bc4da23502a 100644 (file)
@@ -1327,7 +1327,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);
index b149e2af44185d5a78ff7eff1e2c0efb67f35f4d..0e16a5b85d3a91689ea454dc203a84e5e0785b03 100755 (executable)
@@ -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 &&