This library build on top of kekyo/GitReader, which is "Lightweight Git local repository traversal library".
<ItemGroup>
<PackageReference Include="GitReader.SourceGenerator" Version="0.1.4" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup Label="Configure GitInfo">
<!-- optional, default value: $([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), '.gitignore')) -->
<GitInfo_RootDirectory>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'My.Project.sln'))\..\.git</GitInfo_RootDirectory>
<GitInfo_Namespace>My.Project.GitInfoNamespace</GitInfo_Namespace>
<!-- optional, default value 9 -->
<GitInfo_CommitAbbreviatedLength>9</GitInfo_CommitAbbreviatedLength>
<!-- optional, default value true -->
<GitInfo_UseCache>true</GitInfo_UseCache>
<!-- optional, default value false -->
<!-- it check only cache existence, not that HEAD equals HEAD in cache -->
<GitInfo_UseAggressiveCache>true</GitInfo_UseAggressiveCache>
</PropertyGroup>
using My.Project.GitInfoNamespace;
Console.WriteLine(GitInfo.Branch);
Console.WriteLine(GitInfo.CommitAbbreviatedHash);
Console.WriteLine(GitInfo.CommitHash);
Console.WriteLine(GitInfo.CommitDate);
Console.WriteLine(GitInfo.TagName);
Console.WriteLine(GitInfo.Tag);
Console.WriteLine(GitInfo.TagMajor);
Console.WriteLine(GitInfo.TagMinor);
Console.WriteLine(GitInfo.TagBuild);
Console.WriteLine(GitInfo.TagRevision);