Skip to content

Commit 628c876

Browse files
committed
adjust the run_hook api to no longer take Hook
1 parent 48ae18a commit 628c876

File tree

26 files changed

+163
-192
lines changed

26 files changed

+163
-192
lines changed

pre_commit/commands/run.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,14 @@ def _run_single_hook(
190190
time_before = time.time()
191191
language = languages[hook.language]
192192
with language.in_env(hook.prefix, hook.language_version):
193-
retcode, out = language.run_hook(hook, filenames, use_color)
193+
retcode, out = language.run_hook(
194+
hook.prefix,
195+
hook.entry,
196+
hook.args,
197+
filenames,
198+
require_serial=hook.require_serial,
199+
color=use_color,
200+
)
194201
duration = round(time.time() - time_before, 2) or 0
195202
diff_after = _get_diff()
196203

pre_commit/hook.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
import shlex
54
from typing import Any
65
from typing import NamedTuple
76
from typing import Sequence
@@ -37,10 +36,6 @@ class Hook(NamedTuple):
3736
stages: Sequence[str]
3837
verbose: bool
3938

40-
@property
41-
def cmd(self) -> tuple[str, ...]:
42-
return (*shlex.split(self.entry), *self.args)
43-
4439
@property
4540
def install_key(self) -> tuple[Prefix, str, str, tuple[str, ...]]:
4641
return (

pre_commit/languages/all.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import Protocol
55
from typing import Sequence
66

7-
from pre_commit.hook import Hook
87
from pre_commit.languages import conda
98
from pre_commit.languages import coursier
109
from pre_commit.languages import dart
@@ -62,8 +61,12 @@ def in_env(
6261
# execute a hook and return the exit code and output
6362
def run_hook(
6463
self,
65-
hook: Hook,
64+
prefix: Prefix,
65+
entry: str,
66+
args: Sequence[str],
6667
file_args: Sequence[str],
68+
*,
69+
require_serial: bool,
6770
color: bool,
6871
) -> tuple[int, bytes]:
6972
...

pre_commit/languages/conda.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
from pre_commit.envcontext import SubstitutionT
1111
from pre_commit.envcontext import UNSET
1212
from pre_commit.envcontext import Var
13-
from pre_commit.hook import Hook
1413
from pre_commit.languages import helpers
1514
from pre_commit.prefix import Prefix
1615
from pre_commit.util import cmd_output_b
1716

1817
ENVIRONMENT_DIR = 'conda'
1918
get_default_version = helpers.basic_get_default_version
2019
health_check = helpers.basic_health_check
20+
run_hook = helpers.basic_run_hook
2121

2222

2323
def get_env_patch(env: str) -> PatchesT:
@@ -74,15 +74,3 @@ def install_environment(
7474
conda_exe, 'install', '-p', env_dir, *additional_dependencies,
7575
cwd=prefix.prefix_dir,
7676
)
77-
78-
79-
def run_hook(
80-
hook: Hook,
81-
file_args: Sequence[str],
82-
color: bool,
83-
) -> tuple[int, bytes]:
84-
# TODO: Some rare commands need to be run using `conda run` but mostly we
85-
# can run them without which is much quicker and produces a better
86-
# output.
87-
# cmd = ('conda', 'run', '-p', env_dir) + hook.cmd
88-
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/coursier.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pre_commit.envcontext import envcontext
99
from pre_commit.envcontext import PatchesT
1010
from pre_commit.envcontext import Var
11-
from pre_commit.hook import Hook
1211
from pre_commit.languages import helpers
1312
from pre_commit.parse_shebang import find_executable
1413
from pre_commit.prefix import Prefix
@@ -17,6 +16,7 @@
1716

1817
get_default_version = helpers.basic_get_default_version
1918
health_check = helpers.basic_health_check
19+
run_hook = helpers.basic_run_hook
2020

2121

2222
def install_environment(
@@ -64,11 +64,3 @@ def in_env(prefix: Prefix, version: str) -> Generator[None, None, None]:
6464
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
6565
with envcontext(get_env_patch(envdir)):
6666
yield
67-
68-
69-
def run_hook(
70-
hook: Hook,
71-
file_args: Sequence[str],
72-
color: bool,
73-
) -> tuple[int, bytes]: # pragma: win32 no cover
74-
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/dart.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pre_commit.envcontext import envcontext
1111
from pre_commit.envcontext import PatchesT
1212
from pre_commit.envcontext import Var
13-
from pre_commit.hook import Hook
1413
from pre_commit.languages import helpers
1514
from pre_commit.prefix import Prefix
1615
from pre_commit.util import win_exe
@@ -20,6 +19,7 @@
2019

2120
get_default_version = helpers.basic_get_default_version
2221
health_check = helpers.basic_health_check
22+
run_hook = helpers.basic_run_hook
2323

2424

2525
def get_env_patch(venv: str) -> PatchesT:
@@ -95,11 +95,3 @@ def _install_dir(prefix_p: Prefix, pub_cache: str) -> None:
9595
raise AssertionError(
9696
f'could not find pubspec.yaml for {dep_s}',
9797
)
98-
99-
100-
def run_hook(
101-
hook: Hook,
102-
file_args: Sequence[str],
103-
color: bool,
104-
) -> tuple[int, bytes]:
105-
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/docker.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
from typing import Sequence
77

8-
from pre_commit.hook import Hook
98
from pre_commit.languages import helpers
109
from pre_commit.prefix import Prefix
1110
from pre_commit.util import CalledProcessError
@@ -123,16 +122,25 @@ def docker_cmd() -> tuple[str, ...]: # pragma: win32 no cover
123122

124123

125124
def run_hook(
126-
hook: Hook,
125+
prefix: Prefix,
126+
entry: str,
127+
args: Sequence[str],
127128
file_args: Sequence[str],
129+
*,
130+
require_serial: bool,
128131
color: bool,
129132
) -> tuple[int, bytes]: # pragma: win32 no cover
130133
# Rebuild the docker image in case it has gone missing, as many people do
131134
# automated cleanup of docker images.
132-
build_docker_image(hook.prefix, pull=False)
135+
build_docker_image(prefix, pull=False)
133136

134-
entry_exe, *cmd_rest = hook.cmd
137+
entry_exe, *cmd_rest = helpers.hook_cmd(entry, args)
135138

136-
entry_tag = ('--entrypoint', entry_exe, docker_tag(hook.prefix))
139+
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
137140
cmd = (*docker_cmd(), *entry_tag, *cmd_rest)
138-
return helpers.run_xargs(hook, cmd, file_args, color=color)
141+
return helpers.run_xargs(
142+
cmd,
143+
file_args,
144+
require_serial=require_serial,
145+
color=color,
146+
)

pre_commit/languages/docker_image.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from typing import Sequence
44

5-
from pre_commit.hook import Hook
65
from pre_commit.languages import helpers
76
from pre_commit.languages.docker import docker_cmd
7+
from pre_commit.prefix import Prefix
88

99
ENVIRONMENT_DIR = None
1010
get_default_version = helpers.basic_get_default_version
@@ -14,9 +14,18 @@
1414

1515

1616
def run_hook(
17-
hook: Hook,
17+
prefix: Prefix,
18+
entry: str,
19+
args: Sequence[str],
1820
file_args: Sequence[str],
21+
*,
22+
require_serial: bool,
1923
color: bool,
2024
) -> tuple[int, bytes]: # pragma: win32 no cover
21-
cmd = docker_cmd() + hook.cmd
22-
return helpers.run_xargs(hook, cmd, file_args, color=color)
25+
cmd = docker_cmd() + helpers.hook_cmd(entry, args)
26+
return helpers.run_xargs(
27+
cmd,
28+
file_args,
29+
require_serial=require_serial,
30+
color=color,
31+
)

pre_commit/languages/dotnet.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pre_commit.envcontext import envcontext
1313
from pre_commit.envcontext import PatchesT
1414
from pre_commit.envcontext import Var
15-
from pre_commit.hook import Hook
1615
from pre_commit.languages import helpers
1716
from pre_commit.prefix import Prefix
1817

@@ -21,6 +20,7 @@
2120

2221
get_default_version = helpers.basic_get_default_version
2322
health_check = helpers.basic_health_check
23+
run_hook = helpers.basic_run_hook
2424

2525

2626
def get_env_patch(venv: str) -> PatchesT:
@@ -113,11 +113,3 @@ def install_environment(
113113
# Clean the git dir, ignoring the environment dir
114114
clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*')
115115
helpers.run_setup_cmd(prefix, clean_cmd)
116-
117-
118-
def run_hook(
119-
hook: Hook,
120-
file_args: Sequence[str],
121-
color: bool,
122-
) -> tuple[int, bytes]:
123-
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

pre_commit/languages/fail.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from typing import Sequence
44

5-
from pre_commit.hook import Hook
65
from pre_commit.languages import helpers
6+
from pre_commit.prefix import Prefix
77

88
ENVIRONMENT_DIR = None
99
get_default_version = helpers.basic_get_default_version
@@ -13,10 +13,14 @@
1313

1414

1515
def run_hook(
16-
hook: Hook,
16+
prefix: Prefix,
17+
entry: str,
18+
args: Sequence[str],
1719
file_args: Sequence[str],
20+
*,
21+
require_serial: bool,
1822
color: bool,
1923
) -> tuple[int, bytes]:
20-
out = f'{hook.entry}\n\n'.encode()
24+
out = f'{entry}\n\n'.encode()
2125
out += b'\n'.join(f.encode() for f in file_args) + b'\n'
2226
return 1, out

0 commit comments

Comments
 (0)