3

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

Jonathan
  • 732
  • 5
  • 19

2 Answers2

3

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. :)

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • If it doesn't work I'll tweak it, but it seems basically right, but how do I prevent it from adding the include if ../hgrc doesn't exist (or is it safe to `%include` a file that doesn't exist?) It's still a better way than having an external script. I'm going to hold out a while for a way to do this without physically changing the hgrc file (so it can handle creating a lower hgrc if one doesn't already exist), but if I don't hear something in a week or so I'll mark yours as the answer. – Jonathan Feb 11 '11 at 20:10
  • Almost got this working with `post-clone = if [ -e "hgrc" ]; then echo '%include ../hgrc' >> .hg/hgrc`, but I need to be able to direct into the name of the cloned directory. I.E., if I run `hg clone proj1 proj2`, I need to prepend `.hg/hgrc` with `proj2/`. – Jonathan Feb 11 '11 at 20:40
  • @Jonathan: Have a try with the environment variable [`HG_PATS`](http://www.selenic.com/mercurial/hgrc.5.html#hooks), it provides the clone source and - if explicitly given to the `clone` command - its destination. This information should be sufficient to get the destination prefix you need. – Oben Sonne Feb 11 '11 at 22:07
  • The common idiom for creating a file if it does not exist: `touch ../hgrc; echo '%include ../hgrc' >> .hg/hgrc`. But it will top out if you ever lack write access to the parent directory. – alexis Dec 11 '12 at 17:55
  • If you use the ``update`` hook instead, then you will already be in the correct directory. – mforbes Jun 12 '14 at 23:09
0

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.

mforbes
  • 7,062
  • 3
  • 16
  • 21