hgrep icon indicating copy to clipboard operation
hgrep copied to clipboard

Understand .gitignore

Open thumphries opened this issue 7 years ago • 7 comments

We've now got the great ability to recursively traverse directories! (as of #43 )

It would be great to parse and make sense of .gitignore files while doing this.

For example, Haskell files in dist or dist-newstyle should not appear in results.

thumphries avatar Oct 12 '17 12:10 thumphries

What should we do if we're not in the project root directory or we can't find a .gitignore? Pretend it doesn't exist or recursively search parent directories to find one?

vaibhavsagar avatar Oct 12 '17 14:10 vaibhavsagar

... that's a very good question! We should try to figure out what ag / rg do. I feel pretty comfortable copying them.

thumphries avatar Oct 12 '17 14:10 thumphries

In my experience, rg generally tends to be "more correct" in terms of handling .gitignore files than ag although both have incredibly similar/identical behavior (from reading up the docs just now to confirm). I'm just providing the behavior of rg here for reference:

  • Without any extra arguments, rg will automatically respect all .ignore and .gitignore files, ignore hidden files and directories, and skip binary files.
    • If the .ignore/.gitignore file exists in a parent or current folder, it's applied; if it exists in a child folder it applies to files only in that child and lower, and so on.
  • Paramaters are provided to selectively increase how broad the search is: -u ignores all ignore files, -uu searches hidden files and directories, -uuu searches binary files as well.

hazelweakly avatar Oct 17 '17 21:10 hazelweakly

Looks like rg uses https://docs.rs/ignore/0.2.2/ignore/gitignore/index.html for handling .gitignore files.

novemberkilo avatar Oct 18 '17 09:10 novemberkilo

I don't see any equivalent Haskell library on Hackage. Not sure how complex the semantics of gitignore are, but it might be a worthwhile little standalone library :)

@jared-w (hey! thanks for contributing!), do you know how far rg will go up the tree to find .gitignore? e.g. if I run it in /usr/bin/share, will it try to stat /.gitignore?

Skipping binary files always seems like a good idea (although we're filtering by file extension already, you can't be too careful).

thumphries avatar Oct 18 '17 11:10 thumphries

Totally on board for -u -uu -uuu, sounds like a great thing to copy!

thumphries avatar Oct 18 '17 11:10 thumphries

There is this http://hackage.haskell.org/package/ignore although it seems very different from the Rust library referenced above. Will spend a bit of time thinking through what is involved in porting the the Rust ignore across to Haskell.

Also, regarding your question regarding the behaviour of rg and .gitignore, if I have understood the preamble in the Rust library correctly, it claims to do what is described in the .gitignore man page.

novemberkilo avatar Oct 19 '17 09:10 novemberkilo