From da51c4309c518d3c4e81b200f582f83488ff3717 Mon Sep 17 00:00:00 2001 From: Justin Chung <20733699+justin13888@users.noreply.github.com> Date: Sat, 19 Nov 2022 18:35:40 -0500 Subject: [PATCH 1/5] Add linting pre-commit hook and hook install script --- .githooks/pre-commit | 41 +++++++++++++++++++++++++++++++++++++++++ git-hook-install.sh | 4 ++++ 2 files changed, 45 insertions(+) create mode 100755 .githooks/pre-commit create mode 100755 git-hook-install.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..283b468 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,41 @@ +#!/bin/sh + +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".py\{0,1\}$") + +if [[ "$STAGED_FILES" = "" ]]; then + exit 0 +fi + +PASS=true + +# Check for flake8 +which flake8 &> /dev/null +if [[ "$?" == 1 ]]; then + echo -e "\t\033[41mPlease install flake8\033[0m" + exit 1 +fi + +echo -e "\nLinting Python with Flake8:\n" + +for FILE in $STAGED_FILES +do + flake8 "$FILE" + + if [[ "$?" == 0 ]]; then + echo -e "\t\033[32mPassed: $FILE\033[0m" + else + echo -e "\t\033[41mFailed: $FILE\033[0m" + PASS=false + fi +done + +echo -e "\nPython linting complete!\n" + +if ! $PASS; then + echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n" + exit 1 +else + echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" +fi + +exit $? diff --git a/git-hook-install.sh b/git-hook-install.sh new file mode 100755 index 0000000..c94dbb3 --- /dev/null +++ b/git-hook-install.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +# Install pre-configured git hooks +git config --local core.hooksPath .githooks/ -- 2.39.2 From 8109758914ee113ea8eb3bec20654a218e1fc333 Mon Sep 17 00:00:00 2001 From: Justin Chung <20733699+justin13888@users.noreply.github.com> Date: Sat, 4 Feb 2023 21:44:45 -0500 Subject: [PATCH 2/5] Fix typo in linting check --- .githooks/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 283b468..0c2d5dc 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -32,7 +32,7 @@ done echo -e "\nPython linting complete!\n" if ! $PASS; then - echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n" + echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n" exit 1 else echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" -- 2.39.2 From 42a74c2bc9e0b610d00c4426b30fa7bac48359d4 Mon Sep 17 00:00:00 2001 From: Justin Chung <20733699+justin13888@users.noreply.github.com> Date: Sat, 27 May 2023 20:42:20 +0000 Subject: [PATCH 3/5] Implement feedback in pre-commit script --- .githooks/pre-commit | 37 ++++--------------------------------- lint-docker.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 33 deletions(-) create mode 100755 lint-docker.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 0c2d5dc..6eeb21b 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".py\{0,1\}$") @@ -6,36 +6,7 @@ if [[ "$STAGED_FILES" = "" ]]; then exit 0 fi -PASS=true +docker run --rm -v "$PWD:$PWD:z" -w "$PWD" -e "STAGED_FILES=$STAGED_FILES" python:3.7-buster sh -c './lint-docker.sh "$STAGED_FILES"' +RUN_STATUS=$? -# Check for flake8 -which flake8 &> /dev/null -if [[ "$?" == 1 ]]; then - echo -e "\t\033[41mPlease install flake8\033[0m" - exit 1 -fi - -echo -e "\nLinting Python with Flake8:\n" - -for FILE in $STAGED_FILES -do - flake8 "$FILE" - - if [[ "$?" == 0 ]]; then - echo -e "\t\033[32mPassed: $FILE\033[0m" - else - echo -e "\t\033[41mFailed: $FILE\033[0m" - PASS=false - fi -done - -echo -e "\nPython linting complete!\n" - -if ! $PASS; then - echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n" - exit 1 -else - echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" -fi - -exit $? +exit $RUN_STATUS diff --git a/lint-docker.sh b/lint-docker.sh new file mode 100755 index 0000000..e052e69 --- /dev/null +++ b/lint-docker.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +. venv/bin/activate + +STAGED_FILES="$1" + +echo "STAGED_FILES: $STAGED_FILES" + +echo -e "\nLinting Python files with Flake8:\n" + +PASS=true + +while IFS= read -r FILE; do + flake8 "$FILE" + + if [[ "$?" != 0 ]]; then + PASS=false + fi + # echo "Linted $FILE" +done <<< "$STAGED_FILES" + +echo -e "\nPython linting complete!\n" + +if ! $PASS; then + echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n" + exit 1 +else + echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" +fi + +exit $? -- 2.39.2 From 7cec1425b36332454bb3b99b42e362c6c570f091 Mon Sep 17 00:00:00 2001 From: Justin Chung <20733699+justin13888@users.noreply.github.com> Date: Sun, 28 May 2023 21:54:15 +0000 Subject: [PATCH 4/5] Simplify linting git hook script --- .githooks/pre-commit | 11 ++--------- lint-docker.sh | 25 ++++++------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 6eeb21b..67afa1e 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,12 +1,5 @@ #!/bin/bash -STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".py\{0,1\}$") +docker run --rm -v "$PWD:$PWD:z" -w "$PWD" python:3.9-bullseye sh -c './lint-docker.sh' -if [[ "$STAGED_FILES" = "" ]]; then - exit 0 -fi - -docker run --rm -v "$PWD:$PWD:z" -w "$PWD" -e "STAGED_FILES=$STAGED_FILES" python:3.7-buster sh -c './lint-docker.sh "$STAGED_FILES"' -RUN_STATUS=$? - -exit $RUN_STATUS +exit $? diff --git a/lint-docker.sh b/lint-docker.sh index e052e69..f9b3d00 100755 --- a/lint-docker.sh +++ b/lint-docker.sh @@ -2,30 +2,17 @@ . venv/bin/activate -STAGED_FILES="$1" - -echo "STAGED_FILES: $STAGED_FILES" - echo -e "\nLinting Python files with Flake8:\n" -PASS=true - -while IFS= read -r FILE; do - flake8 "$FILE" - - if [[ "$?" != 0 ]]; then - PASS=false - fi - # echo "Linted $FILE" -done <<< "$STAGED_FILES" +flake8 +PASS=$? echo -e "\nPython linting complete!\n" -if ! $PASS; then +if [ "$PASS" -eq 0 ]; then + echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" + exit $? +else echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n" exit 1 -else - echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n" fi - -exit $? -- 2.39.2 From a6ad08d2c05531761e80b6bc34624c3d3ec5a48e Mon Sep 17 00:00:00 2001 From: Justin Chung <20733699+justin13888@users.noreply.github.com> Date: Sun, 28 May 2023 21:54:31 +0000 Subject: [PATCH 5/5] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5593c4..3b66fbd 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Docker containers instead, which are much easier to work with than the VM. First, make sure you create the virtualenv: ```sh -docker run --rm -v "$PWD:$PWD:z" -w "$PWD" python:3.7-buster sh -c 'apt update && apt install -y libaugeas0 && python -m venv venv && . venv/bin/activate && pip install -r requirements.txt -r dev-requirements.txt' +docker run --rm -v "$PWD:$PWD:z" -w "$PWD" python:3.9-bullseye sh -c 'apt update && apt install -y libaugeas0 && python -m venv venv && . venv/bin/activate && pip install -r requirements.txt -r dev-requirements.txt' ``` Then bring up the containers: ```sh -- 2.39.2