Skip to content

Commit a5dae09

Browse files
committed
Merge branch 'kj/stash-onbranch-submodule-fix' into next
"git stash" recorded a wrong branch name when submodules are present in the current checkout, which has been corrected. * kj/stash-onbranch-submodule-fix: stash: fix incorrect branch name in stash message
2 parents e53466b + 47b8819 commit a5dae09

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
@@ -1372,6 +1372,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
13721372
const char *head_short_sha1 = NULL;
13731373
const char *branch_ref = NULL;
13741374
const char *branch_name = "(no branch)";
1375+
char *branch_name_buf = NULL;
13751376
struct commit *head_commit = NULL;
13761377
struct commit_list *parents = NULL;
13771378
struct strbuf msg = STRBUF_INIT;
@@ -1404,8 +1405,13 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14041405

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

t/t3903-stash.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,4 +1614,38 @@ test_expect_success 'stash apply reports a locked index' '
16141614
)
16151615
'
16161616

1617+
test_expect_success 'stash reflog message uses superproject branch, not submodule branch' '
1618+
git init sub_project &&
1619+
(
1620+
cd sub_project &&
1621+
echo "Initial content in sub_project" >sub_file.txt &&
1622+
git add sub_file.txt &&
1623+
git commit -q -m "Initial commit in sub_project"
1624+
) &&
1625+
1626+
git init main_project &&
1627+
(
1628+
cd main_project &&
1629+
echo "Initial content in main_project" >main_file.txt &&
1630+
git add main_file.txt &&
1631+
git commit -q -m "Initial commit in main_project" &&
1632+
1633+
git -c protocol.file.allow=always submodule add --quiet ../sub_project sub &&
1634+
git commit -q -m "Added submodule sub_project" &&
1635+
1636+
git checkout -q -b feature_main &&
1637+
cd sub &&
1638+
git checkout -q -b feature_sub &&
1639+
cd .. &&
1640+
1641+
git checkout -q -b work_branch &&
1642+
echo "Important work to be stashed" >work_item.txt &&
1643+
git add work_item.txt &&
1644+
git stash push -q -m "custom stash for work_branch" &&
1645+
1646+
git stash list >../actual_stash_list.txt &&
1647+
grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1648+
)
1649+
'
1650+
16171651
test_done

0 commit comments

Comments
 (0)