This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGitClient.cs
553 lines (483 loc) · 20.7 KB
/
GitClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using GitHub.Extensions;
using GitHub.Models;
using GitHub.Primitives;
using LibGit2Sharp;
using GitHub.Logging;
using Serilog;
namespace GitHub.Services
{
[Export(typeof(IGitClient))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class GitClient : IGitClient
{
const string defaultOriginName = "origin";
static readonly ILogger log = LogManager.ForContext<GitClient>();
readonly IGitService gitService;
readonly IGitHubCredentialProvider credentialProvider;
readonly PullOptions pullOptions;
readonly PushOptions pushOptions;
readonly FetchOptions fetchOptions;
[ImportingConstructor]
public GitClient(IGitHubCredentialProvider credentialProvider, IGitService gitService)
{
Guard.ArgumentNotNull(credentialProvider, nameof(credentialProvider));
Guard.ArgumentNotNull(gitService, nameof(gitService));
this.gitService = gitService;
this.credentialProvider = credentialProvider;
pushOptions = new PushOptions { CredentialsProvider = credentialProvider.HandleCredentials };
fetchOptions = new FetchOptions { CredentialsProvider = credentialProvider.HandleCredentials };
pullOptions = new PullOptions
{
FetchOptions = fetchOptions,
MergeOptions = new MergeOptions(),
};
}
public Task Pull(IRepository repository)
{
Guard.ArgumentNotNull(repository, nameof(repository));
return Task.Factory.StartNew(() =>
{
var signature = repository.Config.BuildSignature(DateTimeOffset.UtcNow);
#pragma warning disable 0618 // TODO: Replace `Network.Pull` with `Commands.Pull`.
repository.Network.Pull(signature, pullOptions);
#pragma warning restore 0618
});
}
public Task Push(IRepository repository, string branchName, string remoteName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(branchName, nameof(branchName));
Guard.ArgumentNotEmptyString(remoteName, nameof(remoteName));
return Task.Factory.StartNew(() =>
{
if (repository.Head?.Commits != null && repository.Head.Commits.Any())
{
var remote = repository.Network.Remotes[remoteName];
var remoteRef = IsCanonical(branchName) ? branchName : @"refs/heads/" + branchName;
repository.Network.Push(remote, "HEAD", remoteRef, pushOptions);
}
});
}
public Task Fetch(IRepository repository, string remoteName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(remoteName, nameof(remoteName));
return Task.Factory.StartNew(() =>
{
try
{
var remote = repository.Network.Remotes[remoteName];
#pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
repository.Network.Fetch(remote, fetchOptions);
#pragma warning restore 0618
}
catch (Exception ex)
{
log.Error(ex, "Failed to fetch");
#if DEBUG
throw;
#endif
}
});
}
public Task Fetch(IRepository repo, UriString cloneUrl, params string[] refspecs)
{
foreach (var remote in repo.Network.Remotes)
{
if (UriString.RepositoryUrlsAreEqual(new UriString(remote.Url), cloneUrl))
{
return Fetch(repo, remote.Name, refspecs);
}
}
return Task.Factory.StartNew(() =>
{
try
{
var remoteName = cloneUrl.Owner;
var remoteUri = cloneUrl.ToRepositoryUrl();
var removeRemote = false;
if (repo.Network.Remotes[remoteName] != null)
{
// If a remote with this neme already exists, use a unique name and remove remote afterwards
remoteName = cloneUrl.Owner + "-" + Guid.NewGuid();
removeRemote = true;
}
var remote = repo.Network.Remotes.Add(remoteName, remoteUri.ToString());
try
{
#pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
repo.Network.Fetch(remote, refspecs, fetchOptions);
#pragma warning restore 0618
}
finally
{
if (removeRemote)
{
repo.Network.Remotes.Remove(remoteName);
}
}
}
catch (Exception ex)
{
log.Error(ex, "Failed to fetch");
#if DEBUG
throw;
#endif
}
});
}
public Task Fetch(IRepository repository, string remoteName, params string[] refspecs)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(remoteName, nameof(remoteName));
return Task.Factory.StartNew(() =>
{
try
{
var remote = repository.Network.Remotes[remoteName];
#pragma warning disable 0618 // TODO: Replace `Network.Fetch` with `Commands.Fetch`.
repository.Network.Fetch(remote, refspecs, fetchOptions);
#pragma warning restore 0618
}
catch (Exception ex)
{
log.Error(ex, "Failed to fetch");
#if DEBUG
throw;
#endif
}
});
}
public Task<IDictionary<string, string>> ListReferences(IRepository repo, string remoteName)
{
return Task.Run<IDictionary<string, string>>(() =>
{
var dictionary = new Dictionary<string, string>();
var remote = repo.Network.Remotes[remoteName];
var refs = repo.Network.ListReferences(remote, credentialProvider.HandleCredentials);
foreach (var reference in refs)
{
dictionary[reference.CanonicalName] = reference.TargetIdentifier;
}
return dictionary;
});
}
public Task Checkout(IRepository repository, string branchName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(branchName, nameof(branchName));
return Task.Factory.StartNew(() =>
{
#pragma warning disable 0618 // TODO: Replace `IRepository.Checkout` with `Commands.Checkout`.
repository.Checkout(branchName);
#pragma warning restore 0618
});
}
public Task CreateBranch(IRepository repository, string branchName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(branchName, nameof(branchName));
return Task.Factory.StartNew(() =>
{
repository.CreateBranch(branchName);
});
}
public Task<TreeChanges> Compare(
IRepository repository,
string sha1,
string sha2,
bool detectRenames)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
return Task.Factory.StartNew(() =>
{
var options = new CompareOptions
{
Similarity = detectRenames ? SimilarityOptions.Renames : SimilarityOptions.None
};
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);
if (commit1 != null && commit2 != null)
{
return repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, options);
}
else
{
return null;
}
});
}
public Task<Patch> Compare(
IRepository repository,
string sha1,
string sha2,
string path)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
Guard.ArgumentNotEmptyString(path, nameof(path));
return Task.Factory.StartNew(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);
if (commit1 != null && commit2 != null)
{
return repository.Diff.Compare<Patch>(
commit1.Tree,
commit2.Tree,
new[] { path });
}
else
{
return null;
}
});
}
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
Guard.ArgumentNotEmptyString(path, nameof(path));
return Task.Factory.StartNew(() =>
{
var commit1 = repository.Lookup<Commit>(sha1);
var commit2 = repository.Lookup<Commit>(sha2);
var treeChanges = repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree);
var normalizedPath = path.Replace("/", "\\");
var renamed = treeChanges.FirstOrDefault(x => x.Path == normalizedPath);
var oldPath = renamed?.OldPath ?? path;
if (commit1 != null)
{
var contentStream = contents != null ? new MemoryStream(contents) : new MemoryStream();
var blob1 = commit1[oldPath]?.Target as Blob ?? repository.ObjectDatabase.CreateBlob(new MemoryStream());
var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, path);
return repository.Diff.Compare(blob1, blob2);
}
return null;
});
}
public Task<T> GetConfig<T>(IRepository repository, string key)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(key, nameof(key));
return Task.Factory.StartNew(() =>
{
var result = repository.Config.Get<T>(key);
return result != null ? result.Value : default(T);
});
}
public Task SetConfig(IRepository repository, string key, string value)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(key, nameof(key));
Guard.ArgumentNotEmptyString(value, nameof(value));
return Task.Factory.StartNew(() =>
{
repository.Config.Set(key, value);
});
}
public Task SetRemote(IRepository repository, string remoteName, Uri url)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(remoteName, nameof(remoteName));
return Task.Factory.StartNew(() =>
{
repository.Config.Set("remote." + remoteName + ".url", url.ToString());
repository.Config.Set("remote." + remoteName + ".fetch", "+refs/heads/*:refs/remotes/" + remoteName + "/*");
});
}
public Task SetTrackingBranch(IRepository repository, string branchName, string remoteName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(branchName, nameof(branchName));
Guard.ArgumentNotEmptyString(remoteName, nameof(remoteName));
return Task.Factory.StartNew(() =>
{
var remoteBranchName = IsCanonical(remoteName) ? remoteName : "refs/remotes/" + remoteName + "/" + branchName;
var remoteBranch = repository.Branches[remoteBranchName];
// if it's null, it's because nothing was pushed
if (remoteBranch != null)
{
var localBranchName = IsCanonical(branchName) ? branchName : "refs/heads/" + branchName;
var localBranch = repository.Branches[localBranchName];
repository.Branches.Update(localBranch, b => b.TrackedBranch = remoteBranch.CanonicalName);
}
});
}
public Task UnsetConfig(IRepository repository, string key)
{
Guard.ArgumentNotEmptyString(key, nameof(key));
return Task.Factory.StartNew(() =>
{
repository.Config.Unset(key);
});
}
public Task<Remote> GetHttpRemote(IRepository repo, string remote)
{
Guard.ArgumentNotNull(repo, nameof(repo));
Guard.ArgumentNotEmptyString(remote, nameof(remote));
return Task.Factory.StartNew(() =>
{
var uri = gitService.GetRemoteUri(repo, remote);
var remoteName = uri.IsHypertextTransferProtocol ? remote : remote + "-http";
var ret = repo.Network.Remotes[remoteName];
if (ret == null)
ret = repo.Network.Remotes.Add(remoteName, UriString.ToUriString(uri.ToRepositoryUrl()));
return ret;
});
}
public Task<string> ExtractFile(IRepository repository, string commitSha, string fileName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
Guard.ArgumentNotEmptyString(fileName, nameof(fileName));
return Task.Factory.StartNew(() =>
{
var commit = repository.Lookup<Commit>(commitSha);
if (commit == null)
{
throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + ".");
}
var blob = commit[fileName]?.Target as Blob;
return blob?.GetContentText();
});
}
public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha, string fileName)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
Guard.ArgumentNotEmptyString(fileName, nameof(fileName));
return Task.Factory.StartNew(() =>
{
var commit = repository.Lookup<Commit>(commitSha);
if (commit == null)
{
throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + ".");
}
var blob = commit[fileName]?.Target as Blob;
if (blob != null)
{
using (var m = new MemoryStream())
{
var content = blob.GetContentStream();
content.CopyTo(m);
return m.ToArray();
}
}
return null;
});
}
public Task<bool> IsModified(IRepository repository, string path, byte[] contents)
{
Guard.ArgumentNotNull(repository, nameof(repository));
Guard.ArgumentNotEmptyString(path, nameof(path));
return Task.Factory.StartNew(() =>
{
if (repository.RetrieveStatus(path) == FileStatus.Unaltered)
{
var treeEntry = repository.Head[path];
if (treeEntry?.TargetType != TreeEntryTargetType.Blob)
{
return false;
}
var blob1 = (Blob)treeEntry.Target;
using (var s = contents != null ? new MemoryStream(contents) : new MemoryStream())
{
var blob2 = repository.ObjectDatabase.CreateBlob(s, path);
var diff = repository.Diff.Compare(blob1, blob2);
return diff.LinesAdded != 0 || diff.LinesDeleted != 0;
}
}
return true;
});
}
public async Task<string> GetPullRequestMergeBase(IRepository repo,
UriString targetCloneUrl, string baseSha, string headSha, string baseRef, int pullNumber)
{
Guard.ArgumentNotNull(repo, nameof(repo));
Guard.ArgumentNotNull(targetCloneUrl, nameof(targetCloneUrl));
Guard.ArgumentNotEmptyString(baseRef, nameof(baseRef));
var headCommit = repo.Lookup<Commit>(headSha);
if (headCommit == null)
{
// The PR base branch might no longer exist, so we fetch using `refs/pull/<PR>/head` first.
// This will often fetch the base commits, even when the base branch no longer exists.
var headRef = $"refs/pull/{pullNumber}/head";
await Fetch(repo, targetCloneUrl, headRef);
headCommit = repo.Lookup<Commit>(headSha);
if (headCommit == null)
{
throw new NotFoundException($"Couldn't find {headSha} after fetching from {targetCloneUrl}:{headRef}.");
}
}
var baseCommit = repo.Lookup<Commit>(baseSha);
if (baseCommit == null)
{
await Fetch(repo, targetCloneUrl, baseRef);
baseCommit = repo.Lookup<Commit>(baseSha);
if (baseCommit == null)
{
throw new NotFoundException($"Couldn't find {baseSha} after fetching from {targetCloneUrl}:{baseRef}.");
}
}
var mergeBaseCommit = repo.ObjectDatabase.FindMergeBase(baseCommit, headCommit);
if (mergeBaseCommit == null)
{
throw new NotFoundException($"Couldn't find merge base between {baseCommit} and {headCommit}.");
}
return mergeBaseCommit.Sha;
}
public Task<bool> IsHeadPushed(IRepository repo)
{
Guard.ArgumentNotNull(repo, nameof(repo));
return Task.Factory.StartNew(() =>
{
return repo.Head.TrackingDetails.AheadBy == 0;
});
}
public Task<IReadOnlyList<CommitMessage>> GetMessagesForUniqueCommits(
IRepository repo,
string baseBranch,
string compareBranch,
int maxCommits)
{
return Task.Factory.StartNew(() =>
{
var baseCommit = repo.Lookup<Commit>(baseBranch);
var compareCommit = repo.Lookup<Commit>(compareBranch);
if (baseCommit == null || compareCommit == null)
{
var missingBranch = baseCommit == null ? baseBranch : compareBranch;
throw new NotFoundException(missingBranch);
}
var mergeCommit = repo.ObjectDatabase.FindMergeBase(baseCommit, compareCommit);
var commitFilter = new CommitFilter
{
IncludeReachableFrom = baseCommit,
ExcludeReachableFrom = mergeCommit,
};
var commits = repo.Commits
.QueryBy(commitFilter)
.Take(maxCommits)
.Select(c => new CommitMessage(c.Message))
.ToList();
return (IReadOnlyList<CommitMessage>)commits;
});
}
static bool IsCanonical(string s)
{
Guard.ArgumentNotEmptyString(s, nameof(s));
return s.StartsWith("refs/", StringComparison.Ordinal);
}
}
}