Add git-based detection of tags at HEAD to improve PublicRelease detection - #876
Conversation
* Managed git does not support annotated tags yet * json output reads: "NBGV_BuildingTags": "System.Collections.Generic.List`1[System.String]",
|
In 3c93a89 I added a workaround for |
Throwing was intentional to match HeadCanonicalName behaviour. Throwing fails the tests though. Maybe this is because the tests run in a cloud build environment and HeadCanonicalName isn't used there? this.BuildingRef = cloudBuild?.BuildingTag ?? cloudBuild?.BuildingBranch ?? context.HeadCanonicalName; This would mean that the tests would currently fail when executed locally.
Return null if no HEAD could be determined, return an empty collection if there are no matching tags.
|
@AArnott anything I could do from my side to assist/simplify getting this merged? |
|
I'm looking at this now and preparing some local changes to push to your PR. |
AArnott
left a comment
There was a problem hiding this comment.
Thanks for putting this together. I have several comments I look forward to discussing with you.
| var tagObjId = GitObjectId.Parse(line.Substring(0, 40)); | ||
| var refName = line.Substring(41); | ||
|
|
There was a problem hiding this comment.
This seems to not handle a line pattern that I see in one of my repos:
^3d2137a79ee4e3621696cfbde8d4f1c0e98bc5f6
40614e83b9b3892e05efd6155c3b61035bb5542a refs/tags/drop/a.proddiag/official.00000.00
^2f0a22666a958c15f7e7486a0a14490f7b8e210f
9aa2dc36733d3f3076702b31a3dc754c144a9473 refs/tags/drop/a.proddiag/official.26601.00
See the leading caret? I don't know what that means, but it seems your line parsing assumes no such prefix exists.
There was a problem hiding this comment.
The ^ prefix indicates a "peeled line" that lists the object ID referenced by the tag object on the previous line.
There was a problem hiding this comment.
git's source supports that: https://github.com/git/git/blob/d9d677b2d8cc5f70499db04e633ba7a400f64cbf/refs/packed-backend.c#L542
A "record" here is one line for the reference itself and zero or one peel lines that start with '^'.
Thus, if I understand correctly, simply skipping these lines would be functionally equivalent to if they didn't exist, because we'd peel the refs in the next step. Having them does provide us with a way to speed things up though and hits the notch @AArnott mentioned here - we could decide if we're interested in this ref in place. Right?
There was a problem hiding this comment.
I tried a tag referencing another tag referencing a commit, and the peeled line after the first tag listed the object ID of the second tag, not the commit ID. So if you implement some optimisation that uses those lines, please consider this scenario.
There was a problem hiding this comment.
Good to know, thanks! Actually I think this behaviour makes sense and is also in line with what the code in this PR currently does. My take on "tags at HEAD" behaves like git tag --points-at HEAD would. Means it includes any lightweight as well as annotated tags pointing at HEAD. But not any tags that point at HEAD transitively. A git show HEAD on the other hand does include transitive tags. Not including transitive tags might lead to less lookups => increased performance. In a broken repo, lookup including transitive tags could lead to an infinite loop.
I guess transitive tags are kind of an edge case so my plan was to just not support them at this point. I think specifying "We do what git tag --points-at HEAD does" makes sense too. If the need to support transitive tags would arise, a second PR would probably be easier than this first one. What do you think @AArnott?
There was a problem hiding this comment.
My main concern is that the behavior should not depend on whether the peeled lines are in packed-refs or not. Whether nbgv recursively follows tags in general is a matter of policy and either choice is fine with me.
There was a problem hiding this comment.
Makes sense. You might want to take a look at my two recent commits in that regard.
| { | ||
| if (objectId.Equals(tagObjId)) | ||
| { | ||
| tags.Add(tagNameCandidate); |
There was a problem hiding this comment.
Filtering the tags at this point is quite late. NB.GV functions in some repos with tens of thousands of tags, and the code would have parsed and allocated multiple strings for all of them. I think we'll need to optimize this by walking the tags and immediately skipping lines with non-matching object IDs before we allocate anything.
There was a problem hiding this comment.
I agree that this can be solved without the candidate list and that the number of string allocations can be reduced. The current implementation without tag support does however allocate multiple strings per packed-ref line too and it doesn't seem to be a performance hit or is there some detail I'm missing?
Looking up the target of an annotated tag might lead to an IO operation - read the relevant git pack. Thus, I'm wondering if collecting the candidates first wouldn't have a minor impact compared to opening the file.
I don't have any that large repo at hand. If this turns out to have a substantial negative performance impact, it might be an option to make annotated tag support opt-in/out, as reading the packed-refs file itself (plus the files in refs/tags) is sufficient for lightweight tag lookup. Edit: with the two commits incl. 49b9dc9 we don't need additional IO operations for annotated tags in packed-refs if peel lines are available.
I'm working on a commit to remove the candidate list and reduce the string allocations though. Edit: see a84ad8b
Without this fix we might have considered one level of transitive annotated tags, if a peel line is present in packed-refs. With this fix we also avoid reading the git pack of annotated tags that have peel lines and do not match the object id we are looking for.
|
I thought there should be something to detect if reftable is used, and fall back to LibGit2Context in that case, but…
|
|
The build failure reproduces in main, so it's unlikely to be due to this PR. I don't know how that happened, but I'll be investigating it. |
Updated [nbgv](https://github.com/dotnet/Nerdbank.GitVersioning) from 3.6.146 to 3.9.50. <details> <summary>Release notes</summary> _Sourced from [nbgv's releases](https://github.com/dotnet/Nerdbank.GitVersioning/releases)._ ## 3.9.50 ## What's Changed * Add `versionHeightOffsetAppliesTo` property to version.json by @Copilot in dotnet/Nerdbank.GitVersioning#1279 * Fix `nbgv prepare-release` command to honor inheriting version.json files by @AArnott in dotnet/Nerdbank.GitVersioning#1281 * Automatically disable git engine for Dependabot environments by @Copilot in dotnet/Nerdbank.GitVersioning#1284 * Add --what-if switch to nbgv tag command to preview tag names by @Copilot in dotnet/Nerdbank.GitVersioning#1287 * Auto-disable git engine for GitHub Copilot environments by @Copilot in dotnet/Nerdbank.GitVersioning#1291 * Allow uppercase letters in pre-release version identifiers by @Copilot in dotnet/Nerdbank.GitVersioning#1293 ## New Contributors * @micheloliveira-com made their first contribution in dotnet/Nerdbank.GitVersioning#1277 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.8.118...v3.9.50 ## 3.9.37-alpha ## What's Changed * Auto-disable git engine for GitHub Copilot environments by @Copilot in dotnet/Nerdbank.GitVersioning#1291 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.9.32-alpha...v3.9.37-alpha ## 3.9.32-alpha ## What's Changed * Automatically disable git engine for Dependabot environments by @Copilot in dotnet/Nerdbank.GitVersioning#1284 * Add --what-if switch to nbgv tag command to preview tag names by @Copilot in dotnet/Nerdbank.GitVersioning#1287 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.9.17-alpha...v3.9.32-alpha ## 3.9.17-alpha ## What's Changed * Fix `nbgv prepare-release` command to honor inheriting version.json files by @AArnott in dotnet/Nerdbank.GitVersioning#1281 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.9.15-alpha...v3.9.17-alpha ## 3.9.15-alpha ## What's Changed * Add `versionHeightOffsetAppliesTo` property to version.json by @Copilot in dotnet/Nerdbank.GitVersioning#1279 ## New Contributors * @micheloliveira-com made their first contribution in dotnet/Nerdbank.GitVersioning#1277 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.8.118...v3.9.15-alpha ## 3.8.118 ## Fixes * Don't try to disable CA2243 warnings in the generated version info files for F# by @Numpsy in dotnet/Nerdbank.GitVersioning#1174 * Catch a more general JsonException. by @ANGEL-OF-DEV in dotnet/Nerdbank.GitVersioning#1191 * Retarget links to migrated docs by @bencemali in dotnet/Nerdbank.GitVersioning#1193 * Check MSBuild items with case insensitivity by @AArnott in dotnet/Nerdbank.GitVersioning#1213 * Fix inconsistent CLI output format for GitCommitDate by @Copilot in dotnet/Nerdbank.GitVersioning#1246 * Fix version height computed as 0 when project path has non-canonical casing by @Copilot in dotnet/Nerdbank.GitVersioning#1244 * When generating the GitCommitDate field in the AssemblyInfo for F#, d… by @Numpsy in dotnet/Nerdbank.GitVersioning#1253 * Only do Android version check on applications by @dotMorten in dotnet/Nerdbank.GitVersioning#1256 * Fix `nbgv set-version` to write to the best version.json file in scope by @AArnott in dotnet/Nerdbank.GitVersioning#1264 ## Enhancements * Add msbuild-provided prerelease identifiers by @AArnott in dotnet/Nerdbank.GitVersioning#1153 * Add support for stamping version on server.json for MCP servers with 0.0.0-placeholder replacement by @Copilot in dotnet/Nerdbank.GitVersioning#1270 and by @AArnott in dotnet/Nerdbank.GitVersioning#1271 * Add option to set / skip CloudBuildNumber by @MattKotsenas in dotnet/Nerdbank.GitVersioning#1190 * Add Central Package Management (CPM) support to nbgv install command by @Copilot in dotnet/Nerdbank.GitVersioning#1208 * Add --public-release argument to nbgv get-version command by @Copilot in dotnet/Nerdbank.GitVersioning#1245 * Invoke PrivateP2PCaching.proj fewer times by @AArnott in dotnet/Nerdbank.GitVersioning#1263 ## Other changes * Update dependency Cake.Core to v5 by @renovate[bot] in dotnet/Nerdbank.GitVersioning#1183 ## New Contributors * @ANGEL-OF-DEV made their first contribution in dotnet/Nerdbank.GitVersioning#1191 * @bencemali made their first contribution in dotnet/Nerdbank.GitVersioning#1193 * @Copilot made their first contribution in dotnet/Nerdbank.GitVersioning#1208 * @dotMorten made their first contribution in dotnet/Nerdbank.GitVersioning#1256 * @emmanuel-ferdman made their first contribution in dotnet/Nerdbank.GitVersioning#1145 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.7.115...v3.8.118 ## 3.8.106-alpha ## What's Changed ### Enhancements * Add option to set / skip CloudBuildNumber by @MattKotsenas in dotnet/Nerdbank.GitVersioning#1190 * Add Central Package Management (CPM) support to nbgv install command by @Copilot in dotnet/Nerdbank.GitVersioning#1208 * Add --public-release argument to nbgv get-version command by @Copilot in dotnet/Nerdbank.GitVersioning#1245 * Invoke PrivateP2PCaching.proj fewer times by @AArnott in dotnet/Nerdbank.GitVersioning#1263 ### Fixes * Catch a more general JsonException. by @ANGEL-OF-DEV in dotnet/Nerdbank.GitVersioning#1191 * Retarget links to migrated docs by @bencemali in dotnet/Nerdbank.GitVersioning#1193 * Check MSBuild items with case insensitivity by @AArnott in dotnet/Nerdbank.GitVersioning#1213 * Fix inconsistent CLI output format for GitCommitDate by @Copilot in dotnet/Nerdbank.GitVersioning#1246 * Fix version height computed as 0 when project path has non-canonical casing by @Copilot in dotnet/Nerdbank.GitVersioning#1244 * When generating the GitCommitDate field in the AssemblyInfo for F#, d… by @Numpsy in dotnet/Nerdbank.GitVersioning#1253 * Only do Android version check on applications by @dotMorten in dotnet/Nerdbank.GitVersioning#1256 * Fix `nbgv set-version` to write to the best version.json file in scope by @AArnott in dotnet/Nerdbank.GitVersioning#1264 ### Other changes * Update dependency Cake.Core to v5 by @renovate[bot] in dotnet/Nerdbank.GitVersioning#1183 ## New Contributors * @ANGEL-OF-DEV made their first contribution in dotnet/Nerdbank.GitVersioning#1191 * @bencemali made their first contribution in dotnet/Nerdbank.GitVersioning#1193 * @Copilot made their first contribution in dotnet/Nerdbank.GitVersioning#1208 * @dotMorten made their first contribution in dotnet/Nerdbank.GitVersioning#1256 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.8.38-alpha...v3.8.106-alpha ## 3.8.38-alpha ## Fixes * Don't try to disable CA2243 warnings in the generated version info files for F# by @Numpsy in dotnet/Nerdbank.GitVersioning#1174 ## Enhancements * Add msbuild-provided prerelease identifiers by @AArnott in dotnet/Nerdbank.GitVersioning#1153 ## New Contributors * @emmanuel-ferdman made their first contribution in dotnet/Nerdbank.GitVersioning#1145 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.7.115...v3.8.38-alpha ## 3.7.115 ## Changes: ### Fixes: * #1151: Fix exception thrown for repos cloned without tags * #1150: Getting the build version fails for repositories cloned with --no-tags ### Enhancements: * #318: Emit a warning when SemVer 2.0 format version is specified with explicitly selecting SemVer 1.0. This list of changes was [auto generated](https://dev.azure.com/andrewarnott/OSS/_build/results?buildId=11184&view=logs). ## 3.7.112 ## What's Changed * Fix typo in the Cake extension documentation comment by @kapsiR in dotnet/Nerdbank.GitVersioning#939 * Retarget from net462 to net472 by @AArnott in dotnet/Nerdbank.GitVersioning#941 * Add option to include package version in `ThisAssembly` class by @AArnott in dotnet/Nerdbank.GitVersioning#976 * Make GitPackCache include ObjectType by @georg-jung in dotnet/Nerdbank.GitVersioning#942 * Add git-based detection of tags at HEAD to improve PublicRelease detection by @georg-jung in dotnet/Nerdbank.GitVersioning#876 * Remove package reference to Moq by @AArnott in dotnet/Nerdbank.GitVersioning#1003 * Allow customizing commit message pattern in `prepare-release` command by @fmacavilca in dotnet/Nerdbank.GitVersioning#996 * AssemblyVersionInfo: Use `global::` for all references to types in `System.*` namespaces. by @alexrp in dotnet/Nerdbank.GitVersioning#1012 * Invert `Language` special-casing to handle *supported* programming languages. by @alexrp in dotnet/Nerdbank.GitVersioning#1021 * Fix lookup of tags in the presence of lightweight tags by @Rob-Hague in dotnet/Nerdbank.GitVersioning#1029 * ReleaseManager: do not reset VersionHeightOffset if it equals to -1 by @DmitryZhelnin in dotnet/Nerdbank.GitVersioning#1100 * fix GitCommitDate being author date rather than commit date by @DmitryZhelnin in dotnet/Nerdbank.GitVersioning#1103 * Ensure GetBuildVersion runs before Clean by @MattKotsenas in dotnet/Nerdbank.GitVersioning#1107 * Avoid inexact stream reads by @AArnott in dotnet/Nerdbank.GitVersioning#1127 * Introduce `NBGV_UseAssemblyVersionInNativeVersion` msbuild property by @vitezslav-popovsky-solarwinds in dotnet/Nerdbank.GitVersioning#1125 ## New Contributors * @fmacavilca made their first contribution in dotnet/Nerdbank.GitVersioning#996 * @Rob-Hague made their first contribution in dotnet/Nerdbank.GitVersioning#1029 * @DmitryZhelnin made their first contribution in dotnet/Nerdbank.GitVersioning#1100 * @MattKotsenas made their first contribution in dotnet/Nerdbank.GitVersioning#1107 * @github-actions made their first contribution in dotnet/Nerdbank.GitVersioning#1135 * @vitezslav-popovsky-solarwinds made their first contribution in dotnet/Nerdbank.GitVersioning#1125 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.6.146...v3.7.112 ## 3.7.77-alpha ## What's Changed * Bump Microsoft.Build.Locator from 1.6.10 to 1.7.8 by @dependabot in dotnet/Nerdbank.GitVersioning#1062 * Bump Newtonsoft.Json.Schema from 3.0.16 to 4.0.1 by @dependabot in dotnet/Nerdbank.GitVersioning#1063 * Bump node.js version and switch from camel-case to change-case by @AArnott in dotnet/Nerdbank.GitVersioning#1077 * Switch System.Text.Json to non-vulnerable version by @AArnott in dotnet/Nerdbank.GitVersioning#1074 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.7.70-alpha...v3.7.77-alpha ## 3.7.70-alpha ## Changes: ### Fixes: * #250: GetBuildVersion fails with NullReferenceException when version is missing * #943: Upgrading from 3.5.119 to 3.6.132 breaks commit id for NPM * #935: BuildingRef is incorrectly populated on Bitbucket pipelines ### Enhancements: * #251: GetBuildVersion fails with NotSupportedException when version is a simple number * #233: Major, Minor and Patch version parts as separate variables ### Others: * #1056: Bump BenchmarkDotNetVersion from 0.13.10 to 0.13.12 * #1057: Bump Newtonsoft.Json.Schema from 3.0.15 to 3.0.16 <details><summary><b>See More</b></summary> * #1039: Merge latest Library.Template * #1050: Merge v3.6 to main * #1046: Bump typescript from 5.4.3 to 5.4.5 in /src/nerdbank-gitversioning.npm * #265: Add AssemblyInfo.GitCommitId property * #264: Remove `g` prefix in commit IDs * #255: Fix for issue #149 - Adding F# support for AssemblyInfo generation * #252: Not working in .NET Core docker image * #254: Update Cake.Core dependency to 0.30.0 * #1024: Change F# AssemblyInfo generation to always include a do() after the … * #245: Fix attribute name in readme * #241: Switch to Azure Pipelines * #242: Remove typings to fix build break * #243: Capture all nupkg files as artifacts * #240: is it possible to update LibGit2Sharp package version from 0.25.2 to 0.26.0-preview-0054? * #238: Upgrade Typescript + npm cleanup * #236: npm setPackageVersion semver2 support * #235: Add a few properties for easier processing at the command line * #231: Remove names from regex capture groups in version.schema.json * #232: NPM/Yarn-only way of versioning packages * #234: Add cake-contrib user to Cake.GitVersioning NuGet package * #994: Fix build.ps1 script * #992: I have a problem building the solution * #982: Fix WPF incremental build * #175: NB.GV defeats incremental build for .NET SDK style WPF projects * #945: Revert "Drop BuildMetadata from NPM package version" * #938: Downgrade Cake.Core to v2.3.0 ... (truncated) ## 3.7.62-alpha ## What's Changed * AssemblyVersionInfo: Use `global::` for all references to types in `System.*` namespaces. by @alexrp in dotnet/Nerdbank.GitVersioning#1012 * Invert `Language` special-casing to handle *supported* programming languages. by @alexrp in dotnet/Nerdbank.GitVersioning#1021 * Fix lookup of tags in the presence of lightweight tags by @Rob-Hague in dotnet/Nerdbank.GitVersioning#1029 * Bump NuGet.PackageManagement from 6.7.0 to 6.9.1 by @dependabot in dotnet/Nerdbank.GitVersioning#1034 * Bump Cake.Core from 3.0.0 to 3.2.0 by @dependabot in dotnet/Nerdbank.GitVersioning#999 ## New Contributors * @Rob-Hague made their first contribution in dotnet/Nerdbank.GitVersioning#1029 **Full Changelog**: dotnet/Nerdbank.GitVersioning@v3.7.48-alpha...v3.7.62-alpha ## 3.7.48-alpha ## What's Changed ### Features - Added option to include package version in `ThisAssembly` class. [PR #976](dotnet/Nerdbank.GitVersioning#976) - Retargeted from .NET Framework 4.6.2 to 4.7.2 for enhanced compatibility and performance. [PR #941](dotnet/Nerdbank.GitVersioning#941) - Added git-based detection of tags at HEAD to improve PublicRelease detection. [PR #876](dotnet/Nerdbank.GitVersioning#876) - Allowed customizing commit message pattern in `prepare-release` command. [PR #996](dotnet/Nerdbank.GitVersioning#996) - Updated to .NET 8 SDK, aligning with the latest technology standards. [PR #1002](dotnet/Nerdbank.GitVersioning#1002) ### Fixes - Fixed a typo in the Cake extension documentation comment. [PR #939](dotnet/Nerdbank.GitVersioning#939) - Made GitPackCache include ObjectType for more accurate caching. [PR #942](dotnet/Nerdbank.GitVersioning#942) ## New Contributors - @fmacavilca made their first contribution. [View Contribution](dotnet/Nerdbank.GitVersioning#996) [**Full Changelog**](dotnet/Nerdbank.GitVersioning@v3.6.133...v3.7.48-alpha) Commits viewable in [compare view](dotnet/Nerdbank.GitVersioning@v3.6.146...v3.9.50). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Fixes #873.
This implements my second idea described in detail in #873.
My thoughts form there:
This comes with parsing support for annotated tags for managed git and LibGit2. Lightweight tags pointing HEAD are considered as well as annotated tags. Nested annotated tags that indirectly point at HEAD are not considered (intentionally, changing probably wouldn't be hard).
BuildingRefvalue is not changed.Currently, the output of
nbgv get-version -f jsonis extended as follows:Obviously theI added anNBGV_BuildingTagsvalue isn't here to stay. I thought I'd leave this for discussion, which of these values should be generated and what their value should be.[Ignore]to skip it in a3089b1.Probably I made some opinionated decisions when putting this together, don't hesitate to change what you don't like.