Created
May 14, 2017 15:38
-
-
Save Fardinak/9f4b2c5fd12cc345fe3f369740601204 to your computer and use it in GitHub Desktop.
some git hooks
This file contains hidden or 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 | |
# Inspiration and sonarlint function from: https://github.com/janosgyerik/sonarlint-git-hooks | |
# ENVs | |
# $SKIPBRANCHTEST optional boolean | |
# $SKIPDIFFCHECK optional boolean | |
# $SKIPSONARLINT optional boolean | |
# use `-n` or `--no-verify` to bypass pre-commit and commit-msg hooks | |
### BEGIN_COMMON | |
fatal() { | |
echo ERROR: "$@" >&2 | |
exit 1 | |
} | |
info() { | |
echo info: "$@" | |
} | |
success() { | |
echo OK... "$@" | |
} | |
warning() { | |
echo WARNING: "$@" | |
} | |
print_files() { | |
[[ $# != 1 ]] || return | |
info $1 | |
shift | |
for i; do | |
info " $i" | |
done | |
} | |
### END_COMMON | |
# Stops accidental commits to master and develop. https://gist.github.com/stefansundin/9059706 | |
run_branchtest() { | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then | |
fatal "branch test: You are on branch $BRANCH."; | |
fi | |
} | |
# Git diff check | |
run_diffcheck() { | |
if ! git diff --check --cached; then | |
fatal "diff check: there are some issues" | |
fi | |
} | |
# SonarLint | |
run_sonarlint() { | |
if ! type sonarlint &>/dev/null; then | |
fatal "sonarlint: cannot find 'sonarlint' on PATH" | |
fi | |
sources=() | |
sources_globs= | |
tests=() | |
tests_globs= | |
for file; do | |
if ! [ -f "$file" ]; then | |
info "sonarlint: skip deleted file: $file" | |
continue | |
fi | |
if [[ $file == *src/* ]]; then | |
if [[ $file == *[tT]est* ]]; then | |
tests+=("$file") | |
tests_globs="$tests_globs,$file" | |
else | |
sources+=("$file") | |
fi | |
sources_globs="$sources_globs,$file" | |
continue | |
fi | |
info "sonarlint: skip unknown file: $file" | |
done | |
if [ ${#sources} = 0 -a ${#tests} = 0 ]; then | |
info "sonarlint: no files to analyze" | |
return 2 | |
fi | |
args=(--src "{${sources_globs:1}}") | |
test "$tests_globs" && args+=(--tests "{${tests_globs:1}}") | |
print_files "sonarlint: source files to analyze:" "${sources[@]}" | |
print_files "sonarlint: test files to analyze:" "${tests[@]}" | |
info "sonarlint: analyzing..." | |
issues=$(sonarlint "${args[@]}" | sed -ne '/SonarLint Report/,/^---/p' -e '/Report generated/p') | |
echo "$issues" | |
if ! [[ $issues == *"No issues to display"* ]]; then | |
fatal "sonarlint: some analyses have failed" | |
fi | |
} | |
### RUN TESTS | |
if ! [ "$SKIPBRANCHTEST" ]; then | |
if run_branchtest; then | |
success "branch test passed!" | |
fi | |
else | |
info "skipping branch test" | |
fi | |
if ! [ "$SKIPDIFFCHECK" ]; then | |
if run_diffcheck; then | |
success "diff check passed!" | |
fi | |
else | |
info "skipping diff check" | |
fi | |
if ! [ "$SKIPSONARLINT" ]; then | |
files=($(git diff --name-only --diff-filter=AMR --cached HEAD)) | |
if run_sonarlint "${files[@]}"; then | |
success "sonarlint is happy!" | |
fi | |
else | |
info "skipping sonarlint" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment