@@ -12,36 +12,43 @@ public class GitPreparer : IGitPreparer
12
12
{
13
13
private readonly ILog log ;
14
14
private readonly IEnvironment environment ;
15
- private readonly string dynamicRepositoryLocation ;
16
- private readonly Arguments arguments ;
15
+ private readonly IOptions < Arguments > options ;
17
16
18
17
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 ;
20
31
21
32
public GitPreparer ( ILog log , IEnvironment environment , IOptions < Arguments > options )
22
33
{
23
34
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 ) ) ;
31
37
}
32
38
33
39
public void Prepare ( bool normalizeGitDirectory , string currentBranch , bool shouldCleanUpRemotes = false )
34
40
{
41
+ var arguments = options . Value ;
35
42
var authentication = new AuthenticationInfo
36
43
{
37
44
Username = arguments . Authentication ? . Username ,
38
45
Password = arguments . Authentication ? . Password
39
46
} ;
40
- if ( ! string . IsNullOrWhiteSpace ( TargetUrl ) )
47
+ if ( ! string . IsNullOrWhiteSpace ( GetTargetUrl ( ) ) )
41
48
{
42
- var tempRepositoryPath = CalculateTemporaryRepositoryPath ( TargetUrl , dynamicRepositoryLocation ) ;
49
+ var tempRepositoryPath = CalculateTemporaryRepositoryPath ( GetTargetUrl ( ) , arguments . DynamicRepositoryLocation ) ;
43
50
44
- dynamicGitRepositoryPath = CreateDynamicRepository ( tempRepositoryPath , authentication , TargetUrl , currentBranch ) ;
51
+ DynamicGitRepositoryPath = CreateDynamicRepository ( tempRepositoryPath , authentication , GetTargetUrl ( ) , currentBranch ) ;
45
52
}
46
53
else
47
54
{
@@ -52,62 +59,49 @@ public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shoul
52
59
CleanupDuplicateOrigin ( ) ;
53
60
}
54
61
55
- NormalizeGitDirectory ( authentication , currentBranch , GetDotGitDirectory ( ) , IsDynamicGitRepository ( ) ) ;
62
+ NormalizeGitDirectory ( authentication , currentBranch , GetDotGitDirectoryInternal ( ) , IsDynamicGitRepository ) ;
56
63
}
57
64
}
58
65
}
59
66
60
- public TResult WithRepository < TResult > ( Func < IRepository , TResult > action )
67
+ private string GetDotGitDirectoryInternal ( )
61
68
{
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 ( ) ) ;
73
70
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 ) ;
76
74
77
- return dotGitDirectory ;
75
+ return gitDirectory . Contains ( Path . Combine ( ".git" , "worktrees" ) )
76
+ ? Directory . GetParent ( Directory . GetParent ( gitDirectory ) . FullName ) . FullName
77
+ : gitDirectory ;
78
78
}
79
79
80
- public string GetProjectRootDirectory ( )
80
+ public string GetProjectRootDirectoryInternal ( )
81
81
{
82
- log . Info ( $ "IsDynamicGitRepository: { IsDynamicGitRepository ( ) } ") ;
83
- if ( IsDynamicGitRepository ( ) )
82
+ log . Info ( $ "IsDynamicGitRepository: { IsDynamicGitRepository } ") ;
83
+ if ( IsDynamicGitRepository )
84
84
{
85
- log . Info ( $ "Returning Project Root as { WorkingDirectory } ") ;
86
- return WorkingDirectory ;
85
+ log . Info ( $ "Returning Project Root as { GetWorkingDirectory ( ) } ") ;
86
+ return GetWorkingDirectory ( ) ;
87
87
}
88
88
89
- var dotGitDirectory = Repository . Discover ( WorkingDirectory ) ;
89
+ var dotGitDirectory = Repository . Discover ( GetWorkingDirectory ( ) ) ;
90
90
91
91
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 } ") ;
93
93
94
94
using var repo = new Repository ( dotGitDirectory ) ;
95
95
var result = repo . Info . WorkingDirectory ;
96
96
log . Info ( $ "Returning Project Root from DotGitDirectory: { dotGitDirectory } - { result } ") ;
97
97
return result ;
98
98
}
99
99
100
- public string TargetUrl { get ; }
101
-
102
- public string WorkingDirectory { get ; }
103
-
104
- private bool IsDynamicGitRepository ( ) => ! string . IsNullOrWhiteSpace ( dynamicGitRepositoryPath ) ;
105
-
106
100
private void CleanupDuplicateOrigin ( )
107
101
{
108
102
var remoteToKeep = DefaultRemoteName ;
109
103
110
- using var repo = new Repository ( GetDotGitDirectory ( ) ) ;
104
+ using var repo = new Repository ( GetDotGitDirectoryInternal ( ) ) ;
111
105
112
106
// check that we have a remote that matches defaultRemoteName if not take the first remote
113
107
if ( ! repo . Network . Remotes . Any ( remote => remote . Name . Equals ( DefaultRemoteName , StringComparison . InvariantCultureIgnoreCase ) ) )
@@ -193,7 +187,7 @@ private void NormalizeGitDirectory(AuthenticationInfo auth, string targetBranch,
193
187
using ( log . IndentLog ( $ "Normalizing git directory for branch '{ targetBranch } '") )
194
188
{
195
189
// 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 ) ;
197
191
}
198
192
}
199
193
0 commit comments