epcim
5/2/2017 - 7:24 AM

Git pre-commit hook / check to stop accidental commits to master and develop branches. There is also a variant with a core.whitespace check.

Git pre-commit hook / check to stop accidental commits to master and develop branches. There is also a variant with a core.whitespace check.

#!/bin/bash
# Stop commit on SaltMaster reclass branch.
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/raw/pre-commit-5
# chmod +x .git/hooks/pre-commit

BRANCH=`git rev-parse --abbrev-ref HEAD`

if [[ "$BRANCH" == "master" || "$BRANCH" == "next" || "$BRANCH" == "$(cat nodes/cfg*$(hostname -f)* | awk -F: '/reclass_data_revision:/{ print $2}' | head -n1)" ]]; then
        echo "You are on branch $BRANCH, which is a protected reclass branch."
        echo "Any commits directly to this branch on SaltMaster are deprecated practice. Please send PR or gerrit review."
        echo "To force, commit with -n to bypass this pre-commit hook."
        exit 1
fi

exit 0
#!/bin/bash
# Checks yo whitespace on commit.
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/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
#!/bin/bash
# Stops accidental commits to master and develop.
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/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
# Stops accidental commits to master and develop.
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/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.
# Install:
# cd path/to/git/repo
# curl -fL -o .git/hooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/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/sh
# This gist contains pre-commit hooks to prevent you from commiting bad code or to the wrong branch.
# There are four variants that I have built:
# - pre-commit: stops commits to "master" and "develop" branches.
# - pre-commit-2: also includes a core.whitespace check.
# - pre-commit-3: the core.whitespace check and an EOF-newline-check.
# - pre-commit-4: only the core.whitespace check.
# - pre-commit-5: stop SaltMaster direct reclass branch commits.
# Set desired version like this before installing:
# FILE=pre-commit

# Global installation instructions:
# mkdir $HOME/.githooks
# git config --global core.hooksPath $HOME/.githooks
# curl -fL -o $HOME/.githooks/pre-commit https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/raw/${FILE:-pre-commit}
# chmod +x $HOME/.githooks/pre-commit
# Uninstall:
# rm $HOME/.githooks/pre-commit

# Install in current Git repo only:
# curl -fL https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/raw/install-pre-commit.sh | sh -s ${FILE:-pre-commit-5}
# Uninstall:
# rm .git/hooks/pre-commit

GITROOT=`git rev-parse --show-toplevel 2> /dev/null`

echo
echo

if [ "$GITROOT" == "" ]; then
	echo This does not appear to be a git repo.
	exit 1
fi

if [ -f "$GITROOT/.git/hooks/pre-commit" ]; then
	echo There is already a pre-commit hook installed. Delete it first.
	echo
	echo "    rm '$GITROOT/.git/hooks/pre-commit'"
	echo
	exit 2
fi

FILE=${1:-pre-commit}

echo Downloading $FILE hook from https://gist.github.com/epcim/45bff8b6e07ff3d4b061be199b97fe88
echo

curl -fL -o "$GITROOT/.git/hooks/pre-commit" "https://gist.githubusercontent.com/epcim/45bff8b6e07ff3d4b061be199b97fe88/raw/$FILE"
if [ ! -f "$GITROOT/.git/hooks/pre-commit" ]; then
	echo Error downloading pre-commit script!
	exit 3
fi

chmod +x "$GITROOT/.git/hooks/pre-commit"

echo "You're all set! Happy hacking!"

exit 0