Is it possible for my Mercurial hook to call code from another file?

I have a hook function with a name precommit_bad_branchthat imports hook_utils. When called precommit_bad_branchthrough a commit, I get the following error message:

error: precommit.branch_check hook raised an exception: No module named hook_utils
abort: No module named hook_utils!

Looks like I'm not allowed to call hook_utilsfrom precommit_bad_branch. The code works fine if I call it explicitly without using Mercurial.

Is it possible for my hook to call code from another file?

My hgrc hook part is as follows:

[hooks]
precommit.branch_check = python:C:\workspaces\hg_hooks\next_hooks.py:precommit_bad_branch
precommit.debug_code_check = python:C:\workspaces\hg_hooks\common_hooks.py:precommit_contains_debug_code
preupdate.merge_check = python:C:\workspaces\hg_hooks\next_hooks.py:preupdate_bad_merge
+3
source share
1 answer

Put the directory C:\workspaces\hg_hooksin PYTHONPATHand you can write

[hooks]
precommit.branch_check = python:next_hooks.precommit_bad_branch

in your configuration file and you can also do

import hook_utils

Python, next_hooks.py.

sys.path next_hooks.py, , :

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import hook_utils
+2

Source: https://habr.com/ru/post/1771658/


All Articles