@@ -40,50 +40,21 @@ public class GitRepository : IDisposable
4040 /// </returns>
4141 public static GitRepository ? Create ( string ? workingDirectory )
4242 {
43- // Search for the top-level directory of the current git repository. This is the directory
44- // which contains a directory of file named .git.
45- // Loop until Path.GetDirectoryName returns null; in this case, we've reached the root of
46- // the file system (and we're not in a git repository).
47- while ( ! string . IsNullOrEmpty ( workingDirectory )
48- && ! File . Exists ( Path . Combine ( workingDirectory , GitDirectoryName ) )
49- && ! Directory . Exists ( Path . Combine ( workingDirectory , GitDirectoryName ) ) )
50- {
51- workingDirectory = Path . GetDirectoryName ( workingDirectory ) ;
52- }
53-
54- if ( string . IsNullOrEmpty ( workingDirectory ) )
55- {
56- return null ;
57- }
58-
59- var gitDirectory = Path . Combine ( workingDirectory , GitDirectoryName ) ;
60-
61- if ( File . Exists ( gitDirectory ) )
62- {
63- // This is a worktree, and the path to the git directory is stored in the .git file
64- var worktreeConfig = File . ReadAllText ( gitDirectory ) ;
65-
66- var gitDirStart = worktreeConfig . IndexOf ( "gitdir: " ) ;
67- var gitDirEnd = worktreeConfig . IndexOf ( "\n " , gitDirStart ) ;
68-
69- gitDirectory = worktreeConfig . Substring ( gitDirStart + 8 , gitDirEnd - gitDirStart - 8 ) ;
70- }
71-
72- if ( ! Directory . Exists ( gitDirectory ) )
43+ if ( ! GitContext . TryFindGitPaths ( workingDirectory , out string ? gitDirectory , out string ? workingTreeDirectory , out string ? workingTreeRelativePath ) )
7344 {
7445 return null ;
7546 }
7647
77- var commonDirectory = gitDirectory ;
78- var commonDirFile = Path . Combine ( gitDirectory , "commondir" ) ;
48+ string commonDirectory = gitDirectory ;
49+ string commonDirFile = Path . Combine ( gitDirectory , "commondir" ) ;
7950
8051 if ( File . Exists ( commonDirFile ) )
8152 {
8253 var commonDirectoryRelativePath = File . ReadAllText ( commonDirFile ) . Trim ( '\n ' ) ;
8354 commonDirectory = Path . Combine ( gitDirectory , commonDirectoryRelativePath ) ;
8455 }
8556
86- var objectDirectory = Path . Combine ( commonDirectory , "objects" ) ;
57+ string objectDirectory = Path . Combine ( commonDirectory , "objects" ) ;
8758
8859 return new GitRepository ( workingDirectory ! , gitDirectory , commonDirectory , objectDirectory ) ;
8960 }
0 commit comments