Skip to content

Commit

Permalink
Merge pull request #29 from tesla-local-control/githooks
Browse files Browse the repository at this point in the history
add githooks from _core
  • Loading branch information
baylanger authored Jul 8, 2024
2 parents 1068b56 + c48dc86 commit 2c810a7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
57 changes: 57 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
#
export NOCOLOR='\033[0m'
export GREEN='\033[0;32m'
export CYAN='\033[0;36m'
export YELLOW='\033[1;32m'
export MAGENTA='\033[0;35m'
export RED='\033[0;31m'

protected_branch='main'
current_branch=$(git rev-parse --abbrev-ref HEAD)

# Find and fix trailing white space and tabs
# Replace tabs with white space
# Bypass it with the --no-verify option to git-commit
#
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

SAVEIFS="$IFS"
# only use new-line character as separator, introduces EOL-bug?
IFS='
'
# Find files with trailing white space
for FILE in $(
git diff-index --check --cached $against -- |
sed '/^[+-]/d' |
(sed -r 's/:[0-9]+:.*//' || sed -E 's/:[0-9]+:.*//') |
uniq
); do
# replace whitespace-characters with nothing
# if first execution of sed-command fails, try second one (Mac OS X version)
(
sed -i -e 's/[ ]*$//g' -e 's/ / /g' $FILE >/dev/null 2>&1 ||
sed -i '' -e 's/[ ]*$//g' -e 's/ / /g' $FILE
) &&
# (re-)add files that have been altered to Git commit-tree
# when change was a [:space:]-character @EOL|EOF git-history becomes weird...
git add "$FILE"
done
# restore $IFS
IFS="$SAVEIFS"

# Prevent commit on main branch
#
if [ $protected_branch = $current_branch ]; then
echo -e "${YELLOW}You should not commit to branch ${RED}$protected_branch${NOCOLOR}"
echo -e "To bypass the protection, add ${GREEN}--no-verify${NOCOLOR} to the git commit command"
exit 1 # push will not execute
fi

# Exit script with the exit-code of git's check for white space characters
exec git diff-index --check --cached $against --
23 changes: 23 additions & 0 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
export NOCOLOR='\033[0m'
export GREEN='\033[0;32m'
export CYAN='\033[0;36m'
export YELLOW='\033[1;32m'
export MAGENTA='\033[0;35m'
export RED='\033[0;31m'

protected_branch='main'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]; then
echo -e "${RED}ATTENTION:$NOCOLOR You are on protected ${RED}branch $protected_branch${NOCOLOR}"
read -p "Are you sure you want to push here? (yes/no) " -n 3 -r </dev/tty
echo
if echo $REPLY | grep -E '^[Yy][Ee][Ss]$' >/dev/null; then
exit 0 # push will execute
fi
exit 1 # push will not execute
else
exit 0 # push will execute
fi

0 comments on commit 2c810a7

Please sign in to comment.