GitInfo 2.2.1

Icon GitInfo

Git Info from MSBuild, C# and VB

A fresh and transparent approach to Git information retrieval from MSBuild and Code without using any custom tasks or compiled code and tools, obscure settings, format strings, etc.

Build status Latest version Downloads License

Usage

After installing via NuGet:

PM> Install-Package GitInfo

By default, if the containing project is a C#, F# or VB project, a compile-time generated source file will contain all the git information and can be accessed from anywhere within the assembly, as constants in a ThisAssembly (partial) class and its nested Git static class:

Console.WriteLine(ThisAssembly.Git.Commit);

All generated constants also have a Summary documentation tag that shows the current value in the intellisense tooltip, making it easier to see what the different values contain:

NOTE: you may need to close and reopen the solution in order for Visual Studio to refresh intellisense and show the ThisAssembly type the first time after installing the package.

With this information at your fingertips, you can build any versioning attributes you want, with whatever information you want, without resorting to settings, format strings or anything, just plain code:

C#:

[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)]

F#:

module AssemblyInfo

open System.Reflection

[<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)>]

do ()

VB:

<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)>

MSBuild:

<!-- Just edit .csproj file -->  
<ItemGroup>
  <PackageReference Include="GitInfo" PrivateAssets="all" />
</ItemGroup>

<Target Name="PopulateInfo" DependsOnTargets="GitInfo" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <RepositoryBranch>$(GitBranch)</RepositoryBranch>
      <RepositoryCommit>$(GitCommit)</RepositoryCommit>
      <SourceRevisionId>$(GitBranch) $(GitCommit)</SourceRevisionId>
    </PropertyGroup>
</Target>

Because this information is readily available whenever you build the project, you never depend on CI build scripts that generate versions for you, and you can always compile locally exactly the same version of an assembly that was built by a CI server.

You can read more about this project at the GitInfo announcement blog post.

MSBuild

If you want to set other properties in your project, such as $(Version) or $(PackageVersion) based on the populated values from GitInfo, you must do so from a target, such as:

<Target Name="_SetVersion" BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageContents">
    <PropertyGroup>
        <Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version>
        <PackageVersion>$(Version)</PackageVersion>
    </PropertyGroup>
</Target>

In this case, we're setting the version and package version.

Details

Exposes the following information for use directly from any MSBuild target that depends on the GitInfo target:

  $(GitRepositoryUrl)
  $(GitBranch)
  $(GitCommit)
  $(GitCommitDate)
  $(GitCommits)
  $(GitTag)
  $(GitBaseTag)
  $(GitBaseVersionMajor)
  $(GitBaseVersionMinor)
  $(GitBaseVersionPatch)
  $(GitSemVerMajor)
  $(GitSemVerMinor)
  $(GitSemVerPatch)
  $(GitSemVerLabel)
  $(GitSemVerDashLabel)
  $(GitSemVerSource)
  $(GitIsDirty)

From C#, F# and VB, by default code is generated too so that the same information can be accessed from code, to construct your own assembly/file version attributes with whatever format you want:

[assembly: AssemblyVersion (ThisAssembly.Git.SemVer.Major + "." + ThisAssembly.Git.SemVer.Minor + "." + ThisAssembly.Git.SemVer.Patch)]
[assembly: AssemblyInformationalVersion (
  ThisAssembly.Git.SemVer.Major + "." +
  ThisAssembly.Git.SemVer.Minor + "." +
  ThisAssembly.Git.SemVer.Patch + "-" +
  ThisAssembly.Git.Branch + "+" +
  ThisAssembly.Git.Commit)]
// i..e ^: 1.0.2-main+c218617

NOTE: you may need to close and reopen the solution in order for Visual Studio to refresh intellisense and show the ThisAssembly type right after package installation for the first time.

All generated constants also have a Summary documentation tag that shows the current value in the intellisense tooltip, making it very easy to see what the different values contain.

The available constants from code are:

  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

