Skip to content

Commit ad80d51

Browse files
committed
feat(hooks): support interactive hooks scripts
1 parent 0acf5d6 commit ad80d51

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

commitizen/cmd.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,18 @@ def run_shell(cmd: str, env: Mapping[str, str] | None = None) -> Command:
103103
https://github.com/commitizen-tools/commitizen/issues/1918
104104
"""
105105
return _popen(cmd, shell=True, env=env)
106+
107+
108+
def run_interactive(cmd: Sequence[str], env: Mapping[str, str] | None = None) -> int:
109+
"""Run a command without shell interpretation and without redirecting stdin, stdout, or stderr
110+
111+
Args:
112+
cmd: The command to run
113+
env: Extra environment variables to define in the subprocess. Defaults to None.
114+
115+
Returns:
116+
subprocess returncode
117+
"""
118+
if env is not None:
119+
env = {**os.environ, **env}
120+
return subprocess.run(cmd, shell=False, env=env).returncode

commitizen/hooks.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@ def run(hooks: str | list[str], _env_prefix: str = "CZ_", **env: object) -> None
1717
for hook in hooks:
1818
out.info(f"Running hook '{hook}'")
1919

20-
c = cmd.run_shell(hook, env=_format_env(_env_prefix, env))
20+
return_code = cmd.run_interactive(hook, env=_format_env(_env_prefix, env))
2121

22-
if c.out:
23-
out.write(c.out)
24-
if c.err:
25-
out.error(c.err)
26-
27-
if c.return_code != 0:
22+
if return_code != 0:
2823
raise RunHookError(f"Running hook '{hook}' failed")
2924

3025

tests/test_bump_hooks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def test_run(mocker: MockFixture):
1212
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"]
1313

1414
cmd_run_mock = mocker.Mock()
15-
cmd_run_mock.return_value.return_code = 0
16-
mocker.patch.object(cmd, "run_shell", cmd_run_mock)
15+
cmd_run_mock.return_value = 0
16+
mocker.patch.object(cmd, "run_interactive", cmd_run_mock)
1717

1818
hooks.run(bump_hooks)
1919

@@ -29,8 +29,8 @@ def test_run_error(mocker: MockFixture):
2929
bump_hooks = ["pre_bump_hook", "pre_bump_hook_1"]
3030

3131
cmd_run_mock = mocker.Mock()
32-
cmd_run_mock.return_value.return_code = 1
33-
mocker.patch.object(cmd, "run_shell", cmd_run_mock)
32+
cmd_run_mock.return_value = 1
33+
mocker.patch.object(cmd, "run_interactive", cmd_run_mock)
3434

3535
with pytest.raises(RunHookError):
3636
hooks.run(bump_hooks)

0 commit comments

Comments
 (0)