Mercurial Conditional %include?

665 views Asked by At

I would like parent directories of projects to include an hgrc file that a repo in that folder inherits from, e.g.

~/work/
~/work/hgrc
~/work/project1/
~/work/project2/
~/personal/hgrc
~/personal/project1
~/personal/project2
~/personal/project3

Any project in work should inherit from work/hgrc, and any in personal should inherit from personal/hgrc. I was thinking of adding a script to ~/.hgrc that on clone would search for any hgrc files in parent directories and %include them if they exist, but this has the uglyness that if I add an hgrc below it after I clone it it won't be included. Only a 5% of the time consideration, but still...

2

There are 2 answers

5
Ry4an Brase On

How about putting:

%include ../hgrc

inside each repo's .hg/hgrc? You could even do that automatically but putting this in your systemwide /etc/mercurial/hgrc:

[hooks]
post-clone = echo '%include ../hgrc' >> .hg/hgrc

I've not tested that hook. :)

0
mforbes On

Following @Ry4an's suggestion, here is an example that works for me. I add this to my ~/.hgrc file so it works everywhere.

[hooks]
# This hook adds "%include ../.hgrc" to .hg/hgrc if the .hgrc file exists in
# the top level.  This allows one to store a set of paths for example.
# See 
update = if [ -e .hgrc ] && touch .hg/hgrc                                  \
                         && ! grep -q '%include \.\./\.hgrc' .hg/hgrc; then \
           echo '%include ../.hgrc' >> .hg/hgrc;                            \
         fi

This hook adds the line %include ../.hgrc to the .hg/hgrc file iff the file .hgrc exists in the top level of the repo. Note that by using the update hook, we bypass the issue of the with post-clone clone hook of having to try to figure out the directory name of the target clone from environmental variables.