How to determine which rule is causing a file to be ignored in Mercurial?
Asked Answered
U

1

10

Mercurial is presently ignoring a file which I believe shouldn't be ignored. The .hgignore file is remarkably large, and after a cursory read-through it's not obvious which rule(s) is/are the culprit.

Is there a way to get Mercurial to tell me which rules in the .hgignore (if any) match a file?

Ultramontanism answered 2/4, 2014 at 4:1 Comment(0)
W
13

You can't do this out of the box, but you can write Mercurial extension for this.

I've written extension hg-isignored that can show you which rules in .hgignore match specified folder or file.

Installation

Clone extension source somewhere, i.e. in ~/.hgrc.d/hg-isignored folder:

hg clone https://[email protected]/rpeshkov/hg-isignored ~/.hgrc.d/hg-isignored

Add extension in extensions section of your .hgrc file:

[extensions]
isignored=~/.hgrc.d/hg-isignored/hg-isignored.py

Now Mercurial will be able to use isignored command that's implemented by extension.

Usage

While you're in Mercurial repository, run command hg isignored PATH, where instead of PATH insert folder or file that you want to check, i.e. hg isignored readme.txt. Extension will tell you if specified PATH is ignored and will show you used rule.

Testing

Init

[~/hgtest]% hg init

No .hgignore

[~/hgtest]% touch readme.txt
[~/hgtest]% hg st  
? readme.txt
[~/hgtest]% hg isignored readme.txt
abort: .hgignore file not found

Added .hgignore

[~/hgtest]% echo "syntax: glob" > .hgignore
[~/hgtest]% echo "readme.txt" >> .hgignore
[~/hgtest]% hg st
? .hgignore
[~/hgtest]% hg isignored readme.txt
Path 'readme.txt' is ignored by:
relglob:readme.txt

Not ignored file

[~/hgtest]% touch readme2.txt
[~/hgtest]% hg isignored readme2.txt
Path 'readme2.txt' is not ignored

Ignore folder

[~/hgtest]% mkdir build
[~/hgtest]% touch build/keep.txt
[~/hgtest]% echo "build/" >> .hgignore
[~/hgtest]% hg st
? .hgignore
? readme2.txt
[~/hgtest]% hg isignored build
Path 'build' is ignored by:
relglob:build/
[~/hgtest]% hg isignored build/keep.txt
Path 'build/keep.txt' is ignored by:
relglob:build/

Ignore everything in folder

[~/hgtest]% echo "syntax: glob" > .hgignore
[~/hgtest]% echo "build/*" >> .hgignore
[~/hgtest]% hg st
? .hgignore
? readme.txt
? readme2.txt
[~/hgtest]% hg isignored build
Path 'build' is not ignored
[~/hgtest]% hg isignored build/keep.txt
Path 'build/keep.txt' is ignored by:
relglob:build/*
Watercool answered 2/4, 2014 at 6:33 Comment(9)
Did you write this in response to this question? By your commit history, it looks like you did.. You deserve all the upvotes, sir.Ultramontanism
Yes. I've found your question interesting and started digging Mercurial sources to find out how it works with ignored files.Watercool
I get an error when I run the 'hg isgnored <PATH>' command: "abort: no module named ignore!". It could be a simple fix, but am not sure what this is. The extension seems to be installed because I can run 'hg help isignored'. Can you help?Apperception
@Apperception which version of Mercurial do you use?Watercool
@black_wizard, I am using version 3.7.3.Apperception
I'm getting the same error as @Apperception using Mercurial v4.0.2Towardly
@ShawndeWet, I've updated extension so now it should work with Mercurial 4. Sorry for so sloooow update :) I'm currently not using Mercurial.Watercool
FYI if you didn't have an existing .hgrc you need to add the path to the command to an "[extensions]" block or else you will get "unknown command" error.Horsehide
@Horsehide yep, all custom extensions have to be added to config fileWatercool

© 2022 - 2024 — McMap. All rights reserved.