|
#!/bin/bash |
|
. "$( git rev-parse --show-toplevel )/.hooks/commons.sh" |
|
|
|
|
|
function lint_js_files { |
|
local js_files="$1" |
|
local result=0 |
|
|
|
# Check the JS files |
|
if [ -n "${js_files}" ] |
|
then |
|
xargs js-beautify -qr <<<"${js_files}" |
|
|
|
if [ -n "$( git diff --name-only )" ] |
|
then |
|
[ ${result} -ne 0 ] && echo |
|
result=1 |
|
echo '[JS] Some files seem to not be conforming to the coding style (JSBeautify).' |
|
echo |
|
git diff -U0 |
|
fi |
|
fi |
|
|
|
# Check the JS files (again :) |
|
if [ -n "${js_files}" ] |
|
then |
|
local lint_errors=$( xargs jshint <<< "${js_files}" | grep : ) |
|
if [ -n "${lint_errors}" ] |
|
then |
|
[ ${result} -ne 0 ] && echo |
|
result=1 |
|
echo '[JS] Some files seem to not be conforming to the coding style (JSHint).' |
|
echo |
|
cat <<<"${lint_errors}" |
|
fi |
|
fi |
|
return ${result} |
|
} |
|
|
|
|
|
function lint_scss_files { |
|
local scss_files="$1" |
|
local result=0 |
|
|
|
# Check the SCSS files |
|
if [ -n "${scss_files}" ] |
|
then |
|
local lint_errors="$( xargs scss-lint <<<"${scss_files}" )" |
|
|
|
if [ -n "${lint_errors}" ] |
|
then |
|
[ ${result} -ne 0 ] && echo |
|
result=1 |
|
echo '[SCSS] Some files seem to not be conforming to the coding style (SCSS-Lint).' |
|
echo |
|
cat <<<"${lint_errors}" |
|
fi |
|
fi |
|
return ${result} |
|
} |
|
|
|
|
|
function lint_py_files { |
|
local py_files="$1" |
|
local result=0 |
|
|
|
if [ -n "${py_files}" ] |
|
then |
|
local lint_errors="$( xargs flake8 <<<"${py_files}" )" |
|
|
|
if [ -n "${lint_errors}" ] |
|
then |
|
[ ${result} -ne 0 ] && echo |
|
result=1 |
|
echo '[PY] Some files seem to not be conforming to the coding style (Flake8).' |
|
echo |
|
cat <<<"${lint_errors}" |
|
fi |
|
fi |
|
return ${result} |
|
} |
|
|
|
|
|
function lint_files { |
|
local folder="$( pwd )" |
|
cd "$( git rev-parse --show-toplevel )" |
|
|
|
if git status --porcelain 2>/dev/null | grep '^.M '>/dev/null; |
|
then |
|
# Stash the current changes, so that we preserve them |
|
has_unstaged_changes=true |
|
git stash -q --keep-index |
|
else |
|
has_unstaged_changes=false |
|
fi |
|
|
|
# Get the list of changed files from the index |
|
update_list=$( git diff --name-only --staged ) |
|
|
|
# We only have interest in py/js/scss files |
|
py_files=$( grep '\.py$' <<<"${update_list}" ) |
|
js_files=$( grep '\.js$' <<<"${update_list}" ) |
|
scss_files=$( grep '\.scss$' <<<"${update_list}" ) |
|
|
|
lint_js_files "${js_files}" && |
|
lint_scss_files "${scss_files}" && |
|
lint_py_files "${py_files}" |
|
local status=$? |
|
|
|
if ${has_unstaged_changes}; then |
|
# Drop the temporary modifications, and restore the stash |
|
git reset -q --hard |
|
git stash pop -q --index |
|
fi |
|
|
|
cd "${folder}" |
|
return ${status} |
|
} |
|
|
|
|
|
function lint_branch_name { |
|
local branch="$( get_branch_name )" |
|
|
|
if ! is_special_branch "${branch}" |
|
then |
|
if ! has_valid_id "${branch}" |
|
then |
|
echo "Bad branch name: should be (feature/bug/chore/)detail_xxxx." |
|
echo "Please use 'git branch -m ${branch} ${branch}_xxxxx'" |
|
exit 1 |
|
fi |
|
fi |
|
} |
|
|
|
lint_branch_name && \ |
|
if ${AUTO_LINT_FILES:-false}; then lint_files; fi |