Skip to content

Check whether a file path should be ignored, based on a .gitignore file.

License

Notifications You must be signed in to change notification settings

markashleybell/MAB.DotIgnore

Repository files navigation

MAB.DotIgnore

NuGet

Load and parse .gitignore files (or any text files using the same syntax) to produce an IgnoreList which can be used to ignore specific files and folders during (for example) a recursve file copy operation.

The goal of this library is to implement the same file matching behaviour Git uses to determine which files to ignore when adding to a commit. Please feel free to submit any issues with examples or pull requests, if you find instances where this isn't the case.

Basic usage:

.gitignore file contents

*.txt

C# code

var ignores = new IgnoreList(@"path\to\my\.gitignore");

ignores.IsIgnored(@"path\to\ignore.txt"); // Returns true
ignores.IsIgnored(@"path\to\include.cs"); // Returns false

Example usage

A quick example illustrating how you might integrate an IgnoreList into a copy routine:

public static void CopyWithIgnores(DirectoryInfo source, DirectoryInfo target, IgnoreList ignores)
{
    foreach (DirectoryInfo dir in source.GetDirectories().Where(d => !ignores.IsIgnored(d)))
        CopyWithIgnores(dir, target.CreateSubdirectory(dir.Name), ignores);

    foreach (FileInfo file in source.GetFiles().Where(f => !ignores.IsIgnored(f)))
        file.CopyTo(Path.Combine(target.FullName, file.Name));
}

var source = new DirectoryInfo(@"c:\source");
var destination = new DirectoryInfo(@"c:\destination");

var ignores = new IgnoreList(@"c:\source\.gitignore");

CopyWithIgnores(source, destination, ignores);

Note: this code is not an example of good or efficient recursive copying: it's just here to illustrate the use of this library.