Skip to content

Commit d1b9e1a

Browse files
committed
Fix reading remote name sometimes trimming last character when reading from .git
1 parent e5f28d7 commit d1b9e1a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
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: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public string? RepositoryName
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
@@ -83,7 +83,7 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
8383
if (string.IsNullOrEmpty(remote))
8484
remote = "elastic/docs-builder-unknown";
8585

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

8888
return new GitCheckoutInformation
8989
{
@@ -93,11 +93,12 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
9393
RepositoryName = remote.Split('/').Last()
9494
};
9595

96-
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));
9798

98-
string? Read(string path)
99+
string? Read(IDirectoryInfo directoryInfo, string path)
99100
{
100-
var gitPath = Git(path).FullName;
101+
var gitPath = Git(directoryInfo, path).FullName;
101102
return !fileSystem.File.Exists(gitPath)
102103
? null
103104
: fileSystem.File.ReadAllText(gitPath).Trim(Environment.NewLine.ToCharArray());

0 commit comments

Comments
 (0)