@@ -12,36 +12,43 @@ public class GitPreparer : IGitPreparer
1212 {
1313 private readonly ILog log ;
1414 private readonly IEnvironment environment ;
15- private readonly string dynamicRepositoryLocation ;
16- private readonly Arguments arguments ;
15+ private readonly IOptions < Arguments > options ;
1716
1817 private const string DefaultRemoteName = "origin" ;
19- private string dynamicGitRepositoryPath ;
18+ private string dotGitDirectory ;
19+ private string projectRootDirectory ;
20+
21+ public string GetTargetUrl ( ) => options . Value . TargetUrl ;
22+
23+ public string GetWorkingDirectory ( ) => options . Value . TargetPath . TrimEnd ( '/' , '\\ ' ) ;
24+
25+ public string GetDotGitDirectory ( ) => dotGitDirectory ??= GetDotGitDirectoryInternal ( ) ;
26+
27+ public string GetProjectRootDirectory ( ) => projectRootDirectory ??= GetProjectRootDirectoryInternal ( ) ;
28+
29+ private bool IsDynamicGitRepository => ! string . IsNullOrWhiteSpace ( DynamicGitRepositoryPath ) ;
30+ private string DynamicGitRepositoryPath ;
2031
2132 public GitPreparer ( ILog log , IEnvironment environment , IOptions < Arguments > options )
2233 {
2334 this . log = log ?? throw new ArgumentNullException ( nameof ( log ) ) ;
24- this . environment = environment ;
25- arguments = options . Value ;
26-
27- TargetUrl = arguments . TargetUrl ;
28- WorkingDirectory = arguments . TargetPath . TrimEnd ( '/' , '\\ ' ) ;
29-
30- dynamicRepositoryLocation = arguments . DynamicRepositoryLocation ;
35+ this . environment = environment ?? throw new ArgumentNullException ( nameof ( environment ) ) ;
36+ this . options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
3137 }
3238
3339 public void Prepare ( bool normalizeGitDirectory , string currentBranch , bool shouldCleanUpRemotes = false )
3440 {
41+ var arguments = options . Value ;
3542 var authentication = new AuthenticationInfo
3643 {
3744 Username = arguments . Authentication ? . Username ,
3845 Password = arguments . Authentication ? . Password
3946 } ;
40- if ( ! string . IsNullOrWhiteSpace ( TargetUrl ) )
47+ if ( ! string . IsNullOrWhiteSpace ( GetTargetUrl ( ) ) )
4148 {
42- var tempRepositoryPath = CalculateTemporaryRepositoryPath ( TargetUrl , dynamicRepositoryLocation ) ;
49+ var tempRepositoryPath = CalculateTemporaryRepositoryPath ( GetTargetUrl ( ) , arguments . DynamicRepositoryLocation ) ;
4350
44- dynamicGitRepositoryPath = CreateDynamicRepository ( tempRepositoryPath , authentication , TargetUrl , currentBranch ) ;
51+ DynamicGitRepositoryPath = CreateDynamicRepository ( tempRepositoryPath , authentication , GetTargetUrl ( ) , currentBranch ) ;
4552 }
4653 else
4754 {
@@ -52,62 +59,49 @@ public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shoul
5259 CleanupDuplicateOrigin ( ) ;
5360 }
5461
55- NormalizeGitDirectory ( authentication , currentBranch , GetDotGitDirectory ( ) , IsDynamicGitRepository ( ) ) ;
62+ NormalizeGitDirectory ( authentication , currentBranch , GetDotGitDirectoryInternal ( ) , IsDynamicGitRepository ) ;
5663 }
5764 }
5865 }
5966
60- public TResult WithRepository < TResult > ( Func < IRepository , TResult > action )
67+ private string GetDotGitDirectoryInternal ( )
6168 {
62- using IRepository repo = new Repository ( GetDotGitDirectory ( ) ) ;
63- return action ( repo ) ;
64- }
65-
66- public string GetDotGitDirectory ( )
67- {
68- var dotGitDirectory = IsDynamicGitRepository ( ) ? dynamicGitRepositoryPath : Repository . Discover ( WorkingDirectory ) ;
69-
70- dotGitDirectory = dotGitDirectory ? . TrimEnd ( '/' , '\\ ' ) ;
71- if ( string . IsNullOrEmpty ( dotGitDirectory ) )
72- throw new DirectoryNotFoundException ( "Can't find the .git directory in " + WorkingDirectory ) ;
69+ var gitDirectory = IsDynamicGitRepository ? DynamicGitRepositoryPath : Repository . Discover ( GetWorkingDirectory ( ) ) ;
7370
74- if ( dotGitDirectory . Contains ( Path . Combine ( ".git" , "worktrees" ) ) )
75- return Directory . GetParent ( Directory . GetParent ( dotGitDirectory ) . FullName ) . FullName ;
71+ gitDirectory = gitDirectory ? . TrimEnd ( '/' , '\\ ' ) ;
72+ if ( string . IsNullOrEmpty ( gitDirectory ) )
73+ throw new DirectoryNotFoundException ( "Can't find the .git directory in " + gitDirectory ) ;
7674
77- return dotGitDirectory ;
75+ return gitDirectory . Contains ( Path . Combine ( ".git" , "worktrees" ) )
76+ ? Directory . GetParent ( Directory . GetParent ( gitDirectory ) . FullName ) . FullName
77+ : gitDirectory ;
7878 }
7979
80- public string GetProjectRootDirectory ( )
80+ public string GetProjectRootDirectoryInternal ( )
8181 {
82- log . Info ( $ "IsDynamicGitRepository: { IsDynamicGitRepository ( ) } ") ;
83- if ( IsDynamicGitRepository ( ) )
82+ log . Info ( $ "IsDynamicGitRepository: { IsDynamicGitRepository } ") ;
83+ if ( IsDynamicGitRepository )
8484 {
85- log . Info ( $ "Returning Project Root as { WorkingDirectory } ") ;
86- return WorkingDirectory ;
85+ log . Info ( $ "Returning Project Root as { GetWorkingDirectory ( ) } ") ;
86+ return GetWorkingDirectory ( ) ;
8787 }
8888
89- var dotGitDirectory = Repository . Discover ( WorkingDirectory ) ;
89+ var dotGitDirectory = Repository . Discover ( GetWorkingDirectory ( ) ) ;
9090
9191 if ( string . IsNullOrEmpty ( dotGitDirectory ) )
92- throw new DirectoryNotFoundException ( $ "Can't find the .git directory in { WorkingDirectory } ") ;
92+ throw new DirectoryNotFoundException ( $ "Can't find the .git directory in { dotGitDirectory } ") ;
9393
9494 using var repo = new Repository ( dotGitDirectory ) ;
9595 var result = repo . Info . WorkingDirectory ;
9696 log . Info ( $ "Returning Project Root from DotGitDirectory: { dotGitDirectory } - { result } ") ;
9797 return result ;
9898 }
9999
100- public string TargetUrl { get ; }
101-
102- public string WorkingDirectory { get ; }
103-
104- private bool IsDynamicGitRepository ( ) => ! string . IsNullOrWhiteSpace ( dynamicGitRepositoryPath ) ;
105-
106100 private void CleanupDuplicateOrigin ( )
107101 {
108102 var remoteToKeep = DefaultRemoteName ;
109103
110- using var repo = new Repository ( GetDotGitDirectory ( ) ) ;
104+ using var repo = new Repository ( GetDotGitDirectoryInternal ( ) ) ;
111105
112106 // check that we have a remote that matches defaultRemoteName if not take the first remote
113107 if ( ! repo . Network . Remotes . Any ( remote => remote . Name . Equals ( DefaultRemoteName , StringComparison . InvariantCultureIgnoreCase ) ) )
@@ -193,7 +187,7 @@ private void NormalizeGitDirectory(AuthenticationInfo auth, string targetBranch,
193187 using ( log . IndentLog ( $ "Normalizing git directory for branch '{ targetBranch } '") )
194188 {
195189 // Normalize (download branches) before using the branch
196- GitRepositoryHelper . NormalizeGitDirectory ( log , environment , gitDirectory , auth , arguments . NoFetch , targetBranch , isDynamicRepository ) ;
190+ GitRepositoryHelper . NormalizeGitDirectory ( log , environment , gitDirectory , auth , options . Value . NoFetch , targetBranch , isDynamicRepository ) ;
197191 }
198192 }
199193
0 commit comments