From 92d5cd20414d1a619012031d489ed0bfc521d402 Mon Sep 17 00:00:00 2001 From: Paolo Mainardi Date: Mon, 29 Jan 2018 12:48:39 +0100 Subject: [PATCH 01/10] 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 02/10] 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 03/10] 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 04/10] 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 From ad35c64cf4798bd86b74e65ec85c356396c98748 Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Thu, 17 Jun 2021 18:40:47 +0200 Subject: [PATCH 05/10] Try to find container executable Look for podman and docker, in this order. --- Dockerfile | 5 ++--- README.md | 44 +++++++++++++++++++++++++++----------------- pre-commit | 23 +++++++++++++++++------ 3 files changed, 46 insertions(+), 26 deletions(-) mode change 100644 => 100755 pre-commit diff --git a/Dockerfile b/Dockerfile index b15db45..9d84627 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,11 +10,10 @@ RUN apt-get update && apt-get install -y \ python3-pip \ python3 \ && rm -rf /var/lib/apt/lists/* - + RUN git init . -RUN pip3 install pre-commit==${PRE_COMMIT_VERSION} && \ +RUN pip3 install pre-commit==${PRE_COMMIT_VERSION} && \ pre-commit install CMD ["pre-commit", "run", "--all-files"] - diff --git a/README.md b/README.md index e5360f3..2fa6a1c 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 @@ -17,31 +17,41 @@ - { id: end-of-file-fixer } - { id: trailing-whitespace } ``` -- Add `pre-commit` script to `.git/hooks/pre-commit` +- 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 + ```shell + git commit --allow-empty -m "Test pre-commit" ``` -- Create an empty commit to test - ```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 -- You might need to change the command, add volumes etc, based on your needs. - 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 +- You might need to change the command, add volumes etc, based on your needs. + 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 ``` - - diff --git a/pre-commit b/pre-commit old mode 100644 new mode 100755 index 67e0f23..b7e6bd4 --- 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 From 5d0a0cf66634f933c56ec07292b809b5efa65d93 Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Thu, 17 Jun 2021 18:43:05 +0200 Subject: [PATCH 06/10] Fix syntax of config yaml file for 2.13.0 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f4a7bf0..50bb69c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,5 @@ +repos: - repo: git://github.com/pre-commit/pre-commit-hooks - sha: master # Use the ref you want to point at hooks: - { id: check-case-conflict } - { id: check-merge-conflict } From 1f137c9e9ee862e5a70e320808ddfca4c5b620cd Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Thu, 17 Jun 2021 18:43:47 +0200 Subject: [PATCH 07/10] Fix syntax of config yaml file for 2.13.0 --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 50bb69c..32427f9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,5 +1,6 @@ repos: - repo: git://github.com/pre-commit/pre-commit-hooks + rev: master hooks: - { id: check-case-conflict } - { id: check-merge-conflict } From 1b43896dc4f2e4f72f5421a7e45e39802e74abb5 Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Thu, 17 Jun 2021 18:44:37 +0200 Subject: [PATCH 08/10] Fix syntax of config yaml file for 2.13.0 --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 32427f9..50e9712 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: git://github.com/pre-commit/pre-commit-hooks - rev: master + rev: v4.0.1 hooks: - { id: check-case-conflict } - { id: check-merge-conflict } From 72c54edd436eabf7c0552b16c83f5a787f8d0b05 Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Fri, 18 Jun 2021 10:14:19 +0200 Subject: [PATCH 09/10] Make restart of container be quiet At least podman prints the ID of the restarted container on stdtout. We don't need that. --- pre-commit | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pre-commit b/pre-commit index b7e6bd4..c2e4cf1 100755 --- a/pre-commit +++ b/pre-commit @@ -1,9 +1,10 @@ #!/bin/sh toplevel="`git rev-parse --show-toplevel`" -cd "$toplevel" - NAME="`basename "$toplevel"`_precommit" +cd "$toplevel" + +# Find container executable; either podman or docker if command -v podman > /dev/null 2>&1; then container_exec=podman elif command -v docker > /dev/null 2>&1; then @@ -12,11 +13,13 @@ else echo "No container executable found! Looked for \`podman' and \`docker'." exit 1 fi + +# Is there already a container for this source directory? 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" + "$container_exec" restart "$NAME" >/dev/null && "$container_exec" attach --no-stdin "$NAME" else "$container_exec" run -t -v $(pwd):/pre-commit --name "$NAME" alexs77/pre-commit fi From 8a10f4ff61583cef0d2609c0a4ffedf09ada60e9 Mon Sep 17 00:00:00 2001 From: Alexander Skwar Date: Fri, 18 Jun 2021 11:23:00 +0200 Subject: [PATCH 10/10] Max line length 74c and update pre-commit script. --- README.md | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2fa6a1c..fdbe270 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # Pre-commit Docker image -[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. + +[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 ```yaml @@ -21,10 +27,11 @@ ```shell #!/bin/sh toplevel="`git rev-parse --show-toplevel`" - cd "$toplevel" - NAME="`basename "$toplevel"`_precommit" + cd "$toplevel" + + # Find container executable; either podman or docker if command -v podman > /dev/null 2>&1; then container_exec=podman elif command -v docker > /dev/null 2>&1; then @@ -33,11 +40,13 @@ echo "No container executable found! Looked for \`podman' and \`docker'." exit 1 fi + + # Is there already a container for this source directory? 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" + "$container_exec" restart "$NAME" >/dev/null && "$container_exec" attach --no-stdin "$NAME" else "$container_exec" run -t -v $(pwd):/pre-commit --name "$NAME" alexs77/pre-commit fi @@ -48,9 +57,13 @@ ``` # 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 -- You might need to change the command, add volumes etc, based on your needs. - For example, if you add `{ id: detect-aws-credentials }` to `.pre-commit-config.yaml`, you have to mount the directory holding your aws credentials. +- 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 +- You might need to change the command, add volumes etc, based on your + needs. + 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 "$container_exec" run -t -v $(pwd):/pre-commit -v $HOME/.aws:/root/.aws:ro --name $NAME alexs77/pre-commit