Support not running secrets analysis on .gitignore-d files
Description
In other SLVS flavors, the rules are run on text files, unless they are explicitly excluded by being in the .gitignore file i.e. if a project is not under source control then issues will be reported.
This behaviour is nice-to-have; if we can do it easily, great, otherwise it is ok only to run secrets rules for for files we know are under source control.
Notes:
EnvDTE.SourceControl::IsItemUnderSCC(filePath)
Pros:
- public VS interface, VS2015+
- should work for multiple source control systems
Cons:
- limited: doesn't differentiate between (1) ignored files and (2) new files that have been created but not staged or committed:
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\.gitignore
underSCC: True, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\SampleApp.csproj
underSCC: True, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\Program.cs
underSCC: True, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\IgnoredFile.txt
underSCC: False, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\ExistingFile_Committed.txt
underSCC: True, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\ExistingFile_ModifiedLocally.txt
underSCC: True, checkedOut: True
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\NewFile_AddedButNotCommitted.txt
underSCC: False, checkedOut: False
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\NewFile_StagedButNotCommitted.txt
underSCC: True, checkedOut: True
XXX File: D:\proto\SLVS\SCCDetection\SampleApp\MISSING_FILE.txt
underSCC: True, checkedOut: False
LibGit2Sharp
NuGet: https://www.nuget.org/packages/LibGit2Sharp/0.27.0-preview-0119
Cons:
- Git-only
- public NuGet package is v0.26.2
- adds approx 6MB to the VSIX (less if we exclude the Mac/Unix libraries, but that would be more complicated)
Pros:
- correctly detects whether files are ignored or not
- widely used (4.5 million downloads)
- VS ships with a privately-built version
- ?used in the GitHub extension for VS?
var startPath = ...
var repoPath = Repository.Discover(startPath);
using (var repo = new Repository(repoPath))
{
// NOTE: needs a relative file path with Unix directory separators
var isIgnored = repo.Ignore.IsPathIgnored(file);
Debug.WriteLine($"YYY IsIgnored: {isIgnored}, file: {file}");
}
Notes:
The following works for non open-as-folder projects:
var projectHierarchy = GetVsHierarchyForFile(fullFilePath);
var itemId = FindProjectItemId(projectHierarchy, fullFilePath);
var hr = projectHierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_StateIconIndex, out var stateIcon);
stateIcon is of type VsStateIcon and the sample app returns the following values:
ExistingFile_Committed -- STATEICON_CHECKEDIN ExistingFile_ModifiedLocally -- STATEICON_CHECKEDOUT IgnoredFile -- STATEICON_EXCLUDEDFROMSCC NewFile_AddedButNotCommitted -- STATEICON_MAXINDEX NewFile_StagedButNotCommitted -- STATEICON_MAXINDEX
Unfortunately, for open-as-folder projects, stateIcon is null. I've attempted to retrieve projectHierarchy in other ways, i.e. by enumerating IVsSolution projects, in which case I can get ProjectItem of the file; in the debugger I can see ProjectItem.Properties.Node.StateIconIndex -- but I cannot retrieve it at runtime as those are internal VS properties.