Skip to content

Commit c6f90ca

Browse files
authored
Add support for custom GitCheckoutInformation in BuildContext (#794)
* Add support for custom GitCheckoutInformation in BuildContext Introduced an optional parameter to pass a custom GitCheckoutInformation to BuildContext, reducing dependency on internal creation logic. Updated repository name handling and fallback mechanisms to ensure consistency and robustness. Simplified Git-related logging and error handling for better maintainability. * DocumentationSet Name == Git.RepositoryName now that the latter is not nullable * Documentation.Name (debug logging only) try to be as unique on the name in case git is not available
1 parent dd75af1 commit c6f90ca

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ public BuildContext(IFileSystem fileSystem)
5151
public BuildContext(DiagnosticsCollector collector, IFileSystem fileSystem)
5252
: this(collector, fileSystem, fileSystem, null, null) { }
5353

54-
public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem, IFileSystem writeFileSystem)
55-
: this(collector, readFileSystem, writeFileSystem, null, null) { }
56-
57-
public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem, IFileSystem writeFileSystem, string? source, string? output)
54+
public BuildContext(
55+
DiagnosticsCollector collector,
56+
IFileSystem readFileSystem,
57+
IFileSystem writeFileSystem,
58+
string? source = null,
59+
string? output = null,
60+
GitCheckoutInformation? gitCheckoutInformation = null
61+
)
5862
{
5963
Collector = collector;
6064
ReadFileSystem = readFileSystem;
@@ -75,8 +79,7 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem,
7579
if (ConfigurationPath.FullName != DocumentationSourceDirectory.FullName)
7680
DocumentationSourceDirectory = ConfigurationPath.Directory!;
7781

78-
79-
Git = GitCheckoutInformation.Create(DocumentationCheckoutDirectory, ReadFileSystem);
82+
Git = gitCheckoutInformation ?? GitCheckoutInformation.Create(DocumentationCheckoutDirectory, ReadFileSystem);
8083
Configuration = new ConfigurationFile(this);
8184
}
8285

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public record GitCheckoutInformation
2929
public required string Ref { get; init; }
3030

3131
[JsonPropertyName("name")]
32-
public string? RepositoryName { get; init; }
32+
public string RepositoryName { get; init; } = "unavailable";
3333

3434
// manual read because libgit2sharp is not yet AOT ready
3535
public static GitCheckoutInformation Create(IDirectoryInfo? source, IFileSystem fileSystem, ILogger? logger = null)
@@ -52,12 +52,8 @@ public static GitCheckoutInformation Create(IDirectoryInfo? source, IFileSystem
5252
var gitConfig = Git(source, Path.Combine(".git", "config"));
5353
if (!gitConfig.Exists)
5454
{
55-
gitConfig = Git(source, Path.Combine("..", ".git", "config"));
56-
if (!gitConfig.Exists)
57-
{
58-
logger?.LogInformation("Git checkout information not available.");
59-
return Unavailable;
60-
}
55+
logger?.LogInformation("Git checkout information not available.");
56+
return Unavailable;
6157
}
6258

6359
var head = Read(source, Path.Combine(".git", "HEAD")) ?? fakeRef;

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Elastic.Markdown.Diagnostics;
1010
using Elastic.Markdown.Extensions;
1111
using Elastic.Markdown.IO.Configuration;
12+
using Elastic.Markdown.IO.Discovery;
1213
using Elastic.Markdown.IO.Navigation;
1314
using Elastic.Markdown.Myst;
1415
using Microsoft.Extensions.Logging;
@@ -77,7 +78,9 @@ public DocumentationSet(BuildContext build, ILoggerFactory logger, ICrossLinkRes
7778
};
7879
MarkdownParser = new MarkdownParser(build, resolver);
7980

80-
Name = Build.Git.RepositoryName ?? "unavailable";
81+
Name = Build.Git != GitCheckoutInformation.Unavailable
82+
? Build.Git.RepositoryName
83+
: Build.DocumentationCheckoutDirectory?.Name ?? $"unknown-{Build.DocumentationSourceDirectory.Name}";
8184
OutputStateFile = OutputDirectory.FileSystem.FileInfo.New(Path.Combine(OutputDirectory.FullName, ".doc.state"));
8285
LinkReferenceFile = OutputDirectory.FileSystem.FileInfo.New(Path.Combine(OutputDirectory.FullName, "links.json"));
8386

src/docs-assembler/Building/AssemblerBuilder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Elastic.Markdown;
99
using Elastic.Markdown.CrossLinks;
1010
using Elastic.Markdown.IO;
11+
using Elastic.Markdown.IO.Discovery;
1112
using Microsoft.Extensions.Logging;
1213

1314
namespace Documentation.Assembler.Building;
@@ -56,7 +57,15 @@ private async Task BuildAsync(Checkout checkout, PublishEnvironment environment,
5657
var path = checkout.Directory.FullName;
5758
var output = environment.PathPrefix != null ? Path.Combine(context.OutputDirectory.FullName, environment.PathPrefix) : context.OutputDirectory.FullName;
5859

59-
var buildContext = new BuildContext(context.Collector, context.ReadFileSystem, context.WriteFileSystem, path, output)
60+
var gitConfiguration = new GitCheckoutInformation
61+
{
62+
RepositoryName = checkout.Repository.Name,
63+
Ref = checkout.HeadReference,
64+
Remote = $"elastic/${checkout.Repository.Name}",
65+
Branch = checkout.Repository.CurrentBranch
66+
};
67+
68+
var buildContext = new BuildContext(context.Collector, context.ReadFileSystem, context.WriteFileSystem, path, output, gitConfiguration)
6069
{
6170
UrlPathPrefix = environment.PathPrefix,
6271
Force = false,

src/docs-assembler/Navigation/GlobalNavigation.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public string GetSubPath(Uri crossLinkUri, ref string path)
9797
var outputDirectory = documentationSet.OutputDirectory;
9898
var fs = defaultOutputFile.FileSystem;
9999

100-
var l = $"{documentationSet.Name}://{relativePath.TrimStart('/')}";
100+
var repositoryName = documentationSet.Build.Git.RepositoryName;
101+
102+
var l = $"{repositoryName}://{relativePath.TrimStart('/')}";
101103
var lookup = l.AsSpan();
102104
if (lookup.StartsWith("docs-content://serverless/", StringComparison.Ordinal))
103105
return null;
@@ -136,7 +138,7 @@ public string GetSubPath(Uri crossLinkUri, ref string path)
136138
if (relativePath.EndsWith(".asciidoc", StringComparison.Ordinal))
137139
return null;
138140

139-
var fallBack = fs.Path.Combine(outputDirectory.FullName, "_failed", documentationSet.Name, relativePath);
141+
var fallBack = fs.Path.Combine(outputDirectory.FullName, "_failed", repositoryName, relativePath);
140142
_context.Collector.EmitError(_context.NavigationPath, $"No toc for output path: '{lookup}' falling back to: '{fallBack}'");
141143
return fs.FileInfo.New(fallBack);
142144
}

0 commit comments

Comments
 (0)