How to make mercurial ignore all hidden files?
Asked Answered
A

2

12

I hate seeing nearly every directory in my repository list each file twice, once with a dot in front of it and once without. I tried adding .* to my .hgignore file, but it has no effect. Is this the wrong syntax, and more importantly, is it a bad idea to try this in the first place? Thanks.

Adrial answered 7/3, 2010 at 4:26 Comment(7)
The syntax would be something more like \..* but it is very suspicious that you describe having all files, with a dot and without. You wouldn't ordinarily have dot files in a repo. What files are you tracking? (hg manifest)Equilateral
This unfortunately still didn't work. I was exaggerating a bit when I said there are dot files everywhere, but in several directories every file has a dot file equivalent. I edited .hgignore with your syntax, performed hg addremove, and did a commit, and they are all still in my repository.Adrial
The syntax depends on whether you're using glob or re syntax (e.g. a line of the form syntax: re above the directive). But .* is correct glob syntax. If the files are already committed, you'll have to remove them individually (or with hg rm .*), and addremove won't do this for you.Ehrlich
Doing sudo hg rm .* doesn't work. I get abort: .. not under root which doesn't make sense to me. All of the hidden files in question are actually ._ files that the OSX Finder creates for some reason. I tried running their built-in dot_clean utility, but I get undouble . failed. Very frustrating.Adrial
Ugh... I even tried sudo hg rm ._* but it tells me not removing ._*: file is untracked, yet when I get hg manifest I see plenty of ._ files!Adrial
I think the problem is that hg rm .* is not recursively traveling through directories. Is there a way to make it do this?Adrial
find . -name "._*" |xargs hg rmNabila
F
19

You've got almost the right answer in the comments from gavinb, but the match was a little to broad. However, key concept about ignoring after the face was provided by RogerPage, again in a comment (what's with everyone preferring comments to answers?).

Let's look at these nine files:

dir.with.dots/file.with.dots
dir.with.dots/filewithoutdots
dir.with.dots/.filestartwithdot
dirwithoutdots/file.with.dots
dirwithoutdots/filewithoutdots
dirwithoutdots/.filestartwithdot
.startwithdot/file.with.dots
.startwithdot/filewithoutdots
.startwithdot/.filestartwithdot

If in the default regex mode for hgignore you do:

\..*

You ignore eight of those nine files:

$ hg stat -i
I .hgignore
I .startwithdot/.filestartwithdot
I .startwithdot/file.with.dots
I .startwithdot/filewithoutdots
I dir.with.dots/.filestartwithdot
I dir.with.dots/file.with.dots
I dir.with.dots/filewithoutdots
I dirwithoutdots/.filestartwithdot
I dirwithoutdots/file.with.dots

which is more broad than you said you wanted. It's ignoring anything with a dot anywhere in it.

To ignore all files and directores (not what you said, but what you seem to want) beginning with a dot you use this regexp pattern:

(^|/)\.

which says that the thing before the literal dot has to be the start of the line (^) or a slash.

$ hg stat -i
I .hgignore
I .startwithdot/.filestartwithdot
I .startwithdot/file.with.dots
I .startwithdot/filewithoutdots
I dir.with.dots/.filestartwithdot
I dirwithoutdots/.filestartwithdot

That caught only files or directories that start with a dot.

However, the other key concept that came up, was that .hgignore has no effect after a file has been added. It will prevent adding by wild card, but you can always override .hgignore with an explicit hg add. and once files have been added the hgignore is no longer consulted.

That's actually really handy because you can ignore broadly (ex: .*\.jar) and then add the exceptions you do want manually w/o having to futz with your hgignore file. However, in this case it means you need to hg rm the files you've already added accidentally, and tonfa showed how to do that (so long as there are no spaces in your filenames).

In the end it sounds like what you want in your .hgignore file is:

(^|/)\._

and that you need to remove the ones you've already added:

find . -type f -name "._*" -print0 |xargs -0 hg rm
Farinaceous answered 7/3, 2010 at 16:27 Comment(1)
Way, way too thorough. Kudos!Amabelle
L
0

I use this in my .hgignore:

syntax: regexp

# dotfiles
(?<![^/])\.

Which reads: periods not preceded by something other than a forward slash.

Ry4an's solution is also good, I've used that one before too. Not sure which is more efficient.

https://regex101.com/r/dB4bW7/1

Luster answered 5/4, 2016 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.