git hooks: address shellcheck issues

This commit is contained in:
William Ballenthin
2020-06-26 19:04:36 -06:00
parent d1dd997b7b
commit 918a47cfb7
3 changed files with 56 additions and 49 deletions

View File

@@ -1,34 +1,38 @@
#!/usr/bin/env bash
# doesn't matter if this gets repeated later on in a hooks file
# Use a console with emojis support for a better experience # Use a console with emojis support for a better experience
# Stash uncommited changes # Stash uncommited changes
MSG="post-commit-$(date +%s)" MSG="post-commit-$(date +%s)";
git stash push -kqum $MSG git stash push -kqum "$MSG";
STASH_LIST=$(git stash list) STASH_LIST=$(git stash list);
if [[ "$STASH_LIST" == *"$MSG"* ]]; then if [[ "$STASH_LIST" == *"$MSG"* ]]; then
echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`" echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`";
fi fi
# Run style checker and print state (it doesn't block the commit) # Run style checker and print state (it doesn't block the commit)
pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1 pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1;
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo 'Style checker succeeds!! 💘' echo 'Style checker succeeds!! 💘';
else else
echo 'Style checker failed 😭\nCheck style-checker-output.log for details' echo 'Style checker failed 😭';
exit 1 echo 'Check style-checker-output.log for details';
exit 1;
fi fi
# Run rule linter and print state (it doesn't block the commit) # Run rule linter and print state (it doesn't block the commit)
python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1 python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1;
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo 'Rule linter succeeds!! 💖' echo 'Rule linter succeeds!! 💖';
else else
echo 'Rule linter failed 😭\nCheck rule-linter-output.log for details' echo 'Rule linter failed 😭';
exit 2 echo 'Check rule-linter-output.log for details';
exit 2;
fi fi
# Restore stashed changes # Restore stashed changes
if [[ "$STASH_LIST" == *"$MSG"* ]]; then if [[ "$STASH_LIST" == *"$MSG"* ]]; then
git stash pop -q --index git stash pop -q --index;
echo "Stashed changes '$MSG' restored" echo "Stashed changes '$MSG' restored";
fi fi

View File

@@ -1,52 +1,57 @@
#!/usr/bin/env bash
# doesn't matter if this gets repeated later on in a hooks file
# Use a console with emojis support for a better experience # Use a console with emojis support for a better experience
# Stash uncommited changes # Stash uncommited changes
MSG="pre-push-$(date +%s)" MSG="pre-push-$(date +%s)";
git stash push -kqum $MSG git stash push -kqum "$MSG";
STASH_LIST=$(git stash list) STASH_LIST=$(git stash list);
if [[ "$STASH_LIST" == *"$MSG"* ]]; then if [[ "$STASH_LIST" == *"$MSG"* ]]; then
echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`" echo "Uncommited changes stashed with message '$MSG', if you abort before they are restored run \`git stash pop\`";
fi fi
restore_stashed() { restore_stashed() {
if [[ "$STASH_LIST" == *"$MSG"* ]]; then if [[ "$STASH_LIST" == *"$MSG"* ]]; then
git stash pop -q --index git stash pop -q --index;
echo "Stashed changes '$MSG' restored" echo "Stashed changes '$MSG' restored";
fi fi
} }
# Run style checker and print state # Run style checker and print state
pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1 pycodestyle --config=./ci/tox.ini ./capa/ > style-checker-output.log 2>&1;
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo 'Style checker succeeds!! 💘' echo 'Style checker succeeds!! 💘';
else else
echo 'Style checker failed 😭 PUSH ABORTED\nCheck style-checker-output.log for details' echo 'Style checker failed 😭 PUSH ABORTED';
restore_stashed echo 'Check style-checker-output.log for details';
exit 1 restore_stashed;
exit 1;
fi fi
# Run rule linter and print state # Run rule linter and print state
python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1 python ./scripts/lint.py ./rules/ > rule-linter-output.log 2>&1;
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo 'Rule linter succeeds!! 💖' echo 'Rule linter succeeds!! 💖';
else else
echo 'Rule linter failed 😭 PUSH ABORTED\nCheck rule-linter-output.log for details' echo 'Rule linter failed 😭 PUSH ABORTED';
restore_stashed echo 'Check rule-linter-output.log for details';
exit 2 restore_stashed;
exit 2;
fi fi
# Run tests # Run tests
echo 'Running tests, please wait ⌛' echo 'Running tests, please wait ⌛';
pytest tests/ --maxfail=1 pytest tests/ --maxfail=1;
if [ $? == 0 ]; then if [ $? == 0 ]; then
echo 'Tests succeed!! 🎉' echo 'Tests succeed!! 🎉';
else else
echo 'Tests failed 😓 PUSH ABORTED\nRun `pytest -v --cov=capa test/` if you need more details' echo 'Tests failed 😓 PUSH ABORTED';
restore_stashed echo 'Run `pytest -v --cov=capa test/` if you need more details';
exit 3 restore_stashed;
exit 3;
fi fi
echo 'PUSH SUCCEEDED 🎉🎉' echo 'PUSH SUCCEEDED 🎉🎉';
restore_stashed restore_stashed;

View File

@@ -1,23 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -euo pipefail
set -u
set -o pipefail
GIT_DIR=`git rev-parse --show-toplevel` GIT_DIR=$(git rev-parse --show-toplevel);
cd $GIT_DIR cd "$GIT_DIR";
# hooks may exist already (e.g. git-lfs configuration) # hooks may exist already (e.g. git-lfs configuration)
# If the `.git/hooks/$arg` file doesn't exist it, initialize with `#!/bin/sh` # If the `.git/hooks/$arg` file doesn't exist it, initialize with `#!/bin/sh`
# After that append `scripts/hooks/$arg` and ensure they can be run # After that append `scripts/hooks/$arg` and ensure they can be run
create_hook() { create_hook() {
if [[ ! -e .git/hooks/$1 ]]; then if [[ ! -e .git/hooks/$1 ]]; then
echo "#!/bin/sh" > ".git/hooks/$1" echo "#!/bin/sh" > ".git/hooks/$1";
fi fi
cat scripts/hooks/$1 >> ".git/hooks/$1" cat scripts/hooks/"$1" >> ".git/hooks/$1";
chmod +x .git/hooks/$1 chmod +x .git/hooks/"$1";
} }
echo '\n#### Copying hooks into .git/hooks' printf '\n#### Copying hooks into .git/hooks';
create_hook 'post-commit' create_hook 'post-commit';
create_hook 'pre-push' create_hook 'pre-push';