问题
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.
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/2395179/how-to-make-mercurial-ignore-all-hidden-files