Skip to content

Commit

Permalink
feat: add ability to get git information
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Oct 11, 2024
1 parent 90d9094 commit 4bbdf12
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T

## [Unreleased]

### Added

- Added support for git information. Please see the README.md for more details and how to activate it.

## [1.2.1] - 2024-05-02

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
<!-- Custom properties of LinkDotNet.BuildInformation -->
<UseRootNamespaceForBuildInformation>true</UseRootNamespaceForBuildInformation>
<AllowProjectDirectoryBuildOutput>true</AllowProjectDirectoryBuildOutput>
<IncludeGitInformation>true</IncludeGitInformation>
</PropertyGroup>

<ItemGroup Label="Needed so that the source generator can access the variables">
<CompilerVisibleProperty Include="UseRootNamespaceForBuildInformation" />
<CompilerVisibleProperty Include="AllowProjectDirectoryBuildOutput" />
<CompilerVisibleProperty Include="IncludeGitInformation" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion LinkDotNet.BuildInformation.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
Console.WriteLine($"Analysis level: {BuildInformation.AnalysisLevel}");
Console.WriteLine($"Project directory: {BuildInformation.ProjectDirectory}");
Console.WriteLine($"Language: {BuildInformation.Language}");
Console.WriteLine($"Language version: {BuildInformation.LanguageVersion}");
Console.WriteLine($"Language version: {BuildInformation.LanguageVersion}");

Console.WriteLine();
Console.WriteLine("Git information:");
Console.WriteLine($"Branch: {GitInformation.Branch}");
Console.WriteLine($"Commit: {GitInformation.Commit}");
Console.WriteLine($"Short commit: {GitInformation.ShortCommit}");
Console.WriteLine($"Nearest tag: {GitInformation.NearestTag}");
Console.WriteLine($"Detailed tag description: {GitInformation.DetailedTagDescription}");
10 changes: 10 additions & 0 deletions LinkDotNet.BuildInformation/GitInformationInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace LinkDotNet.BuildInformation;

public sealed class GitInformationInfo
{
public string Branch { get; init; } = string.Empty;
public string Commit { get; init; } = string.Empty;
public string ShortCommit => Commit.Length > 7 ? Commit[..7] : Commit;
public string NearestTag { get; init; } = string.Empty;
public string DetailedTagDescription { get; init; } = string.Empty;
}
41 changes: 41 additions & 0 deletions LinkDotNet.BuildInformation/GitRetriever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Diagnostics;

namespace LinkDotNet.BuildInformation;

public static class GitRetriever
{
public static GitInformationInfo GetGitInformation(bool useGitInfo)
{
if (!useGitInfo)
{
return new GitInformationInfo();
}

return new GitInformationInfo
{
Branch = GetGitInfoByCommand("rev-parse --abbrev-ref HEAD"),
Commit = GetGitInfoByCommand("rev-parse HEAD"),
NearestTag = GetGitInfoByCommand("describe --tags --abbrev=0"),
DetailedTagDescription = GetGitInfoByCommand("describe --tags"),
};

static string GetGitInfoByCommand(string command)
{
var processInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = command,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};

var process = new Process { StartInfo = processInfo };

process.Start();
var result = process.StandardOutput.ReadToEnd().Trim();
process.WaitForExit();
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Diagnostics;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -49,8 +50,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
Language = CSharpParseOptions.Default.Language,
LanguageVersion = CSharpParseOptions.Default.LanguageVersion.ToDisplayString(),
};
analyzer.GlobalOptions.TryGetValue("build_property.IncludeGitInformation", out var useGitInfoOption);
var useGitInfo = useGitInfoOption?.Equals("true", StringComparison.InvariantCultureIgnoreCase) ?? false;
productionContext.AddSource("LinkDotNet.BuildInformation.g", GenerateBuildInformationClass(buildInformation));
var gitInfo = GitRetriever.GetGitInformation(useGitInfo);
productionContext.AddSource("LinkDotNet.BuildInformation.g", GenerateBuildInformationClass(buildInformation, gitInfo));
});
}

