Skip to content

Commit 71971f4

Browse files
committed
Merge branch 'kj/stash-onbranch-submodule-fix' into jch
* kj/stash-onbranch-submodule-fix: stash: fix incorrect branch name in stash message
2 parents 3caf8f8 + 47b8819 commit 71971f4

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
@@ -1431,6 +1431,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14311431
const char *head_short_sha1 = NULL;
14321432
const char *branch_ref = NULL;
14331433
const char *branch_name = "(no branch)";
1434+
char *branch_name_buf = NULL;
14341435
struct commit *head_commit = NULL;
14351436
struct commit_list *parents = NULL;
14361437
struct strbuf msg = STRBUF_INIT;
@@ -1463,8 +1464,13 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14631464

14641465
branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
14651466
"HEAD", 0, NULL, &flags);
1466-
if (flags & REF_ISSYMREF)
1467-
skip_prefix(branch_ref, "refs/heads/", &branch_name);
1467+
1468+
if (flags & REF_ISSYMREF) {
1469+
if (skip_prefix(branch_ref, "refs/heads/", &branch_name))
1470+
branch_name = branch_name_buf = xstrdup(branch_name);
1471+
} else
1472+
branch_name = "(no branch)";
1473+
14681474
head_short_sha1 = repo_find_unique_abbrev(the_repository,
14691475
&head_commit->object.oid,
14701476
DEFAULT_ABBREV);
@@ -1554,6 +1560,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
15541560
strbuf_release(&msg);
15551561
strbuf_release(&untracked_files);
15561562
free_commit_list(parents);
1563+
free(branch_name_buf);
15571564
return ret;
15581565
}
15591566

t/t3903-stash.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,4 +1708,38 @@ test_expect_success 'stash apply reports a locked index' '
17081708
)
17091709
'
17101710

1711+
test_expect_success 'stash reflog message uses superproject branch, not submodule branch' '
1712+
git init sub_project &&
1713+
(
1714+
cd sub_project &&
1715+
echo "Initial content in sub_project" >sub_file.txt &&
1716+
git add sub_file.txt &&
1717+
git commit -q -m "Initial commit in sub_project"
1718+
) &&
1719+
1720+
git init main_project &&
1721+
(
1722+
cd main_project &&
1723+
echo "Initial content in main_project" >main_file.txt &&
1724+
git add main_file.txt &&
1725+
git commit -q -m "Initial commit in main_project" &&
1726+
1727+
git -c protocol.file.allow=always submodule add --quiet ../sub_project sub &&
1728+
git commit -q -m "Added submodule sub_project" &&
1729+
1730+
git checkout -q -b feature_main &&
1731+
cd sub &&
1732+
git checkout -q -b feature_sub &&
1733+
cd .. &&
1734+
1735+
git checkout -q -b work_branch &&
1736+
echo "Important work to be stashed" >work_item.txt &&
1737+
git add work_item.txt &&
1738+
git stash push -q -m "custom stash for work_branch" &&
1739+
1740+
git stash list >../actual_stash_list.txt &&
1741+
grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1742+
)
1743+
'
1744+
17111745
test_done

0 commit comments

Comments
 (0)