From b19b0c8662942cfb365857b32b07060227392e45 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 7 Jan 2025 15:26:21 +0100 Subject: [PATCH] Make .git folder optional so tool can be run on any folder (#144) --- src/Elastic.Markdown/IO/GitConfiguration.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Elastic.Markdown/IO/GitConfiguration.cs b/src/Elastic.Markdown/IO/GitConfiguration.cs index 510f04f1..ea451bc3 100644 --- a/src/Elastic.Markdown/IO/GitConfiguration.cs +++ b/src/Elastic.Markdown/IO/GitConfiguration.cs @@ -10,9 +10,21 @@ namespace Elastic.Markdown.IO; public record GitConfiguration { - [JsonPropertyName("branch")] public required string Branch { get; init; } - [JsonPropertyName("remote")] public required string Remote { get; init; } - [JsonPropertyName("ref")] public required string Ref { get; init; } + private static GitConfiguration Unavailable { get; } = new() + { + Branch = "unavailable", + Remote = "unavailable", + Ref = "unavailable" + }; + + [JsonPropertyName("branch")] + public required string Branch { get; init; } + + [JsonPropertyName("remote")] + public required string Remote { get; init; } + + [JsonPropertyName("ref")] + public required string Ref { get; init; } // manual read because libgit2sharp is not yet AOT ready public static GitConfiguration Create(IFileSystem fileSystem) @@ -26,7 +38,7 @@ public static GitConfiguration Create(IFileSystem fileSystem) var gitConfig = Git(".git/config"); if (!gitConfig.Exists) - throw new Exception($"{Paths.Root.FullName} is not a git repository."); + return Unavailable; var head = Read(".git/HEAD"); var gitRef = head;