From: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
To: l.s.r@web.de
Cc: git@vger.kernel.org, gitster@pobox.com,
jayatheerthkulkarni2005@gmail.com, smacdonald@kaimaging.com,
sunshine@sunshineco.com
Subject: [PATCH v2] stash: fix incorrect branch name in stash message
Date: Sun, 8 Jun 2025 20:15:42 +0530 [thread overview]
Message-ID: <20250608144542.275836-1-jayatheerthkulkarni2005@gmail.com> (raw)
In-Reply-To: <f46443ac-eb7f-47db-8f4b-a06384e6fde5@web.de>
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>
---
All good points,
Removed multiple if else blocks and kept them
within a single if/else block.
Removed the white space at the branch_ref = refs_r... line
removed the spaces in the test case.
Thank you for the feedback.
builtin/stash.c | 13 ++++++++++---
t/t3903-stash.sh | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c
index cfbd92852a..c71e20a8cd 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;
@@ -1401,11 +1402,16 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
ret = 1;
goto done;
}
-
+
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);
+ } else
+ branch_name = "(no branch)";
+
head_short_sha1 = repo_find_unique_abbrev(the_repository,
&head_commit->object.oid,
DEFAULT_ABBREV);
@@ -1495,6 +1501,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
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 74666ff3e4..b606827a73 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1592,4 +1592,38 @@ test_expect_success 'stash apply reports a locked index' '
)
'
+test_expect_success 'stash reflog message uses superproject branch, not submodule branch' '
+ git init sub_project &&
+ (
+ cd sub_project &&
+ echo "Initial content in sub_project" >sub_file.txt &&
+ git add sub_file.txt &&
+ git commit -q -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 -q -m "Initial commit in main_project" &&
+
+ git -c protocol.file.allow=always submodule add --quiet ../sub_project sub &&
+ git commit -q -m "Added submodule sub_project" &&
+
+ git checkout -q -b feature_main &&
+ cd sub &&
+ git checkout -q -b feature_sub &&
+ cd .. &&
+
+ git checkout -q -b work_branch &&
+ echo "Important work to be stashed" >work_item.txt &&
+ git add work_item.txt &&
+ git stash push -q -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
--
2.49.GIT
next prev parent reply other threads:[~2025-06-08 14:46 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-12 15:19 [BUG] git stash incorrectly showing submodule branch instead of superproject branch Stuart MacDonald
2025-05-12 16:12 ` Re " K Jayatheerth
2025-05-12 16:26 ` Stuart MacDonald
2025-05-12 16:40 ` [PATCH] stash: fix incorrect branch name in stash message K Jayatheerth
2025-05-12 16:42 ` JAYATHEERTH K
2025-05-12 17:09 ` Stuart MacDonald
2025-05-12 17:49 ` Junio C Hamano
2025-05-12 18:54 ` Eric Sunshine
2025-05-13 1:21 ` JAYATHEERTH K
2025-05-14 13:28 ` Junio C Hamano
2025-06-08 6:35 ` K Jayatheerth
2025-06-08 13:11 ` René Scharfe
2025-06-08 14:45 ` K Jayatheerth [this message]
2025-06-08 16:20 ` [PATCH v2] " Junio C Hamano
2025-06-11 1:32 ` JAYATHEERTH K
2025-06-11 1:42 ` [PATCH v3] " K Jayatheerth
2025-06-11 16:00 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250608144542.275836-1-jayatheerthkulkarni2005@gmail.com \
--to=jayatheerthkulkarni2005@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=smacdonald@kaimaging.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).