Skip to content

Commit

Permalink
Merge pull request #39 from flcdrg/GitIgnores
Browse files Browse the repository at this point in the history
Suport loading child .gitignore files
  • Loading branch information
flcdrg committed Jul 27, 2017
2 parents fd628dc + 64de2ae commit c6d6602
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
55 changes: 46 additions & 9 deletions Gardiner.VsShowMissing/Gardiner.VsShowMissingPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public sealed class Gardiner_VsShowMissingPackage : Package, IVsSolutionEvents
private IList<Project> _projects;
private string _solutionDirectory;
private List<Regex> _filters;
private IgnoreList _gitignores;
private readonly Dictionary<string, IgnoreList> _gitignores;

/// <summary>
/// Default constructor of the package.
Expand All @@ -70,6 +70,7 @@ public sealed class Gardiner_VsShowMissingPackage : Package, IVsSolutionEvents
public Gardiner_VsShowMissingPackage()
{
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", ToString()));
_gitignores = new Dictionary<string, IgnoreList>();
}

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -181,12 +182,8 @@ private void BuildEventsOnOnBuildBegin(vsBuildScope scope, vsBuildAction action)

_errorListProvider.Tasks.Clear();

var gitIgnoreFile = Path.Combine(_solutionDirectory, ".gitignore");
_gitignores = null;
if (Options.UseGitIgnore && File.Exists(gitIgnoreFile))
{
_gitignores = new IgnoreList(gitIgnoreFile);
}
_gitignores.Clear();
AddGitIgnoreFromDirectory(_solutionDirectory);

_filters = new List<Regex>();

Expand Down Expand Up @@ -224,8 +221,7 @@ private void FindMissingFiles(Project proj)
{
Debug.WriteLine($"Physical file: {file}");

if (_filters.Any(f => f.IsMatch(file)) ||
(_gitignores != null && _gitignores.IsIgnored(file, false)))
if (_filters.Any(f => f.IsMatch(file)) || (CheckIgnored(file)))
{
Debug.WriteLine("\tIgnored by filter");
continue;
Expand Down Expand Up @@ -255,6 +251,36 @@ private void FindMissingFiles(Project proj)
}
}

private bool CheckIgnored(string file)
{
if (!Options.UseGitIgnore)
{
return false;
}

var directory = Path.GetDirectoryName(file);

// Chance we're linking to a file outside our hierarchy
while (directory != null && directory.StartsWith(_solutionDirectory, StringComparison.CurrentCultureIgnoreCase))
{
var truncatedFile = file.Substring(directory.Length + 1); // allow for removing separator
if (_gitignores.ContainsKey(directory) && _gitignores[directory].IsIgnored(truncatedFile, false))
{
return true;
}

// move up to parent
directory = Path.GetDirectoryName(directory);
}

if (directory != null && _gitignores.ContainsKey(directory))
{
return _gitignores[directory].IsIgnored(file, false);
}

return false;
}

protected override void Dispose(bool disposing)
{
if (_solutionCookie != 0)
Expand Down Expand Up @@ -338,6 +364,8 @@ private void NavigateProjectItems(ProjectItems projectItems, Microsoft.Build.Eva
// If we haven't seen this directory before, find the files inside it
if (processedPhysicalDirectories.Add(directoryName))
{
AddGitIgnoreFromDirectory(directoryName);

var physicalFiles =
new DirectoryInfo(directoryName).GetFiles()
.Where(
Expand All @@ -357,6 +385,15 @@ private void NavigateProjectItems(ProjectItems projectItems, Microsoft.Build.Eva
}
}

private void AddGitIgnoreFromDirectory(string directoryName)
{
var gitIgnoreFile = Path.Combine(directoryName, ".gitignore");
if (Options.UseGitIgnore && File.Exists(gitIgnoreFile))
{
_gitignores.Add(directoryName, new IgnoreList(gitIgnoreFile));
}
}

private void SelectParentProjectInSolution(object sender, EventArgs e)
{
var error = (MissingErrorTask)sender;
Expand Down
24 changes: 15 additions & 9 deletions Libs/MAB.DotIgnore.XML

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Libs/MAB.DotIgnore.dll
Binary file not shown.
Binary file modified Libs/MAB.DotIgnore.pdb
Binary file not shown.

0 comments on commit c6d6602

Please sign in to comment.