From 8413f24c99d57dcd81c0036e2ee05e4ba1072fb1 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 16 Oct 2026 19:06:34 +0800 Subject: [PATCH] Fix corrupted branch name when stripping 'ref:' from HEAD/refs TrimStart("ref:") strips the characters r/e/f/: repeatedly, so: - The .NET SDK 9+ SourceRoot BranchName metadata, which is a full ref ('refs/heads/main', no 'ref: ' prefix), gets corrupted to 's/heads/main', making the subsequent Replace('refs/heads/', ...) ineffective. Local (non-CI) builds therefore get a wrong ThisAssembly.Git.Branch. - A detached HEAD fallback sha starting with 'e' or 'f' loses its leading character, e.g. 'f0fa678...' becomes '0fa678...'. Now the 'ref: ' prefix used by git HEAD files is removed with an exact prefix match before the refs/tags replacements, leaving both bare refs and raw shas intact. --- src/ThisAssembly.Git/ThisAssembly.Git.targets | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ThisAssembly.Git/ThisAssembly.Git.targets b/src/ThisAssembly.Git/ThisAssembly.Git.targets index 41e34f44..78e25666 100644 --- a/src/ThisAssembly.Git/ThisAssembly.Git.targets +++ b/src/ThisAssembly.Git/ThisAssembly.Git.targets @@ -86,7 +86,10 @@ <_ThisAssemblyGitHead Condition="'$(_ThisAssemblyGitHead)' == '' and '$(_ThisAssemblyGitDir)' != '' and Exists('$(_ThisAssemblyGitDir)/HEAD')">$(_ThisAssemblyGitDir)/HEAD <_ThisAssemblyGitBranchName Condition="'$(_ThisAssemblyGitBranchName)' == '' and '$(_ThisAssemblyGitHead)' != ''">$([System.IO.File]::ReadAllText('$(_ThisAssemblyGitHead)').Trim()) - $(_ThisAssemblyGitBranchName.TrimStart("ref:").Trim().Replace('refs/heads/', '').Replace('refs/tags/', '')) + + <_ThisAssemblyGitBranchName Condition="'$(_ThisAssemblyGitBranchName)' != '' and $(_ThisAssemblyGitBranchName.StartsWith('ref: '))">$(_ThisAssemblyGitBranchName.Substring(5)) + $(_ThisAssemblyGitBranchName.Trim().Replace('refs/heads/', '').Replace('refs/tags/', ''))