How to verify all modified lua files via luachecks? - tsafin/tarantool GitHub Wiki
There are several scenarios how you may conveniently check all modified Lua files using luachecks:
- You either run script to verify all those modified (whether they have been added to git index or not),
- Or you could check only files added to the index before committing them to git.
How to check all modified files?
git diff --name-only
provides us a list of files according to the selected filters. --cached
will use files
added to the index, --diff-filter=X
further filters the list using criteria: A
- added files, M
- modified.
We need to find all lua files which have been added (to the index) or modified (in the index or outside of it), thus command will be
git diff --cached --name-only --diff-filter=AM | grep '.lua$'
for added/modified in the index, and git diff --name-only --diff-filter=M | grep '.lua$'
for those, not et placed into index.
Portions of the same file may be added to the index, while the rest left outside in modified state. So we need to filter out non-unique file names.
unique_list=$(cat <<EOF | sort | uniq
$modified_index
$modified_not_index
EOF
Also we do have extensive list of exclusions defined in the .luacheckrc
, but there is no much point try to replicate these extensions in our script - luacheck
will handle it itself.
Putting it altogether we could write smart-luacheck.sh
script as below...
smart-luacheck.sh
#!/usr/bin/env bash
gitroot=$(git rev-parse --show-toplevel)
modified_index=`git diff --cached --name-only --diff-filter=AM | grep '.lua$'`
modified_not_index=`git diff --name-only --diff-filter=M | grep '.lua$'`
unique_list=$(cat <<EOF | sort | uniq
$modified_index
$modified_not_index
EOF
)
cd $gitroot
test ! -z "unique_list" && luacheck --codes $unique_list
How to enable luacheck in pre-commit hook?
Despite smart-luacheck.sh
script above, pre-commit script does not care about files
outside of index file, we going to commit only those in the index. So we do not need
to merge 2 lists together and interested only in the result of this command
git diff --cached --name-only --diff-filter=AM $against | grep '.lua$'
The most important thing here - is to propagate non-zero exit code from luacheck
if
there are any warnings or errors reported, thus luacheck --codes $modified_index || exit 1
Otherwise, if there are no warnings/errors reported then we should return 0 as exit code, so commit would be completed successfully.
.git/hooks/pre-commit
#!/bin/sh
#
# Hook to verify what modified lua files pass luacheck checks before got committed.
# Called by "git commit" with no arguments.
# To enable this hook, put it to `.git/hooks/pre-commit` and make it executable.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# Redirect output to stderr.
exec 1>&2
gitroot=$(git rev-parse --show-toplevel)
modified_index=`git diff --cached --name-only --diff-filter=AM $against | grep '.lua$'`
if test ! -z "$modified_index"
then
cd $gitroot && luacheck --codes $modified_index || exit 1
fi
exit 0
gist with scripts
I've uploaded these 2 scripts to the gist https://gist.github.com/tsafin/1b1326eca583ef39f6b4d9d05e47f7a4