@@ -86,25 +86,8 @@ private string ResolveCurrentBranch()
86
86
87
87
private void CleanupDuplicateOrigin ( )
88
88
{
89
- var remoteToKeep = DefaultRemoteName ;
90
- using var repo = new Repository ( options . Value . GitRootPath ) ;
91
-
92
- // check that we have a remote that matches defaultRemoteName if not take the first remote
93
- if ( ! repo . Network . Remotes . Any ( remote => remote . Name . Equals ( DefaultRemoteName , StringComparison . InvariantCultureIgnoreCase ) ) )
94
- {
95
- remoteToKeep = repo . Network . Remotes . First ( ) . Name ;
96
- }
97
-
98
- var duplicateRepos = repo . Network
99
- . Remotes
100
- . Where ( remote => ! remote . Name . Equals ( remoteToKeep , StringComparison . InvariantCultureIgnoreCase ) )
101
- . Select ( remote => remote . Name ) ;
102
-
103
- // remove all remotes that are considered duplicates
104
- foreach ( var repoName in duplicateRepos )
105
- {
106
- repo . Network . Remotes . Remove ( repoName ) ;
107
- }
89
+ using IGitRepository repo = new GitRepository ( options . Value . GitRootPath ) ;
90
+ repo . CleanupDuplicateOrigin ( DefaultRemoteName ) ;
108
91
}
109
92
110
93
private void CreateDynamicRepository ( string targetBranch )
@@ -144,53 +127,10 @@ private void NormalizeGitDirectory(string targetBranch, string gitDirectory, boo
144
127
145
128
private void CloneRepository ( string repositoryUrl , string gitDirectory , AuthenticationInfo auth )
146
129
{
147
- Credentials credentials = null ;
148
-
149
- if ( auth != null )
130
+ using ( log . IndentLog ( $ "Cloning repository from url '{ repositoryUrl } '") )
150
131
{
151
- if ( ! string . IsNullOrWhiteSpace ( auth . Username ) )
152
- {
153
- log . Info ( $ "Setting up credentials using name '{ auth . Username } '") ;
154
-
155
- credentials = new UsernamePasswordCredentials
156
- {
157
- Username = auth . Username ,
158
- Password = auth . Password ?? string . Empty
159
- } ;
160
- }
161
- }
162
-
163
- try
164
- {
165
- using ( log . IndentLog ( $ "Cloning repository from url '{ repositoryUrl } '") )
166
- {
167
- var cloneOptions = new CloneOptions
168
- {
169
- Checkout = false ,
170
- CredentialsProvider = ( url , usernameFromUrl , types ) => credentials
171
- } ;
172
-
173
- var returnedPath = Repository . Clone ( repositoryUrl , gitDirectory , cloneOptions ) ;
174
- log . Info ( $ "Returned path after repository clone: { returnedPath } ") ;
175
- }
176
- }
177
- catch ( LibGit2SharpException ex )
178
- {
179
- var message = ex . Message ;
180
- if ( message . Contains ( "401" ) )
181
- {
182
- throw new Exception ( "Unauthorized: Incorrect username/password" ) ;
183
- }
184
- if ( message . Contains ( "403" ) )
185
- {
186
- throw new Exception ( "Forbidden: Possibly Incorrect username/password" ) ;
187
- }
188
- if ( message . Contains ( "404" ) )
189
- {
190
- throw new Exception ( "Not found: The repository was not found" ) ;
191
- }
192
-
193
- throw new Exception ( "There was an unknown problem with the Git repository you provided" , ex ) ;
132
+ var returnedPath = GitRepository . Clone ( repositoryUrl , gitDirectory , auth ) ;
133
+ log . Info ( $ "Returned path after repository clone: { returnedPath } ") ;
194
134
}
195
135
}
196
136
@@ -201,7 +141,7 @@ private void CloneRepository(string repositoryUrl, string gitDirectory, Authenti
201
141
private void NormalizeGitDirectory ( string gitDirectory , bool noFetch , string currentBranch , bool isDynamicRepository )
202
142
{
203
143
var authentication = options . Value . Authentication ;
204
- using var repository = new GitRepository ( ( ) => gitDirectory ) ;
144
+ using IGitRepository repository = new GitRepository ( gitDirectory ) ;
205
145
// Need to ensure the HEAD does not move, this is essentially a BugCheck
206
146
var expectedSha = repository . Head . Tip . Sha ;
207
147
var expectedBranchName = repository . Head . CanonicalName ;
@@ -218,7 +158,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
218
158
else
219
159
{
220
160
log . Info ( $ "Fetching from remote '{ remote . Name } ' using the following refspecs: { string . Join ( ", " , remote . FetchRefSpecs . Select ( r => r . Specification ) ) } .") ;
221
- repository . Commands . Fetch ( remote . Name , new string [ 0 ] , authentication . ToFetchOptions ( ) , null ) ;
161
+ repository . Commands . Fetch ( remote . Name , new string [ 0 ] , authentication , null ) ;
222
162
}
223
163
224
164
EnsureLocalBranchExistsForCurrentBranch ( repository , log , remote , currentBranch ) ;
@@ -292,7 +232,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
292
232
else if ( localBranchesWhereCommitShaIsHead . Count == 0 )
293
233
{
294
234
log . Info ( $ "No local branch pointing at the commit '{ headSha } '. Fake branch needs to be created.") ;
295
- repository . CreateFakeBranchPointingAtThePullRequestTip ( log , authentication ) ;
235
+ repository . CreateBranchForPullRequestBranch ( log , authentication ) ;
296
236
}
297
237
else
298
238
{
0 commit comments