Address review comments

This commit is contained in:
Tom Hvitved
2020-09-08 09:29:35 +02:00
parent 37f1ce3122
commit 4d0a1ee857
3 changed files with 27 additions and 22 deletions

View File

@@ -31,10 +31,10 @@ namespace Semmle.Extraction.Tests
var pathTransformer = new PathTransformer(new PathCacheStub(), spec);
// Windows-style matching
Assert.Equal(@"C:\bar.cs", pathTransformer.Transform(@"C:\bar.cs").Value);
Assert.Equal(@"C:/bar.cs", pathTransformer.Transform(@"C:\bar.cs").Value);
Assert.Equal("D:/src/file.cs", pathTransformer.Transform(@"C:\agent42\src\file.cs").Value);
Assert.Equal("D:/src/file.cs", pathTransformer.Transform(@"C:\agent43\src\file.cs").Value);
Assert.Equal(@"C:\agent43\src\external\file.cs", pathTransformer.Transform(@"C:\agent43\src\external\file.cs").Value);
Assert.Equal(@"C:/agent43/src/external/file.cs", pathTransformer.Transform(@"C:\agent43\src\external\file.cs").Value);
// Linux-style matching
Assert.Equal(@"src2/src/file.cs", pathTransformer.Transform(@"/agent/src/file.cs").Value);

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
@@ -15,7 +16,7 @@ namespace Semmle.Extraction
}
/// <summary>
/// An file pattern, as used in either an extractor layout file or
/// A file pattern, as used in either an extractor layout file or
/// a path transformer file.
/// </summary>
public sealed class FilePattern
@@ -27,11 +28,12 @@ namespace Semmle.Extraction
public FilePattern(string pattern)
{
Include = false;
Include = true;
if (pattern.StartsWith("-"))
{
pattern = pattern.Substring(1);
else
Include = true;
Include = false;
}
pattern = FileUtils.ConvertToUnix(pattern.Trim()).TrimStart('/');
RegexPattern = BuildRegex(pattern).ToString();
}
@@ -103,19 +105,23 @@ namespace Semmle.Extraction
public static bool Matches(IEnumerable<FilePattern> patterns, string path, [NotNullWhen(true)] out string? transformerSuffix)
{
path = FileUtils.ConvertToUnix(path).TrimStart('/');
Match? lastMatch = null;
foreach (var pattern in patterns)
foreach (var pattern in patterns.Reverse())
{
var m = new Regex(pattern.RegexPattern).Match(path);
if (m.Success)
lastMatch = pattern.Include ? m : null;
}
if (lastMatch is Match)
{
transformerSuffix = lastMatch.Groups.TryGetValue("doubleslash", out var group)
? path.Substring(group.Index)
: path;
return true;
{
if (pattern.Include)
{
transformerSuffix = m.Groups.TryGetValue("doubleslash", out var group)
? path.Substring(group.Index)
: path;
return true;
}
transformerSuffix = null;
return false;
}
}
transformerSuffix = null;

View File

@@ -154,13 +154,12 @@ namespace Semmle.Extraction
public TransformerSection(string[] lines, ref int i)
{
name = lines[i++].Substring(1);
while (i < lines.Length && !lines[i].StartsWith("#"))
name = lines[i++].Substring(1); // skip the '#'
for (; i < lines.Length && !lines[i].StartsWith("#"); i++)
{
if (string.IsNullOrEmpty(lines[i]))
i++;
else
filePatterns.Add(new FilePattern(lines[i++]));
var line = lines[i];
if (!string.IsNullOrWhiteSpace(line))
filePatterns.Add(new FilePattern(line));
}
}