Available MSBuild properties:

  $(GitThisAssembly): set to 'false' to prevent assembly 
                      metadata and constants generation.

  $(GitThisAssemblyMetadata): set to 'false' to prevent assembly 
                              metadata generation only. Defaults 
                              to 'false'.

  $(ThisAssemblyNamespace): allows overriding the namespace
                            for the ThisAssembly class.
                            Defaults to the global namespace.

  $(GitRemote): name of remote to get repository url for.
                Defaults to 'origin'.

  $(GitDefaultBranch): determines the base branch used to 
                       calculate commits on top of current branch.
                       Defaults to 'master'.

  $(GitVersionFile): determines the name of a file in the Git 
                     repository root used to provide the base 
                     version info.
                     Defaults to 'GitInfo.txt'.

  $(GitInfoReportImportance): allows rendering all the retrieved
                              git information with the specified
                              message importance ('high', 
                              'normal' or 'low').
                              Defaults to 'low'.

  $(GitIgnoreBranchVersion) and $(GitIgnoreTagVersion): determines 
                            whether the branch and tags (if any) 
                            will be used to find a base version.
                            Defaults to empty value (no ignoring).

  $(GitSkipCache): whether to cache the Git information determined
           in a previous build in a GitInfo.cache for
           performance reasons.
           Defaults to empty value (no ignoring).

  $(GitNameRevOptions): Options passed to git name-rev when finding
              a branch name for the current commit (Detached head). The default is
              '--refs=refs/heads/* --no-undefined --alwas'
              meaning branch names only, falling back to commit hash.
              For legacy behavior where $(GitBranch) for detached head
              can also be a tag name, use '--refs=refs/*'.
              Refs can be included and excluded, see git name-rev docs.

  $(GitTagRegex): Regular expression used with git describe to filter the tags 
                  to consider for base version lookup.
                  Defaults to * (all)
           
  $(GitBaseVersionRegex): Regular expression used to match and validate valid base versions
                          in branch, tag or file sources. By default, matches any string that 
                          *ends* in a valid SemVer2 string.
                          Defaults to 'v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)(?:\-(?<LABEL>[\dA-Za-z\-\.]+))?$|^(?<LABEL>[\dA-Za-z\-\.]+)\-v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)$'

Goals

  • No compiled code or tools -> 100% transparency
  • Trivially added/installed via a NuGet package
  • No format strings or settings to learn
  • Single well-structured .targets file with plain MSBuild and no custom tasks
  • Optional embedding of Git info in assembly as metadata
  • Optional use of Git info to build arbitrary assembly/file version information, both in C# as well as VB.
  • Trivially modified/improved generated code by just adjusting a C# or F# or VB template included in the NuGet package
  • Easily modified/improved by just adjusting a single .targets file
  • 100% incremental build-friendly and high-performing (all proper Inputs/Outputs in place, smart caching of Git info, etc.)

Sponsors

Clarius Org Christian Findlay C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. SandRock Andy Gocke Shahzad Huq

Sponsor this project  

Learn more about GitHub Sponsors

No packages depend on GitInfo.

https://github.com/devlooped/GitInfo/blob/main/changelog.md

This package has no dependencies.

