This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
/
VSGitExt.cs
145 lines (126 loc) · 5.14 KB
/
VSGitExt.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
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using GitHub.Models;
using GitHub.Services;
using GitHub.Logging;
using GitHub.Extensions;
using GitHub.TeamFoundation.Services;
using Serilog;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
using Task = System.Threading.Tasks.Task;
namespace GitHub.VisualStudio.Base
{
/// <summary>
/// This service acts as an always available version of <see cref="IGitExt"/>.
/// </summary>
/// <remarks>
/// Initialization for this service will be done asynchronously and the <see cref="IGitExt" /> service will be
/// retrieved using <see cref="GetServiceAsync" />. This means the service can be constructed and subscribed to from a background thread.
/// </remarks>
public class VSGitExt : IVSGitExt
{
static readonly ILogger log = LogManager.ForContext<VSGitExt>();
readonly IServiceProvider serviceProvider;
readonly IGitService gitService;
readonly object refreshLock = new object();
IGitExt gitExt;
IReadOnlyList<LocalRepositoryModel> activeRepositories;
/// NOTE: Used by VSGitExtFactory.
public VSGitExt(IServiceProvider serviceProvider, IGitService gitService, JoinableTaskContext joinableTaskContext)
: this(serviceProvider, new VSUIContextFactory(), gitService, joinableTaskContext)
{
}
public VSGitExt(IServiceProvider serviceProvider, IVSUIContextFactory factory, IGitService gitService,
JoinableTaskContext joinableTaskContext)
{
JoinableTaskCollection = joinableTaskContext.CreateCollection();
JoinableTaskCollection.DisplayName = nameof(VSGitExt);
JoinableTaskFactory = joinableTaskContext.CreateFactory(JoinableTaskCollection);
this.serviceProvider = serviceProvider;
this.gitService = gitService;
// Start with empty array until we have a chance to initialize.
ActiveRepositories = Array.Empty<LocalRepositoryModel>();
// The IGitExt service isn't available when a TFS based solution is opened directly.
// It will become available when moving to a Git based solution (and cause a UIContext event to fire).
// NOTE: I tried using the RepositoryOpen context, but it didn't work consistently.
var context = factory.GetUIContext(new Guid(Guids.GitSccProviderId));
context.WhenActivated(() =>
{
log.Debug("WhenActivated");
JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log);
});
}
async Task InitializeAsync()
{
gitExt = await GetServiceAsync<IGitExt>();
if (gitService == null)
{
log.Error("Couldn't find IGitExt service");
return;
}
// Refresh on background thread
await Task.Run(() => RefreshActiveRepositories());
gitExt.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(gitExt.ActiveRepositories))
{
RefreshActiveRepositories();
}
};
}
public void RefreshActiveRepositories()
{
try
{
lock (refreshLock)
{
log.Debug(
"IGitExt.ActiveRepositories (#{Id}) returned {Repositories}",
gitExt.GetHashCode(),
gitExt.ActiveRepositories.Select(x => x.RepositoryPath));
ActiveRepositories = gitExt?.ActiveRepositories.Select(x => gitService.CreateLocalRepositoryModel(x.RepositoryPath)).ToList();
}
}
catch (Exception e)
{
log.Error(e, "Error refreshing repositories");
ActiveRepositories = Array.Empty<LocalRepositoryModel>();
}
}
public IReadOnlyList<LocalRepositoryModel> ActiveRepositories
{
get
{
return activeRepositories;
}
private set
{
if (value != activeRepositories)
{
log.Debug("ActiveRepositories changed to {Repositories}", value?.Select(x => x.CloneUrl));
activeRepositories = value;
ActiveRepositoriesChanged?.Invoke();
}
}
}
public void JoinTillEmpty()
{
JoinableTaskFactory.Context.Factory.Run(async () =>
{
await JoinableTaskCollection.JoinTillEmptyAsync();
});
}
async Task<T> GetServiceAsync<T>()
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
return (T)serviceProvider.GetService(typeof(T));
}
public event Action ActiveRepositoriesChanged;
JoinableTaskCollection JoinableTaskCollection { get; }
JoinableTaskFactory JoinableTaskFactory { get; }
}
}