Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't respect "match-beginning" gitignore patterns in subdirs #285

Open
purcell opened this issue Oct 28, 2013 · 31 comments
Open

Doesn't respect "match-beginning" gitignore patterns in subdirs #285

purcell opened this issue Oct 28, 2013 · 31 comments
Labels

Comments

@purcell
Copy link

purcell commented Oct 28, 2013

In .gitignore files, ignore patterns can be prefixed with a "/". man gitignore says:

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c"
but not "mozilla-sha1/sha1.c".

ag currently doesn't apply such patterns when they match files in subdirectories. Steps to reproduce:

% mkdir ag-test                                                                                            % cd ag-test                                                                                               % mkdir ignored
% echo 'hello' > ignored/a
% echo 'hello' > b
% ag hello
b
1:hello

ignored/a
1:hello
% echo '/ignored/*' > .gitignore  ## should ignore "a", but doesn't
% ag hello
b
1:hello

ignored/a
1:hello
% echo 'ignored/*' > .gitignore
% ag hello
b
1:hello

However, the non-subdir case currently works correctly:

% echo '/b' > .gitignore
% ag hello
ignored/a
1:hello

Other than this minor hiccup, it's awesomeness all the way. :-)

@mcphail
Copy link
Contributor

mcphail commented Oct 28, 2013

What filesystem are you using to test this? Is this affected by a similar problem to issue #275?

@purcell
Copy link
Author

purcell commented Oct 28, 2013

This is on OS X, so HFS+.

While initially attempting to create "steps to reproduce" as above, I certainly noticed something strange, whereby the presence of one file seemed to affect the "searchability" of another, but I unfortunately neglected to note my actions or the exact details. That behaviour does sound related to #275.

@mcphail
Copy link
Contributor

mcphail commented Oct 28, 2013

Can you try pull request #284 to see if it helps at all?

@purcell
Copy link
Author

purcell commented Oct 28, 2013

Yep, I can hopefully give that a try later today.

mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Oct 28, 2013
Fix ggreer#285 but needs testing for more complex directories
@purcell
Copy link
Author

purcell commented Oct 28, 2013

Bah, having a non-trivial time getting this to compile here on my Mac. Do you happen to know the magic configure incantation to include the homebrew libs & headers?

@mcphail
Copy link
Contributor

mcphail commented Oct 28, 2013

I don't have OSX so I'm not sure I can help. I'd have thought pkg-config would have taken the strain. Have you installed the dependencies from the build section of the README.md and run ./build.sh? Beyond that, ./configure can accept variables like ./configure LDFLAGS='-Lwhatever_lib_dir' LIBS='-lwhatever_lib' but really pkg-config should be doing that for you automatically.

@purcell
Copy link
Author

purcell commented Oct 28, 2013

Thanks. No, pkg-config isn't making everything work magically, even though it's the one from homebrew, and all the deps are installed there. I'll have a further tinker with it in the morning: I'd like to be able to give some helpful feedback.

@purcell
Copy link
Author

purcell commented Oct 29, 2013

Okay, got it built -- sorry for the noise. So PR #284 doesn't fix this specific problem: the ignored/a file doesn't get ignored. :-(

mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Oct 29, 2013
Fix ggreer#285 but needs testing for more complex directories
@mcphail
Copy link
Contributor

mcphail commented Oct 29, 2013

Hmm. That's odd. #284 works on Linux on both hfsplus and ntfs filesystems here.

I'm sure this patch is getting close to the nub of the problem. I can't quite get my head around the recursion and appropriate manipulation of scandir_baton->level and ig->parent to do the right thing. I'll keep playing with files until it breaks again so I can debug it further.

mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Oct 30, 2013
Fix ggreer#285 but needs testing for more complex directories
@mcphail
Copy link
Contributor

mcphail commented Oct 30, 2013

Can you try the current version of #284 and see if it fixes your problem?

@purcell
Copy link
Author

purcell commented Oct 30, 2013

I updated to the latest version of the PR (7f4f4d1) and nope, it still doesn't ignore ignored/a in the scenario above.

@mcphail
Copy link
Contributor

mcphail commented Oct 30, 2013

Back to the drawing board! To be honest, I'm struggling here. It is working fine on Linux with various filesystems and I don't think I'm going to be able to take it further without running the debugger on a Mac. I wonder if the problem is even more esoteric. Is there a difference in pcre? Perhaps some difference in the Linux and Mac implementations in HFS+? (Or, more likely, my code just stinks...!)

@purcell
Copy link
Author

purcell commented Oct 30, 2013

