@@ -17,7 +17,6 @@ namespace GitHub.Services
17
17
[ PartCreationPolicy ( CreationPolicy . Shared ) ]
18
18
public class GitClient : IGitClient
19
19
{
20
- const string defaultOriginName = "origin" ;
21
20
static readonly ILogger log = LogManager . ForContext < GitClient > ( ) ;
22
21
readonly IGitService gitService ;
23
22
readonly PullOptions pullOptions ;
@@ -44,12 +43,17 @@ public GitClient(IGitHubCredentialProvider credentialProvider, IGitService gitSe
44
43
public Task Pull ( IRepository repository )
45
44
{
46
45
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
47
- return Task . Factory . StartNew ( ( ) =>
46
+ return Task . Run ( ( ) =>
48
47
{
49
48
var signature = repository . Config . BuildSignature ( DateTimeOffset . UtcNow ) ;
50
- #pragma warning disable 0618 // TODO: Replace `Network.Pull` with `Commands.Pull`.
51
- repository . Network . Pull ( signature , pullOptions ) ;
52
- #pragma warning restore 0618
49
+ if ( repository is Repository repo )
50
+ {
51
+ LibGit2Sharp . Commands . Pull ( repo , signature , pullOptions ) ;
52
+ }
53
+ else
54
+ {
55
+ log . Error ( "Couldn't pull because {Variable} isn't an instance of {Type}" , nameof ( repository ) , typeof ( Repository ) ) ;
56
+ }
53
57
} ) ;
54
58
}
55
59
@@ -59,7 +63,7 @@ public Task Push(IRepository repository, string branchName, string remoteName)
59
63
Guard . ArgumentNotEmptyString ( branchName , nameof ( branchName ) ) ;
60
64
Guard . ArgumentNotEmptyString ( remoteName , nameof ( remoteName ) ) ;
61
65
62
- return Task . Factory . StartNew ( ( ) =>
66
+ return Task . Run ( ( ) =>
63
67
{
64
68
if ( repository . Head ? . Commits != null && repository . Head . Commits . Any ( ) )
65
69
{
@@ -75,14 +79,11 @@ public Task Fetch(IRepository repository, string remoteName)
75
79
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
76
80
Guard . ArgumentNotEmptyString ( remoteName , nameof ( remoteName ) ) ;
77
81
78
- return Task . Factory . StartNew ( ( ) =>
82
+ return Task . Run ( ( ) =>
79
83
{
80
84
try
81
85
{
82
- var remote = repository . Network . Remotes [ remoteName ] ;
83
- #pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
84
- repository . Network . Fetch ( remote , fetchOptions ) ;
85
- #pragma warning restore 0618
86
+ repository . Network . Fetch ( remoteName , new [ ] { "+refs/heads/*:refs/remotes/origin/*" } , fetchOptions ) ;
86
87
}
87
88
catch ( Exception ex )
88
89
{
@@ -104,7 +105,7 @@ public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs
104
105
}
105
106
}
106
107
107
- return Task . Factory . StartNew ( ( ) =>
108
+ return Task . Run ( ( ) =>
108
109
{
109
110
try
110
111
{
@@ -114,17 +115,15 @@ public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs
114
115
var removeRemote = false ;
115
116
if ( repo . Network . Remotes [ remoteName ] != null )
116
117
{
117
- // If a remote with this neme already exists, use a unique name and remove remote afterwards
118
+ // If a remote with this name already exists, use a unique name and remove remote afterwards
118
119
remoteName = cloneUrl . Owner + "-" + Guid . NewGuid ( ) ;
119
120
removeRemote = true ;
120
121
}
121
122
122
- var remote = repo . Network . Remotes . Add ( remoteName , remoteUri . ToString ( ) ) ;
123
+ repo . Network . Remotes . Add ( remoteName , remoteUri . ToString ( ) ) ;
123
124
try
124
125
{
125
- #pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
126
- repo . Network . Fetch ( remote , refspecs , fetchOptions ) ;
127
- #pragma warning restore 0618
126
+ repo . Network . Fetch ( remoteName , refspecs , fetchOptions ) ;
128
127
}
129
128
finally
130
129
{
@@ -149,14 +148,11 @@ public Task Fetch(IRepository repository, string remoteName, params string[] ref
149
148
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
150
149
Guard . ArgumentNotEmptyString ( remoteName , nameof ( remoteName ) ) ;
151
150
152
- return Task . Factory . StartNew ( ( ) =>
151
+ return Task . Run ( ( ) =>
153
152
{
154
153
try
155
154
{
156
- var remote = repository . Network . Remotes [ remoteName ] ;
157
- #pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
158
- repository . Network . Fetch ( remote , refspecs , fetchOptions ) ;
159
- #pragma warning restore 0618
155
+ repository . Network . Fetch ( remoteName , refspecs , fetchOptions ) ;
160
156
}
161
157
catch ( Exception ex )
162
158
{
@@ -173,117 +169,32 @@ public Task Checkout(IRepository repository, string branchName)
173
169
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
174
170
Guard . ArgumentNotEmptyString ( branchName , nameof ( branchName ) ) ;
175
171
176
- return Task . Factory . StartNew ( ( ) =>
177
- {
178
- #pragma warning disable 0618 // TODO: Replace `IRepository.Checkout` with `Commands.Checkout`.
179
- repository . Checkout ( branchName ) ;
180
- #pragma warning restore 0618
181
- } ) ;
182
- }
183
-
184
- public async Task < bool > CommitExists ( IRepository repository , string sha )
185
- {
186
- return await Task . Run ( ( ) => repository . Lookup < Commit > ( sha ) != null ) . ConfigureAwait ( false ) ;
187
- }
188
-
189
- public Task CreateBranch ( IRepository repository , string branchName )
190
- {
191
- Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
192
- Guard . ArgumentNotEmptyString ( branchName , nameof ( branchName ) ) ;
193
-
194
- return Task . Factory . StartNew ( ( ) =>
195
- {
196
- repository . CreateBranch ( branchName ) ;
197
- } ) ;
198
- }
199
-
200
- public Task < TreeChanges > Compare (
201
- IRepository repository ,
202
- string sha1 ,
203
- string sha2 ,
204
- bool detectRenames )
205
- {
206
- Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
207
- Guard . ArgumentNotEmptyString ( sha1 , nameof ( sha1 ) ) ;
208
- Guard . ArgumentNotEmptyString ( sha2 , nameof ( sha2 ) ) ;
209
-
210
- return Task . Factory . StartNew ( ( ) =>
172
+ return Task . Run ( ( ) =>
211
173
{
212
- var options = new CompareOptions
174
+ if ( repository is Repository repo )
213
175
{
214
- Similarity = detectRenames ? SimilarityOptions . Renames : SimilarityOptions . None
215
- } ;
216
-
217
- var commit1 = repository . Lookup < Commit > ( sha1 ) ;
218
- var commit2 = repository . Lookup < Commit > ( sha2 ) ;
219
-
220
- if ( commit1 != null && commit2 != null )
221
- {
222
- return repository . Diff . Compare < TreeChanges > ( commit1 . Tree , commit2 . Tree , options ) ;
176
+ LibGit2Sharp . Commands . Checkout ( repo , branchName ) ;
223
177
}
224
178
else
225
179
{
226
- return null ;
180
+ log . Error ( "Couldn't checkout because {Variable} isn't an instance of {Type}" , nameof ( repository ) , typeof ( Repository ) ) ;
227
181
}
228
182
} ) ;
229
183
}
230
184
231
- public Task < Patch > Compare (
232
- IRepository repository ,
233
- string sha1 ,
234
- string sha2 ,
235
- string path )
185
+ public async Task < bool > CommitExists ( IRepository repository , string sha )
236
186
{
237
- Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
238
- Guard . ArgumentNotEmptyString ( sha1 , nameof ( sha1 ) ) ;
239
- Guard . ArgumentNotEmptyString ( sha2 , nameof ( sha2 ) ) ;
240
- Guard . ArgumentNotEmptyString ( path , nameof ( path ) ) ;
241
-
242
- return Task . Factory . StartNew ( ( ) =>
243
- {
244
- var commit1 = repository . Lookup < Commit > ( sha1 ) ;
245
- var commit2 = repository . Lookup < Commit > ( sha2 ) ;
246
-
247
- if ( commit1 != null && commit2 != null )
248
- {
249
- return repository . Diff . Compare < Patch > (
250
- commit1 . Tree ,
251
- commit2 . Tree ,
252
- new [ ] { path } ) ;
253
- }
254
- else
255
- {
256
- return null ;
257
- }
258
- } ) ;
187
+ return await Task . Run ( ( ) => repository . Lookup < Commit > ( sha ) != null ) . ConfigureAwait ( false ) ;
259
188
}
260
189
261
- public Task < ContentChanges > CompareWith ( IRepository repository , string sha1 , string sha2 , string path , byte [ ] contents )
190
+ public Task CreateBranch ( IRepository repository , string branchName )
262
191
{
263
192
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
264
- Guard . ArgumentNotEmptyString ( sha1 , nameof ( sha1 ) ) ;
265
- Guard . ArgumentNotEmptyString ( sha2 , nameof ( sha1 ) ) ;
266
- Guard . ArgumentNotEmptyString ( path , nameof ( path ) ) ;
193
+ Guard . ArgumentNotEmptyString ( branchName , nameof ( branchName ) ) ;
267
194
268
- return Task . Factory . StartNew ( ( ) =>
195
+ return Task . Run ( ( ) =>
269
196
{
270
- var commit1 = repository . Lookup < Commit > ( sha1 ) ;
271
- var commit2 = repository . Lookup < Commit > ( sha2 ) ;
272
-
273
- var treeChanges = repository . Diff . Compare < TreeChanges > ( commit1 . Tree , commit2 . Tree ) ;
274
- var normalizedPath = path . Replace ( "/" , "\\ " ) ;
275
- var renamed = treeChanges . FirstOrDefault ( x => x . Path == normalizedPath ) ;
276
- var oldPath = renamed ? . OldPath ?? path ;
277
-
278
- if ( commit1 != null )
279
- {
280
- var contentStream = contents != null ? new MemoryStream ( contents ) : new MemoryStream ( ) ;
281
- var blob1 = commit1 [ oldPath ] ? . Target as Blob ?? repository . ObjectDatabase . CreateBlob ( new MemoryStream ( ) ) ;
282
- var blob2 = repository . ObjectDatabase . CreateBlob ( contentStream , path ) ;
283
- return repository . Diff . Compare ( blob1 , blob2 ) ;
284
- }
285
-
286
- return null ;
197
+ repository . CreateBranch ( branchName ) ;
287
198
} ) ;
288
199
}
289
200
@@ -292,7 +203,7 @@ public Task<T> GetConfig<T>(IRepository repository, string key)
292
203
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
293
204
Guard . ArgumentNotEmptyString ( key , nameof ( key ) ) ;
294
205
295
- return Task . Factory . StartNew ( ( ) =>
206
+ return Task . Run ( ( ) =>
296
207
{
297
208
var result = repository . Config . Get < T > ( key ) ;
298
209
return result != null ? result . Value : default ( T ) ;
@@ -305,7 +216,7 @@ public Task SetConfig(IRepository repository, string key, string value)
305
216
Guard . ArgumentNotEmptyString ( key , nameof ( key ) ) ;
306
217
Guard . ArgumentNotEmptyString ( value , nameof ( value ) ) ;
307
218
308
- return Task . Factory . StartNew ( ( ) =>
219
+ return Task . Run ( ( ) =>
309
220
{
310
221
repository . Config . Set ( key , value ) ;
311
222
} ) ;
@@ -316,7 +227,7 @@ public Task SetRemote(IRepository repository, string remoteName, Uri url)
316
227
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
317
228
Guard . ArgumentNotEmptyString ( remoteName , nameof ( remoteName ) ) ;
318
229
319
- return Task . Factory . StartNew ( ( ) =>
230
+ return Task . Run ( ( ) =>
320
231
{
321
232
repository . Config . Set ( "remote." + remoteName + ".url" , url . ToString ( ) ) ;
322
233
repository . Config . Set ( "remote." + remoteName + ".fetch" , "+refs/heads/*:refs/remotes/" + remoteName + "/*" ) ;
@@ -329,7 +240,7 @@ public Task SetTrackingBranch(IRepository repository, string branchName, string
329
240
Guard . ArgumentNotEmptyString ( branchName , nameof ( branchName ) ) ;
330
241
Guard . ArgumentNotEmptyString ( remoteName , nameof ( remoteName ) ) ;
331
242
332
- return Task . Factory . StartNew ( ( ) =>
243
+ return Task . Run ( ( ) =>
333
244
{
334
245
var remoteBranchName = IsCanonical ( remoteName ) ? remoteName : "refs/remotes/" + remoteName + "/" + branchName ;
335
246
var remoteBranch = repository . Branches [ remoteBranchName ] ;
@@ -347,7 +258,7 @@ public Task UnsetConfig(IRepository repository, string key)
347
258
{
348
259
Guard . ArgumentNotEmptyString ( key , nameof ( key ) ) ;
349
260
350
- return Task . Factory . StartNew ( ( ) =>
261
+ return Task . Run ( ( ) =>
351
262
{
352
263
repository . Config . Unset ( key ) ;
353
264
} ) ;
@@ -358,7 +269,7 @@ public Task<Remote> GetHttpRemote(IRepository repo, string remote)
358
269
Guard . ArgumentNotNull ( repo , nameof ( repo ) ) ;
359
270
Guard . ArgumentNotEmptyString ( remote , nameof ( remote ) ) ;
360
271
361
- return Task . Factory . StartNew ( ( ) =>
272
+ return Task . Run ( ( ) =>
362
273
{
363
274
var uri = gitService . GetRemoteUri ( repo , remote ) ;
364
275
var remoteName = uri . IsHypertextTransferProtocol ? remote : remote + "-http" ;
@@ -373,9 +284,9 @@ public Task<string> ExtractFile(IRepository repository, string commitSha, string
373
284
{
374
285
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
375
286
Guard . ArgumentNotEmptyString ( commitSha , nameof ( commitSha ) ) ;
376
- Guard . ArgumentNotEmptyString ( fileName , nameof ( fileName ) ) ;
287
+ Guard . ArgumentIsGitPath ( fileName , nameof ( fileName ) ) ;
377
288
378
- return Task . Factory . StartNew ( ( ) =>
289
+ return Task . Run ( ( ) =>
379
290
{
380
291
var commit = repository . Lookup < Commit > ( commitSha ) ;
381
292
if ( commit == null )
@@ -392,9 +303,9 @@ public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha,
392
303
{
393
304
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
394
305
Guard . ArgumentNotEmptyString ( commitSha , nameof ( commitSha ) ) ;
395
- Guard . ArgumentNotEmptyString ( fileName , nameof ( fileName ) ) ;
306
+ Guard . ArgumentIsGitPath ( fileName , nameof ( fileName ) ) ;
396
307
397
- return Task . Factory . StartNew ( ( ) =>
308
+ return Task . Run ( ( ) =>
398
309
{
399
310
var commit = repository . Lookup < Commit > ( commitSha ) ;
400
311
if ( commit == null )
@@ -421,9 +332,9 @@ public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha,
421
332
public Task < bool > IsModified ( IRepository repository , string path , byte [ ] contents )
422
333
{
423
334
Guard . ArgumentNotNull ( repository , nameof ( repository ) ) ;
424
- Guard . ArgumentNotEmptyString ( path , nameof ( path ) ) ;
335
+ Guard . ArgumentIsGitPath ( path , nameof ( path ) ) ;
425
336
426
- return Task . Factory . StartNew ( ( ) =>
337
+ return Task . Run ( ( ) =>
427
338
{
428
339
if ( repository . RetrieveStatus ( path ) == FileStatus . Unaltered )
429
340
{
@@ -491,7 +402,7 @@ public Task<bool> IsHeadPushed(IRepository repo)
491
402
{
492
403
Guard . ArgumentNotNull ( repo , nameof ( repo ) ) ;
493
404
494
- return Task . Factory . StartNew ( ( ) =>
405
+ return Task . Run ( ( ) =>
495
406
{
496
407
return repo . Head . TrackingDetails . AheadBy == 0 ;
497
408
} ) ;
@@ -503,7 +414,7 @@ public Task<IReadOnlyList<CommitMessage>> GetMessagesForUniqueCommits(
503
414
string compareBranch ,
504
415
int maxCommits )
505
416
{
506
- return Task . Factory . StartNew ( ( ) =>
417
+ return Task . Run ( ( ) =>
507
418
{
508
419
var baseCommit = repo . Lookup < Commit > ( baseBranch ) ;
509
420
var compareCommit = repo . Lookup < Commit > ( compareBranch ) ;
0 commit comments