Repository navigation
Fix corrupted ThisAssembly.Git.Branch when stripping 'ref:' from refs - #526
Open
dajiaohuang wants to merge 1 commit into
Open
dajiaohuang wants to merge 1 commit into
dajiaohuang wants to merge 1 commit into
Conversation
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.
|
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the bug
ThisAssembly.Gitproduces a corruptedThisAssembly.Git.Branch/RepositoryBranchin local (non-CI) builds, because the'ref:'prefix intended for raw gitHEADfile contents is removed withTrimStart("ref:"), which strips the charactersr,e,f,:repeatedly from the start of the value:@(_ThisAssemblyGitSourceRoot -> '%(BranchName)')is populated by the SDK as a full ref (refs/heads/main, with noref:prefix).TrimStart("ref:")eats the leadingref, producings/heads/main, so the subsequentReplace('refs/heads/', '')/Replace('refs/tags/', '')never match again. In CI this is masked because the branch typically comes fromGITHUB_REF-style environment variables first..git/HEADfallback runs (e.g. .NET SDK 8, see also ThisAssembly.Git.targets fails to resolve branch name when working with worktrees #506) and the repo is on a detachedHEAD, the value is the raw commit sha; a sha starting witheorfloses its leading character (f0fa678…becomes0fa678…).Repro
Minimal SDK-style project referencing
ThisAssembly.Git2.1.6, built locally (no CI env vars), printingThisAssembly.Git.Branch:Actual
ThisAssembly.Git.Branchmains/heads/mainwt/feature-probes/heads/wt/feature-probef0fa6785…0fa6785…(leadingflost)Expected
main;wt/feature-probe; an unmangled value for the detached-HEAD case (git rev-parse --abbrev-ref HEADyieldsHEAD, but at minimum the sha must not be silently truncated).Fix
Strip the
ref:prefix used by gitHEADfiles with an exactStartsWith/Substringmatch before therefs/heads//refs/tags/replacements, so full refs and raw shas are left intact:Validation
Reproduced the failure with the released 2.1.6 package, then re-ran the same scenarios against a locally-built package with this fix (net8.0, Windows):
mains/heads/main❌main✅gitdir:pointer),wt/feature-probes/heads/wt/feature-probe❌wt/feature-probe✅mainmain✅ (path designed for)main✅wt/feature-probe✅wt/feature-probe✅gitdir:pointer),wt/relbranchwt/relbranch✅wt/relbranch✅f0fa6785…(worktree)0fa6785…❌f0fa6785…✅Also ran the repo test suite locally with .NET SDK 9.0.312 (
ThisAssembly.Tests): 45/45 passed.Not validated locally: the repository CI matrix-related SDK pinned via the repo
DOTNETcustom property (not readable); note that building the solution with the currently-installed SDK 10.0.100 already fails on pristinemain(pre-existingCS8336clash between PolySharp 1.16.0's generatedMicrosoft.CodeAnalysis.EmbeddedAttributeand that compiler), so CI will be the authority for the full suite.Scope
Single-file change to
src/ThisAssembly.Git/ThisAssembly.Git.targets; no generator, package layout or behavior changes beyond the correctedRepositoryBranchcomputation.