I'll dig into the code myself when I get a chance and see if I can figure things out. Would be nice to have a simple shell-scripted test suite for the various exclusion patterns etc.

@mcphail
Copy link
Contributor

mcphail commented Oct 30, 2013

Does the ignore still fail if you remove the asterisks from the .gitignore patterns?

@purcell
Copy link
Author

purcell commented Oct 30, 2013

You know, scratch that -- your fix works. I was picking up the wrong ag in my environment.

@purcell
Copy link
Author

purcell commented Oct 30, 2013

Sorry.

@purcell
Copy link
Author

purcell commented Oct 30, 2013

Now all of the following patterns correctly ignore ignored/a:

/ignored/*
/ignored/
ignored/
ignored/*
/ignored
ignored
a
/a

@mcphail
Copy link
Contributor

mcphail commented Oct 30, 2013

Ha! Great.

@nebirhos
Copy link

Hi folks! This fix has been published somewhere?

@mcphail
Copy link
Contributor

mcphail commented Oct 31, 2013

You can use the code from my pull request #284 but it hasn't been merged into the master branch by @ggreer yet.

@nebirhos
Copy link

great, thanks!

mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Nov 4, 2013
Fix ggreer#285 but needs testing for more complex directories
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Nov 6, 2013
Fix ggreer#285 but needs testing for more complex directories
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Nov 29, 2013
Fix ggreer#285 but needs testing for more complex directories
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Dec 4, 2013
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Dec 4, 2013
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Dec 8, 2013
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
@raine
Copy link

raine commented Dec 14, 2013

I assume this hasn't been fixed yet in any released version?

Got slightly confused when ag started acting weird after making a change to gitignore.

More specifically, replaced dir/ with /dir which makes git ignore the path from root only, but ag doesn't see it that way.

@deepfire
Copy link

Ok, I just submitted a duplicate bug for this..

@purcell
Copy link
Author

purcell commented Dec 21, 2013

Hey @ggreer -- would you mind reviewing #284 at some point please, which is a fix for this issue and the duplicate issue @deepfire just filed & closed? If @mcphail would need to make any further changes in order for the PR to be acceptable, I'm sure he'd be happy to do so. :-)

mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Jan 22, 2014
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Jan 24, 2014
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Jan 31, 2014
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
mcphail pushed a commit to mcphail/the_silver_searcher that referenced this issue Feb 19, 2014
Attempt to fix issue 275

Only increment the scandir baton when recursing

Fix ggreer#285 but needs testing for more complex directories

Check ignore paths based on correct baton level

Fix ggreer#287 but doesn't fix ggreer#144

Stop comparing terminating NULL in path string

Might save a few cycles...

Handle absolute .gitignore patterns properly

Pattern /b/c/d should ignore /b/c/d but not a/b/c/d
@ggreer ggreer added the ignore label Sep 26, 2014
@ctrueden
Copy link

I also filed a duplicate of this issue a while back (#595). FWIW, I wrote a patch to the cram tests that illustrates the problem, and could be handy in verifying a future fix: https://github.com/ctrueden/the_silver_searcher/commit/4f1399723ecb6db069af455b2f37ee38b7e3ddab

@cartolari
Copy link

I can also confirm the issue. And another problem that I think it's still related to the .gitignore file is a case where you ignore a entire folder but some files as exemplified by the following hypothetical .gitigore:

ignored/*
!ignored/not_ignored

If I search for some content the file ignored/not_ignored will not be included in the files for the search.

@bdesham
Copy link
Contributor

bdesham commented Aug 13, 2015

This fix has still not been merged in, correct?

@ctrueden
Copy link

@bdesham I don't think anyone has actually submitted a patch that fixes this... have they?

@bdesham
Copy link
Contributor

bdesham commented Aug 14, 2015

@ctrueden After reading through this thread again, I guess you’re right. PR #284 fixed this but @mcphail closed it because it went so long without being merged 😐

@mcphail
Copy link
Contributor

mcphail commented Aug 14, 2015

I'm afraid there have been layers of breakage added to the ignore code since I made that patch, so there is no way I could keep it rebased against upstream. You can use my fork if you want to try the code. It isn't perfect, but I'm sure some enterprising soul could fix the edge cases where it breaks.

@mbaer3000
Copy link

Can confirm the issue. It's a really painful having ag go through dist assets -- which are commonly exempted from version control via "/dist" in the .gitignore file.

alexkuang added a commit to alexkuang/dotfiles that referenced this issue Nov 10, 2019
because ag picks up too many dist + node_module files:
ggreer/the_silver_searcher#285

(and possibly no longer maintained?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants