diff options
| author | Junio C Hamano <gitster@pobox.com> | 2025-06-24 09:48:48 -0700 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2025-06-24 09:48:48 -0700 |
| commit | 77eb1dc722ff32d7608ed696de37efb086f858d3 (patch) | |
| tree | a71c314b468126f2fc211ed91f8b8e97e9db3348 | |
| parent | 91e15c5e0cc236cd6dc740c762c4b70f0d9c58a6 (diff) | |
| parent | ffb36c64f2b39833f1ac95b79d39c881ed60de24 (diff) | |
| download | git-77eb1dc722ff32d7608ed696de37efb086f858d3.tar.gz | |
Merge branch 'kj/stash-onbranch-submodule-fix'
"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
| -rw-r--r-- | builtin/stash.c | 10 | ||||
| -rwxr-xr-x | t/t3903-stash.sh | 32 |
2 files changed, 40 insertions, 2 deletions
diff --git a/builtin/stash.c b/builtin/stash.c index b12fd6c40f..7cd3ad8aa4 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -1372,6 +1372,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b const char *head_short_sha1 = NULL; const char *branch_ref = NULL; const char *branch_name = "(no branch)"; + char *branch_name_buf = NULL; struct commit *head_commit = NULL; struct commit_list *parents = NULL; struct strbuf msg = STRBUF_INIT; @@ -1404,8 +1405,12 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flags); - if (flags & REF_ISSYMREF) - skip_prefix(branch_ref, "refs/heads/", &branch_name); + + if (flags & REF_ISSYMREF) { + if (skip_prefix(branch_ref, "refs/heads/", &branch_name)) + branch_name = branch_name_buf = xstrdup(branch_name); + } + head_short_sha1 = repo_find_unique_abbrev(the_repository, &head_commit->object.oid, DEFAULT_ABBREV); @@ -1495,6 +1500,7 @@ done: strbuf_release(&msg); strbuf_release(&untracked_files); free_commit_list(parents); + free(branch_name_buf); return ret; } diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 2bba3baa10..35b85c7909 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1614,4 +1614,36 @@ test_expect_success 'stash apply reports a locked index' ' ) ' +test_expect_success 'submodules does not affect the branch recorded in stash message' ' + git init sub_project && + ( + cd sub_project && + echo "Initial content in sub_project" >sub_file.txt && + git add sub_file.txt && + git commit -m "Initial commit in sub_project" + ) && + + git init main_project && + ( + cd main_project && + echo "Initial content in main_project" >main_file.txt && + git add main_file.txt && + git commit -m "Initial commit in main_project" && + + git -c protocol.file.allow=always submodule add ../sub_project sub && + git commit -m "Added submodule sub_project" && + + git checkout -b feature_main && + git -C sub checkout -b feature_sub && + + git checkout -b work_branch && + echo "Important work to be stashed" >work_item.txt && + git add work_item.txt && + git stash push -m "custom stash for work_branch" && + + git stash list >../actual_stash_list.txt && + grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt + ) +' + test_done |
