git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] stash: fix incorrect branch name in stash message

From
René Scharfe <l.s.r@web.de>
Date
Jun 8, 2025, 13:11 UTC
Message-ID
<f46443ac-eb7f-47db-8f4b-a06384e6fde5@web.de>
In-Reply-To
<20250608063537.233243-1-jayatheerthkulkarni2005@gmail.com>
Am 08.06.25 um 08:35 schrieb K Jayatheerth:
Show 11 quoted lines
> 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.
Makes sense.
Show 30 quoted lines
> 
> Signed-off-by: K Jayatheerth <jayatheerthkulkarni2005@gmail.com>
> ---
> This this patch was long due, and submodules patchs had a design choices 
> Refined this patch Added tests and freed the leak as intended.
> 
>  builtin/stash.c  | 19 +++++++++++++++----
>  t/t3903-stash.sh | 39 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 54 insertions(+), 4 deletions(-)
> 
> diff --git a/builtin/stash.c b/builtin/stash.c
> index cfbd92852a..13606efb12 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,20 @@ 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),
> +	
> +	branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 
This part just adds whitespace at the end of the lines.  Please don't.
Show 14 quoted lines
>  					     "HEAD", 0, NULL, &flags);
> -	if (flags & REF_ISSYMREF)
> -		skip_prefix(branch_ref, "refs/heads/", &branch_name);
> +
> +	if (flags & REF_ISSYMREF) {
> +		const char *tmp = NULL;
> +		if (skip_prefix(branch_ref, "refs/heads/", &tmp))
> +			branch_name_buf = xstrdup(tmp); 
> +	}
> +	if (branch_name_buf)
> +		branch_name = branch_name_buf;
> +	else
> +		branch_name = "(no branch)";
> +					    
Looks complicated.

The if/else can be avoided by assigning both variables in the same block, or in the same statement even. You can see this pattern in the code base by searching for "to_free".

And I'm not sure the tmp variable is providing much benefit.
	if (flags & REF_ISSYMREF) {
		if (skip_prefix(branch_ref, "refs/heads/", &branch_name))
			branch_name = branch_name_buf = xstrdup(branch_name);
	}
Show 24 quoted lines
>  	head_short_sha1 = repo_find_unique_abbrev(the_repository,
>  						  &head_commit->object.oid,
>  						  DEFAULT_ABBREV);
> @@ -1495,6 +1505,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..5d5aac8b15 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -1592,4 +1592,43 @@ test_expect_success 'stash apply reports a locked index' '
>  	)
>  '
>  
> +test_expect_success 'stash reflog message uses superproject branch, not submodule branch' '
> +	git init main_project &&
> +	(
> +		cd main_project &&
> +		echo "Initial content in main_project" > main_file.txt &&

Documentation/CodingGuidelines recommends: "Redirection operators should be written with space before, but no space after them.", i.e., write '>main_file.txt' instead of '> main_file.txt'.

Show 8 quoted lines
> +		git add main_file.txt &&
> +		git commit -q -m "Initial commit in main_project"
> +	) &&
> +
> +	git init sub_project &&
> +	(
> +		cd sub_project &&
> +		echo "Initial content in sub_project" > sub_file.txt &&
Same here.
Show 17 quoted lines
> +		git add sub_file.txt &&
> +		git commit -q -m "Initial commit in sub_project"
> +	) &&> +
> +	(
> +		cd 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
> +		) &&
> +
> +		git checkout -q -b work_branch &&
> +
> +		echo "Important work to be stashed" > work_item.txt &&
Same.
> +		git add work_item.txt &&
> +		git stash push -q -m "custom stash for work_branch" &&
> +
> +		git stash list > ../actual_stash_list.txt &&
And here.
> +		grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
> +	)

You could avoid spawning one subshell by building sub_project first and then doing all the work in main_project without interruption afterwards.

> +'
> +
>  test_done
Previous: K JayatheerthNext: K Jayatheerth
Message 12 of 17 in “[BUG] git stash incorrectly showing submodule branch instead of superproject branch”
  1. Stuart MacDonaldMay 12, 2025
  2. Re [BUG] git stash incorrectly showing submodule branch instead of superproject branchK Jayatheerth, May 12, 2025
  3. Stuart MacDonaldMay 12, 2025
  4. stash: fix incorrect branch name in stash messageK Jayatheerth, May 12, 2025
  5. JAYATHEERTH KMay 12, 2025
  6. Stuart MacDonaldMay 12, 2025
  7. Junio C HamanoMay 12, 2025
  8. Eric SunshineMay 12, 2025
  9. JAYATHEERTH KMay 13, 2025
  10. Junio C HamanoMay 14, 2025
  11. stash: fix incorrect branch name in stash messageK Jayatheerth, Jun 8, 2025
  12. René ScharfeJun 8, 2025
  13. stash: fix incorrect branch name in stash messageK Jayatheerth, Jun 8, 2025
  14. Junio C HamanoJun 8, 2025
  15. JAYATHEERTH KJun 11, 2025
  16. stash: fix incorrect branch name in stash messageK Jayatheerth, Jun 11, 2025
  17. Junio C HamanoJun 11, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.