Expand Down Expand Up @@ -105,7 +111,9 @@ private static string GetProjectDirectory(AnalyzerConfigOptionsProvider analyzer
: projectDir;
}

private static string GenerateBuildInformationClass(BuildInformationInfo buildInformation)
private static string GenerateBuildInformationClass(
BuildInformationInfo buildInformation,
GitInformationInfo gitInfo)
{
var rootNamespace = string.IsNullOrEmpty(buildInformation.RootNamespace)
? string.Empty
Expand Down Expand Up @@ -209,6 +217,39 @@ internal static class BuildInformation
/// <remarks>Value is {{buildInformation.LanguageVersion}}</remarks>
public const string LanguageVersion = "{{buildInformation.LanguageVersion}}";
}

internal static class GitInformation
{
/// <summary>
/// Returns the branch of the git repository.
/// </summary>
/// <remarks>Value is: {{gitInfo.Branch}}</remarks>
public const string Branch = "{{gitInfo.Branch}}";

/// <summary>
/// Returns the commit hash of the git repository.
/// </summary>
/// <remarks>Value is: {{gitInfo.Commit}}</remarks>
public const string Commit = "{{gitInfo.Commit}}";

/// <summary>
/// Returns the short commit hash of the git repository.
/// </summary>
/// <remarks>Value is: {{gitInfo.ShortCommit}}</remarks>
public const string ShortCommit = "{{gitInfo.ShortCommit}}";

/// <summary>
/// Returns the nearest tag of the git repository. This uses <c>git describe --tags --abbrev=0</c>.
/// </summary>
/// <remarks>Value is: {{gitInfo.NearestTag}}</remarks>
public const string NearestTag = "{{gitInfo.NearestTag}}";

/// <summary>
/// Returns the detailed tag description of the git repository. This uses <c>git describe --tags</c>.
/// </summary>
/// <remarks>Value is: {{gitInfo.DetailedTagDescription}}</remarks>
public const string DetailedTagDescription = "{{gitInfo.DetailedTagDescription}}";
}
""";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
<Description>Supplies information about the build like build time.</Description>
<PackageProjectUrl>https://github.com/linkdotnet/BuildInformation</PackageProjectUrl>
<RepositoryUrl>https://github.com/linkdotnet/BuildInformation</RepositoryUrl>
<PackageTags>csharp,dotnet,msbuild,roslyn</PackageTags>
<PackageTags>csharp,dotnet,msbuild,roslyn,git,build information</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>logo.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.40">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" PrivateAssets="all" />
</ItemGroup>

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ The `AllowProjectDirectoryBuildOutput` property is used to allow the generator t
</ItemGroup>
```

### Git Information
By default, the generator does not include git information. If you want to include the git information, you can add the following to your project file:

```xml
<PropertyGroup>
<IncludeGitInformation>true</IncludeGitInformation>
</PropertyGroup>

<ItemGroup>
<CompilerVisibleProperty Include="IncludeGitInformation" />
</ItemGroup>
```

This will fill the `GitInformation` class with the following properties:
```csharp
Console.WriteLine("Git information:");
Console.WriteLine($"Branch: {GitInformation.Branch}");
Console.WriteLine($"Commit: {GitInformation.Commit}");
Console.WriteLine($"Short commit: {GitInformation.ShortCommit}");
Console.WriteLine($"Nearest tag: {GitInformation.NearestTag}");
Console.WriteLine($"Detailed tag description: {GitInformation.DetailedTagDescription}");
```

This will spawn a `git` process to get the information.
Therefore, git has to be accessible from the command line. Be advised, that it will increase your build time slightly.

## Usage
To use the `BuildInformation` class in your project, add the NuGet package:

Expand Down

0 comments on commit 4bbdf12

Please sign in to comment.