5

I have a script that is in the top level of my working copy and would like to use it as a Mercurial hook. If I use an absolute pathname to the hook then everything is fine, but I want a relative pathname so the whole thing can be easily moved around, used in other working copies and other developers can copy the hgrc as is.

/space/project/.hg/hgrc contains

[hooks]
update = genid

The genid script is at /space/project/genid

The hook is invoked just fine if I am in /space/project but if my current directory is /space/project/src/tools then 'hg update' will give an error as the hook cannot be found.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Roger Binns
  • 3,203
  • 1
  • 24
  • 33
  • What would happen when someone did an `hg update null`, thereby clearing the working directory? When you then tried to run any `hg` command it would try to load the now non-existent extension. Of course this can still be a problem with absolute paths, but it's much more likely if the extension script is in the repo you're actively working on. – Tim Delaney Nov 18 '10 at 22:07
  • 1
    Well don't do that then! Hooks are empty by default and have to be proactively added so if there is going to be no working copy then don't add the hook. This will not be an issue for the other developers working on the project. – Roger Binns Nov 18 '10 at 22:38
  • 1
    @TimDelaney that wont have any catastrophic effects - running the hook will just fail but the commit still happens – JonnyRaa Feb 10 '14 at 14:39

3 Answers3

2

In certain cases, environment variables are expanded in mercurial configuration. So you can check out if you can use a environment variable.

[hooks]
update = $MercurialHooks/genid

See Faq (12) in https://www.mercurial-scm.org/wiki/TipsAndTricks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • No environment variable is set pointing to the repository currently being operated on. – Roger Binns Nov 18 '10 at 22:46
  • @Roger Binns: What I meant is you could set it and use it. – pyfunc Nov 19 '10 at 00:40
  • Yes I understood you to mean that. But it won't work for my scenario such as if there are checkouts in multiple different locations or if you move the repository to a different directory. If you are having to maintain an environment variable then it may as well be $PATH. If Mercurial set one such as $HG_REPO_DIR then it would would wonderful. – Roger Binns Nov 19 '10 at 04:18
  • @Roger: $HG_ROOT is the one you want, but I'm not sure it's set there. – Macke Nov 29 '10 at 16:56
  • It isn't set while the config file is read - ie you can't say $HG_ROOT/genid. It is set once the script is running, but the issue is getting the script found in the first place. – Roger Binns Dec 02 '10 at 20:44
2

Python hooks cannot use a relative path. Script hooks can like this:

[hooks]
update = ./genid
Roger Binns
  • 3,203
  • 1
  • 24
  • 33
  • 1
    could you clarify - I couldnt get this working. I want the directory above the .hg directory. Wouldnt ./something just refer to the current directory? – JonnyRaa Feb 10 '14 at 14:35
  • *Python hooks cannot use a relative path* – could you please provide reference? – Piotr Dobrogost Mar 04 '14 at 14:26
0

I had the same problem and couldnt resolve it. The workaround was easy though! I versioned the file in the repo and just copied it to my .hg folder! Not ideal but it isnt that likely to change and other repo users can still get a copy of the file

JonnyRaa
  • 7,559
  • 6
  • 45
  • 49