views:

108

answers:

3

Using Mercurial, I need to ignore all files and directories except directories "tests" and "languages" and files in those directories. That all must be done with regex and .hgignoere

Files:

  • tests\junk.txt
  • cms\tests\some_file.txt
  • cms\languages\users\lang.txt
  • languages\lang2.txt
  • tests.txt
  • proba/tests.txt

I tried with this:

^(?!test|test/|test$|languages|languages/|languages$)

but this only matches the files that starts with test and language:

  • tests\junk.txt
  • languages\lang2.txt

What I want is to matches also

  • cms\tests\some_file.txt
  • cms\languages\users\lang.txt

Any advice would be appreciated.

+2  A: 

Use globs, they're simpler:

C:> dir
tests  languages pr0n espionage more_pr0n

C:> type .hgignore
syntax: glob
pr0n/*
espionage/*
more_pr0n/*

added: If you are concerned that there will be new directories that won't be ignored then you should:

hg add .hgignore
hg commit 
echo "new_directory/*" >> .hgignore
hg commit

If you have too many directories and files popping up in your source directory for this method to work, make a source-only directory.

msw
I get the impression he's looking for a suggestion that works even when new top level directories.
Ry4an
I got that impression too, in which case the cure is added above.
msw
Thanks for fast answer. I tried to put this in my .hgignore:syntax: globtests/*languages/*But this is opposite of that what I want to achieve. I don't want to ignore files in those folder. I want to ignore all files/folders that are not member of "tests" and "languages"
Danilo
I understand what you want, I'm suggesting that what you want is bad practice which is partly why it is difficult.
msw
+1  A: 

It's not the answer you're looking for but almost no one uses the negative-width-look-ahead ?: syntax in their .hgignore files. In cases where they want just a small subset of all files to be unignored they usually just ignore everything and then hg add the exceptions -- remember that in mercurial (unlike in cvs/svn) adding a file overrides ignore.

So if your .hgignore you'd have:

.*

and then you'd hg add the files in your tests and languages directories explicitly with 'hg add'. You do then, of course have to remember to add any new files as you go as everything will be ignored, but if most files are supposed to be ignored it still the easier way to go.

Ry4an
It is often unclear to me whether the question should be answered according to the constraints of the question or if the root problem should be addressed. If the latter, this answer is Correct and Complete but not Good.
msw
Thanks Ray4an for the answer but this is to hard to do every time. My idea is to ignore all files that are not in the folders tests and languages. Do you know how can I do this?
Danilo
A: 

It's not possible.

See the manpage.

For example, say we have an untracked file, file.c, at a/b/file.c inside our repository. Mercurial will ignore file.c if any pattern in .hgignore matches a/b/file.c, a/b or a.

So let's say you make a regex that will not match any path containing test and the file proba/tests.txt is in your working directory. That regex will first be applied to proba and will match which will result in the file being ignored.

hwiechers
It actually is possible. Part of the reason you're wrong is that it's possible to anchor regular expressions so they only match at the start of the path.
Omnifarious
You're going to have to prove it by supplying one because, as far as I can figure, rooting or not shouldn't matter.
hwiechers
@hwiechers - I'm sorry, I missed an important part of the question. You're right, it isn't possible.
Omnifarious