Search code examples
regexmercurialhgignore

Mercurial .hgignore regular expression


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.


Solution

  • 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.