Skip to content

Commit eae1c9c

Browse files
authored
Prefer GITHUB_REPOSITORY for remote name (#594)
* Prefer GITHUB_REPOSITORY for remote name * Fix reading remote name sometimes trimming last character when reading from .git * update all usages
1 parent c60a9ea commit eae1c9c

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem,
6565
if (ConfigurationPath.FullName != SourcePath.FullName)
6666
SourcePath = ConfigurationPath.Directory!;
6767

68-
Git = GitCheckoutInformation.Create(ReadFileSystem);
68+
Git = GitCheckoutInformation.Create(SourcePath, ReadFileSystem);
6969
Configuration = new ConfigurationFile(ConfigurationPath, SourcePath, this);
7070
}
7171

src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public record GitCheckoutInformation
3333
public string? RepositoryName
3434
{
3535
get => _repositoryName ??= Remote.Split('/').Last();
36-
set => _repositoryName = value;
36+
init => _repositoryName = value;
3737
}
3838

3939
// manual read because libgit2sharp is not yet AOT ready
40-
public static GitCheckoutInformation Create(IFileSystem fileSystem)
40+
public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem)
4141
{
4242
if (fileSystem is not FileSystem)
4343
{
@@ -51,18 +51,18 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
5151
}
5252

5353
var fakeRef = Guid.NewGuid().ToString()[..16];
54-
var gitConfig = Git(".git/config");
54+
var gitConfig = Git(source, ".git/config");
5555
if (!gitConfig.Exists)
5656
return Unavailable;
5757

58-
var head = Read(".git/HEAD") ?? fakeRef;
58+
var head = Read(source, ".git/HEAD") ?? fakeRef;
5959
var gitRef = head;
6060
var branch = head.Replace("refs/heads/", string.Empty);
6161
//not detached HEAD
6262
if (head.StartsWith("ref:"))
6363
{
6464
head = head.Replace("ref: ", string.Empty);
65-
gitRef = Read(".git/" + head) ?? fakeRef;
65+
gitRef = Read(source, ".git/" + head) ?? fakeRef;
6666
branch = branch.Replace("ref: ", string.Empty);
6767
}
6868
else
@@ -73,15 +73,17 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
7373
using var streamReader = new StreamReader(stream);
7474
ini.Load(streamReader);
7575

76-
var remote = BranchTrackingRemote(branch, ini);
76+
var remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY");
77+
if (string.IsNullOrEmpty(remote))
78+
remote = BranchTrackingRemote(branch, ini);
7779
if (string.IsNullOrEmpty(remote))
7880
remote = BranchTrackingRemote("main", ini);
7981
if (string.IsNullOrEmpty(remote))
8082
remote = BranchTrackingRemote("master", ini);
8183
if (string.IsNullOrEmpty(remote))
82-
remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY") ?? "elastic/docs-builder-unknown";
84+
remote = "elastic/docs-builder-unknown";
8385

84-
remote = remote.AsSpan().TrimEnd(".git").ToString();
86+
remote = remote.AsSpan().TrimEnd("git").TrimEnd('.').ToString();
8587

8688
return new GitCheckoutInformation
8789
{
@@ -91,11 +93,12 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
9193
RepositoryName = remote.Split('/').Last()
9294
};
9395

94-
IFileInfo Git(string path) => fileSystem.FileInfo.New(Path.Combine(Paths.Root.FullName, path));
96+
IFileInfo Git(IDirectoryInfo directoryInfo, string path) =>
97+
fileSystem.FileInfo.New(Path.Combine(directoryInfo.FullName, path));
9598

96-
string? Read(string path)
99+
string? Read(IDirectoryInfo directoryInfo, string path)
97100
{
98-
var gitPath = Git(path).FullName;
101+
var gitPath = Git(directoryInfo, path).FullName;
99102
return !fileSystem.File.Exists(gitPath)
100103
? null
101104
: fileSystem.File.ReadAllText(gitPath).Trim(Environment.NewLine.ToCharArray());

src/docs-assembler/Cli/LinkCommands.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using ConsoleAppFramework;
1111
using Documentation.Assembler.Links;
1212
using Elastic.Markdown.CrossLinks;
13+
using Elastic.Markdown.IO;
1314
using Elastic.Markdown.IO.Discovery;
1415
using Microsoft.Extensions.Logging;
1516

@@ -48,7 +49,9 @@ public async Task<int> ValidateLocalInboundLinks(string? repository = null, stri
4849
{
4950
AssignOutputLogger();
5051
file ??= ".artifacts/docs/html/links.json";
51-
repository ??= GitCheckoutInformation.Create(new FileSystem()).RepositoryName;
52+
var fs = new FileSystem();
53+
var root = fs.DirectoryInfo.New(Paths.Root.FullName);
54+
repository ??= GitCheckoutInformation.Create(root, new FileSystem()).RepositoryName;
5255
if (repository == null)
5356
throw new Exception("Unable to determine repository name");
5457

tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Markdown.IO;
56
using Elastic.Markdown.IO.Discovery;
67
using Elastic.Markdown.IO.State;
78
using FluentAssertions;
@@ -32,7 +33,8 @@ public class GitCheckoutInformationTests(ITestOutputHelper output) : NavigationT
3233
[Fact]
3334
public void Create()
3435
{
35-
var git = GitCheckoutInformation.Create(ReadFileSystem);
36+
var root = ReadFileSystem.DirectoryInfo.New(Paths.Root.FullName);
37+
var git = GitCheckoutInformation.Create(root, ReadFileSystem);
3638

3739
git.Should().NotBeNull();
3840
git.Branch.Should().NotBeNullOrWhiteSpace();

0 commit comments

Comments
 (0)