Skip to content

Commit 98d622f

Browse files
committed
feat(hooks): support interactive hooks scripts
1 parent 35196da commit 98d622f

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

commitizen/cmd.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def _try_decode(bytes_: bytes) -> str:
3636

3737

3838
def run(cmd: str, env: Mapping[str, str] | None = None) -> Command:
39+
"""Run a command in a subprocess and capture stdout and stderr
40+
41+
Args:
42+
cmd: The command to run
43+
env: Extra environment variables to define in the subprocess. Defaults to None.
44+
45+
Returns:
46+
Command: _description_
47+
"""
3948
if env is not None:
4049
env = {**os.environ, **env}
4150
process = subprocess.Popen(
@@ -55,3 +64,18 @@ def run(cmd: str, env: Mapping[str, str] | None = None) -> Command:
5564
stderr,
5665
return_code,
5766
)
67+
68+
69+
def run_interactive(cmd: str, env: Mapping[str, str] | None = None) -> int:
70+
"""Run a command in a subprocess without redirecting stdin, stdout, or stderr
71+
72+
Args:
73+
cmd: The command to run
74+
env: Extra environment variables to define in the subprocess. Defaults to None.
75+
76+
Returns:
77+
subprocess returncode
78+
"""
79+
if env is not None:
80+
env = {**os.environ, **env}
81+
return subprocess.run(cmd, shell=True, 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(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_run(mocker: MockFixture):
1313

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

1818
hooks.run(bump_hooks)
1919

@@ -30,7 +30,7 @@ def test_run_error(mocker: MockFixture):
3030

3131
cmd_run_mock = mocker.Mock()
3232
cmd_run_mock.return_value.return_code = 1
33-
mocker.patch.object(cmd, "run", cmd_run_mock)
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)