GitInfo
repository·main·Indexed 20 days ago
https://github.com/devlooped/gitinfoA tool for retrieving Git metadata directly within MSBuild and application code (C#, F#, VB) without custom tasks or compiled tools. It provides Git information as MSBuild properties and language-specific constants via a generated ThisAssembly class, and can automatically set $(Version) and $(PackageVersion) based on Git tags and commits.
What's inside GitInfo
- GitInfo is a tool designed to retrieve Git information from MSBuild, C#, and VB. It provides a transparent approach to accessing Git metadata without requiring custom tasks, compiled code, obscure settings, or complex format strings.
Access Git information in C#, F#, or VB code
mainBy default, GitInfo generates a compile-time source file for C#, F#, and VB projects. You can access Git metadata as constants within a
ThisAssembly(partial) class and its nestedGitstatic class.Note: You may need to close and reopen your solution in Visual Studio for IntelliSense to recognize the
ThisAssemblytype after the initial installation.Console.WriteLine(ThisAssembly.Git.Commit);Install GitInfo via NuGet
mainInstall the
GitInfopackage using the NuGet Package Manager to retrieve Git information directly within MSBuild and your application code.PM> Install-Package GitInfoConfigure GitInfo versioning behavior
mainBy default, GitInfo automatically sets
$(Version)and$(PackageVersion)MSBuild properties, which the .NET SDK uses forAssemblyInfo,FileVersion, andInformationalVersion. The default format is:$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit).To opt-out of this automatic versioning (e.g., to implement your own custom versioning logic), set
GitVersiontofalsein your project file:<PropertyGroup> <GitVersion>false</GitVersion> </PropertyGroup>Customize GitInfo behavior via MSBuild properties
mainGitInfo can be customized using MSBuild properties. These properties allow you to control versioning logic, assembly metadata generation, caching, and how Git information is retrieved and parsed.
Versioning and Assembly Control
$(GitVersion): Set to'false'to prevent settingVersionandPackageVersionto the default GitSemVer format.$(GitThisAssembly): Set to'false'to prevent the generation of assembly metadata and constants.$(GitThisAssemblyMetadata): Set to'false'to prevent only assembly metadata generation (defaults to'false'). If'true', it provides assembly metadata attributes for all populated values.$(ThisAssemblyNamespace): Overrides the namespace for theThisAssemblyclass. Defaults to the global namespace.
Git Source and Branching
$(GitRemote): The name of the remote to use for retrieving the repository URL. Defaults to'origin'.$(GitBranchCI): If'true', branch names are populated from standard CI environment variables. Defaults to'true'.$(GitDefaultBranch): The base branch used to calculate commits on top of the current branch. Defaults to'main'.$(GitIgnoreBranchVersion)and$(GitIgnoreTagVersion): Determines if branch or tags should be ignored when finding a base version.$(GitNameRevOptions): Options passed togit name-revfor finding branch names during detached HEAD states. Default:'--refs=refs/heads/* --no-undefined --always'.
Version Lookup and Parsing
$(GitVersionFile): The filename in the repository root used for base version info. Defaults to'GitInfo.txt'.$(GitCommitsRelativeTo): An alternative directory for counting commits. Defaults to the directory of$(GitVersionFile).$(GitCommitsIgnoreMerges): Set to'true'to ignore merge commits when counting. Defaults to'false'.$(GitTagRegex): A regular expression used withgit describeto filter tags for base version lookup. Defaults to*(all).$(GitBaseVersionRegex): A regex used to validate base versions in branch, tag, or file sources. Defaults to a pattern matching SemVer2 strings.$(GitCommitDateFormat): The format passed to retrieve the commit date. Defaults to%%cI(Windows) or%cI(non-Windows).
Caching and Reporting
$(GitSkipCache): Enables caching of Git information in aGitInfo.cachefile for performance. Defaults to empty (no caching).$(GitCachePath): The location for the cache file. Must end with a path separator. Defaults to'$(IntermediateOutputPath)'.$(GitInfoReportImportance): Sets the message importance for rendering Git info ('high','normal', or'low'). Defaults to'low'.
Implement custom assembly versioning in code
mainIf you opt-out of default versioning, you can use the
ThisAssembly.Gitconstants to manually build your assembly attributes.Important: When generating your own attributes, you must disable the .NET SDK's automatic generation by setting
GenerateAssemblyVersionAttribute,GenerateAssemblyFileVersionAttribute, andGenerateAssemblyInformationalVersionAttributetofalsein your project file.[assembly: AssemblyVersion(ThisAssembly.Git.BaseVersion.Major + "." + ThisAssembly.Git.BaseVersion.Minor + "." + ThisAssembly.Git.BaseVersion.Patch)] [assembly: AssemblyFileVersion(ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.SemVer.Patch)] [assembly: AssemblyInformationalVersion( ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.Commits + "-" + ThisAssembly.Git.Branch + "+" + ThisAssembly.Git.Commit)]Use GitInfo properties in MSBuild targets
mainYou can use GitInfo properties within MSBuild targets. Because these properties are populated via targets, you cannot use them directly in a top-level
PropertyGroup. Instead, your target must depend on the GitInfo target (typicallyGitVersion).Example of a custom target using
GitVersionas a dependency:<PropertyGroup> <GitVersion>false</GitVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="GitInfo" PrivateAssets="all" /> </ItemGroup> <Target Name="PopulateInfo" DependsOnTargets="GitVersion" BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageContents"> <PropertyGroup> <Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version> <PackageVersion>$(Version)</PackageVersion> <RepositoryBranch>$(GitBranch)</RepositoryBranch> <RepositoryCommit>$(GitCommit)</RepositoryCommit> <SourceRevisionId>$(GitBranch) $(GitCommit)</SourceRevisionId> </PropertyGroup> </Target>Reference: GitInfo MSBuild properties and Code constants
mainGitInfo exposes the following metadata through both MSBuild properties and language-specific constants (
ThisAssembly.Git).### MSBuild Properties $(GitRepositoryUrl) $(GitBranch) $(GitCommit) $(GitCommitDate) $(GitCommits) $(GitTag) $(GitBaseTag) $(GitBaseVersionMajor) $(GitBaseVersionMinor) $(GitBaseVersionPatch) $(GitSemVerMajor) $(GitSemVerMinor) $(GitSemVerPatch) $(GitSemVerLabel) $(GitSemVerDashLabel) $(GitSemVerSource) $(GitIsDirty) ### Code Constants (C#, F#, VB) ThisAssembly.Git.RepositoryUrl ThisAssembly.Git.Branch ThisAssembly.Git.Commit ThisAssembly.Git.Commits ThisAssembly.Git.Tag ThisAssembly.Git.BaseTag ThisAssembly.Git.BaseVersion.Major ThisAssembly.Git.BaseVersion.Minor ThisAssembly.Git.BaseVersion.Patch ThisAssembly.Git.SemVer.Major ThisAssembly.Git.SemVer.Minor ThisAssembly.Git.SemVer.Patch ThisAssembly.Git.SemVer.Label ThisAssembly.Git.SemVer.DashLabel ThisAssembly.Git.SemVer.Source ThisAssembly.Git.IsDirty