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

document the difference between -tmd and -g*.md #1635

Closed
lamyergeier opened this issue Jul 4, 2020 · 5 comments
Closed

document the difference between -tmd and -g*.md #1635

lamyergeier opened this issue Jul 4, 2020 · 5 comments
Labels
doc An issue with or an improvement to documentation. rollup A PR that has been merged with many others in a rollup.

Comments

@lamyergeier
Copy link

lamyergeier commented Jul 4, 2020

What version of ripgrep are you using?

ripgrep 12.0.1
-SIMD -AVX (compiled)
+SIMD +AVX (runtime)

How did you install ripgrep?

linuxbrew

What operating system are you using ripgrep on?

Ubuntu 20.04

Describe your bug.

rg does not respects .gitignore. See example below:

What is the actual behavior?

Test-Glob() {
	mkdir -p Temp
	cd Temp || exit
	git init > /dev/null
	cp "../Data/.gitignore" .
	mkdir -p a/.Commit && touch a/.Commit/a.md
	touch a/a.md
	echo "PWD= ${PWD}"
	echo "${Blue:=}------------Cat gitignore--------------------${Normal:=}"
	cat .gitignore
	echo "${Blue:=}------------List files not ignored--------------------${Normal:=}"
	git check-ignore -v ./**/*
	echo "${Blue:=}------------Find--------------------${Normal:=}"
	find . -type f -name "*.md"
	echo "${Blue:=}------------Ripgrep Glob--------------------${Normal:=}"
	rg --glob "*.md" --files
	echo "${Blue:=}------------Ripgrep--------------------${Normal:=}"
	rg --files
	cd ..
	#Trash Temp > /dev/null
}
PWD= /home/nikhil/Documents/Git/Cs/Architecture/ArchitectureSrc/Hardware/MemoryUnit/Harddisk/FileSystem/File/ListSearch/AckAgRipgrep/Src/Temp
------------Cat gitignore--------------------
# Ignore everything
*

# Unignore all dirs
!*/

# Unignore
!*.md

# Ignore directory
/**/.Commit/**
------------List files not ignored--------------------
.gitignore:8:!*.md      ./a/a.md
------------Find--------------------
./a/a.md
./a/.Commit/a.md
------------Ripgrep Glob--------------------
a/a.md
a/.Commit/a.md
------------Ripgrep--------------------
a/a.md

What is the expected behavior?

rg should ignore ./a/.Commit/a.md

@lamyergeier lamyergeier changed the title rg does not respects .gitignore rg --glob does not respects .gitignore Jul 4, 2020
@BurntSushi
Copy link
Owner

Thank you for the detailed bug report, but this is correct behavior. Glob overrides always have the highest precedent. So if you use -g '*.md' and ripgrep would otherwise descend into a directory containing a .md file (i.e., the directory itself wasn't ignored), then ripgrep will search ever .md file in that directory.

Interestingly, the same is not true for file types. If you use -tmd instead for example, then a/.Commit/a.md is ignored.

This does seem like something that the docs could be more explicit about, so I'll mark this as a doc bug.

@BurntSushi BurntSushi added bug A bug. doc An issue with or an improvement to documentation. and removed bug A bug. labels Jul 5, 2020
@BurntSushi BurntSushi changed the title rg --glob does not respects .gitignore document the difference between -tmd and -g*.md Jul 5, 2020
@lamyergeier
Copy link
Author

lamyergeier commented Jul 5, 2020

Hi I knew about -tmd, but was using glob only because I wanted to search for random extensions (non-standard).

Not complaining, but I thought rg followed the behavior of .gitignore, as you mentioned it here: #689 (comment)

You said: However, if you had read the README or the blog post introducing ripgrep, then it should have been very clear that respecting gitignore by default is a fundamental design decision.

If I am mistaken, I request you to reconsider this as by this we could support random extensions and also respect .gitigore


Also please see the following, when I modified .gitignore (Is this consistent with your comment? I am not sure.):

Test-Glob() {
	mkdir -p Temp
	cd Temp || exit
	git init > /dev/null
	echo '/**/.Commit/**' > .gitignore
	mkdir -p a/.Commit && touch a/.Commit/a.md
	touch a/a.md
	echo "PWD= ${PWD}"
	echo "${Blue:=}------------Cat gitignore--------------------${Normal:=}"
	cat .gitignore
	echo "${Blue:=}------------List files not ignored--------------------${Normal:=}"
	git check-ignore -v ./**/*
	echo "${Blue:=}------------Find--------------------${Normal:=}"
	find . -type f -name "*.md"
	echo "${Blue:=}------------Ripgrep Glob--------------------${Normal:=}"
	rg --glob "*.md" --files
	echo "${Blue:=}------------Ripgrep--------------------${Normal:=}"
	rg --files
	cd ..
}
------------Cat gitignore--------------------
/**/.Commit/**
------------List files not ignored--------------------
------------Find--------------------
./a/a.md
./a/.Commit/a.md
------------Ripgrep Glob--------------------
a/a.md
------------Ripgrep--------------------
a/a.md

@BurntSushi
Copy link
Owner

but I thought rg followed the behavior of .gitignore

It does. The fact that rules in a .ignore override .gitignore does not mean ripgrep doesn't follow the behavior of gitignore. And similarly for the --glob flag. ripgrep does more than just gitignore anyway. It also filters out hidden and binary files by default.

I request you to reconsider this

No, the behavior isn't changing. It would be a breaking change that feels big enough to me that I'm not comfortable with it, and while I see that there are problems with the current behavior, there would also be problems with your proposed behavior. Ultimately, I judged that the status quo was the least bad option and went with that.

Also please see the following, when I modified .gitignore (Is this consistent with your comment? I am not sure.):

Yes, because /**/.Commit/** ends up ignoring .Commit itself, which means ripgrep doesn't descend into that directory. Use the --debug flag to see how filtering rules are applied:

DEBUG|ignore::walk|crates/ignore/src/walk.rs:1730: ignoring ./a/.Commit: Ignore(IgnoreMatch(Hidden))

@lamyergeier
Copy link
Author

lamyergeier commented Jul 5, 2020

Sorry for the confusion: I am really not sure why /**/.Commit/** is ignored in second case but not in the first case?

For the first case you said:

Glob overrides always have the highest precedent. So if you use -g '*.md' and ripgrep would otherwise descend into a directory containing a .md file (i.e., the directory itself wasn't ignored), then ripgrep will search ever .md file in that directory.

What i understood (from your comments): If glob finds *.md even in a gitignored directory, it will still print it.

Going by this explanation, if glob overrides always has the highest precedent, then rg should also print a/.Commit/a.md in the second case. Isn't it?

@BurntSushi
Copy link
Owner

BurntSushi commented Jul 5, 2020

It has to descend into the directory in the first place. Look at the --debug output. With your second gitignore file, ripgrep doesn't descend into a/.Commit at all. So there is no opportunity for *.md to match it.

This is why I wrote, "and ripgrep would otherwise descend into the directory."

@BurntSushi BurntSushi added the rollup A PR that has been merged with many others in a rollup. label Nov 25, 2023
BurntSushi added a commit that referenced this issue Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc An issue with or an improvement to documentation. rollup A PR that has been merged with many others in a rollup.
Projects
None yet
Development

No branches or pull requests

2 participants