mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Address review comments
This commit is contained in:
@@ -31,10 +31,10 @@ namespace Semmle.Extraction.Tests
|
|||||||
var pathTransformer = new PathTransformer(new PathCacheStub(), spec);
|
var pathTransformer = new PathTransformer(new PathCacheStub(), spec);
|
||||||
|
|
||||||
// Windows-style matching
|
// 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:\agent42\src\file.cs").Value);
|
||||||
Assert.Equal("D:/src/file.cs", pathTransformer.Transform(@"C:\agent43\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
|
// Linux-style matching
|
||||||
Assert.Equal(@"src2/src/file.cs", pathTransformer.Transform(@"/agent/src/file.cs").Value);
|
Assert.Equal(@"src2/src/file.cs", pathTransformer.Transform(@"/agent/src/file.cs").Value);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
@@ -15,7 +16,7 @@ namespace Semmle.Extraction
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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.
|
/// a path transformer file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class FilePattern
|
public sealed class FilePattern
|
||||||
@@ -27,11 +28,12 @@ namespace Semmle.Extraction
|
|||||||
|
|
||||||
public FilePattern(string pattern)
|
public FilePattern(string pattern)
|
||||||
{
|
{
|
||||||
Include = false;
|
Include = true;
|
||||||
if (pattern.StartsWith("-"))
|
if (pattern.StartsWith("-"))
|
||||||
|
{
|
||||||
pattern = pattern.Substring(1);
|
pattern = pattern.Substring(1);
|
||||||
else
|
Include = false;
|
||||||
Include = true;
|
}
|
||||||
pattern = FileUtils.ConvertToUnix(pattern.Trim()).TrimStart('/');
|
pattern = FileUtils.ConvertToUnix(pattern.Trim()).TrimStart('/');
|
||||||
RegexPattern = BuildRegex(pattern).ToString();
|
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)
|
public static bool Matches(IEnumerable<FilePattern> patterns, string path, [NotNullWhen(true)] out string? transformerSuffix)
|
||||||
{
|
{
|
||||||
path = FileUtils.ConvertToUnix(path).TrimStart('/');
|
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);
|
var m = new Regex(pattern.RegexPattern).Match(path);
|
||||||
if (m.Success)
|
if (m.Success)
|
||||||
lastMatch = pattern.Include ? m : null;
|
{
|
||||||
}
|
if (pattern.Include)
|
||||||
if (lastMatch is Match)
|
{
|
||||||
{
|
transformerSuffix = m.Groups.TryGetValue("doubleslash", out var group)
|
||||||
transformerSuffix = lastMatch.Groups.TryGetValue("doubleslash", out var group)
|
? path.Substring(group.Index)
|
||||||
? path.Substring(group.Index)
|
: path;
|
||||||
: path;
|
return true;
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
transformerSuffix = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transformerSuffix = null;
|
transformerSuffix = null;
|
||||||
|
|||||||
@@ -154,13 +154,12 @@ namespace Semmle.Extraction
|
|||||||
|
|
||||||
public TransformerSection(string[] lines, ref int i)
|
public TransformerSection(string[] lines, ref int i)
|
||||||
{
|
{
|
||||||
name = lines[i++].Substring(1);
|
name = lines[i++].Substring(1); // skip the '#'
|
||||||
while (i < lines.Length && !lines[i].StartsWith("#"))
|
for (; i < lines.Length && !lines[i].StartsWith("#"); i++)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(lines[i]))
|
var line = lines[i];
|
||||||
i++;
|
if (!string.IsNullOrWhiteSpace(line))
|
||||||
else
|
filePatterns.Add(new FilePattern(line));
|
||||||
filePatterns.Add(new FilePattern(lines[i++]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user