Created
July 15, 2017 16:10
-
-
Save acerosalazar/3618ae1fdb3ae47d883d9ee0f605d567 to your computer and use it in GitHub Desktop.
Pre-push hook for checking swift and bash files using swiftlint and shellcheck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# set -o xtrace | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
function error() { | |
local -r TAG="$1" | |
local -r ERROR="$2" | |
local -r BOC="\033[0;31m" | |
local -r EOC="\033[m" | |
local -r PROMPT="${BOC}[${TAG}]${EOC} %s\n" | |
printf "$PROMPT" "$ERROR" >&2 | |
} | |
function main() { | |
local -r REMOTE="$1" | |
local -r BRANCH="$(git rev-parse --abbrev-ref HEAD)" | |
local SUCCESS=0 | |
if [[ -e "/usr/local/bin/swiftlint" ]]; then | |
local CONFIG_FLAG="" | |
local CONFIG_FILE=".swiftlint.yml" | |
if [[ -f "$CONFIG_FILE" ]]; then | |
CONFIG_FLAG="--config $CONFIG_FILE" | |
fi | |
# Use swiftlint to check modified swift files | |
for SWIFT_FILE in $(git diff --stat $REMOTE/$BRANCH | grep --color=none ".swift" | cut -d' ' -f2); do | |
if [[ -n "$SWIFT_FILE" ]]; then | |
if ! cat "$SWIFT_FILE" | swiftlint lint --strict --quiet --use-stdin $CONFIG_FLAG &> /dev/null; then | |
error "swiflint" "Lint errors found in $SWIFT_FILE" | |
swiftlint autocorrect --path "$SWIFT_FILE" --format --quiet $CONFIG_FLAG | |
SUCCESS=1 | |
fi | |
fi | |
done | |
fi | |
if [[ -e "/usr/local/bin/shellcheck" ]]; then | |
# Use shellcheck to check modified bash files | |
for BASH_FILE in $(git diff --stat $REMOTE/$BRANCH | grep --color=none ".sh" | cut -d' ' -f2); do | |
if [[ -n "$BASH_FILE" ]]; then | |
if ! shellcheck "$BASH_FILE" &> /dev/null; then | |
error "shellcheck" "Lint errors found in $BASH_FILE" | |
SUCCESS=1 | |
fi | |
fi | |
done | |
fi | |
exit "$SUCCESS" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment