GitInfo

repository·main·Indexed 20 days ago

https://github.com/devlooped/gitinfo

A 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.

Tokens
2.3K
Snippets
6
Records
8
Agent score
21%

What's inside GitInfo

  1. Access Git information in C#, F#, or VB code

    main

    By 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 nested Git static class.

    Note: You may need to close and reopen your solution in Visual Studio for IntelliSense to recognize the ThisAssembly type after the initial installation.

    Console.WriteLine(ThisAssembly.Git.Commit);
  2. Configure GitInfo versioning behavior

    main

    By default, GitInfo automatically sets $(Version) and $(PackageVersion) MSBuild properties, which the .NET SDK uses for AssemblyInfo, FileVersion, and InformationalVersion. 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 GitVersion to false in your project file:

    <PropertyGroup>
      <GitVersion>false</GitVersion>
    </PropertyGroup>
  3. Customize GitInfo behavior via MSBuild properties

    main

    GitInfo 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 setting Version and PackageVersion to 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 the ThisAssembly class. 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 to git name-rev for 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 with git describe to 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 a GitInfo.cache file 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'.
  4. Implement custom assembly versioning in code

    main

    If you opt-out of default versioning, you can use the ThisAssembly.Git constants 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, and GenerateAssemblyInformationalVersionAttribute to false in 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)]
  5. Use GitInfo properties in MSBuild targets

    main

    You 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 (typically GitVersion).

    Example of a custom target using GitVersion as 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>
  6. Reference: GitInfo MSBuild properties and Code constants

    main

    GitInfo 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