From 92d5cd20414d1a619012031d489ed0bfc521d402 Mon Sep 17 00:00:00 2001 From: Paolo Mainardi Date: Mon, 29 Jan 2018 12:48:39 +0100 Subject: [PATCH 1/4] Update dockerfile by adding a specific pre-commit version --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e4005ea..3238106 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ FROM debian:stable-slim +ENV PRE_COMMIT_VERSION 1.5.1 RUN apt-get update && apt-get install -y \ build-essential \ git \ python-pip \ python3 && \ - pip install pre-commit \ + pip install pre-commit==${PRE_COMMIT_VERSION} \ && mkdir /pre-commit && \ cd /pre-commit && \ git init . && \ From beaaf041e60cacc3ab5c40958fdd57a59775bd2d Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 17 Jun 2021 17:08:21 +0200 Subject: [PATCH 2/4] Use pip3, pre-commit v2.13.0 and split RUN pre-commit is now at v2.13.0 and requires Python 3. Install the `python3-pip` package for that and use `pip3`. Also split the one `RUN` command into separate ones, so that an update of pre-commit doesn't require a rebuild of everything. --- Dockerfile | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3238106..b15db45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,20 @@ FROM debian:stable-slim -ENV PRE_COMMIT_VERSION 1.5.1 +ENV PRE_COMMIT_VERSION 2.13.0 + +WORKDIR /pre-commit + RUN apt-get update && apt-get install -y \ build-essential \ git \ - python-pip \ - python3 && \ - pip install pre-commit==${PRE_COMMIT_VERSION} \ - && mkdir /pre-commit && \ - cd /pre-commit && \ - git init . && \ - pre-commit install + python3-pip \ + python3 \ + && rm -rf /var/lib/apt/lists/* + +RUN git init . -WORKDIR /pre-commit +RUN pip3 install pre-commit==${PRE_COMMIT_VERSION} && \ + pre-commit install CMD ["pre-commit", "run", "--all-files"] From 7ac8fe03f5e2d6817e53ff61713cef681f4c71de Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 17 Jun 2021 18:07:49 +0200 Subject: [PATCH 3/4] Use podman instead of docker executable Look for podman as an exeuctable for running the container. If not found, look for docker as well. --- README.md | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e5360f3..34ee322 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [Pre-commit](http://pre-commit.com/) is a tool created by Yelp that allows you to run pre-commit sanity checks against your repo, to do things like ensuring private keys aren't being added etc. This image packages `pre-commit` in a docker-container, so you can ship it with a setup script you might be using to setup local development environment. # Usage -- Create `.pre-commit-config.yaml` in the root of your repo. For example +- Create `.pre-commit-config.yaml` in the root of your repo. For example ```yaml - repo: git://github.com/pre-commit/pre-commit-hooks @@ -19,21 +19,33 @@ ``` - Add `pre-commit` script to `.git/hooks/pre-commit` ```shell - cd $(git rev-parse --show-toplevel) + #!/bin/sh + toplevel="`git rev-parse --show-toplevel`" + cd "$toplevel" - NAME=$(basename `git rev-parse --show-toplevel`)_precommit - docker ps -a | grep $NAME &> /dev/null - CONTAINER_EXISTS=$? + NAME="`basename "$toplevel"`_precommit" - if [[ CONTAINER_EXISTS -eq 0 ]]; then - docker restart $NAME && docker attach --no-stdin $NAME + if command -v podman > /dev/null 2>&1; then + container_exec=podman + elif command -v docker > /dev/null 2>&1; then + container_exec=docker else - docker run -t -v $(pwd):/pre-commit --name $NAME taghash/pre-commit + echo "No container executable found! Looked for \`podman' and \`docker'." + exit 1 fi + test -n "`"$container_exec" ps -a -q --no-trunc --filter 'name=^/?'"$NAME"'$'`" + CONTAINER_EXISTS=$? + if [ "$CONTAINER_EXISTS" -eq 0 ]; then + "$container_exec" restart "$NAME" && "$container_exec" attach --no-stdin "$NAME" + else + "$container_exec" run -t -v $(pwd):/pre-commit --name "$NAME" alexs77/pre-commit + fi ``` - Create an empty commit to test - ```git commit --allow-empty -m "Test pre-commit"``` + ```shell + git commit --allow-empty -m "Test pre-commit" + ``` # Note - If you are going to use a `pre-commit` plugin that needs dependencies not packaged in this image, you can extend this image and install the dependencies you need @@ -41,7 +53,5 @@ For example, if you add `{ id: detect-aws-credentials }` to `.pre-commit-config.yaml`, you have to mount the directory holding your aws credentials. The docker command (as in the `pre-commit` script) would then become ```shell - docker run -t -v $(pwd):/pre-commit -v $HOME/.aws:/root/.aws:ro --name $NAME taghash/pre-commit + "$container_exec" run -t -v $(pwd):/pre-commit -v $HOME/.aws:/root/.aws:ro --name $NAME alexs77/pre-commit ``` - - From 6b4238e9b130e6f15c0ce1eaee010ff6d6cb947e Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 17 Jun 2021 18:08:44 +0200 Subject: [PATCH 4/4] Use podman instead of docker executable Look for podman as an exeuctable for running the container. If not found, look for docker as well. --- pre-commit | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pre-commit b/pre-commit index 67e0f23..b7e6bd4 100644 --- a/pre-commit +++ b/pre-commit @@ -1,11 +1,22 @@ -cd $(git rev-parse --show-toplevel) +#!/bin/sh +toplevel="`git rev-parse --show-toplevel`" +cd "$toplevel" -NAME=$(basename `git rev-parse --show-toplevel`)_precommit -docker ps -a | grep nucleus_precommit &> /dev/null +NAME="`basename "$toplevel"`_precommit" + +if command -v podman > /dev/null 2>&1; then + container_exec=podman +elif command -v docker > /dev/null 2>&1; then + container_exec=docker +else + echo "No container executable found! Looked for \`podman' and \`docker'." + exit 1 +fi +test -n "`"$container_exec" ps -a -q --no-trunc --filter 'name=^/?'"$NAME"'$'`" CONTAINER_EXISTS=$? -if [[ CONTAINER_EXISTS -eq 0 ]]; then - docker restart $NAME && docker attach --no-stdin $NAME +if [ "$CONTAINER_EXISTS" -eq 0 ]; then + "$container_exec" restart "$NAME" && "$container_exec" attach --no-stdin "$NAME" else - docker run -t -v $(pwd):/pre-commit --name $NAME taghash/pre-commit + "$container_exec" run -t -v $(pwd):/pre-commit --name "$NAME" alexs77/pre-commit fi