forked from yysun/Git-Source-Control-Provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicSccProvider.cs
420 lines (347 loc) · 19.8 KB
/
BasicSccProvider.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
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio;
using MsVsShell = Microsoft.VisualStudio.Shell;
using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;
using Microsoft.VisualStudio.Shell;
using System.IO;
using System.Collections.Generic;
namespace GitScc
{
/////////////////////////////////////////////////////////////////////////////
// BasicSccProvider
[MsVsShell.ProvideLoadKey("Standard", "0.1", "Git Source Control Provider", "[email protected]", 15261)]
[MsVsShell.DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\10.0Exp")]
// Register the package to have information displayed in Help/About dialog box
[MsVsShell.InstalledProductRegistration(false, "#100", "#101", "1.0.0.0", IconResourceID = CommandId.iiconProductIcon)]
// Declare that resources for the package are to be found in the managed assembly resources, and not in a satellite dll
[MsVsShell.PackageRegistration(UseManagedResourcesOnly = true)]
// Register the resource ID of the CTMENU section (generated from compiling the VSCT file), so the IDE will know how to merge this package's menus with the rest of the IDE when "devenv /setup" is run
// The menu resource ID needs to match the ResourceName number defined in the csproj project file in the VSCTCompile section
// Everytime the version number changes VS will automatically update the menus on startup; if the version doesn't change, you will need to run manually "devenv /setup /rootsuffix:Exp" to see VSCT changes reflected in IDE
[MsVsShell.ProvideMenuResource(1000, 1)]
// Register a sample options page visible as Tools/Options/SourceControl/SampleOptionsPage when the provider is active
[MsVsShell.ProvideOptionPageAttribute(typeof(SccProviderOptions), "Source Control", "Git Source Control Provider Options", 106, 107, false)]
[ProvideToolsOptionsPageVisibility("Source Control", "Git Source Control Provider Options", "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
// Register a sample tool window visible only when the provider is active
//[MsVsShell.ProvideToolWindow(typeof(PendingChangesToolWindow), Style = VsDockStyle.Tabbed, Orientation = ToolWindowOrientation.Bottom)]
//[MsVsShell.ProvideToolWindowVisibility(typeof(PendingChangesToolWindow), "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
//[MsVsShell.ProvideToolWindow(typeof(HistoryToolWindow), Style = VsDockStyle.Tabbed, Orientation = ToolWindowOrientation.Bottom)]
//[MsVsShell.ProvideToolWindowVisibility(typeof(HistoryToolWindow), "C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
//Register the source control provider's service (implementing IVsScciProvider interface)
[MsVsShell.ProvideService(typeof(SccProviderService), ServiceName = "Git Source Control Service")]
// Register the source control provider to be visible in Tools/Options/SourceControl/Plugin dropdown selector
[ProvideSourceControlProvider("Git Source Control Provider", "#100")]
// Pre-load the package when the command UI context is asserted (the provider will be automatically loaded after restarting the shell if it was active last time the shell was shutdown)
[MsVsShell.ProvideAutoLoad("C4128D99-0000-41D1-A6C3-704E6C1A3DE2")]
//UICONTEXT_SolutionExists
//[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
// Declare the package guid
[Guid("C4128D99-2000-41D1-A6C3-704E6C1A3DE2")]
public class BasicSccProvider : MsVsShell.Package, IOleCommandTarget
{
private SccOnIdleEvent _OnIdleEvent = new SccOnIdleEvent();
private List<GitFileStatusTracker> projects;
private SccProviderService sccService = null;
public BasicSccProvider()
{
Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Entering constructor for: {0}", this.ToString()));
}
/////////////////////////////////////////////////////////////////////////////
// BasicSccProvider Package Implementation
#region Package Members
protected override void Initialize()
{
Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize();
projects = new List<GitFileStatusTracker>();
sccService = new SccProviderService(this, projects);
((IServiceContainer)this).AddService(typeof(SccProviderService), sccService, true);
// Add our command handlers for menu (commands must exist in the .vsct file)
MsVsShell.OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as MsVsShell.OleMenuCommandService;
if (mcs != null)
{
CommandID cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandRefresh);
var menu = new MenuCommand(new EventHandler(OnRefreshCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandGitBash);
menu = new MenuCommand(new EventHandler(OnGitBashCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandGitExtension);
menu = new MenuCommand(new EventHandler(OnGitExtensionCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandCompare);
menu = new MenuCommand(new EventHandler(OnCompareCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandUndo);
menu = new MenuCommand(new EventHandler(OnUndoCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandInit);
menu = new MenuCommand(new EventHandler(OnInitCommand), cmd);
mcs.AddCommand(menu);
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdSccCommandGitTortoise);
menu = new MenuCommand(new EventHandler(OnTortoiseGitCommand), cmd);
mcs.AddCommand(menu);
for (int i = 0; i < GitToolCommands.GitExtCommands.Count; i++)
{
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdGitExtCommand1 + i);
var mc = new MenuCommand(new EventHandler(OnGitExtCommandExec), cmd);
mcs.AddCommand(mc);
}
for (int i = 0; i < GitToolCommands.GitTorCommands.Count; i++)
{
cmd = new CommandID(GuidList.guidSccProviderCmdSet, CommandId.icmdGitTorCommand1 + i);
var mc = new MenuCommand(new EventHandler(OnGitTorCommandExec), cmd);
mcs.AddCommand(mc);
}
}
// Register the provider with the source control manager
// If the package is to become active, this will also callback on OnActiveStateChange and the menu commands will be enabled
IVsRegisterScciProvider rscp = (IVsRegisterScciProvider)GetService(typeof(IVsRegisterScciProvider));
rscp.RegisterSourceControlProvider(GuidList.guidSccProvider);
_OnIdleEvent.RegisterForIdleTimeCallbacks(GetGlobalService(typeof(SOleComponentManager)) as IOleComponentManager);
_OnIdleEvent.OnIdleEvent += new OnIdleEvent(sccService.UpdateNodesGlyphs);
}
protected override void Dispose(bool disposing)
{
Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Entering Dispose() of: {0}", this.ToString()));
_OnIdleEvent.OnIdleEvent -= new OnIdleEvent(sccService.UpdateNodesGlyphs);
_OnIdleEvent.UnRegisterForIdleTimeCallbacks();
sccService.Dispose();
base.Dispose(disposing);
}
#endregion
#region menu commands
int IOleCommandTarget.QueryStatus(ref Guid guidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
Debug.Assert(cCmds == 1, "Multiple commands");
Debug.Assert(prgCmds != null, "NULL argument");
if ((prgCmds == null)) return VSConstants.E_INVALIDARG;
// Filter out commands that are not defined by this package
if (guidCmdGroup != GuidList.guidSccProviderCmdSet)
{
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
OLECMDF cmdf = OLECMDF.OLECMDF_SUPPORTED;
// All source control commands needs to be hidden and disabled when the provider is not active
if (!sccService.Active)
{
cmdf = cmdf | OLECMDF.OLECMDF_INVISIBLE;
cmdf = cmdf & ~(OLECMDF.OLECMDF_ENABLED);
prgCmds[0].cmdf = (uint)cmdf;
return VSConstants.S_OK;
}
// Process our Commands
switch (prgCmds[0].cmdID)
{
case CommandId.imnuGitSourceControlMenu:
OLECMDTEXT cmdtxtStructure = (OLECMDTEXT)Marshal.PtrToStructure(pCmdText, typeof(OLECMDTEXT));
if (cmdtxtStructure.cmdtextf == (uint)OLECMDTEXTF.OLECMDTEXTF_NAME)
{
string menuText = string.IsNullOrEmpty(sccService.CurrentBranchName) ?
"Git" : "Git (" + sccService.CurrentBranchName + ")";
SetOleCmdText(pCmdText, menuText);
}
break;
case CommandId.icmdSccCommandGitBash:
var gitBashPath = GitSccOptions.Current.GitBashPath;
if (!string.IsNullOrEmpty(gitBashPath) && File.Exists(gitBashPath))
{
cmdf |= OLECMDF.OLECMDF_ENABLED;
}
break;
case CommandId.icmdSccCommandGitExtension:
var gitExtensionPath = GitSccOptions.Current.GitExtensionPath;
if (!string.IsNullOrEmpty(gitExtensionPath) && File.Exists(gitExtensionPath) && GitSccOptions.Current.NotExpandGitExtensions)
{
cmdf |= OLECMDF.OLECMDF_ENABLED;
}
else
cmdf |= OLECMDF.OLECMDF_INVISIBLE;
break;
case CommandId.icmdSccCommandGitTortoise:
var tortoiseGitPath = GitSccOptions.Current.TortoiseGitPath;
if (!string.IsNullOrEmpty(tortoiseGitPath) && File.Exists(tortoiseGitPath) && GitSccOptions.Current.NotExpandTortoiseGit)
{
cmdf |= OLECMDF.OLECMDF_ENABLED;
}
else
cmdf |= OLECMDF.OLECMDF_INVISIBLE;
break;
case CommandId.icmdSccCommandUndo:
case CommandId.icmdSccCommandCompare:
if (sccService.CanCompareSelectedFile) cmdf |= OLECMDF.OLECMDF_ENABLED;
break;
case CommandId.icmdSccCommandRefresh:
//if (sccService.IsSolutionGitControlled)
cmdf |= OLECMDF.OLECMDF_ENABLED;
break;
case CommandId.icmdSccCommandInit:
if (!sccService.IsSolutionGitControlled)
cmdf |= OLECMDF.OLECMDF_ENABLED;
else
cmdf |= OLECMDF.OLECMDF_INVISIBLE;
break;
default:
var gitExtPath = GitSccOptions.Current.GitExtensionPath;
var torGitPath = GitSccOptions.Current.TortoiseGitPath;
if (prgCmds[0].cmdID >= CommandId.icmdGitExtCommand1 &&
prgCmds[0].cmdID < CommandId.icmdGitExtCommand1 + GitToolCommands.GitExtCommands.Count &&
!string.IsNullOrEmpty(gitExtPath) && File.Exists(gitExtPath) && !GitSccOptions.Current.NotExpandGitExtensions)
{
int idx = (int)prgCmds[0].cmdID - CommandId.icmdGitExtCommand1;
SetOleCmdText(pCmdText, GitToolCommands.GitExtCommands[idx].Name);
cmdf |= OLECMDF.OLECMDF_ENABLED;
break;
}
else if (prgCmds[0].cmdID >= CommandId.icmdGitTorCommand1 &&
prgCmds[0].cmdID < CommandId.icmdGitTorCommand1 + GitToolCommands.GitTorCommands.Count &&
!string.IsNullOrEmpty(torGitPath) && File.Exists(torGitPath) && !GitSccOptions.Current.NotExpandTortoiseGit)
{
int idx = (int)prgCmds[0].cmdID - CommandId.icmdGitTorCommand1;
SetOleCmdText(pCmdText, GitToolCommands.GitTorCommands[idx].Name);
cmdf |= OLECMDF.OLECMDF_ENABLED;
break;
}
else
return (int)(Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED);
}
prgCmds[0].cmdf = (uint) (cmdf);
return VSConstants.S_OK;
}
public void SetOleCmdText(IntPtr pCmdText, string text)
{
OLECMDTEXT CmdText = (OLECMDTEXT)Marshal.PtrToStructure(pCmdText, typeof(OLECMDTEXT));
char[] buffer = text.ToCharArray();
IntPtr pText = (IntPtr)((long)pCmdText + (long)Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz"));
IntPtr pCwActual = (IntPtr)((long)pCmdText + (long)Marshal.OffsetOf(typeof(OLECMDTEXT), "cwActual"));
// The max chars we copy is our string, or one less than the buffer size,
// since we need a null at the end.
int maxChars = (int)Math.Min(CmdText.cwBuf - 1, buffer.Length);
Marshal.Copy(buffer, 0, pText, maxChars);
// append a null
Marshal.WriteInt16((IntPtr)((long)pText + (long)maxChars * 2), (Int16)0);
// write out the length + null char
Marshal.WriteInt32(pCwActual, maxChars + 1);
}
private void OnRefreshCommand(object sender, EventArgs e)
{
sccService.Refresh();
}
private void OnCompareCommand(object sender, EventArgs e)
{
sccService.CompareSelectedFile();
}
private void OnUndoCommand(object sender, EventArgs e)
{
sccService.UndoSelectedFile();
}
private void OnGitBashCommand(object sender, EventArgs e)
{
var gitBashPath = GitSccOptions.Current.GitBashPath;
RunDetatched("cmd.exe", string.Format("/c \"{0}\" --login -i", gitBashPath));
}
private void OnGitExtensionCommand(object sender, EventArgs e)
{
var gitExtensionPath = GitSccOptions.Current.GitExtensionPath;
RunDetatched(gitExtensionPath, "");
}
internal void RunDiffCommand(string file1, string file2)
{
var difftoolPath = GitSccOptions.Current.DifftoolPath;
RunCommand(difftoolPath, "\"" + file1 + "\" \"" + file2 + "\"");
}
private void OnInitCommand(object sender, EventArgs e)
{
sccService.InitRepo();
}
private void OnTortoiseGitCommand(object sender, EventArgs e)
{
var tortoiseGitPath = GitSccOptions.Current.TortoiseGitPath;
RunDetatched(tortoiseGitPath, "/command:commit");
}
private void OnGitTorCommandExec(object sender, EventArgs e)
{
var menuCommand = sender as MenuCommand;
if (null != menuCommand)
{
int idx = menuCommand.CommandID.ID - CommandId.icmdGitTorCommand1;
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture,
"Run GitTor Command {0}", GitToolCommands.GitTorCommands[idx].Command));
var tortoiseGitPath = GitSccOptions.Current.TortoiseGitPath;
RunDetatched(tortoiseGitPath, GitToolCommands.GitTorCommands[idx].Command + " /path:\"" + sccService.CurrentGitWorkingDirectory + "\"");
}
}
private void OnGitExtCommandExec(object sender, EventArgs e)
{
var menuCommand = sender as MenuCommand;
if (null != menuCommand)
{
int idx = menuCommand.CommandID.ID - CommandId.icmdGitExtCommand1;
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture,
"Run GitExt Command {0}", GitToolCommands.GitExtCommands[idx].Command));
var gitExtensionPath = GitSccOptions.Current.GitExtensionPath;
RunDetatched(gitExtensionPath, GitToolCommands.GitExtCommands[idx].Command);
}
}
#endregion
// This function is called by the IVsSccProvider service implementation when the active state of the provider changes
// The package needs to show or hide the scc-specific commands
public virtual void OnActiveStateChange()
{
}
public new Object GetService(Type serviceType)
{
return base.GetService(serviceType);
}
#region Run Command
internal void RunCommand(string cmd, string args)
{
var pinfo = new ProcessStartInfo(cmd)
{
Arguments = args,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = sccService.CurrentGitWorkingDirectory ??
Path.GetDirectoryName(sccService.GetSolutionFileName())
};
Process.Start(pinfo);
//using (var process = Process.Start(pinfo))
//{
// string output = process.StandardOutput.ReadToEnd();
// string error = process.StandardError.ReadToEnd();
// process.WaitForExit();
// if (!string.IsNullOrEmpty(error))
// throw new Exception(error);
// return output;
//}
}
internal void RunDetatched(string cmd, string arguments)
{
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.FileName = cmd;
process.StartInfo.Arguments = arguments;
process.StartInfo.WorkingDirectory = sccService.CurrentGitWorkingDirectory ??
Path.GetDirectoryName(sccService.GetSolutionFileName());
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.StartInfo.LoadUserProfile = true;
process.Start();
}
}
#endregion
}
}