Skip to content

Commit 47b8819

Browse files
jayatheerthkulkarnigitster
authored andcommitted
stash: fix incorrect branch name in stash message
When creating a stash, Git uses the current branch name of the superproject to construct the stash commit message. However, in repositories with submodules, the message may mistakenly display the submodule branch name instead. This is because `refs_resolve_ref_unsafe()` returns a pointer to a static buffer. Subsequent calls to the same function overwrite the buffer, corrupting the originally fetched `branch_name` used for the stash message. Use `xstrdup()` to duplicate the branch name immediately after resolving it, so that later buffer overwrites do not affect the stash message. Signed-off-by: K Jayatheerth <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d50a5e8 commit 47b8819

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

builtin/stash.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
13731373
const char *head_short_sha1 = NULL;
13741374
const char *branch_ref = NULL;
13751375
const char *branch_name = "(no branch)";
1376+
char *branch_name_buf = NULL;
13761377
struct commit *head_commit = NULL;
13771378
struct commit_list *parents = NULL;
13781379
struct strbuf msg = STRBUF_INIT;
@@ -1405,8 +1406,13 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14051406

14061407
branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
14071408
"HEAD", 0, NULL, &flags);
1408-
if (flags & REF_ISSYMREF)
1409-
skip_prefix(branch_ref, "refs/heads/", &branch_name);
1409+
1410+
if (flags & REF_ISSYMREF) {
1411+
if (skip_prefix(branch_ref, "refs/heads/", &branch_name))
1412+
branch_name = branch_name_buf = xstrdup(branch_name);
1413+
} else
1414+
branch_name = "(no branch)";
1415+
14101416
head_short_sha1 = repo_find_unique_abbrev(the_repository,
14111417
&head_commit->object.oid,
14121418
DEFAULT_ABBREV);
@@ -1496,6 +1502,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14961502
strbuf_release(&msg);
14971503
strbuf_release(&untracked_files);
14981504
free_commit_list(parents);
1505+
free(branch_name_buf);
14991506
return ret;
15001507
}
15011508

t/t3903-stash.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,4 +1592,38 @@ test_expect_success 'stash apply reports a locked index' '
15921592
)
15931593
'
15941594

1595+
test_expect_success 'stash reflog message uses superproject branch, not submodule branch' '
1596+
git init sub_project &&
1597+
(
1598+
cd sub_project &&
1599+
echo "Initial content in sub_project" >sub_file.txt &&
1600+
git add sub_file.txt &&
1601+
git commit -q -m "Initial commit in sub_project"
1602+
) &&
1603+
1604+
git init main_project &&
1605+
(
1606+
cd main_project &&
1607+
echo "Initial content in main_project" >main_file.txt &&
1608+
git add main_file.txt &&
1609+
git commit -q -m "Initial commit in main_project" &&
1610+
1611+
git -c protocol.file.allow=always submodule add --quiet ../sub_project sub &&
1612+
git commit -q -m "Added submodule sub_project" &&
1613+
1614+
git checkout -q -b feature_main &&
1615+
cd sub &&
1616+
git checkout -q -b feature_sub &&
1617+
cd .. &&
1618+
1619+
git checkout -q -b work_branch &&
1620+
echo "Important work to be stashed" >work_item.txt &&
1621+
git add work_item.txt &&
1622+
git stash push -q -m "custom stash for work_branch" &&
1623+
1624+
git stash list >../actual_stash_list.txt &&
1625+
grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1626+
)
1627+
'
1628+
15951629
test_done

0 commit comments

Comments
 (0)