Skip to content

Instantly share code, notes, and snippets.

@rnarian
Forked from stefansundin/install-pre-commit.sh
Last active March 14, 2016 10:26
Show Gist options
  • Save rnarian/5bb974bd876beb5de11f to your computer and use it in GitHub Desktop.
Save rnarian/5bb974bd876beb5de11f to your computer and use it in GitHub Desktop.
Git pre-commit check to stop accidental commits to master and develop branches. There is also a variant with a core.whitespace check.
#!/bin/bash
# Stops accidental commits to master and develop. Exceptions to commits starting with "Release".
# https://gist.github.com/rnarian/5bb974bd876beb5de11f
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/commit-msg https://gist.githubusercontent.com/rnarian/5bb974bd876beb5de11f/raw/commit-msg
# chmod +x .git/hooks/commit-msg
BRANCH=`git rev-parse --abbrev-ref HEAD`
MESSAGE=$(cat .git/COMMIT_EDITMSG)
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
if [[ ! "$MESSAGE" =~ ^Release.* ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
fi
exit 0
#!/bin/bash
# Stops accidental commits to master and develop. https://gist.github.com/rnarian/5bb974bd876beb5de11f
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/rnarian/5bb974bd876beb5de11f/raw/pre-commit
# chmod +x .git/hooks/pre-commit
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
exit 0
#!/bin/bash
# Stops accidental commits to master and develop. https://gist.github.com/rnarian/5bb974bd876beb5de11f
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/rnarian/5bb974bd876beb5de11f/raw/pre-commit-2
# chmod +x .git/hooks/pre-commit
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
if [ "`git diff --check --cached | wc -c`" -gt 0 ]; then
echo "Your spaces don't agree with your core.whitespace rules."
echo 'Please run `git diff --check HEAD` to see your errors.'
echo "You can commit with -n to bypass this pre-commit hook."
exit 2
fi
exit 0
#!/bin/bash
# Stops accidental commits to master and develop. https://gist.github.com/rnarian/5bb974bd876beb5de11f
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/rnarian/5bb974bd876beb5de11f/raw/pre-commit-3
# chmod +x .git/hooks/pre-commit
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
if [ "`git diff --check --cached | wc -c`" -gt 0 ]; then
echo "Your spaces don't agree with your core.whitespace rules."
echo 'Please run `git diff --check HEAD` to see your errors.'
echo "You can commit with -n to bypass this pre-commit hook."
exit 2
fi
NOEOF=()
FILES=`git status --porcelain | grep "^M" | cut -b4-`
while read -r f; do
if [ "`tail -c 1 $f`" != "" ]; then
NOEOF+=("$f")
fi
done <<< "$FILES"
if [ ${#NOEOF[@]} -gt 0 ]; then
echo "No newlines at the end of these files:"
for f in "${NOEOF[@]}"; do
echo " $f"
done
echo
echo "To check your whole repository, run this:"
echo
echo ' git ls-tree -r -z --name-only HEAD | xargs -0 file | grep text | cut -d: -f1 | xargs -I {} bash -c '\''if [ -n "`tail -c 1 "{}"`" ]; then echo {}; fi'\'''
echo
echo "You can commit with -n to bypass this pre-commit hook."
exit 3
fi
exit 0
#!/bin/bash
# Checks yo whitespace on commit. https://gist.github.com/rnarian/5bb974bd876beb5de11f
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/rnarian/5bb974bd876beb5de11f/raw/pre-commit-4
# chmod +x .git/hooks/pre-commit
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ "`git diff --check --cached | wc -c`" -gt 0 ]; then
echo "Your spaces don't agree with your core.whitespace rules."
echo 'Please run `git diff --check HEAD` to see your errors.'
echo "You can commit with -n to bypass this pre-commit hook."
exit 2
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment