4

I'm attempting to write a Mercurial hook that blocks pushes under certain conditions, but to determine whether to block the push it needs to know the repository the push would be going out to. I know that ideally, this would be done in a prechangegroup hook on the remote repository side, but that's not possible in my case - see my earlier question for why. (I liked Ry4an's suggestion, but I have to do this for a whole bunch of repositories, so creating a duplicate for each one doesn't seem like the way to go.)

Anyway, I was initially doing this in a pre-push hook, but that's only called under very specific circumstances; in particular, if the user has an alias defined for push, then my hook won't get called, which means it could be inadvertently bypassed. My next thought was the preoutgoing hook, which is definitely more foolproof - it could get called in situations other than a push, but those are easy enough to filter out using the passed source parameter.

However, the preoutgoing hook doesn't have the destination repository available to it; there's even a bug filed for that, but it doesn't do me much good right now. Presently, I'm working around this by having two hooks; a pre-push one that determines the destination, and then the preoutgoing one which actually acts on it. Of course, it's possible the pre-push hook doesn't run, but in that circumstance I just warn the user their push is potentially unsafe and give them the option to abandon it.

I have two questions. Firstly, is there a better way to accomplish this? Secondly, if there isn't, is it safe to assume that the repo object passed to both my hooks (they're both in-process) will be the same? In other words, if I do something like

repo.some_custom_property = outgoing_url

in my pre-push hook, is it guaranteed that the preoutgoing hook that's called subsequently will receive the same repo object and therefore have access to my custom property through that? I know I can do this through environment variables or temp files, but this seemed nicer and I don't have to worry about cleaning up after it. I also saw this question, but it didn't really help much.

Community
  • 1
  • 1
Shoaib
  • 561
  • 3
  • 11

0 Answers0