Skip to content

Commit 4a9b8aa

Browse files
authored
Fallback to main for remote tracking (#81)
1 parent ccb780f commit 4a9b8aa

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/Elastic.Markdown/IO/GitConfiguration.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
using System.IO.Abstractions;
66
using System.Text.Json.Serialization;
77
using IniParser;
8+
using IniParser.Model;
89

910
namespace Elastic.Markdown.IO;
1011

1112
public record GitConfiguration
1213
{
13-
[JsonPropertyName("branch")]
14-
public required string Branch { get; init; }
15-
[JsonPropertyName("remote")]
16-
public required string Remote { get; init; }
17-
[JsonPropertyName("ref")]
18-
public required string Ref { get; init; }
14+
[JsonPropertyName("branch")] public required string Branch { get; init; }
15+
[JsonPropertyName("remote")] public required string Remote { get; init; }
16+
[JsonPropertyName("ref")] public required string Ref { get; init; }
1917

2018
// manual read because libgit2sharp is not yet AOT ready
2119
public static GitConfiguration Create(IFileSystem fileSystem)
@@ -24,12 +22,7 @@ public static GitConfiguration Create(IFileSystem fileSystem)
2422
if (fileSystem is not FileSystem)
2523
{
2624
var fakeRef = Guid.NewGuid().ToString().Substring(0, 16);
27-
return new GitConfiguration
28-
{
29-
Branch = $"test-{fakeRef}",
30-
Remote = "elastic/docs-builder",
31-
Ref = fakeRef,
32-
};
25+
return new GitConfiguration { Branch = $"test-{fakeRef}", Remote = "elastic/docs-builder", Ref = fakeRef, };
3326
}
3427

3528
var gitConfig = Git(".git/config");
@@ -44,19 +37,25 @@ public static GitConfiguration Create(IFileSystem fileSystem)
4437
using var stream = gitConfig.OpenRead();
4538
using var streamReader = new StreamReader(stream);
4639
var config = ini.ReadData(streamReader);
47-
var remoteName = config[$"branch \"{branch}\""]["remote"];
48-
var remote = config[$"remote \"{remoteName}\""]["url"];
40+
var remote = BranchTrackingRemote(branch, config);
41+
if (string.IsNullOrEmpty(remote))
42+
remote = BranchTrackingRemote("main", config);
43+
if (string.IsNullOrEmpty(remote))
44+
remote = BranchTrackingRemote("master", config);
4945

50-
return new GitConfiguration
51-
{
52-
Ref = gitRef,
53-
Branch = branch,
54-
Remote = remote
55-
};
46+
47+
return new GitConfiguration { Ref = gitRef, Branch = branch, Remote = remote };
5648

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

5951
string Read(string path) =>
6052
fileSystem.File.ReadAllText(Git(path).FullName).Trim(Environment.NewLine.ToCharArray());
53+
54+
string BranchTrackingRemote(string b, IniData c)
55+
{
56+
var remoteName = c[$"branch \"{b}\""]["remote"];
57+
remote = c[$"remote \"{remoteName}\""]["url"];
58+
return remote;
59+
}
6160
}
6261
}

0 commit comments

Comments
 (0)