@@ -264,18 +264,47 @@ private static string FormatBuildMetadataSemVerV1(IEnumerable<string> identifier
264264 private static LibGit2Sharp . Repository OpenGitRepo ( string repoRoot )
265265 {
266266 Requires . NotNullOrEmpty ( repoRoot , nameof ( repoRoot ) ) ;
267- while ( ! Directory . Exists ( Path . Combine ( repoRoot , ".git" ) ) )
267+ var gitDir = FindGitDir ( repoRoot ) ;
268+ return gitDir == null ? null : new LibGit2Sharp . Repository ( gitDir ) ;
269+ }
270+
271+ private static string FindGitDir ( string startingDir )
272+ {
273+ while ( startingDir != null )
268274 {
269- repoRoot = Path . GetDirectoryName ( repoRoot ) ;
270- if ( repoRoot == null )
275+ var dirOrFilePath = Path . Combine ( startingDir , ".git" ) ;
276+ if ( Directory . Exists ( dirOrFilePath ) )
277+ return dirOrFilePath ;
278+ else if ( File . Exists ( dirOrFilePath ) )
271279 {
272- return null ;
280+ var relativeGitDirPath = ReadGitDirFromFile ( dirOrFilePath ) ;
281+ if ( ! String . IsNullOrWhiteSpace ( relativeGitDirPath ) )
282+ {
283+ var fullGitDirPath = Path . GetFullPath ( Path . Combine ( startingDir , relativeGitDirPath ) ) ;
284+ if ( Directory . Exists ( fullGitDirPath ) )
285+ return fullGitDirPath ;
286+ }
273287 }
288+
289+ startingDir = Path . GetDirectoryName ( startingDir ) ;
274290 }
291+ return null ;
292+ }
275293
276- return new LibGit2Sharp . Repository ( repoRoot ) ;
294+ private static string ReadGitDirFromFile ( string fileName )
295+ {
296+ try
297+ {
298+ var firstLineOfFile = File . ReadLines ( fileName ) . FirstOrDefault ( ) ;
299+ return Regex . Replace ( firstLineOfFile , "^gitdir: *" , String . Empty ) ;
300+ }
301+ catch
302+ {
303+ return String . Empty ;
304+ }
277305 }
278306
307+
279308 private static Version GetAssemblyVersion ( Version version , VersionOptions versionOptions )
280309 {
281310 // If there is no repo, "version" could have uninitialized components (-1).
0 commit comments