Skip to content

Commit

Permalink
Respect project() clause in $copy search for full files and directories
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillOsenkov committed Jan 26, 2024
1 parent 332ac84 commit 144500f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
71 changes: 47 additions & 24 deletions src/StructuredLogger/Analyzers/FileCopyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ public bool TryGetResults(NodeQueryMatcher matcher, IList<SearchResult> resultSe
object data = TryGetDirectoryOrFile(text);
if (data is DirectoryData directoryData)
{
GetResults(directoryData, resultSet, maxResults);
GetResults(directoryData, resultSet, matcher, maxResults);
}
else if (data is FileData fileData)
{
GetResults(fileData, resultSet, maxResults);
GetResults(fileData, resultSet, matcher, maxResults);
if (resultSet.Count == 1)
{
FoundSingleFileCopy?.Invoke(fileData, resultSet);
Expand Down Expand Up @@ -340,35 +340,43 @@ private void TryGetFiles(string text, IList<SearchResult> resultSet, NodeQueryMa

private bool FileMatches(FileData file, NodeQueryMatcher matcher)
{
foreach (var includeMatcher in matcher.IncludeMatchers)
foreach (var fileCopyInfo in file.Incoming.Concat(file.Outgoing))
{
if (includeMatcher.UnderProject)
if (FileCopyMatches(fileCopyInfo, matcher))
{
bool matched = false;
return true;
}
}

foreach (var fileCopyInfo in file.Incoming.Concat(file.Outgoing))
{
if (fileCopyInfo.Project is { } project)
{
if (includeMatcher.IsMatch(project.Name, project.SourceFilePath) != null)
{
matched = true;
break;
}
}
}
return false;
}

if (!matched)
{
return false;
}
private bool FileCopyMatches(FileCopyInfo fileCopyInfo, NodeQueryMatcher matcher)
{
if (fileCopyInfo.Project is not { } project)
{
return true;
}

var projectMatchers = matcher.ProjectMatchers;
if (projectMatchers.Count == 0)
{
return true;
}

foreach (var includeMatcher in projectMatchers)
{
var matchResult = includeMatcher.IsMatch(project.Name, project.SourceFilePath);
if (matchResult != null)
{
return true;
}
}

return true;
return false;
}

private void GetResults(FileData fileData, IList<SearchResult> resultSet, int maxResults, string matchText = null)
private void GetResults(FileData fileData, IList<SearchResult> resultSet, NodeQueryMatcher matcher, int maxResults, string matchText = null)
{
if (matchText == null)
{
Expand All @@ -377,6 +385,11 @@ private void GetResults(FileData fileData, IList<SearchResult> resultSet, int ma

foreach (var incoming in fileData.Incoming)
{
if (!FileCopyMatches(incoming, matcher))
{
continue;
}

var message = incoming.FileCopyOperation.Message;
var result = new SearchResult(message);
result.AddMatch(message.Text, matchText);
Expand All @@ -390,6 +403,11 @@ private void GetResults(FileData fileData, IList<SearchResult> resultSet, int ma

foreach (var outgoing in fileData.Outgoing)
{
if (!FileCopyMatches(outgoing, matcher))
{
continue;
}

var message = outgoing.FileCopyOperation.Message;
var result = new SearchResult(message);
result.AddMatch(message.Text, matchText);
Expand All @@ -402,13 +420,18 @@ private void GetResults(FileData fileData, IList<SearchResult> resultSet, int ma
}
}

private void GetResults(DirectoryData directoryData, IList<SearchResult> resultSet, int maxResults)
private void GetResults(DirectoryData directoryData, IList<SearchResult> resultSet, NodeQueryMatcher matcher, int maxResults)
{
string directoryPath = directoryData.ToString();

foreach (var fileData in directoryData.Files)
{
GetResults(fileData, resultSet, maxResults, matchText: directoryPath);
if (!FileMatches(fileData, matcher))
{
continue;
}

GetResults(fileData, resultSet, matcher, maxResults, matchText: directoryPath);
if (resultSet.Count >= maxResults)
{
return;
Expand Down
11 changes: 11 additions & 0 deletions src/StructuredLogger/Search/NodeQueryMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public class NodeQueryMatcher

public IList<NodeQueryMatcher> IncludeMatchers { get; } = new List<NodeQueryMatcher>();
public IList<NodeQueryMatcher> ExcludeMatchers { get; } = new List<NodeQueryMatcher>();
public IList<NodeQueryMatcher> ProjectMatchers { get; } = new List<NodeQueryMatcher>();

// avoid allocating this for every node
[ThreadStatic]
Expand Down Expand Up @@ -232,6 +233,7 @@ private void ParseTerms()
var underMatcher = new NodeQueryMatcher(word);
underMatcher.UnderProject = true;
IncludeMatchers.Add(underMatcher);
ProjectMatchers.Add(underMatcher);
continue;
}

Expand Down Expand Up @@ -736,6 +738,15 @@ public SearchResult IsMatch(string field)
return result ?? SearchResult.EmptyQueryMatch;
}

/// <summary>
/// Matches Terms against the given set of fields
/// </summary>
/// <param name="fields">Fields to match against</param>
/// <returns>
/// EmptyQueryMatch if there are no terms.
/// null if there was a term that didn't match any fields.
/// SearchResult if all terms matched at least one field.
/// </returns>
public SearchResult IsMatch(params string[] fields)
{
SearchResult result = null;
Expand Down

0 comments on commit 144500f

Please sign in to comment.