Skip to content

Commit ffb36c6

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 ffb36c6

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

builtin/stash.c

Lines changed: 8 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,12 @@ 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+
}
1414+
14101415
head_short_sha1 = repo_find_unique_abbrev(the_repository,
14111416
&head_commit->object.oid,
14121417
DEFAULT_ABBREV);
@@ -1496,6 +1501,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14961501
strbuf_release(&msg);
14971502
strbuf_release(&untracked_files);
14981503
free_commit_list(parents);
1504+
free(branch_name_buf);
14991505
return ret;
15001506
}
15011507

t/t3903-stash.sh

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

1595+
test_expect_success 'submodules does not affect the branch recorded in stash message' '
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 -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 -m "Initial commit in main_project" &&
1610+
1611+
git -c protocol.file.allow=always submodule add ../sub_project sub &&
1612+
git commit -m "Added submodule sub_project" &&
1613+
1614+
git checkout -b feature_main &&
1615+
git -C sub checkout -b feature_sub &&
1616+
1617+
git checkout -b work_branch &&
1618+
echo "Important work to be stashed" >work_item.txt &&
1619+
git add work_item.txt &&
1620+
git stash push -m "custom stash for work_branch" &&
1621+
1622+
git stash list >../actual_stash_list.txt &&
1623+
grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1624+
)
1625+
'
1626+
15951627
test_done

0 commit comments

Comments
 (0)