Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for all files except .hg_keep

I use empty .hg_keep files to keep some (otherwise empty) folders in Mercurial.

The problem is that I can't find a working regex which excludes everything but the .hg_keep files.

lets say we have this filestructure:

a/b/c2/.hg_keep
a/b/c/d/.hg_keep
a/b/c/d/file1
a/b/c/d2/.hg_keep
a/b/.hg_keep
a/b/file2
a/b/file1
a/.hg_keep
a/file2
a/file1

and I want to keep only the .hg_keep files under a/b/.

with the help of http://gskinner.com/RegExr/ I created the following .hgignore:

syntax: regexp
.*b.*/(?!.*\.hg_keep)

but Mercurial ignores all .hg_keep files in subfolders of b.

# hg status
? .hgignore
? a/.hg_keep
? a/b/.hg_keep
? a/file1
? a/file

# hg status -i
I a/b/c/d/.hg_keep
I a/b/c/d/file1
I a/b/c/d2/.hg_keep
I a/b/c2/.hg_keep
I a/b/file1
I a/b/file2

I know that I a can hd add all the .hg_keep files, but is there a solution with a regular expression (or glob)?

like image 996
rmweiss Avatar asked Mar 16 '26 06:03

rmweiss


1 Answers

Regexp negation might work for this. If you want to ignore everything except the a/b/.hg_keep file, you can probably use:

^(?!a/b/\.hg_keep)$

The parts of this regexp that matter are:

^                   anchor the match to the beginning of the file path
(?!  ... )          negation of the expression between '!' and ')'
a/b/\.hg_keep       the full path of the file you want to match
$                   anchor the match to the end of the file path

The regular expression

^a/b/\.hg_keep$

would match only the file called a/b/.hg_keep.

Its negation

^(?!a/b/\.hg_keep)$

will match everything else.

like image 82
Giorgos Keramidas Avatar answered Mar 18 '26 03:03

Giorgos Keramidas