Skip to content

Instantly share code, notes, and snippets.

@bultas
Created June 13, 2017 15:25
Show Gist options
  • Save bultas/15d65cea5f3e46897973f029e9cd407e to your computer and use it in GitHub Desktop.
Save bultas/15d65cea5f3e46897973f029e9cd407e to your computer and use it in GitHub Desktop.
Git Hooks
#!/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/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/check.sh
#!/bin/sh
.git/hooks/eslint.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment