-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from tesla-local-control/githooks
add githooks from _core
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
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 -- |
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
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 |