Version Downloads Last updated
3.5.0 3 12/13/2024
3.3.5 3 12/19/2024
3.3.4 3 12/11/2024
3.3.3 4 12/02/2024
3.3.2 4 12/14/2024
3.3.1 4 12/15/2024
3.3.0 3 12/08/2024
3.2.0 3 12/11/2024
2.3.0 3 12/15/2024
2.2.1 3 12/15/2024
2.2.0 3 12/15/2024
2.1.2 0 09/24/2020
2.1.1 5 12/11/2024
2.0.40 4 12/06/2024
2.0.39 4 12/01/2024
2.0.38 5 12/12/2024
2.0.37 0 09/20/2020
2.0.36 4 11/22/2024
2.0.35 3 12/11/2024
2.0.34 8 01/13/2023
2.0.33 3 12/11/2024
2.0.32 4 12/19/2024
2.0.31 3 12/15/2024
2.0.30 5 12/11/2024
2.0.29 3 12/11/2024
2.0.28 3 12/12/2024
2.0.27 3 12/11/2024
2.0.26 3 12/11/2024
2.0.25 3 12/11/2024
2.0.21 3 12/12/2024
2.0.20 4 12/08/2024
2.0.19 3 12/12/2024
2.0.18 3 12/12/2024
2.0.17 3 12/11/2024
2.0.16 3 12/19/2024
2.0.15 5 12/16/2024
2.0.14 3 12/11/2024
2.0.11 4 12/12/2024
2.0.10 3 12/15/2024
2.0.9 4 12/14/2024
2.0.8 3 12/15/2024
2.0.7 3 12/21/2024
2.0.6 3 12/09/2024
2.0.5 3 12/11/2024
2.0.3 3 12/16/2024
2.0.2 3 12/12/2024
2.0.1 3 12/16/2024
2.0.0 3 12/11/2024
1.1.72 3 12/15/2024
1.1.71 4 12/07/2024
1.1.70 3 12/15/2024
1.1.68 4 11/29/2024
1.1.67 4 12/12/2024
1.1.66 3 12/15/2024
1.1.65 3 12/19/2024
1.1.63 3 12/11/2024
1.1.62 3 12/11/2024
1.1.61 3 12/22/2024
1.1.60 3 12/11/2024
1.1.59 3 12/11/2024
1.1.58 4 12/11/2024
1.1.57 4 12/14/2024
1.1.56 3 12/15/2024
1.1.55 3 12/15/2024
1.1.54 6 12/04/2024
1.1.53 5 12/11/2024
1.1.48 5 12/11/2024
1.1.47 4 11/25/2024
1.1.45 0 01/27/2017
1.1.44 3 12/11/2024
1.1.43 3 12/15/2024
1.1.41 3 12/22/2024
1.1.40 3 12/11/2024
1.1.39 3 12/12/2024
1.1.38 3 12/15/2024
1.1.37 3 12/11/2024
1.1.35 3 12/11/2024
1.1.34 3 12/16/2024
1.1.31 3 12/15/2024
1.1.30 1 12/08/2024
1.1.29 4 12/15/2024
1.1.28 3 12/19/2024
1.1.27 3 12/16/2024
1.1.26 3 12/19/2024
1.1.25 3 12/15/2024
1.1.24 3 12/15/2024
1.1.23 3 12/12/2024
1.1.22 3 12/12/2024
1.1.20 3 12/16/2024
1.1.19 3 12/11/2024
1.1.17 3 12/12/2024
1.1.15 3 12/16/2024
1.1.14 3 12/15/2024
1.1.13 6 11/24/2024
1.1.12 3 12/11/2024
1.1.10 3 12/11/2024
1.1.9 4 12/11/2024
1.1.8 3 12/11/2024
1.1.7 3 12/16/2024
1.1.5 3 12/15/2024
1.1.4 3 12/15/2024
1.1.2 5 11/12/2024
1.1.1 4 12/03/2024
1.1.0 3 12/11/2024
1.0.64-pre 3 12/11/2024
1.0.63-pre 5 12/11/2024
1.0.62-pre 3 12/15/2024
1.0.61-pre 3 12/19/2024
1.0.60-pre 3 12/09/2024
1.0.59-pre 3 12/19/2024
1.0.58-pre 3 12/11/2024
1.0.56-pre 5 12/11/2024
1.0.55-pre 3 12/19/2024
1.0.54-pre 3 12/23/2024
1.0.53-pre 3 12/11/2024
1.0.52-pre 3 12/11/2024
1.0.51-pre 3 12/09/2024
1.0.50-pre 3 12/11/2024
1.0.49-pre 3 12/19/2024
1.0.48-pre 3 12/20/2024
1.0.47-pre 3 12/19/2024
1.0.46-pre 3 12/15/2024
1.0.45-pre 3 12/11/2024
1.0.44-pre 0 09/01/2015
1.0.43-pre 4 12/11/2024
1.0.42-pre 3 12/19/2024
1.0.41-pre 3 12/11/2024
1.0.40-pre 1 12/20/2024
1.0.39-pre 3 12/16/2024
1.0.38-pre 3 12/11/2024
1.0.37-pre 5 12/12/2024
1.0.36-pre 3 12/15/2024
1.0.35-pre 3 12/16/2024
1.0.34-pre 3 12/19/2024
1.0.33-pre 6 11/22/2024
1.0.31-pre 3 12/22/2024
1.0.30-pre 5 12/12/2024
1.0.29-pre 4 12/03/2024
1.0.28-pre 3 12/11/2024
1.0.27-pre 3 12/16/2024
1.0.26-pre 3 12/11/2024
1.0.25-pre 0 06/14/2015
1.0.24-pre 0 06/11/2015
1.0.23-pre 0 06/08/2015
1.0.22-pre 3 12/11/2024
1.0.21-pre 3 12/19/2024
1.0.20-pre 3 12/19/2024
1.0.19-pre 3 12/20/2024
1.0.18-pre 3 12/06/2024
1.0.16-pre 3 12/15/2024
1.0.15-pre 4 12/07/2024
1.0.14-pre 1 12/12/2024
1.0.11-pre 3 12/11/2024
1.0.10-pre 4 12/06/2024
1.0.9-pre 3 12/15/2024
1.0.8-pre 3 12/11/2024
1.0.7-pre 3 12/12/2024
1.0.6-pre 3 12/12/2024
1.0.5-pre 2 12/18/2024
1.0.4-pre 3 12/11/2024
1.0.3-pre 3 12/11/2024
1.0.1-pre 5 12/11/2024
1.0.0 3 12/19/2024
1.0.0-pre 3 12/11/2024
0.0.196 3 12/20/2024
0.0.195 0 10/05/2020
0.0.194 3 12/11/2024