Skip to content

Commit 0de174f

Browse files
authored
Merge pull request #462 from bpicolo/support_args_in_docker_entrypoint
Support docker hooks with args
2 parents 58df7c0 + 8cbd56a commit 0de174f

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

pre_commit/languages/docker.py

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

44
import hashlib
55
import os
6+
import shlex
67

78
from pre_commit import five
89
from pre_commit.languages import helpers
@@ -81,14 +82,18 @@ def run_hook(repo_cmd_runner, hook, file_args):
8182
# Rebuild the docker image in case it has gone missing, as many people do
8283
# automated cleanup of docker images.
8384
build_docker_image(repo_cmd_runner, pull=False)
85+
86+
entry_parts = shlex.split(hook['entry'])
87+
entry_executable, entry_args = entry_parts[0], entry_parts[1:]
88+
8489
cmd = (
8590
'docker', 'run',
8691
'--rm',
8792
'-u', '{}:{}'.format(os.getuid(), os.getgid()),
8893
'-v', '{}:/src:rw'.format(os.getcwd()),
8994
'--workdir', '/src',
90-
'--entrypoint', hook['entry'],
95+
'--entrypoint', entry_executable,
9196
docker_tag(repo_cmd_runner)
9297
)
9398

94-
return xargs(cmd + tuple(hook['args']), file_args)
99+
return xargs(cmd + tuple(entry_args) + tuple(hook['args']), file_args)

testing/resources/docker_hooks_repo/hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
language: docker
55
files: \.txt$
66

7+
- id: docker-hook-arg
8+
name: Docker test hook
9+
entry: echo -n
10+
language: docker
11+
files: \.txt$
12+
713
- id: docker-hook-failing
814
name: Docker test hook with nonzero exit code
915
entry: bork

tests/repository_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ def test_run_a_docker_hook(tempdir_factory, store):
141141
)
142142

143143

144+
@skipif_slowtests_false
145+
@skipif_cant_run_docker
146+
@pytest.mark.integration
147+
def test_run_a_docker_hook_with_entry_args(tempdir_factory, store):
148+
_test_hook_repo(
149+
tempdir_factory, store, 'docker_hooks_repo',
150+
'docker-hook-arg',
151+
['Hello World from docker'], b'Hello World from docker',
152+
)
153+
154+
144155
@skipif_slowtests_false
145156
@skipif_cant_run_docker
146157
@pytest.mark.integration

0 commit comments

Comments
 (0)