GlobCollection Class

Represents a collection of glob patterns that can be evaluated together.

Example

Combine multiple glob patterns with include and exclude rules:

var globs = new GlobCollection(
    Glob.Parse("**/*.txt", GlobDialect.Standard),
    Glob.Parse("!temp/**/*", GlobDialect.Standard));

foreach (var file in globs.EnumerateFiles("C:/MyProject"))
{
    Console.WriteLine(file);
}
Implements

Constructors

NameDescription
GlobCollection(IGlobEvaluatable[])Initializes a new instance of the GlobCollection class.

GlobCollection(IGlobEvaluatable[])

GlobCollection.cs

Initializes a new instance of the GlobCollection class.

public GlobCollection(params IGlobEvaluatable[] globs)

Parameters

globs IGlobEvaluatable[]
The glob patterns to include in the collection. The collection keeps a copy of the array.

Exceptions

ArgumentNullException
globs is null.
ArgumentException
globs contains a null element.

Properties

NameDescription
CountGets the number of glob patterns in the collection.
Meziantou.Framework.Globbing.IGlobEvaluatable.CanMatchDirectories
Meziantou.Framework.Globbing.IGlobEvaluatable.CanMatchFiles
Meziantou.Framework.Globbing.IGlobEvaluatable.Mode
Meziantou.Framework.Globbing.IGlobEvaluatable.TraverseDirectories

Gets the number of glob patterns in the collection.

public int Count { get; }

Property value

int

Meziantou.Framework.Globbing.IGlobEvaluatable.CanMatchDirectories

GlobCollection.cs
bool IGlobEvaluatable.CanMatchDirectories { get; }

Property value

bool

No documentation is available.

Meziantou.Framework.Globbing.IGlobEvaluatable.CanMatchFiles

GlobCollection.cs
bool IGlobEvaluatable.CanMatchFiles { get; }

Property value

bool

No documentation is available.

Meziantou.Framework.Globbing.IGlobEvaluatable.Mode

GlobCollection.cs

Property value

GlobMode

No documentation is available.

Meziantou.Framework.Globbing.IGlobEvaluatable.TraverseDirectories

GlobCollection.cs
bool IGlobEvaluatable.TraverseDirectories { get; }

Property value

bool

No documentation is available.

Indexers

NameDescription
this[int]Gets the glob pattern at the specified index.

Gets the glob pattern at the specified index.

public IGlobEvaluatable this[int index] { get; }

Parameters

index int
The zero-based index of the glob pattern to get.

Property value

IGlobEvaluatable

Methods

NameDescription
Create(ReadOnlySpan<IGlobEvaluatable>)
GetEnumerator()
IsMatch(ReadOnlySpan<char>, ReadOnlySpan<char>, PathItemType?)Determines whether the specified path matches any pattern in the collection.
IsPartialMatch(ReadOnlySpan<char>, ReadOnlySpan<char>)Determines whether a directory should be recursed into when enumerating files.
LoadGitIgnoreAsync(Stream, CancellationToken)Loads a gitignore stream asynchronously and creates a GlobCollection from it.
LoadGitIgnoreAsync(TextReader, CancellationToken)Loads a gitignore reader asynchronously and creates a GlobCollection from it.
LoadGitIgnoreAsync(string, string?, CancellationToken)Loads a gitignore file located in a subdirectory asynchronously and creates a GlobCollection from it.
LoadGitIgnoreAsync(string, CancellationToken)Loads a gitignore file asynchronously and creates a GlobCollection from it.
ParseGitIgnore(ReadOnlySpan<char>)Loads gitignore content and creates a GlobCollection from it.
ParseGitIgnore(ReadOnlySpan<char>, string?)Loads the content of a gitignore file located in a subdirectory and creates a GlobCollection from it.
System.Collections.IEnumerable.GetEnumerator()

Create(ReadOnlySpan<IGlobEvaluatable>)

GlobCollection.cs
public static GlobCollection Create(ReadOnlySpan<IGlobEvaluatable> globs)

No documentation is available.

GetEnumerator()

GlobCollection.cs
public IEnumerator<IGlobEvaluatable> GetEnumerator()

No documentation is available.

IsMatch(ReadOnlySpan<char>, ReadOnlySpan<char>, PathItemType?)

GlobCollection.cs

Determines whether the specified path matches any pattern in the collection.

public bool IsMatch(ReadOnlySpan<char> directory, ReadOnlySpan<char> filename, PathItemType? itemType)

Parameters

directory ReadOnlySpan<char>
The directory part of the path to match.
filename ReadOnlySpan<char>
The filename part of the path to match.
itemType PathItemType?
The type of the path item (file or directory), or null if unknown.

Returns

bool true if the path matches any include pattern and no exclude pattern; otherwise, false. A collection created from gitignore content resolves the path against the last pattern that matches it instead, as git does, and reports a path as matched when one of its ancestor directories is: git cannot re-include a path whose parent directory is excluded.

