Last active
November 22, 2017 14:27
-
-
Save edwinwright/cf2135c4dd99a87dd78cb7284229e0db to your computer and use it in GitHub Desktop.
git hooks install script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
HOOK_NAMES="pre-commit" | |
HOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks | |
INSTALL_DIR=$(git rev-parse --show-toplevel)/hooks | |
COLOR_GREEN=`tput setaf 2` | |
COLOR_RESET=`tput sgr0` | |
for hook in $HOOK_NAMES; do | |
echo "Installing $hook hook..." | |
# If the hook already exists, is executable, and is not a symlink | |
if [ ! -h $HOOK_DIR/$hook -a -x $HOOK_DIR/$hook ]; then | |
echo "Hook already exists, saving old hook backup at $HOOK_DIR/$hook.local..." | |
mv $HOOK_DIR/$hook $HOOK_DIR/$hook.local | |
fi | |
# create the symlink, overwriting the file if it exists | |
# probably the only way this would happen is if you're using an old version of git | |
# -- back when the sample hooks were not executable, instead of being named ____.sample | |
echo -n "Creating symlink..." | |
ln -s -f $INSTALL_DIR/$hook $HOOK_DIR | |
echo "${COLOR_GREEN} Done! ✓${COLOR_RESET}" | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FILE_NAMES=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.json" "*.scss" "*.md" | tr '\n' ' ') | |
[ -z "$FILE_NAMES" ] && exit 0 | |
# Prettify all staged .js files | |
echo "$FILE_NAMES" | xargs ./node_modules/.bin/prettier --write | |
# Add back the modified/prettified files to staging | |
echo "$FILE_NAMES" | xargs git add | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment