2

I know Hg's ignore is useful, but is there a way that I can specify what files I do care, and just ignore the rest, for instance.

if I have a.c, a.h, a.o, a.exe, a.err in the repo, is there a way I can tell Hg, I just want to keep *.c *.h and just ignore everything not on the list.

shengy
  • 9,461
  • 4
  • 37
  • 61
  • Seems to be similar to whitelist, like in http://stackoverflow.com/questions/1512058/hgignore-help-ignoring-all-files-but-certain-ones but Ry4an won't be happy: http://stackoverflow.com/a/10434978/6309 – VonC May 07 '12 at 10:34

1 Answers1

3

Heh, @vonC is right, I couldn't let this go by. There isn't a way to turn hgignore into a whitelist instead of a blacklist. I gather it's that way by design (I certainly didn't design it).

You have a few options:

  1. Add everything (.*) to your .hgignore and then hg add file you want to be tracked -- adding always overrides ignoring

  2. Just break down and make a proper black list: hg status --no-status --unknown >> .hgignore is a great starting point.

The bottom line is accidentally tracking something you didn't want tracked is less dangerous a situation than accidentally forgetting to track something you wished was tracked. The former creates a little noise in the your repo, while the latter results in potential data loss.

If you really wanted to get fancy you could add '.*' to your .hgignore and then create a hook like this:

[hooks]
precommit.add = hg add $(hg status --unknown --no-status -I '**.c' -I '**.h')

which would automatically add any new .c and .h files when you commit.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • is `[hooks]` a plug in for hg? – shengy May 08 '12 at 04:08
  • btw, what's the difference between `*.c` and `**.c` although this is another question:) – shengy May 08 '12 at 04:09
  • no, hooks are in-built functionality. Try `man hgrc` or read chapter 10 of the Mercurial book for an explanation. **.c includes *.c in all paths. In brief `*` matches anything except a slash, and `**` matches _anything_. – Ry4an Brase May 09 '12 at 04:44
  • It is not working for me. I think the hook should be `pre-commit` with the dash as @Ry4an says in [this answer](http://stackoverflow.com/questions/11708929/mercurial-including-precommit-changed-file/11714777#11714777), and the `--unknown` option should be `--ignored` as ignored files are known. So the code could be `pre-commit.add = hg add $(hg status --ignored --no-status -I '**.c' -I '**.h')` – santiagopim Nov 22 '13 at 19:09