Coder Social home page Coder Social logo

markashleybell / mab.dotignore Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 5.0 727 KB

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

License: MIT License

C# 97.68% HTML 0.46% PowerShell 1.87%
ignore-files library git gitignore dotnet

mab.dotignore's Introduction

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.

mab.dotignore's People

Contributors

atifaziz avatar jairbubbles avatar markashleybell avatar nojaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mab.dotignore's Issues

Documentation

We need:

  • Actual API documentation
  • More/better examples (including explanation of relative path requirements)
  • Contributor guide (how to get up and running, tests etc)

Strong signing

Hey, great project. However I went to use it as part of a free Visual Studio Extension I've written and VS freaks out because the assembly isn't signed.

Improve logging

This needs to be a bit more in-depth to be helpful - group matches by file so that the log for a whole list of files will be readable.

Unit tests fail on Linux

Hello, when using this NuGet package I developed on Windows and everything seemed fine on my machine.
But when I run my CI pipeline I noticed that my build is failing on Linux.

Then I tried to run the unit tests inside a Docker container and I noticed that 14 tests fails.

Steps to reproduce:

docker run -it --rm -w "/usr/src/app" mcr.microsoft.com/dotnet/core/sdk:2.1
git clone https://github.com/markashleybell/MAB.DotIgnore
cd MAB.DotIgnore
dotnet build
cd MAB.DotIgnore.Test/
dotnet test

Is this project meant to be cross-platform?

Behavior not coherent with git docs

Hello,

When looking at examples on https://git-scm.com/docs/gitignore#_examples and more precisely:

The pattern "foo/*", matches "foo/test.json" (a regular file), "foo/bar" (a directory), but it does not match "foo/bar/hello.c" (a regular file), as the asterisk in the pattern does not match "bar/hello.c" which has a slash in it.

I noticed it's not working properly with that lib, foo/bar/hello.c will be considered ignored.

[Test]
public void Test_git_example()
{
    var ignoreList = new IgnoreList( new[] {"foo/*"});
    Assert.IsTrue(ignoreList.IsIgnored("foo/bar", pathIsDirectory: true));
    Assert.IsTrue(ignoreList.IsIgnored("foo/test.json", pathIsDirectory: false));
    Assert.IsFalse(ignoreList.IsIgnored("foo/bar/hello.c", pathIsDirectory: false)); // Fails here
}

Benchmarking and performance improvements

There are several shortcuts and optimisations which could be made fairly easily, and would improve performance when dealing with large pattern and file lists:

  • When a list is built, remove (or mark as invalid) relative path rules where the parent directory has already been excluded

Add a Filter method

Should accept a list of files/FileInfos/whatever and return a copy of the list with ignored files removed (or modify original list? TBD).

Merge efforts in one C# library

Hello @markashleybell,

I recently stumbled unto another C# library (https://github.com/goelhardik/ignore) for parsing ignore files and I really like the fact that it was testing the behavior against git (using libGit2sharp).

I don't think C# needs more than one library on the subject so I'm opening a friendly discussion to see if/how efforts could be coordinated.

Let me know your thoughts!

Give base IsMatch method a clearer signature?

Currently we have public bool IsMatch(string path, bool pathIsDirectory), but it does cause a bit of "boolean blindness" unless you label the parameter... for example, without looking at the source, what does the second parameter mean in this case?

rule.IsMatch(directory.FullName, true);

Might be nicer to either replace it with an enum, or make that method internal (or even private) and add IsFileMatch and IsDirectoryMatch methods, so it's really explicit?

Add constructor which allows specification of base directory

If the same ignore list is used at various levels (e.g. solution root and project roots), relative rules do not get applied correctly in child paths (because rules are assumed to be relative to the overall root.

If we allowed a base directory to be passed in, we could dynamically replace the base path in any rules in the list with a forward slash, which would mean the list still worked correctly from within the base path.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.