Skip to content

Commit

Permalink
Prefer GITHUB_REPOSITORY for remote name (#594)
Browse files Browse the repository at this point in the history
* Prefer GITHUB_REPOSITORY for remote name

* Fix reading remote name sometimes trimming last character when reading from .git

* update all usages
  • Loading branch information
Mpdreamz authored Feb 24, 2025
1 parent c60a9ea commit eae1c9c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Elastic.Markdown/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem,
if (ConfigurationPath.FullName != SourcePath.FullName)
SourcePath = ConfigurationPath.Directory!;

Git = GitCheckoutInformation.Create(ReadFileSystem);
Git = GitCheckoutInformation.Create(SourcePath, ReadFileSystem);
Configuration = new ConfigurationFile(ConfigurationPath, SourcePath, this);
}

Expand Down
25 changes: 14 additions & 11 deletions src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public record GitCheckoutInformation
public string? RepositoryName
{
get => _repositoryName ??= Remote.Split('/').Last();
set => _repositoryName = value;
init => _repositoryName = value;
}

// manual read because libgit2sharp is not yet AOT ready
public static GitCheckoutInformation Create(IFileSystem fileSystem)
public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem)
{
if (fileSystem is not FileSystem)
{
Expand All @@ -51,18 +51,18 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
}

var fakeRef = Guid.NewGuid().ToString()[..16];
var gitConfig = Git(".git/config");
var gitConfig = Git(source, ".git/config");
if (!gitConfig.Exists)
return Unavailable;

var head = Read(".git/HEAD") ?? fakeRef;
var head = Read(source, ".git/HEAD") ?? fakeRef;
var gitRef = head;
var branch = head.Replace("refs/heads/", string.Empty);
//not detached HEAD
if (head.StartsWith("ref:"))
{
head = head.Replace("ref: ", string.Empty);
gitRef = Read(".git/" + head) ?? fakeRef;
gitRef = Read(source, ".git/" + head) ?? fakeRef;
branch = branch.Replace("ref: ", string.Empty);
}
else
Expand All @@ -73,15 +73,17 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
using var streamReader = new StreamReader(stream);
ini.Load(streamReader);

var remote = BranchTrackingRemote(branch, ini);
var remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY");
if (string.IsNullOrEmpty(remote))
remote = BranchTrackingRemote(branch, ini);
if (string.IsNullOrEmpty(remote))
remote = BranchTrackingRemote("main", ini);
if (string.IsNullOrEmpty(remote))
remote = BranchTrackingRemote("master", ini);
if (string.IsNullOrEmpty(remote))
remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY") ?? "elastic/docs-builder-unknown";
remote = "elastic/docs-builder-unknown";

remote = remote.AsSpan().TrimEnd(".git").ToString();
remote = remote.AsSpan().TrimEnd("git").TrimEnd('.').ToString();

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

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

string? Read(string path)
string? Read(IDirectoryInfo directoryInfo, string path)
{
var gitPath = Git(path).FullName;
var gitPath = Git(directoryInfo, path).FullName;
return !fileSystem.File.Exists(gitPath)
? null
: fileSystem.File.ReadAllText(gitPath).Trim(Environment.NewLine.ToCharArray());
Expand Down
5 changes: 4 additions & 1 deletion src/docs-assembler/Cli/LinkCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ConsoleAppFramework;
using Documentation.Assembler.Links;
using Elastic.Markdown.CrossLinks;
using Elastic.Markdown.IO;
using Elastic.Markdown.IO.Discovery;
using Microsoft.Extensions.Logging;

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

Expand Down
4 changes: 3 additions & 1 deletion tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Markdown.IO;
using Elastic.Markdown.IO.Discovery;
using Elastic.Markdown.IO.State;
using FluentAssertions;
Expand Down Expand Up @@ -32,7 +33,8 @@ public class GitCheckoutInformationTests(ITestOutputHelper output) : NavigationT
[Fact]
public void Create()
{
var git = GitCheckoutInformation.Create(ReadFileSystem);
var root = ReadFileSystem.DirectoryInfo.New(Paths.Root.FullName);
var git = GitCheckoutInformation.Create(root, ReadFileSystem);

git.Should().NotBeNull();
git.Branch.Should().NotBeNullOrWhiteSpace();
Expand Down

0 comments on commit eae1c9c

Please sign in to comment.