IsPartialMatch(ReadOnlySpan<char>, ReadOnlySpan<char>)

GlobCollection.cs

Determines whether a directory should be recursed into when enumerating files.

public bool IsPartialMatch(ReadOnlySpan<char> folderPath, ReadOnlySpan<char> filename)

Parameters

folderPath ReadOnlySpan<char>
The folder path to check.
filename ReadOnlySpan<char>
The filename part of the path to check.

Returns

bool true if the directory could contain matches; otherwise, false. A directory whose whole content is excluded by an exclude pattern, such as !**/node_modules/**, cannot contain matches. For a collection created from gitignore content, a directory can contain matches when it is excluded, or when a pattern can exclude a path below it.

LoadGitIgnoreAsync overloads

LoadGitIgnoreAsync(Stream, CancellationToken)

GlobCollection.cs

Loads a gitignore stream asynchronously and creates a GlobCollection from it.

net10.0

public static Task<GlobCollection> LoadGitIgnoreAsync(Stream stream, CancellationToken cancellationToken = default)

net11.0

public static Task<GlobCollection> LoadGitIgnoreAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
stream Stream
The stream containing the gitignore content.
cancellationToken CancellationToken
The cancellation token.
Returns

Task<GlobCollection>

Remarks

The content is decoded as UTF-8, unless it starts with the byte order mark of another encoding (UTF-16 or UTF-32). As git does, only that first byte order mark is skipped.

LoadGitIgnoreAsync(TextReader, CancellationToken)

GlobCollection.cs

Loads a gitignore reader asynchronously and creates a GlobCollection from it.

net10.0

public static Task<GlobCollection> LoadGitIgnoreAsync(TextReader reader, CancellationToken cancellationToken = default)

net11.0

public static Task<GlobCollection> LoadGitIgnoreAsync(TextReader reader, CancellationToken cancellationToken = default)
Parameters
reader TextReader
The reader providing the gitignore content.
cancellationToken CancellationToken
The cancellation token.
Returns

Task<GlobCollection>

LoadGitIgnoreAsync(string, string?, CancellationToken)

GlobCollection.cs

Loads a gitignore file located in a subdirectory asynchronously and creates a GlobCollection from it.

net10.0

public static Task<GlobCollection> LoadGitIgnoreAsync(string path, string? baseDirectory, CancellationToken cancellationToken = default)

net11.0

public static Task<GlobCollection> LoadGitIgnoreAsync(string path, string? baseDirectory, CancellationToken cancellationToken = default)
Parameters
path string
The path to the gitignore file.
baseDirectory string?
The directory that holds the gitignore file, relative to the root of the paths given to the collection, such as src/App. null, an empty string or . stand for the root.
cancellationToken CancellationToken
The cancellation token.
Returns

Task<GlobCollection>

Remarks

The file is decoded as GlobCollection.LoadGitIgnoreAsync decodes a stream. The rules apply as described in GlobCollection.ParseGitIgnore.

LoadGitIgnoreAsync(string, CancellationToken)

GlobCollection.cs

Loads a gitignore file asynchronously and creates a GlobCollection from it.

public static Task<GlobCollection> LoadGitIgnoreAsync(string path, CancellationToken cancellationToken = default)
Parameters
path string
The path to the gitignore file.
cancellationToken CancellationToken
The cancellation token.
Returns

Task<GlobCollection>

Remarks

The file is decoded as GlobCollection.LoadGitIgnoreAsync decodes a stream.

ParseGitIgnore overloads

ParseGitIgnore(ReadOnlySpan<char>)

GlobCollection.cs

Loads gitignore content and creates a GlobCollection from it.

public static GlobCollection ParseGitIgnore(ReadOnlySpan<char> gitIgnoreContent)
Parameters
gitIgnoreContent ReadOnlySpan<char>
The gitignore content.
Returns

GlobCollection

Remarks

As git does, an entry that no path can match, such as one holding an unterminated bracket expression or ending with an escape character, is ignored instead of making the whole content invalid.

ParseGitIgnore(ReadOnlySpan<char>, string?)

GlobCollection.cs

Loads the content of a gitignore file located in a subdirectory and creates a GlobCollection from it.

public static GlobCollection ParseGitIgnore(ReadOnlySpan<char> gitIgnoreContent, string? baseDirectory)
Parameters
gitIgnoreContent ReadOnlySpan<char>
The gitignore content.
baseDirectory string?
The directory that holds the gitignore file, relative to the root of the paths given to the collection, such as src/App. null, an empty string or . stand for the root.
Returns

GlobCollection

Remarks

The rules only apply to the paths below baseDirectory, and they are matched relative to it, as git does for the .gitignore file of a subdirectory. The collection does not match the other paths, nor baseDirectory itself.

System.Collections.IEnumerable.GetEnumerator()

GlobCollection.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.