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
Expand file tree
/
Copy pathGitHubContextService.cs
More file actions
611 lines (522 loc) · 22.4 KB
/
GitHubContextService.cs
File metadata and controls
611 lines (522 loc) · 22.4 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Windows;
using System.Globalization;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using GitHub.Exports;
using GitHub.Extensions;
using GitHub.Primitives;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextManager.Interop;
using LibGit2Sharp;
using Task = System.Threading.Tasks.Task;
namespace GitHub.Services
{
[Export(typeof(IGitHubContextService))]
public class GitHubContextService : IGitHubContextService
{
readonly IGitHubServiceProvider serviceProvider;
readonly IGitService gitService;
readonly IVSServices vsServices;
readonly Lazy<IVsTextManager2> textManager;
// USERID_REGEX = /[a-z0-9][a-z0-9\-\_]*/i
const string owner = "(?<owner>[a-zA-Z0-9][a-zA-Z0-9-_]*)";
// REPO_REGEX = /(?:\w|\.|\-)+/i
// This supports "_" for legacy superfans with logins that still contain "_".
const string repo = @"(?<repo>(?:\w|\.|\-)+)";
//BRANCH_REGEX = /[^\/]+(\/[^\/]+)?/
const string branch = @"(?<branch>[^./ ~^:?*\[\\][^/ ~^:?*\[\\]*(/[^./ ~^:?*\[\\][^/ ~^:?*\[\\]*)*)";
const string pull = "(?<pull>[0-9]+)";
const string issue = "(?<issue>[0-9]+)";
static readonly string tree = $"^{repo}/(?<tree>[^ ]+)";
static readonly string blobName = $"^{repo}/(?<blobName>[^ /]+)";
static readonly Regex windowTitleRepositoryRegex = new Regex($"^(GitHub - )?{owner}/{repo}(: .*)? - ", RegexOptions.Compiled);
static readonly Regex windowTitleBranchRegex = new Regex($"^(GitHub - )?{owner}/{repo} at {branch} ", RegexOptions.Compiled);
static readonly Regex windowTitlePullRequestRegex = new Regex($" · Pull Request #{pull} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
static readonly Regex windowTitleIssueRegex = new Regex($" · Issue #{issue} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
static readonly Regex windowTitleBlobRegex = new Regex($"{blobName} at {branch} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
static readonly Regex windowTitleTreeRegex = new Regex($"{tree} at {branch} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
static readonly Regex urlLineRegex = new Regex($"#L(?<line>[0-9]+)(-L(?<lineEnd>[0-9]+))?$", RegexOptions.Compiled);
static readonly Regex urlBlobRegex = new Regex($"blob/(?<treeish>[^/]+(/[^/]+)*)/(?<blobName>[^/#]+)", RegexOptions.Compiled);
static readonly Regex treeishCommitRegex = new Regex($"(?<commit>[a-z0-9]{{40}})(/(?<tree>.+))?", RegexOptions.Compiled);
static readonly Regex treeishBranchRegex = new Regex($"(?<branch>master)(/(?<tree>.+))?", RegexOptions.Compiled);
static readonly Regex tempFileObjectishRegex = new Regex(@"\\TFSTemp\\[^\\]*[.](?<objectish>[a-z0-9]{8})[.][^.\\]*$", RegexOptions.Compiled);
[ImportingConstructor]
public GitHubContextService(IGitHubServiceProvider serviceProvider, IGitService gitService, IVSServices vsServices)
{
this.serviceProvider = serviceProvider;
this.gitService = gitService;
this.vsServices = vsServices;
textManager = new Lazy<IVsTextManager2>(() => serviceProvider.GetService<SVsTextManager, IVsTextManager2>());
}
/// <inheritdoc/>
public void TryNavigateToContext(string repositoryDir, GitHubContext context)
{
if (context?.LinkType == LinkType.Blob)
{
var (commitish, path, commitSha) = ResolveBlob(repositoryDir, context);
if (commitish == null && path == null)
{
var message = string.Format(CultureInfo.CurrentCulture, Resources.CouldntFindCorrespondingFile, context.Url);
vsServices.ShowMessageBoxInfo(message);
return;
}
var hasChanges = HasChangesInWorkingDirectory(repositoryDir, commitish, path);
if (hasChanges)
{
var message = string.Format(CultureInfo.CurrentCulture, Resources.ChangesInWorkingDirectoryMessage, commitish);
vsServices.ShowMessageBoxInfo(message);
}
TryOpenFile(repositoryDir, context);
}
}
/// <inheritdoc/>
public GitHubContext FindContextFromClipboard()
{
var text = Clipboard.GetText(TextDataFormat.Text);
return FindContextFromUrl(text);
}
/// <inheritdoc/>
public GitHubContext FindContextFromUrl(string url)
{
var uri = new UriString(url);
if (!uri.IsValidUri)
{
return null;
}
if (!uri.IsHypertextTransferProtocol)
{
return null;
}
var context = new GitHubContext
{
Host = uri.Host,
Owner = uri.Owner,
RepositoryName = uri.RepositoryName,
Url = uri
};
if (uri.Owner == null)
{
context.LinkType = LinkType.Unknown;
return context;
}
if (uri.RepositoryName == null)
{
context.LinkType = LinkType.Unknown;
return context;
}
var repositoryUrl = uri.ToRepositoryUrl().ToString();
if (string.Equals(url, repositoryUrl, StringComparison.OrdinalIgnoreCase) ||
string.Equals(url, repositoryUrl + ".git", StringComparison.OrdinalIgnoreCase))
{
context.LinkType = LinkType.Repository;
return context;
}
var repositoryPrefix = repositoryUrl + "/";
if (!url.StartsWith(repositoryPrefix, StringComparison.OrdinalIgnoreCase))
{
return context;
}
var subpath = url.Substring(repositoryPrefix.Length);
(context.Line, context.LineEnd) = FindLine(subpath);
context.PullRequest = FindPullRequest(url);
var match = urlBlobRegex.Match(subpath);
if (match.Success)
{
context.TreeishPath = match.Groups["treeish"].Value;
context.BlobName = match.Groups["blobName"].Value;
context.LinkType = LinkType.Blob;
return context;
}
return context;
}
/// <inheritdoc/>
public GitHubContext FindContextFromBrowser()
{
return
FindWindowTitlesForClass("Chrome_WidgetWin_1") // Chrome
.Concat(FindWindowTitlesForClass("MozillaWindowClass")) // Firefox
.Select(FindContextFromWindowTitle).Where(x => x != null)
.FirstOrDefault();
}
/// <inheritdoc/>
public Uri ToRepositoryUrl(GitHubContext context)
{
var builder = new UriBuilder("https", context.Host ?? "github.com");
builder.Path = $"{context.Owner}/{context.RepositoryName}";
return builder.Uri;
}
/// <inheritdoc/>
public GitHubContext FindContextFromWindowTitle(string windowTitle)
{
var match = windowTitleBlobRegex.Match(windowTitle);
if (match.Success)
{
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
BranchName = match.Groups["branch"].Value,
BlobName = match.Groups["blobName"].Value
};
}
match = windowTitleTreeRegex.Match(windowTitle);
if (match.Success)
{
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
BranchName = match.Groups["branch"].Value,
TreeishPath = $"{match.Groups["branch"].Value}/{match.Groups["tree"].Value}"
};
}
match = windowTitleRepositoryRegex.Match(windowTitle);
if (match.Success)
{
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
};
}
match = windowTitleBranchRegex.Match(windowTitle);
if (match.Success)
{
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
BranchName = match.Groups["branch"].Value,
};
}
match = windowTitleBranchesRegex.Match(windowTitle);
if (match.Success)
{
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value
};
}
match = windowTitlePullRequestRegex.Match(windowTitle);
if (match.Success)
{
int.TryParse(match.Groups["pull"].Value, out int pullRequest);
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
PullRequest = pullRequest
};
}
match = windowTitleIssueRegex.Match(windowTitle);
if (match.Success)
{
int.TryParse(match.Groups["issue"].Value, out int issue);
return new GitHubContext
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
Issue = issue
};
}
return null;
}
/// <inheritdoc/>
public bool TryOpenFile(string repositoryDir, GitHubContext context)
{
var (commitish, path, isSha) = ResolveBlob(repositoryDir, context);
if (path == null)
{
return false;
}
var fullPath = Path.Combine(repositoryDir, path.Replace('/', '\\'));
var textView = OpenDocument(fullPath);
SetSelection(textView, context);
return true;
}
/// <inheritdoc/>
public (string commitish, string path, string commitSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin")
{
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
Guard.ArgumentNotNull(context, nameof(context));
using (var repository = gitService.GetRepository(repositoryDir))
{
if (context.TreeishPath == null)
{
// Blobs without a TreeishPath aren't currently supported
return (null, null, null);
}
if (context.BlobName == null)
{
// Not a blob
return (null, null, null);
}
var objectishPath = $"{context.TreeishPath}/{context.BlobName}";
var objectish = ToObjectish(objectishPath);
var (commitSha, pathSha) = objectish.First();
if (ObjectId.TryParse(commitSha, out ObjectId objectId) && repository.Lookup(objectId) != null)
{
if (repository.Lookup($"{commitSha}:{pathSha}") != null)
{
return (commitSha, pathSha, commitSha);
}
}
foreach (var (commitish, path) in objectish)
{
var resolveRefs = new[] { $"refs/remotes/{remoteName}/{commitish}", $"refs/tags/{commitish}" };
foreach (var resolveRef in resolveRefs)
{
var commit = repository.Lookup(resolveRef);
if (commit != null)
{
var blob = repository.Lookup($"{resolveRef}:{path}");
if (blob != null)
{
return (resolveRef, path, commit.Sha);
}
// Resolved commitish but not path
return (resolveRef, null, commit.Sha);
}
}
}
return (null, null, null);
}
IEnumerable<(string commitish, string path)> ToObjectish(string treeishPath)
{
var index = 0;
while ((index = treeishPath.IndexOf('/', index + 1)) != -1)
{
var commitish = treeishPath.Substring(0, index);
var path = treeishPath.Substring(index + 1);
yield return (commitish, path);
}
}
}
/// <inheritdoc/>
public string FindObjectishForTFSTempFile(string tempFile)
{
var match = tempFileObjectishRegex.Match(tempFile);
if (match.Success)
{
return match.Groups["objectish"].Value;
}
return null;
}
/// <inheritdoc/>
public (string commitSha, string blobPath) ResolveBlobFromHistory(string repositoryDir, string objectish)
{
using (var repo = gitService.GetRepository(repositoryDir))
{
var blob = repo.Lookup<Blob>(objectish);
if (blob == null)
{
return (null, null);
}
foreach (var commit in repo.Commits)
{
var trees = new Stack<Tree>();
trees.Push(commit.Tree);
while (trees.Count > 0)
{
foreach (var treeEntry in trees.Pop())
{
if (treeEntry.Target == blob)
{
return (commit.Sha, treeEntry.Path);
}
if (treeEntry.TargetType == TreeEntryTargetType.Tree)
{
trees.Push((Tree)treeEntry.Target);
}
}
}
}
return (null, null);
}
}
/// <inheritdoc/>
public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string path)
{
Guard.ArgumentNotNull(path, nameof(repositoryDir));
Guard.ArgumentNotNull(path, nameof(commitish));
Guard.ArgumentIsGitPath(path, nameof(path));
using (var repo = gitService.GetRepository(repositoryDir))
{
var commit = repo.Lookup<Commit>(commitish);
var paths = new[] { path };
return repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, paths).Count() > 0;
}
}
/// <inheritdoc/>
public async Task<bool> TryAnnotateFile(string repositoryDir, string currentBranch, GitHubContext context)
{
var (commitish, path, commitSha) = ResolveBlob(repositoryDir, context);
if (path == null)
{
return false;
}
if (!AnnotateFile(repositoryDir, currentBranch, path, commitSha))
{
return false;
}
if (context.Line != null)
{
await Task.Delay(1000);
var activeView = FindActiveView();
SetSelection(activeView, context);
}
return true;
}
IVsTextView FindActiveView()
{
if (!ErrorHandler.Succeeded(textManager.Value.GetActiveView2(1, null, (uint)_VIEWFRAMETYPE.vftToolWindow, out IVsTextView textView)))
{
return null;
}
return textView;
}
/// <summary>
/// Call AnnotateFile of the IGitExt2 service if it can be found.
/// </summary>
/// <remarks>
/// The IGitExt2 interface was introduced in an update of Visual Studio 2017.
/// The <see cref="branchName"/> must exist but doesn't appear to be used in the UI.
/// </remarks>
/// <param name="repositoryPath">Path of the target repository</param>
/// <param name="branchName">A branch of the target repository</param>
/// <param name="relativePath">A path the the target blob</param>
/// <param name="versionSha">The commit version of the blob</param>
/// <returns>True if AnnotateFile functionality is available.</returns>
bool AnnotateFile(string repositoryPath, string branchName, string relativePath, string versionSha)
{
var serviceType = Type.GetType("Microsoft.VisualStudio.TeamFoundation.Git.Extensibility.IGitExt2, Microsoft.TeamFoundation.Git.Provider", false);
if (serviceType == null)
{
return false;
}
var service = serviceProvider.GetService(serviceType);
if (service == null)
{
return false;
}
try
{
Invoke(service, "AnnotateFile", repositoryPath, branchName, relativePath, versionSha);
return true;
}
catch (Exception)
{
return false;
}
void Invoke<T1, T2, T3, T4>(object target, string method, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
var action = (Action<T1, T2, T3, T4>)Delegate.CreateDelegate(typeof(Action<T1, T2, T3, T4>), target, method);
action.Invoke(arg1, arg2, arg3, arg4);
}
}
static void SetSelection(IVsTextView textView, GitHubContext context)
{
var line = context.Line;
var lineEnd = context.LineEnd ?? line;
if (line != null)
{
ErrorHandler.ThrowOnFailure(textView.GetBuffer(out IVsTextLines buffer));
buffer.GetLengthOfLine(lineEnd.Value - 1, out int lineEndLength);
ErrorHandler.ThrowOnFailure(textView.SetSelection(line.Value - 1, 0, lineEnd.Value - 1, lineEndLength));
ErrorHandler.ThrowOnFailure(textView.CenterLines(line.Value - 1, lineEnd.Value - line.Value + 1));
}
}
IVsTextView OpenDocument(string fullPath)
{
var logicalView = VSConstants.LOGVIEWID.TextView_guid;
IVsUIHierarchy hierarchy;
uint itemID;
IVsWindowFrame windowFrame;
IVsTextView view;
VsShellUtilities.OpenDocument(serviceProvider, fullPath, logicalView, out hierarchy, out itemID, out windowFrame, out view);
return view;
}
static (int? lineStart, int? lineEnd) FindLine(UriString gitHubUrl)
{
var url = gitHubUrl.ToString();
var match = urlLineRegex.Match(url);
if (match.Success)
{
int.TryParse(match.Groups["line"].Value, out int line);
var lineEndGroup = match.Groups["lineEnd"];
if (string.IsNullOrEmpty(lineEndGroup.Value))
{
return (line, null);
}
int.TryParse(lineEndGroup.Value, out int lineEnd);
return (line, lineEnd);
}
return (null, null);
}
static int? FindPullRequest(UriString gitHubUrl)
{
var pullRequest = FindSubPath(gitHubUrl, "/pull/")?.Split('/').First();
if (pullRequest == null)
{
return null;
}
if (!int.TryParse(pullRequest, out int number))
{
return null;
}
return number;
}
static string FindSubPath(UriString gitHubUrl, string matchPath)
{
var url = gitHubUrl.ToString();
var prefix = gitHubUrl.ToRepositoryUrl() + matchPath;
if (!url.StartsWith(prefix))
{
return null;
}
var endIndex = url.IndexOf('#');
if (endIndex == -1)
{
endIndex = gitHubUrl.Length;
}
var path = url.Substring(prefix.Length, endIndex - prefix.Length);
return path;
}
static IEnumerable<string> FindWindowTitlesForClass(string className)
{
IntPtr handleWin = IntPtr.Zero;
while (IntPtr.Zero != (handleWin = User32.FindWindowEx(IntPtr.Zero, handleWin, className, IntPtr.Zero)))
{
// Allocate correct string length first
int length = User32.GetWindowTextLength(handleWin);
if (length == 0)
{
continue;
}
var titleBuilder = new StringBuilder(length + 1);
User32.GetWindowText(handleWin, titleBuilder, titleBuilder.Capacity);
yield return titleBuilder.ToString();
}
}
static class User32
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetWindowTextLength(IntPtr hWnd);
}
}
}