@@ -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 ( ) ) ;
0 commit comments