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 <jayatheerthkulkarni2005@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
K Jayatheerth committed
Jun 11, 2025 at 07:12 UTC
ffb36c64f2b39833f1ac95b79d39c881ed60de24
2 files changed
+40
-2
builtin/stash.c
+8
-2
@@ -1373,6 +1373,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1373
const char *head_short_sha1 = NULL;
1374
const char *branch_ref = NULL;
1375
const char *branch_name = "(no branch)";
1376
+ char *branch_name_buf = NULL;
1377
struct commit *head_commit = NULL;
1378
struct commit_list *parents = NULL;
1379
struct strbuf msg = STRBUF_INIT;
@@ -1405,8 +1406,12 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1406
1407
branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1408
"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
+
1415
head_short_sha1 = repo_find_unique_abbrev(the_repository,
1416
&head_commit->object.oid,
1417
DEFAULT_ABBREV);
@@ -1496,6 +1501,7 @@ done:
1501
strbuf_release(&msg);
1502
strbuf_release(&untracked_files);
1503
free_commit_list(parents);
1504
+ free(branch_name_buf);
1505
return ret;
1506
}
1507
t/t3903-stash.sh
+32
@@ -1592,4 +1592,36 @@ test_expect_success 'stash apply reports a locked index' '
1592
)
1593
'
1594
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
+
1627
test_done