-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathhooks.py
More file actions
35 lines (24 loc) · 1014 Bytes
/
hooks.py
File metadata and controls
35 lines (24 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations
import os
from typing import TYPE_CHECKING
from commitizen import cmd, out
from commitizen.exceptions import RunHookError
if TYPE_CHECKING:
from collections.abc import Mapping
def run(hooks: str | list[str], _env_prefix: str = "CZ_", **env: object) -> None:
if isinstance(hooks, str):
hooks = [hooks]
for hook in hooks:
out.info(f"Running hook '{hook}'")
return_code = cmd.run_interactive(hook, env=_format_env(_env_prefix, env))
if return_code != 0:
raise RunHookError(f"Running hook '{hook}' failed")
def _format_env(prefix: str, env: Mapping[str, object]) -> dict[str, str]:
"""_format_env() prefixes all given environment variables with the given
prefix so it can be passed directly to cmd.run()."""
penv = dict(os.environ)
for name, value in env.items():
name = prefix + name.upper()
value = str(value) if value is not None else ""
penv[name] = value
return penv