1

I'm trying to implement a mercurial pre-push hook which checks the target repo path and adds the appropriate id by ssh-add. The not so nice solution would be checking the command line parameters and if the path isn't forced, then reading the default from the hgrc file but is there a cleaner way to just obtain the remote path?

I printed the kwargs passed into the hook method but there isn't any which seem to hold what I need. I also tried googling but the info available is next to nothing and this appears to be a bit like a black art really. So, any reference to a decent documentation and/or examples would be appreciated too.

Cheers,

abdus_salam
  • 738
  • 2
  • 7
  • 19

2 Answers2

0

Looking in hg help config, it seems like you can use the 'prechangegroup' hook and the HG_URL environment variable:

"prechangegroup"
  Run before a changegroup is added via push, pull or unbundle. Exit
  status 0 allows the changegroup to proceed. Non-zero status will cause
  the push, pull or unbundle to fail. URL from which changes will come is
  in "$HG_URL".
Mathiasdm
  • 1,791
  • 14
  • 26
  • Even though this looks promising, unfortunately, this hook doesn't get called before or after a regular push. – abdus_salam Mar 23 '15 at 13:10
  • Hm, I was confused by the documentation, I guess. This hook is run if changes are added to the current repository (so by others pushing to you or you pulling from others). It will indeed not be useful for you. – Mathiasdm Mar 23 '15 at 14:00
0

You should be able to use the 'pre-changegroup' and 'pre-push' hook (mind the dash). Which supplies the command line arguments as $HG_ARGS.

If the $HG_ARGS is a valid url you can use that url. If nothing is supplied use the ui object that is given as a keyword argument to the hook.

Use the following to retreive the default path from the configuration: ui.config('paths', 'default')

As you can also write other named urls/paths in the configuration file you should also be able to verify the $HG_ARGS if it doesn't contain a valid url a a keyword to the ui.config paths object

rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
  • this doesn't get called either and even if it did, I do not wan't to rely on the $HG_ARGS because, if there are no args provided, we'll be getting the default off .hgrc. – abdus_salam Mar 23 '15 at 20:08
  • Sorry. That hook is on the remote repo. I didn't realised that you want it local – rfkortekaas Mar 23 '15 at 20:17
  • I actually want something for both and I tried with the remote repo but it still doesn't get called on my ubuntu setup, I don't know if the environment makes any difference. – abdus_salam Mar 23 '15 at 20:18
  • According to the doc you can use 'pre-push' (didn't test it). And again look at the $HG_ARGS to retrieve the url. And use ui.config to get the default path from .hgrc if no argument supplied – rfkortekaas Mar 23 '15 at 20:20
  • It took a while for me to get back to this but here is my python way of (don't like saying pythonic) achieving this. If the method is receiving 'ui' as a parameter, ui.config('paths', 'default') does the job. @rfkortekaas suggested using the ui.config but I wasn't quite sure how to do it and figured it out after playing a bit. If he updates his answer, I'm happy to up vote it. – abdus_salam May 08 '15 at 14:33