aboutsummaryrefslogtreecommitdiffstats
path: root/builtin/stash.c
diff options
context:
space:
mode:
authorK Jayatheerth <jayatheerthkulkarni2005@gmail.com>2025-06-11 07:12:03 +0530
committerJunio C Hamano <gitster@pobox.com>2025-06-11 08:59:32 -0700
commitffb36c64f2b39833f1ac95b79d39c881ed60de24 (patch)
treeb4d90b0abcb8fe6f007bfee0ecca1a9a098f5e34 /builtin/stash.c
parentd50a5e8939abfc07c2ff97ae72e9330939b36ee0 (diff)
downloadgit-ffb36c64f2b39833f1ac95b79d39c881ed60de24.tar.gz
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>
Diffstat (limited to 'builtin/stash.c')
-rw-r--r--builtin/stash.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/builtin/stash.c b/builtin/stash.c
index dbaa999cf1..9c0dde0b2e 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1373,6 +1373,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;
@@ -1405,8 +1406,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);
@@ -1496,6 +1501,7 @@ done:
strbuf_release(&msg);
strbuf_release(&untracked_files);
free_commit_list(parents);
+ free(branch_name_buf);
return ret;
}