Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to main for remote tracking #81

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading