Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Last active October 19, 2016 01:48
Show Gist options
  • Save altbdoor/0718a5313e4dda2f3eca to your computer and use it in GitHub Desktop.
Save altbdoor/0718a5313e4dda2f3eca to your computer and use it in GitHub Desktop.
Script for Git pre-commit hook
#!/bin/bash
# to add a protected file, please open protected_file.txt and append a path into
# the file, relative to the project directory.
# ========================================
# defaults
# ========================================
abort_commit_msg="Aborting commit"
text=""
set_text() {
text=$(echo -e "\n$1\n\nAre you sure about this? [y/N] ")
}
# ========================================
# checking protected files
# ========================================
# get current dir, to get protected files
current_dir=$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )
protected_files="$current_dir/protected_files.txt"
# get staged files
staged_files=$(git diff --cached --name-only)
# check staged for protected
protected_stage=""
while read -r line; do
if [[ -n "$line" ]]; then
match=$(echo "$staged_files" | grep -o -P "$line")
if [[ -n "$match" ]]; then
protected_stage=$(echo -e "$protected_stage\n- $match")
fi
fi
done < "$protected_files"
# allow input now
exec < /dev/tty
if [[ -n $protected_stage ]]; then
set_text "You are attempting to commit one or more of the protected files.$protected_stage"
read -p "$text" proceed_with_commit
if [[ "$proceed_with_commit" != 'y' ]]; then
echo "$abort_commit_msg"
exit 1
fi
fi
# ========================================
# checking current branch
# ========================================
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" == 'test' ]]; then
set_text "You are attempting to commit into the \"test\" branch."
read -p "$text" proceed_with_commit
if [[ "$proceed_with_commit" != 'y' ]]; then
echo "$abort_commit_msg"
exit 1
fi
fi
#!/bin/bash
current_dir=$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )
cd "$current_dir/../.git/hooks"
declare -a git_hooks=(
"applypatch-msg"
"pre-applypatch"
"post-applypatch"
"pre-commit"
"prepare-commit-msg"
"commit-msg"
"post-commit"
"pre-rebase"
"post-checkout"
"post-merge"
"pre-push"
"pre-receive"
"update"
"post-receive"
"post-update"
"push-to-checkout"
"pre-auto-gc"
"post-rewrite"
)
for hook in "${git_hooks[@]}"; do
# check if the hook exists
if [[ -f "../../script/hooks/$hook.sh" ]]; then
# pre defined content to execute hook
content="#!/bin/bash
current_dir=\$( cd \"\$(dirname \"\${BASH_SOURCE}\")\" ; pwd -P )
cd \"\$current_dir/../../\"
./script/hooks/$hook.sh"
# output into the sh script
echo "$content" | sed 's/^ *//' > "./$hook"
chmod +x "./$hook"
chmod +x "../../script/hooks/$hook.sh"
echo "Set up $hook hook"
# check if we have unused hook (exist in .git/hook, but not in hook)
elif [[ (! -f "../../script/hooks/$hook.sh") && (-f "./$hook") ]]; then
rm "./$hook"
echo "Remove unused $hook hook"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment