Skip to content

Instantly share code, notes, and snippets.

@s-m-i-t-a
Forked from bultas/pre-commit
Last active March 6, 2019 08:26
Show Gist options
  • Save s-m-i-t-a/41e3295c6cf85fe7341c to your computer and use it in GitHub Desktop.
Save s-m-i-t-a/41e3295c6cf85fe7341c to your computer and use it in GitHub Desktop.
Pre-commit to check tracked/commited js/jsx/py files with ESlint and pep8
#!/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
# Example usage
# In this example it's used to run `npm install` if package.json changed and `bower install` if `bower.json` changed.
check_run package.json "npm install"
#!/bin/sh
set -e
PATH="/usr/local/bin:$PATH"
# for python only
build_tags -i -v
# for generic use
# trap 'rm -f "$$.tags"' exit
# git ls-files | \
# ctags --tag-relative -L - -f"$$.tags"
# # Filter out false matches from class method regex
# sed -i '' -E '/^(if|switch|function|module\.exports|it|describe).+language:js$/d' "$$.tags"
# # Filter out false matches from object definition regex
# sed -i '' -E '/var[ ]+[a-zA-Z0-9_$]+[ ]+=[ ]+require\(.+language:js$/d' "$$.tags"
# mv "$$.tags" "tags"
#!/bin/bash
# use eslint set from environment, otherwise use default
ESLINT=${ESLINT:-"node_modules/eslint/bin/eslint.js"}
if [ ! -f "$ESLINT" ]; then
exit 0
fi
files=$(git diff --diff-filter=ACMRT --cached --name-only | grep '\.jsx\|\.js\?$')
# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
exit 0
fi
failed=0
for file in ${files}; do
git show :$file | $ESLINT --stdin --stdin-filename $file
if [[ $? != 0 ]] ; then
failed=1
fi
done;
if [[ $failed != 0 ]] ; then
echo "ESLint check failed, commit denied"
exit $failed
fi
#!/bin/sh
.git/hooks/ctags >/dev/null 2>&1 &
#!/bin/sh
.git/hooks/ctags >/dev/null 2>&1 &
#!/bin/sh
.git/hooks/check.sh
.git/hooks/ctags >/dev/null 2>&1 &
#!/bin/sh
case "$1" in
rebase) exec .git/hooks/post-merge ;;
esac
#!/bin/sh
# for python
.git/hooks/pylint.sh
# for javascript
.git/hooks/eslint.sh
.git/hooks/tslint.sh
#!/bin/sh
FILES=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$ | grep -v migrations)
if [ -n "$FILES" ]; then
for f in $FILES; do
pep8 -r $f
done
fi
#!/bin/bash
# use tslint set from environment, otherwise use default
TSLINT=${TSLINT:-"node_modules/tslint/bin/tslint"}
if [ ! -f "$TSLINT" ]; then
exit 0
fi
files=$(git diff --diff-filter=ACMRT --cached --name-only | grep '\.tsx\|\.ts\?$')
# Prevent TSlint help message if no files matched
if [[ $files = "" ]] ; then
exit 0
fi
failed=0
for file in ${files}; do
$TSLINT $file
if [[ $? != 0 ]] ; then
failed=1
fi
done;
if [[ $failed != 0 ]] ; then
echo "TSLint check failed, commit denied"
exit $failed
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment