1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-28 09:20:52 +01:00

Refactored githook for pre-commit linting

This commit is contained in:
squidfunk 2016-12-27 10:32:02 +01:00
parent b812c54f66
commit fc024da6e1

View File

@ -20,51 +20,31 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
# Check, if all changes are added to the index # Patch file to store unindexed changes
CWD_CLEAN="$(git diff-index HEAD --)" PATCH_FILE=".working-tree.patch"
# This variables stores the state of the stash. # Revert changes that have been registered in the patch file
# 0 = not restored yet.
# 1 = stash was already reset
STASH_POPPED=0
# Pop the stash in case of trap
function cleanup { function cleanup {
if test -n "$CWD_CLEAN"; then EXIT_CODE=$?
if test "$STASH_POPPED" != 1 ; then if [ -f "$PATCH_FILE" ]; then
# Pop the changes (= stash) back to the work space git apply "$PATCH_FILE" 2> /dev/null
git stash pop -q rm "$PATCH_FILE"
fi fi
fi exit $EXIT_CODE
exit
} }
# Register signal handler # Register signal handlers
trap cleanup SIGHUP SIGINT SIGTERM trap cleanup EXIT SIGINT SIGHUP
# Stash all changes that were not added to the commit # Cancel any changes to the working tree that are not going to be committed
if test -n "$CWD_CLEAN"; then git diff > "$PATCH_FILE"
git stash -q --keep-index --include-untracked git checkout -- .
fi
# Filter relevant files for linting
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep "\.js\|scss$")
# Run the check and print indicator # Run the check and print indicator
echo "Hook[pre-commit]: Running linter..." if [ "$FILES" ]; then
STD_OUT=$(npm run lint --silent) echo "Hook[pre-commit]: Running linter..."
npm run lint --silent || exit 1
# Grab exit code of check
HAD_ERROR=$?
# Pop the changes (= stash) back to the work space
if test -n "$CWD_CLEAN"; then
git stash pop -q
STASH_POPPED=1
fi fi
# In case of error, print output of check
if test "$HAD_ERROR" != 0 ; then
echo ${STD_OUT}
exit 1
fi
# Reset indicator and exit
exit 0