Repository navigation
Expand file tree
/
Copy pathVsSourceControlTracker.cs
More file actions
245 lines (203 loc) · 9.85 KB
/
Copy pathVsSourceControlTracker.cs
File metadata and controls
245 lines (203 loc) · 9.85 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
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.Shell.Interop;
namespace NuGet.VisualStudio
{
[Export(typeof(IVsSourceControlTracker))]
internal class VsSourceControlTracker : IVsSourceControlTracker
{
private readonly TrackProjectDocumentEventListener _projectDocumentListener;
private readonly IVsTrackProjectDocuments2 _projectTracker;
private readonly ISolutionManager _solutionManager;
private readonly IFileSystemProvider _fileSystemProvider;
private readonly ISettings _solutionSettings;
private uint? _trackingCookie;
[ImportingConstructor]
public VsSourceControlTracker(ISolutionManager solutionManager, IFileSystemProvider fileSystemProvider, ISettings solutionSettings) :
this(solutionManager, fileSystemProvider, ServiceLocator.GetGlobalService<SVsTrackProjectDocuments, IVsTrackProjectDocuments2>(), solutionSettings)
{
}
public VsSourceControlTracker(
ISolutionManager solutionManager,
IFileSystemProvider fileSystemProvider,
IVsTrackProjectDocuments2 projectTracker,
ISettings solutionSettings)
{
if (projectTracker == null)
{
throw new ArgumentNullException("projectTracker");
}
_solutionManager = solutionManager;
_projectTracker = projectTracker;
_fileSystemProvider = fileSystemProvider;
_solutionSettings = solutionSettings;
_projectDocumentListener = new TrackProjectDocumentEventListener(this);
_solutionManager.SolutionOpened += OnSolutionOpened;
_solutionManager.SolutionClosed += OnSolutionClosed;
if (_solutionManager.IsSolutionOpen)
{
StartTracking();
}
}
[Import]
public Lazy<IRepositorySettings> RepositorySettings { get; set; }
public event EventHandler SolutionBoundToSourceControl = delegate { };
private void OnSolutionOpened(object sender, EventArgs e)
{
StartTracking();
}
private void OnSolutionClosed(object sender, EventArgs e)
{
StopTracking();
}
private bool IsTracking
{
get
{
return _trackingCookie != null;
}
}
private void StartTracking()
{
// don't track again if already tracking
if (IsTracking)
{
return;
}
// don't do anything if user explicitly disables source control integration
if (_solutionSettings.IsSourceControlDisabled())
{
return;
}
uint cookie;
_projectTracker.AdviseTrackProjectDocumentsEvents(_projectDocumentListener, out cookie);
_trackingCookie = cookie;
}
private void StopTracking()
{
if (!IsTracking)
{
return;
}
_projectTracker.UnadviseTrackProjectDocumentsEvents((uint)_trackingCookie);
_trackingCookie = null;
}
private void OnSourceControlBound()
{
Debug.Assert(RepositorySettings.Value != null);
if (RepositorySettings.Value == null)
{
return;
}
try
{
string solutionRepositoryPath = RepositorySettings.Value.RepositoryPath;
if (Directory.Exists(solutionRepositoryPath))
{
var activeFileSystem = _fileSystemProvider.GetFileSystem(solutionRepositoryPath) as ISourceControlFileSystem;
// only proceed if the file system in use is a source-control file system.
if (activeFileSystem != null)
{
IEnumerable<string> allFiles = Directory.EnumerateFiles(solutionRepositoryPath, "*.*",
SearchOption.AllDirectories);
string file = allFiles.FirstOrDefault();
if (file != null && !activeFileSystem.IsSourceControlBound(file))
{
// If there are any files under the packages directory and any given file (in this case the first one we come across) is not under
// source control, bind it.
activeFileSystem.BindToSourceControl(allFiles);
}
}
}
}
finally
{
StopTracking();
// raise event
SolutionBoundToSourceControl(this, EventArgs.Empty);
}
}
private class TrackProjectDocumentEventListener : IVsTrackProjectDocumentsEvents2
{
private readonly VsSourceControlTracker _parent;
public TrackProjectDocumentEventListener(VsSourceControlTracker parent)
{
_parent = parent;
}
public int OnAfterSccStatusChanged(int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, uint[] rgdwSccStatus)
{
// The value 0x1F is the AND of the following bits.
// We want to check if any of these 5 bits is on.
// SCC_STATUS_CONTROLLED = 1,
// SCC_STATUS_CHECKEDOUT = 2,
// SCC_STATUS_OUTOTHER = 4,
// SCC_STATUS_OUTEXCLUSIVE = 8,
// SCC_STATUS_OUTMULTIPLE = 16,
if (cProjects > 0 &&
cFiles > 0 &&
rgdwSccStatus != null &&
rgdwSccStatus.Any(f => (f & 0x1F) != 0) &&
rgpszMkDocuments != null &&
rgpszMkDocuments.Any(s => s.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)))
{
_parent.OnSourceControlBound();
}
return VsConstants.S_OK;
}
#region Irrelevant members
public int OnAfterAddDirectoriesEx(int cProjects, int cDirectories, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, VSADDDIRECTORYFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnAfterAddFilesEx(int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, VSADDFILEFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnAfterRemoveDirectories(int cProjects, int cDirectories, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, VSREMOVEDIRECTORYFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnAfterRemoveFiles(int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgpszMkDocuments, VSREMOVEFILEFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnAfterRenameDirectories(int cProjects, int cDirs, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgszMkOldNames, string[] rgszMkNewNames, VSRENAMEDIRECTORYFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnAfterRenameFiles(int cProjects, int cFiles, IVsProject[] rgpProjects, int[] rgFirstIndices, string[] rgszMkOldNames, string[] rgszMkNewNames, VSRENAMEFILEFLAGS[] rgFlags)
{
return VsConstants.S_OK;
}
public int OnQueryAddDirectories(IVsProject pProject, int cDirectories, string[] rgpszMkDocuments, VSQUERYADDDIRECTORYFLAGS[] rgFlags, VSQUERYADDDIRECTORYRESULTS[] pSummaryResult, VSQUERYADDDIRECTORYRESULTS[] rgResults)
{
return VsConstants.S_OK;
}
public int OnQueryAddFiles(IVsProject pProject, int cFiles, string[] rgpszMkDocuments, VSQUERYADDFILEFLAGS[] rgFlags, VSQUERYADDFILERESULTS[] pSummaryResult, VSQUERYADDFILERESULTS[] rgResults)
{
return VsConstants.S_OK;
}
public int OnQueryRemoveDirectories(IVsProject pProject, int cDirectories, string[] rgpszMkDocuments, VSQUERYREMOVEDIRECTORYFLAGS[] rgFlags, VSQUERYREMOVEDIRECTORYRESULTS[] pSummaryResult, VSQUERYREMOVEDIRECTORYRESULTS[] rgResults)
{
return VsConstants.S_OK;
}
public int OnQueryRemoveFiles(IVsProject pProject, int cFiles, string[] rgpszMkDocuments, VSQUERYREMOVEFILEFLAGS[] rgFlags, VSQUERYREMOVEFILERESULTS[] pSummaryResult, VSQUERYREMOVEFILERESULTS[] rgResults)
{
return VsConstants.S_OK;
}
public int OnQueryRenameDirectories(IVsProject pProject, int cDirs, string[] rgszMkOldNames, string[] rgszMkNewNames, VSQUERYRENAMEDIRECTORYFLAGS[] rgFlags, VSQUERYRENAMEDIRECTORYRESULTS[] pSummaryResult, VSQUERYRENAMEDIRECTORYRESULTS[] rgResults)
{
return VsConstants.S_OK;
}
public int OnQueryRenameFiles(IVsProject pProject, int cFiles, string[] rgszMkOldNames, string[] rgszMkNewNames, VSQUERYRENAMEFILEFLAGS[] rgFlags, VSQUERYRENAMEFILERESULTS[] pSummaryResult, VSQUERYRENAMEFILERESULTS[] rgResults)
{
return VsConstants.S_OK;
}
#endregion
}
}
}