Skip to content

Commit

Permalink
Fallback to main for remote tracking (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Nov 20, 2024
1 parent ccb780f commit 4a9b8aa
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/Elastic.Markdown/IO/GitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
using System.IO.Abstractions;
using System.Text.Json.Serialization;
using IniParser;
using IniParser.Model;

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; }
[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)
Expand All @@ -24,12 +22,7 @@ public static GitConfiguration Create(IFileSystem fileSystem)
if (fileSystem is not FileSystem)
{
var fakeRef = Guid.NewGuid().ToString().Substring(0, 16);
return new GitConfiguration
{
Branch = $"test-{fakeRef}",
Remote = "elastic/docs-builder",
Ref = fakeRef,
};
return new GitConfiguration { Branch = $"test-{fakeRef}", Remote = "elastic/docs-builder", Ref = fakeRef, };
}

var gitConfig = Git(".git/config");
Expand All @@ -44,19 +37,25 @@ public static GitConfiguration Create(IFileSystem fileSystem)
using var stream = gitConfig.OpenRead();
using var streamReader = new StreamReader(stream);
var config = ini.ReadData(streamReader);
var remoteName = config[$"branch \"{branch}\""]["remote"];
var remote = config[$"remote \"{remoteName}\""]["url"];
var remote = BranchTrackingRemote(branch, config);
if (string.IsNullOrEmpty(remote))
remote = BranchTrackingRemote("main", config);
if (string.IsNullOrEmpty(remote))
remote = BranchTrackingRemote("master", config);

return new GitConfiguration
{
Ref = gitRef,
Branch = branch,
Remote = remote
};

return new GitConfiguration { Ref = gitRef, Branch = branch, Remote = remote };

IFileInfo Git(string path) => fileSystem.FileInfo.New(Path.Combine(Paths.Root.FullName, path));

string Read(string path) =>
fileSystem.File.ReadAllText(Git(path).FullName).Trim(Environment.NewLine.ToCharArray());

string BranchTrackingRemote(string b, IniData c)
{
var remoteName = c[$"branch \"{b}\""]["remote"];
remote = c[$"remote \"{remoteName}\""]["url"];
return remote;
}
}
}

0 comments on commit 4a9b8aa

Please sign in to comment.