From a6b217035d14e5838863588dde4f59bba2ae2569 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 17:08:16 -0500 Subject: [PATCH 1/6] copy yapf hook --- tools/pre-commit.sh | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tools/pre-commit.sh diff --git a/tools/pre-commit.sh b/tools/pre-commit.sh new file mode 100644 index 0000000..3e68739 --- /dev/null +++ b/tools/pre-commit.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +# Git pre-commit hook to check staged Python files for formatting issues with +# yapf. +# +# INSTALLING: Copy this script into `.git/hooks/pre-commit`, and mark it as +# executable. +# +# This requires that yapf is installed and runnable in the environment running +# the pre-commit hook. +# +# When running, this first checks for unstaged changes to staged files, and if +# there are any, it will exit with an error. Files with unstaged changes will be +# printed. +# +# If all staged files have no unstaged changes, it will run yapf against them, +# leaving the formatting changes unstaged. Changed files will be printed. +# +# BUGS: This does not leave staged changes alone when used with the -a flag to +# git commit, due to the fact that git stages ALL unstaged files when that flag +# is used. + +# Find all staged Python files, and exit early if there aren't any. +PYTHON_FILES=() +while IFS=$'\n' read -r line; do PYTHON_FILES+=("$line"); done \ + < <(git diff --name-only --cached --diff-filter=AM | grep --color=never '.py$') +if [ ${#PYTHON_FILES[@]} -eq 0 ]; then + exit 0 +fi + +########## PIP VERSION ############# +# Verify that yapf is installed; if not, warn and exit. +if ! command -v yapf >/dev/null; then + echo 'yapf not on path; can not format. Please install yapf:' + echo ' pip install yapf' + exit 2 +fi +######### END PIP VERSION ########## + +########## PIPENV VERSION ########## +# if ! pipenv run yapf --version 2>/dev/null 2>&1; then +# echo 'yapf not on path; can not format. Please install yapf:' +# echo ' pipenv install yapf' +# exit 2 +# fi +###### END PIPENV VERSION ########## + + +# Check for unstaged changes to files in the index. +CHANGED_FILES=() +while IFS=$'\n' read -r line; do CHANGED_FILES+=("$line"); done \ + < <(git diff --name-only "${PYTHON_FILES[@]}") +if [ ${#CHANGED_FILES[@]} -gt 0 ]; then + echo 'You have unstaged changes to some files in your commit; skipping ' + echo 'auto-format. Please stage, stash, or revert these changes. You may ' + echo 'find `git stash -k` helpful here.' + echo 'Files with unstaged changes:' "${CHANGED_FILES[@]}" + exit 1 +fi + +# Format all staged files, then exit with an error code if any have uncommitted +# changes. +echo 'Formatting staged Python files . . .' + +########## PIP VERSION ############# +yapf -i -r "${PYTHON_FILES[@]}" +######### END PIP VERSION ########## + +########## PIPENV VERSION ########## +# pipenv run yapf -i -r "${PYTHON_FILES[@]}" +###### END PIPENV VERSION ########## + + +CHANGED_FILES=() +while IFS=$'\n' read -r line; do CHANGED_FILES+=("$line"); done \ + < <(git diff --name-only "${PYTHON_FILES[@]}") +if [ ${#CHANGED_FILES[@]} -gt 0 ]; then + echo 'Reformatted staged files. Please review and stage the changes.' + echo 'Files updated: ' "${CHANGED_FILES[@]}" + exit 1 +else + exit 0 +fi From 77859775e2fdc4b0bbc73c62404ed5d4f95bf050 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 22:51:23 -0500 Subject: [PATCH 2/6] create poorly formatted file --- tmp.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tmp.py diff --git a/tmp.py b/tmp.py new file mode 100644 index 0000000..397fedb --- /dev/null +++ b/tmp.py @@ -0,0 +1,16 @@ + + +import torch + + +def a( b): + print(b ) + + + +if __name__ == '__main__': + a('test') + + + + From 945a3ec2855e8aab11462175ba90db2f7db4d8b2 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 23:00:39 -0500 Subject: [PATCH 3/6] modify poorly formatted file --- tmp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tmp.py b/tmp.py index 397fedb..e6191bf 100644 --- a/tmp.py +++ b/tmp.py @@ -5,6 +5,7 @@ def a( b): print(b ) + print( b) From 9d38605259a63ae32302ff2b5a3773b42950deb0 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 23:02:10 -0500 Subject: [PATCH 4/6] format fix --- tmp.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/tmp.py b/tmp.py index e6191bf..b1389b5 100644 --- a/tmp.py +++ b/tmp.py @@ -1,17 +1,10 @@ - - import torch -def a( b): - print(b ) - print( b) - +def a(b): + print(b) + print(b) if __name__ == '__main__': - a('test') - - - - + a('test') From 8badd57f3fcbfdb84b43c4ddd02e0056b06061a6 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 23:12:13 -0500 Subject: [PATCH 5/6] fix formatting: test 2 --- tmp.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tmp.py b/tmp.py index b1389b5..30df86c 100644 --- a/tmp.py +++ b/tmp.py @@ -4,6 +4,9 @@ def a(b): print(b) print(b) + print("adkalfkal") + + print("adfaosdfjasdlkfjaldsjfalkdj" + "adjfaisdjf") if __name__ == '__main__': From 8fa5d929bd38e12c9ae1069a9fb7cd91f5ca8ea2 Mon Sep 17 00:00:00 2001 From: Misha Shvets Date: Fri, 21 Feb 2020 23:13:27 -0500 Subject: [PATCH 6/6] modify yapf pre-commit script --- tools/pre-commit.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pre-commit.sh b/tools/pre-commit.sh index 3e68739..93b05ea 100644 --- a/tools/pre-commit.sh +++ b/tools/pre-commit.sh @@ -63,7 +63,7 @@ fi echo 'Formatting staged Python files . . .' ########## PIP VERSION ############# -yapf -i -r "${PYTHON_FILES[@]}" +yapf -i -r "${PYTHON_FILES[@]}" || { echo 'yapf failed' ; exit 1; } ######### END PIP VERSION ########## ########## PIPENV VERSION ##########