forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindFilePredicateProvider.cs
42 lines (36 loc) · 1.46 KB
/
FindFilePredicateProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using GitCommands;
namespace GitUI
{
public interface IFindFilePredicateProvider
{
/// <summary>
/// Returns the names of files that match the specified search pattern.
/// </summary>
/// <param name="searchPattern">The search string to match against the paths of files.</param>
Func<string?, bool> Get(string searchPattern, string workingDir);
}
public sealed class FindFilePredicateProvider : IFindFilePredicateProvider
{
public Func<string?, bool> Get(string searchPattern, string workingDir)
{
if (searchPattern is null)
{
throw new ArgumentNullException(nameof(searchPattern));
}
if (workingDir is null)
{
throw new ArgumentNullException(nameof(workingDir));
}
var pattern = searchPattern.ToPosixPath();
var dir = workingDir.ToPosixPath();
if (pattern.StartsWith(dir, StringComparison.OrdinalIgnoreCase))
{
pattern = pattern.Substring(dir.Length).TrimStart('/');
return fileName => fileName is not null && fileName.StartsWith(pattern, StringComparison.OrdinalIgnoreCase);
}
// Method Contains have no override with StringComparison parameter
return fileName => fileName is not null && fileName.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0;
}
}
}