Code Monkey home page Code Monkey logo

nix-gitignore's Introduction

Supposedly (I haven't used it), there's a better version of nix-gitignore (inspired by this one) over at https://github.com/hercules-ci/gitignore that has the details of its functionality and superiority in its README. I hope you found this project useful and thank you for the many github stars. :)

nix-gitignore

(for nix 2.0 or higher)

This implements primitive a gitignore filter for builtins.filterSource via translation to regexes. Please add give this a star iff this project proves to be useful to you.

Motivation

If you want to deploy your code from the development directory, it would make sense to clean out the development/tmp/cache files before copying your project's source to the nix store. The set of development files you'll want to clean is likely the same one your gitignore patterns match, so this is why this is useful.

Example

This project has been included in nixpkgs since 2019.02.18. and it should land in the 19.03 release, so you can start using it like this:

with (import <nixpkgs> {});

let
  additionalIgnores = ''
    /this
    /that/**.html
  '';

  source = nix-gitignore.gitignoreSource additionalIgnores ./source;
in
  "use ${source} here"
Here's the `fetchFromGitHub` nix example if it's ever needed.

fetchFromGitHub example

Replace the rev and sha256 lines with the output of this command:

nix-prefetch-git https://github.com/siers/nix-gitignore 2> /dev/null | jq -r '"rev = \"\(.rev)\";\nsha256 = \"\(.sha256)\";"'

in this snippet:

with (import <nixpkgs> {});

let
  gitignore = callPackage (pkgs.fetchFromGitHub {
    owner = "siers";
    repo = "nix-gitignore";
    rev = "…";
    sha256 = "…";
  }) {};
in
  with gitignore;

let
  additionalIgnores = ''
    /this
    /that/**.html
  '';

  source = gitignoreSource additionalIgnores ./source;
in
  "use ${source} here"

Usage

The default.nix exports (among other things) four functions. Three of these are:

gitignoreSource [] ./source
    # Simplest version

gitignoreSource "supplemental-ignores\n" ./source
    # This one reads the ./source/.gitignore and concats the auxiliary ignores

gitignoreSourcePure "ignore-this\nignore-that\n" ./source
    # Use this string as gitignore, don't read ./source/.gitignore.

gitignoreSourcePure ["ignore-this\nignore-that\n" ~/.gitignore] ./source
    # It also accepts a list (of strings and paths) that will be concatenated
    # once the paths are turned to strings via readFile.

They're all derived from the Filter functions with the first filter argument hardcoded as (_: _: true):

gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
gitignoreSource = gitignoreFilterSource (_: _: true);

The filter accepts the same arguments the filterSource function would pass to its filters. Thus fn: gitignoreFilterSourcePure fn "" is extensionally equivalent to filterSource. The file is blacklisted iff it's blacklisted by either your filter or the gitignoreFilter.

If you want to make your own filter from scratch, you may use

gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;

.gitignore files in subdirectories

If you wish to use a filter that would search for .gitignore files in subdirectories, just like git does by default, use this function.

gitignoreFilterRecursiveSource = filter: patterns: root:
gitignoreRecursiveSource = gitignoreFilterRecursiveSource (_: _: true);

Testing

I highly recommend taking a look at the test files test.nix and test.sh which show how closely the actual git implementation's being mimicked. If you find any deviances, please file an issue. Even though it probably works 99% of the time, the pattern [^b/]har-class-pathalogic is the only one found that doesn't work like in git.

nix-gitignore's People

Contributors

addict3d avatar mightybyte avatar obfusk avatar roberth avatar siers avatar srghma avatar tomberek avatar

Watchers

 avatar  avatar

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.