forked from blinkbox/Git-Source-Control-Provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SccProviderService.cs
1143 lines (967 loc) · 44.1 KB
/
SccProviderService.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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.Windows.Threading;
namespace GitScc
{
[Guid("C4128D99-1000-41D1-A6C3-704E6C1A3DE2")]
public class SccProviderService : IVsSccProvider,
IVsSccManager2,
IVsSccManagerTooltip,
IVsSolutionEvents,
IVsSolutionEvents2,
IVsFileChangeEvents,
IVsSccGlyphs,
IDisposable,
IVsUpdateSolutionEvents2
{
private bool _active = false;
private BasicSccProvider _sccProvider = null;
private List<GitFileStatusTracker> trackers;
private uint _vsSolutionEventsCookie, _vsIVsFileChangeEventsCookie, _vsIVsUpdateSolutionEventsCookie;
#region SccProvider Service initialization/unitialization
public SccProviderService(BasicSccProvider sccProvider, List<GitFileStatusTracker> trackers)
{
this._sccProvider = sccProvider;
this.trackers = trackers;
// Subscribe to solution events
IVsSolution sol = (IVsSolution)sccProvider.GetService(typeof(SVsSolution));
sol.AdviseSolutionEvents(this, out _vsSolutionEventsCookie);
var sbm = sccProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;
if (sbm != null)
{
sbm.AdviseUpdateSolutionEvents(this, out _vsIVsUpdateSolutionEventsCookie);
}
}
public void Dispose()
{
// Unregister from receiving solution events
if (VSConstants.VSCOOKIE_NIL != _vsSolutionEventsCookie)
{
IVsSolution sol = (IVsSolution)_sccProvider.GetService(typeof(SVsSolution));
sol.UnadviseSolutionEvents(_vsSolutionEventsCookie);
_vsSolutionEventsCookie = VSConstants.VSCOOKIE_NIL;
}
if (VSConstants.VSCOOKIE_NIL != _vsIVsUpdateSolutionEventsCookie)
{
var sbm = _sccProvider.GetService(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;
sbm.UnadviseUpdateSolutionEvents(_vsIVsUpdateSolutionEventsCookie);
}
}
#endregion
#region IVsSccProvider interface functions
/// <summary>
/// Returns whether this source control provider is the active scc provider.
/// </summary>
public bool Active
{
get { return _active; }
}
// Called by the scc manager when the provider is activated.
// Make visible and enable if necessary scc related menu commands
public int SetActive()
{
Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Git Source Control Provider set active"));
_active = true;
Refresh();
return VSConstants.S_OK;
}
// Called by the scc manager when the provider is deactivated.
// Hides and disable scc related menu commands
public int SetInactive()
{
Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Git Source Control Provider set inactive"));
_active = false;
CloseTracker();
NodesGlyphsDirty = true;
return VSConstants.S_OK;
}
public int AnyItemsUnderSourceControl(out int pfResult)
{
pfResult = 0;
return VSConstants.S_OK;
}
#endregion
#region IVsSccManager2 interface functions
public int BrowseForProject(out string pbstrDirectory, out int pfOK)
{
// Obsolete method
pbstrDirectory = null;
pfOK = 0;
return VSConstants.E_NOTIMPL;
}
public int CancelAfterBrowseForProject()
{
// Obsolete method
return VSConstants.E_NOTIMPL;
}
/// <summary>
/// Returns whether the source control provider is fully installed
/// </summary>
public int IsInstalled(out int pbInstalled)
{
// All source control packages should always return S_OK and set pbInstalled to nonzero
pbInstalled = 1;
return VSConstants.S_OK;
}
/// <summary>
/// Provide source control icons for the specified files and returns scc status of files
/// </summary>
/// <returns>The method returns S_OK if at least one of the files is controlled, S_FALSE if none of them are</returns>
public int GetSccGlyph([InAttribute] int cFiles, [InAttribute] string[] rgpszFullPaths, [OutAttribute] VsStateIcon[] rgsiGlyphs, [OutAttribute] uint[] rgdwSccStatus)
{
Debug.Assert(cFiles == 1, "Only getting one file icon at a time is supported");
// Return the icons and the status. While the status is a combination a flags, we'll return just values
// with one bit set, to make life easier for GetSccGlyphsFromStatus
if (rgpszFullPaths[0] == null) return 0;
GitFileStatus status = _active ? GetFileStatus(rgpszFullPaths[0]) : GitFileStatus.NotControlled;
//Debug.WriteLine("==== GetSccGlyph {0} : {1}", rgpszFullPaths[0], status);
if (rgdwSccStatus != null) rgdwSccStatus[0] = (uint)__SccStatus.SCC_STATUS_CONTROLLED;
switch (status)
{
case GitFileStatus.Tracked:
rgsiGlyphs[0] = GitSccOptions.Current.UseTGitIconSet ?
(VsStateIcon)(this._customSccGlyphBaseIndex + (uint)CustomSccGlyphs.Tracked) :
VsStateIcon.STATEICON_CHECKEDIN;
break;
case GitFileStatus.Modified:
rgsiGlyphs[0] = GitSccOptions.Current.UseTGitIconSet ?
(VsStateIcon)(this._customSccGlyphBaseIndex + (uint)CustomSccGlyphs.Modified):
VsStateIcon.STATEICON_CHECKEDOUT;
break;
case GitFileStatus.New:
rgsiGlyphs[0] = (VsStateIcon)(this._customSccGlyphBaseIndex + (uint)CustomSccGlyphs.Untracked);
break;
case GitFileStatus.Added:
case GitFileStatus.Staged:
rgsiGlyphs[0] = (VsStateIcon)(this._customSccGlyphBaseIndex + (uint)CustomSccGlyphs.Staged);
break;
case GitFileStatus.NotControlled:
rgsiGlyphs[0] = VsStateIcon.STATEICON_BLANK;
break;
case GitFileStatus.Ignored:
rgsiGlyphs[0] = VsStateIcon.STATEICON_EXCLUDEDFROMSCC;
break;
case GitFileStatus.Conflict:
rgsiGlyphs[0] = VsStateIcon.STATEICON_DISABLED;
break;
}
return VSConstants.S_OK;
}
/// <summary>
/// Determines the corresponding scc status glyph to display, given a combination of scc status flags
/// </summary>
public int GetSccGlyphFromStatus([InAttribute] uint dwSccStatus, [OutAttribute] VsStateIcon[] psiGlyph)
{
//switch (dwSccStatus)
//{
// case (uint)__SccStatus.SCC_STATUS_CHECKEDOUT:
// psiGlyph[0] = VsStateIcon.STATEICON_CHECKEDOUT;
// break;
// case (uint)__SccStatus.SCC_STATUS_CONTROLLED:
// psiGlyph[0] = VsStateIcon.STATEICON_CHECKEDIN;
// break;
// default:
// psiGlyph[0] = VsStateIcon.STATEICON_BLANK;
// break;
//}
return VSConstants.S_OK;
}
/// <summary>
/// One of the most important methods in a source control provider, is called by projects that are under source control when they are first opened to register project settings
/// </summary>
public int RegisterSccProject([InAttribute] IVsSccProject2 pscp2Project, [InAttribute] string pszSccProjectName, [InAttribute] string pszSccAuxPath, [InAttribute] string pszSccLocalPath, [InAttribute] string pszProvider)
{
return VSConstants.S_OK;
}
/// <summary>
/// Called by projects registered with the source control portion of the environment before they are closed.
/// </summary>
public int UnregisterSccProject([InAttribute] IVsSccProject2 pscp2Project)
{
return VSConstants.S_OK;
}
#endregion
#region IVsSccManagerTooltip interface functions
/// <summary>
/// Called by solution explorer to provide tooltips for items. Returns a text describing the source control status of the item.
/// </summary>
public int GetGlyphTipText([InAttribute] IVsHierarchy phierHierarchy, [InAttribute] uint itemidNode, out string pbstrTooltipText)
{
pbstrTooltipText = "";
GitFileStatus status = GetFileStatus(phierHierarchy, itemidNode);
pbstrTooltipText = status.ToString(); //TODO: use resources
return VSConstants.S_OK;
}
#endregion
#region IVsSolutionEvents interface functions
public int OnAfterOpenSolution([InAttribute] Object pUnkReserved, [InAttribute] int fNewSolution)
{
//automatic switch the scc provider
if (!Active && !GitSccOptions.Current.DisableAutoLoad)
{
OpenTracker();
if (trackers.Count > 0)
{
IVsRegisterScciProvider rscp = (IVsRegisterScciProvider) _sccProvider.GetService(typeof(IVsRegisterScciProvider));
rscp.RegisterSourceControlProvider(GuidList.guidSccProvider);
}
}
Refresh();
return VSConstants.S_OK;
}
public int OnAfterCloseSolution([InAttribute] Object pUnkReserved)
{
CloseTracker();
return VSConstants.S_OK;
}
public int OnAfterLoadProject([InAttribute] IVsHierarchy pStubHierarchy, [InAttribute] IVsHierarchy pRealHierarchy)
{
return VSConstants.S_OK;
}
public int OnAfterOpenProject([InAttribute] IVsHierarchy pHierarchy, [InAttribute] int fAdded)
{
return VSConstants.S_OK;
}
public int OnBeforeCloseProject([InAttribute] IVsHierarchy pHierarchy, [InAttribute] int fRemoved)
{
return VSConstants.S_OK;
}
public int OnBeforeCloseSolution([InAttribute] Object pUnkReserved)
{
return VSConstants.S_OK;
}
public int OnBeforeUnloadProject([InAttribute] IVsHierarchy pRealHierarchy, [InAttribute] IVsHierarchy pStubHierarchy)
{
return VSConstants.S_OK;
}
public int OnQueryCloseProject([InAttribute] IVsHierarchy pHierarchy, [InAttribute] int fRemoving, [InAttribute] ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnQueryCloseSolution([InAttribute] Object pUnkReserved, [InAttribute] ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnQueryUnloadProject([InAttribute] IVsHierarchy pRealHierarchy, [InAttribute] ref int pfCancel)
{
return VSConstants.S_OK;
}
public int OnAfterMergeSolution([InAttribute] Object pUnkReserved)
{
return VSConstants.S_OK;
}
#endregion
#region IVsSccGlyphs Members
// Remember the base index where our custom scc glyph start
private uint _customSccGlyphBaseIndex = 0;
// Our custom image list
ImageList _customSccGlyphsImageList;
// Indexes of icons in our custom image list
enum CustomSccGlyphs
{
Untracked = 0,
Staged = 1,
Modified = 2,
Tracked = 3,
};
public int GetCustomGlyphList(uint BaseIndex, out uint pdwImageListHandle)
{
// If this is the first time we got called, construct the image list, remember the index, etc
if (this._customSccGlyphsImageList == null)
{
// The shell calls this function when the provider becomes active to get our custom glyphs
// and to tell us what's the first index we can use for our glyphs
// Remember the index in the scc glyphs (VsStateIcon) where our custom glyphs will start
this._customSccGlyphBaseIndex = BaseIndex;
// Create a new imagelist
this._customSccGlyphsImageList = new ImageList();
// Set the transparent color for the imagelist (the SccGlyphs.bmp uses magenta for background)
this._customSccGlyphsImageList.TransparentColor = Color.FromArgb(255, 0, 255);
// Set the corret imagelist size (7x16 pixels, otherwise the system will either stretch the image or fill in with black blocks)
this._customSccGlyphsImageList.ImageSize = new Size(7, 16);
// Add the custom scc glyphs we support to the list
// NOTE: VS2005 and VS2008 are limited to 4 custom scc glyphs (let's hope this will change in future versions)
Image sccGlyphs = (Image)Resources.SccGlyphs;
this._customSccGlyphsImageList.Images.AddStrip(sccGlyphs);
}
// Return a Win32 HIMAGELIST handle to our imagelist to the shell (by keeping the ImageList a member of the class we guarantee the Win32 object is still valid when the shell needs it)
pdwImageListHandle = (uint)this._customSccGlyphsImageList.Handle;
// Return success (If you don't want to have custom glyphs return VSConstants.E_NOTIMPL)
return VSConstants.S_OK;
}
#endregion
#region File Names
private void SetSolutionExplorerTitle(string message)
{
var dte = (DTE)_sccProvider.GetService(typeof(DTE));
dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Caption = message;
}
/// <summary>
/// Returns the filename of the solution
/// </summary>
public string GetSolutionFileName()
{
IVsSolution sol = (IVsSolution)_sccProvider.GetService(typeof(SVsSolution));
string solutionDirectory, solutionFile, solutionUserOptions;
if (sol.GetSolutionInfo(out solutionDirectory, out solutionFile, out solutionUserOptions) == VSConstants.S_OK)
{
return solutionFile;
}
else
{
return null;
}
}
private string GetProjectFileName(IVsHierarchy hierHierarchy)
{
if (!(hierHierarchy is IVsSccProject2)) return GetSolutionFileName();
var files = GetNodeFiles(hierHierarchy as IVsSccProject2, VSConstants.VSITEMID_ROOT);
string fileName = files.Count <= 0 ? null : files[0];
//try hierHierarchy.GetCanonicalName to get project name for web site
if (fileName == null)
{
if (hierHierarchy.GetCanonicalName(VSConstants.VSITEMID_ROOT, out fileName) != VSConstants.S_OK) return null;
return GetCaseSensitiveFileName(fileName);
}
return fileName;
}
private string GetFileName(IVsHierarchy hierHierarchy, uint itemidNode)
{
if (itemidNode == VSConstants.VSITEMID_ROOT)
{
if (hierHierarchy == null)
return GetSolutionFileName();
else
return GetProjectFileName(hierHierarchy);
}
else
{
string fileName = null;
if (hierHierarchy.GetCanonicalName(itemidNode, out fileName) != VSConstants.S_OK) return null;
return GetCaseSensitiveFileName(fileName);
}
}
private static string GetCaseSensitiveFileName(string fileName)
{
if (fileName == null) return fileName;
if (Directory.Exists(fileName) || File.Exists(fileName))
{
try
{
StringBuilder sb = new StringBuilder(1024);
GetShortPathName(fileName.ToUpper(), sb, 1024);
GetLongPathName(sb.ToString(), sb, 1024);
var fn = sb.ToString();
return string.IsNullOrWhiteSpace(fn) ? fileName : fn;
}
catch { }
}
return fileName;
}
[DllImport("kernel32.dll")]
static extern uint GetShortPathName(string longpath, StringBuilder sb, int buffer);
[DllImport("kernel32.dll")]
static extern uint GetLongPathName(string shortpath, StringBuilder sb, int buffer);
/// <summary>
/// Returns a list of source controllable files associated with the specified node
/// </summary>
private IList<string> GetNodeFiles(IVsSccProject2 pscp2, uint itemid)
{
// NOTE: the function returns only a list of files, containing both regular files and special files
// If you want to hide the special files (similar with solution explorer), you may need to return
// the special files in a hastable (key=master_file, values=special_file_list)
// Initialize output parameters
IList<string> sccFiles = new List<string>();
if (pscp2 != null)
{
CALPOLESTR[] pathStr = new CALPOLESTR[1];
CADWORD[] flags = new CADWORD[1];
if (pscp2.GetSccFiles(itemid, pathStr, flags) == 0)
{
for (int elemIndex = 0; elemIndex < pathStr[0].cElems; elemIndex++)
{
IntPtr pathIntPtr = Marshal.ReadIntPtr(pathStr[0].pElems, elemIndex);
String path = Marshal.PtrToStringAuto(pathIntPtr);
sccFiles.Add(path);
// See if there are special files
if (flags.Length > 0 && flags[0].cElems > 0)
{
int flag = Marshal.ReadInt32(flags[0].pElems, elemIndex);
if (flag != 0)
{
// We have special files
CALPOLESTR[] specialFiles = new CALPOLESTR[1];
CADWORD[] specialFlags = new CADWORD[1];
pscp2.GetSccSpecialFiles(itemid, path, specialFiles, specialFlags);
for (int i = 0; i < specialFiles[0].cElems; i++)
{
IntPtr specialPathIntPtr = Marshal.ReadIntPtr(specialFiles[0].pElems, i * IntPtr.Size);
String specialPath = Marshal.PtrToStringAuto(specialPathIntPtr);
sccFiles.Add(specialPath);
Marshal.FreeCoTaskMem(specialPathIntPtr);
}
if (specialFiles[0].cElems > 0)
{
Marshal.FreeCoTaskMem(specialFiles[0].pElems);
}
}
}
Marshal.FreeCoTaskMem(pathIntPtr);
}
if (pathStr[0].cElems > 0)
{
Marshal.FreeCoTaskMem(pathStr[0].pElems);
}
}
}
else if (itemid == VSConstants.VSITEMID_ROOT)
{
sccFiles.Add(GetSolutionFileName());
}
return sccFiles;
}
/// <summary>
/// Gets the list of directly selected VSITEMSELECTION objects
/// </summary>
/// <returns>A list of VSITEMSELECTION objects</returns>
private IList<VSITEMSELECTION> GetSelectedNodes()
{
// Retrieve shell interface in order to get current selection
IVsMonitorSelection monitorSelection = _sccProvider.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
Debug.Assert(monitorSelection != null, "Could not get the IVsMonitorSelection object from the services exposed by this project");
if (monitorSelection == null)
{
throw new InvalidOperationException();
}
List<VSITEMSELECTION> selectedNodes = new List<VSITEMSELECTION>();
IntPtr hierarchyPtr = IntPtr.Zero;
IntPtr selectionContainer = IntPtr.Zero;
try
{
// Get the current project hierarchy, project item, and selection container for the current selection
// If the selection spans multiple hierachies, hierarchyPtr is Zero
uint itemid;
IVsMultiItemSelect multiItemSelect = null;
ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainer));
if (itemid != VSConstants.VSITEMID_SELECTION)
{
// We only care if there are nodes selected in the tree
if (itemid != VSConstants.VSITEMID_NIL)
{
if (hierarchyPtr == IntPtr.Zero)
{
// Solution is selected
VSITEMSELECTION vsItemSelection;
vsItemSelection.pHier = null;
vsItemSelection.itemid = itemid;
selectedNodes.Add(vsItemSelection);
}
else
{
IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(hierarchyPtr);
// Single item selection
VSITEMSELECTION vsItemSelection;
vsItemSelection.pHier = hierarchy;
vsItemSelection.itemid = itemid;
selectedNodes.Add(vsItemSelection);
}
}
}
else
{
if (multiItemSelect != null)
{
// This is a multiple item selection.
//Get number of items selected and also determine if the items are located in more than one hierarchy
uint numberOfSelectedItems;
int isSingleHierarchyInt;
ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectionInfo(out numberOfSelectedItems, out isSingleHierarchyInt));
bool isSingleHierarchy = (isSingleHierarchyInt != 0);
// Now loop all selected items and add them to the list
Debug.Assert(numberOfSelectedItems > 0, "Bad number of selected itemd");
if (numberOfSelectedItems > 0)
{
VSITEMSELECTION[] vsItemSelections = new VSITEMSELECTION[numberOfSelectedItems];
ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectedItems(0, numberOfSelectedItems, vsItemSelections));
foreach (VSITEMSELECTION vsItemSelection in vsItemSelections)
{
selectedNodes.Add(vsItemSelection);
}
}
}
}
}
finally
{
if (hierarchyPtr != IntPtr.Zero)
{
Marshal.Release(hierarchyPtr);
}
if (selectionContainer != IntPtr.Zero)
{
Marshal.Release(selectionContainer);
}
}
return selectedNodes;
}
#endregion
#region open and close tracker
string lastMinotorFolder = "";
string monitorFolder;
internal void OpenTracker()
{
Debug.WriteLine("==== Open Tracker");
trackers.Clear();
var solutionFileName = GetSolutionFileName();
if (!string.IsNullOrEmpty(solutionFileName))
{
monitorFolder = Path.GetDirectoryName(solutionFileName);
GetLoadedControllableProjects().ForEach(h => AddProject(h as IVsHierarchy));
if (monitorFolder != lastMinotorFolder)
{
RemoveFolderMonitor();
IVsFileChangeEx fileChangeService = _sccProvider.GetService(typeof(SVsFileChangeEx)) as IVsFileChangeEx;
if (VSConstants.VSCOOKIE_NIL != _vsIVsFileChangeEventsCookie)
{
fileChangeService.UnadviseDirChange(_vsIVsFileChangeEventsCookie);
}
fileChangeService.AdviseDirChange(monitorFolder, 1, this, out _vsIVsFileChangeEventsCookie);
lastMinotorFolder = monitorFolder;
Debug.WriteLine("==== Monitoring: " + monitorFolder + " " + _vsIVsFileChangeEventsCookie);
}
}
}
private void CloseTracker()
{
Debug.WriteLine("==== Close Tracker");
trackers.Clear();
RemoveFolderMonitor();
NodesGlyphsDirty = true; // set refresh flag
//RefreshToolWindows();
}
private void RemoveFolderMonitor()
{
if (VSConstants.VSCOOKIE_NIL != _vsIVsFileChangeEventsCookie)
{
IVsFileChangeEx fileChangeService = _sccProvider.GetService(typeof(SVsFileChangeEx)) as IVsFileChangeEx;
fileChangeService.UnadviseDirChange(_vsIVsFileChangeEventsCookie);
Debug.WriteLine("==== Stop Monitoring: " + _vsIVsFileChangeEventsCookie.ToString());
_vsIVsFileChangeEventsCookie = VSConstants.VSCOOKIE_NIL;
lastMinotorFolder = "";
}
}
#endregion
#region IVsFileChangeEvents
public int DirectoryChanged(string pszDirectory)
{
//Debug.WriteLine("==== dir changed REFRESH: " + pszDirectory);
Refresh();
return VSConstants.S_OK;
}
public int FilesChanged(uint cChanges, string[] rgpszFile, uint[] rggrfChange)
{
return VSConstants.S_OK;
}
#endregion
#region Compare and undo
internal bool CanCompareSelectedFile
{
get
{
var fileName = GetSelectFileName();
GitFileStatus status = GetFileStatus(fileName);
return status == GitFileStatus.Modified || status == GitFileStatus.Staged;
}
}
internal string GetSelectFileName()
{
var selectedNodes = GetSelectedNodes();
if (selectedNodes.Count <= 0) return null;
return GetFileName(selectedNodes[0].pHier, selectedNodes[0].itemid);
}
internal void CompareSelectedFile()
{
var fileName = GetSelectFileName();
CompareFile(fileName);
}
internal void CompareFile(string fileName)
{
GitFileStatus status = GetFileStatus(fileName);
if (status == GitFileStatus.Modified || status == GitFileStatus.Staged)
{
string tempFile = Path.GetFileName(fileName);
tempFile = Path.Combine(Path.GetTempPath(), tempFile);
CurrentTracker.SaveFileFromRepository(fileName, tempFile);
_sccProvider.RunDiffCommand(tempFile, fileName);
}
}
internal void UndoSelectedFile()
{
var fileName = GetSelectFileName();
UndoFileChanges(fileName);
}
internal void UndoFileChanges(string fileName)
{
GitFileStatus status = GetFileStatus(fileName);
if (status == GitFileStatus.Modified || status == GitFileStatus.Staged ||
status == GitFileStatus.Deleted || status == GitFileStatus.Removed)
{
var deleteMsg = "";
if (status == GitFileStatus.Deleted || status == GitFileStatus.Removed)
{
deleteMsg = @"
Note: you will need to click 'Show All Files' in solution explorer to see the file.";
}
if (MessageBox.Show("Are you sure you want to undo changes for " + Path.GetFileName(fileName) +
" and restore a version from the last commit? " + deleteMsg,
"Undo Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//SaveFileFromRepository(fileName, fileName);
//if (status == GitFileStatus.Staged || status == GitFileStatus.Removed)
//{
// CurrentTracker.UnStageFile(fileName);
//}
CurrentTracker.CheckOutFile(fileName);
}
}
}
internal void EditIgnore()
{
if (this.CurrentTracker != null && this.CurrentTracker.HasGitRepository)
{
var dte = BasicSccProvider.GetServiceEx<EnvDTE.DTE>();
var fn = Path.Combine(this.CurrentTracker.GitWorkingDirectory, ".gitignore");
if (!File.Exists(fn)) File.WriteAllText(fn, "# git ignore file");
dte.ItemOperations.OpenFile(fn);
}
}
#endregion
#region IVsUpdateSolutionEvents2 Members
public int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{
return VSConstants.S_OK;
}
public int UpdateProjectCfg_Begin(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, ref int pfCancel)
{
return VSConstants.S_OK;
}
public int UpdateProjectCfg_Done(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, int fSuccess, int fCancel)
{
return VSConstants.S_OK;
}
public int UpdateSolution_Begin(ref int pfCancelUpdate)
{
Debug.WriteLine("Git Source Control Provider: suppress refresh before build...");
NoRefresh = true;
return VSConstants.S_OK;
}
public int UpdateSolution_Cancel()
{
return VSConstants.S_OK;
}
public int UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand)
{
Debug.WriteLine("Git Source Control Provider: resume refresh after build...");
NoRefresh = false;
return VSConstants.S_OK;
}
public int UpdateSolution_StartUpdate(ref int pfCancelUpdate)
{
return VSConstants.S_OK;
}
#endregion
#region project trackers
private void AddProject(IVsHierarchy pHierarchy)
{
string projectName = GetProjectFileName(pHierarchy);
if (string.IsNullOrEmpty(projectName)) return;
string projectDirecotry = Path.GetDirectoryName(projectName);
//Debug.WriteLine("==== Adding project: " + projectDirecotry);
string gitfolder = GitFileStatusTracker.GetRepositoryDirectory(projectDirecotry);
if (string.IsNullOrEmpty(gitfolder) ||
trackers.Any(t => t.HasGitRepository &&
string.Compare(t.GitWorkingDirectory, gitfolder, true)==0)) return;
if (gitfolder.Length < monitorFolder.Length) monitorFolder = gitfolder;
trackers.Add(new GitFileStatusTracker(gitfolder));
//Debug.WriteLine("==== Added git tracker: " + gitfolder);
}
internal string CurrentBranchName
{
get
{
return CurrentTracker == null ? null : CurrentTracker.CurrentBranch;
}
}
internal string CurrentGitWorkingDirectory
{
get
{
return CurrentTracker == null ? null : CurrentTracker.GitWorkingDirectory;
}
}
internal GitFileStatusTracker CurrentTracker
{
get
{
string fileName = GetSelectFileName();
if (trackers.Count == 1)
return trackers[0];
else
return GetTracker(fileName);
}
}
internal GitFileStatusTracker GetSolutionTracker()
{
return GetTracker(GetSolutionFileName());
}
internal GitFileStatusTracker GetTracker(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return null;
return trackers.Where(t => t.HasGitRepository &&
IsParentFolder(t.GitWorkingDirectory, fileName))
.OrderByDescending(t => t.GitWorkingDirectory.Length)
.FirstOrDefault();
}
private bool IsParentFolder(string folder, string fileName)
{
if (string.IsNullOrEmpty(folder) || string.IsNullOrEmpty(fileName) ||
!Directory.Exists(folder)) return false;
bool b = false;
var dir = new DirectoryInfo(Path.GetDirectoryName(fileName));
while (!b && dir != null)
{
b = string.Compare(dir.FullName, folder, true) == 0;
dir = dir.Parent;
}
return b;
}
private GitFileStatus GetFileStatus(string fileName)
{
var tracker = GetTracker(fileName);
return tracker == null ? GitFileStatus.NotControlled :
tracker.GetFileStatus(fileName);
}
private GitFileStatus GetFileStatus(IVsHierarchy phierHierarchy, uint itemidNode)
{
var fileName = GetFileName(phierHierarchy, itemidNode);
return GetFileStatus(fileName);
}
//private void SaveFileFromRepository(string fileName, string tempFile)
//{
// var tracker = CurrentTracker;
// if (tracker == null) return;
// var data = tracker.GetFileContent(fileName);
// using (var binWriter = new BinaryWriter(File.Open(tempFile, FileMode.Create)))
// {
// binWriter.Write(data ?? new byte[] { });
// }
//}
#endregion
#region new Refresh methods
internal bool NodesGlyphsDirty = false;
internal bool NoRefresh = false;
internal DateTime lastTimeRefresh = DateTime.Now.AddDays(-1);
internal DateTime nextTimeRefresh = DateTime.Now;
internal void Refresh()
{
if (!NoRefresh)
{
double delta = DateTime.Now.Subtract(lastTimeRefresh).TotalMilliseconds;
if (delta > 500)
{
NodesGlyphsDirty = true;
lastTimeRefresh = DateTime.Now;
nextTimeRefresh = DateTime.Now;
}
}
}
public void UpdateNodesGlyphs()
{
if (NodesGlyphsDirty && !NoRefresh)
{
double delta = DateTime.Now.Subtract(nextTimeRefresh).TotalMilliseconds;
if (delta > 200)
{
Debug.WriteLine("==== UpdateNodesGlyphs: " + delta.ToString());
//Stopwatch stopwatch = new Stopwatch();
//stopwatch.Start();
NoRefresh = true;
OpenTracker();
RefreshNodesGlyphs();
RefreshToolWindows();
NoRefresh = false;
NodesGlyphsDirty = false;
nextTimeRefresh = DateTime.Now; //important !!
//stopwatch.Stop();
//Debug.WriteLine("==== UpdateNodesGlyphs: " + stopwatch.ElapsedMilliseconds);
}
}
}
public void RefreshNodesGlyphs()
{
var solHier = (IVsHierarchy)_sccProvider.GetService(typeof(SVsSolution));
var projectList = GetLoadedControllableProjects();
// We'll also need to refresh the solution folders glyphs
// to reflect the controlled state
IList<VSITEMSELECTION> nodes = new List<VSITEMSELECTION>();
// add project node items
foreach (IVsHierarchy hr in projectList)
{
VSITEMSELECTION vsItem;
vsItem.itemid = VSConstants.VSITEMID_ROOT;
vsItem.pHier = hr;
nodes.Add(vsItem);
}
RefreshNodesGlyphs(nodes);
var caption = "Solution Explorer";
string branch = CurrentBranchName;
if (!string.IsNullOrEmpty(branch))
{
caption += " (" + branch + ")";
SetSolutionExplorerTitle(caption);
}
}
/// <summary>