Git Hooks
Updated on 28 Dec 2018
Hooks
I will be adding 4 hooks.
prepare-commit-msg
to add the branch name to the commit messagepre-commit
to use git-secret to hide files and add them to the commitpost-merge
to use git-secret to reveal files and clean the encrypted onespost-commit
occurs after the commit, I can clean the encrypted files
git hooks get added to the .git/hooks directory. The hooks available are:
- applypatch-msg
- pre-applypatch
- post-applypatch
- pre-commit
- prepare-commit-msg
- commit-msg
- post-commit
- pre-rebase
- post-checkout
- post-merge
- pre-receive
- update
- post-receive
- post-update
- pre-auto-gc
- post-rewrite
- pre-push
Some are server hooks, and others are client hooks. In my examples, we are using client hooks.
prepare-commit-msg
#!/bin/bash
#
# this hook adds the branch name into the commit message.
#
# To enable this hook, rename this file to "prepare-commit-msg".
# This way you can customize which branches should be skipped when
# prepending commit message.
# if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
# fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
fi
Here we can see a commit message to normal_file2.txt
that was done under the jj branch. The message is automatically pre-pended with the branch name.
pre-commit
This pre-commit hook is used to automatically encrypt the files with git secret hide
command, and then add them to the commit. This hook will be called whenever we call commit
, however this only works if a file not part of the chain has been modified or you call commit
twice (first commit will encrypt, second commit will add them).
#!/bin/bash
#
# An example of how to automatically encrypt certain files and add them to the commit.
#
# To enable this hook, rename this file to "pre-commit".
git secret hide
array=( `git secret list` )
for i in "${array[@]}"
do
git add $i".secret"
done
post-merge
This hook is used to unencrypt the files (reveal
) and remove the encrypted versions (clean
) from the local system. This will be done whenever a pull
is issued.
#!/bin/bash
#
# this hook gets called when a 'pull' is done. It will be used to decrypt files
#
# use the -f option in git secret reveal -f so that you are not prompted with the over-write confirmation.
# To enable this hook, rename this file to "post-merge".
git secret reveal -f
git secret clean
post-commit
This hook gets called after a ‘commit’ but does not affect the files already in the commit. It will be used to clean the encrypted files so that they don’t clutter up your workspace. It will however clutter up your git status…
#!/bin/bash
#
# this hook gets called after a 'commit' but does not affect the files already in the commit.
# It will be used to clean the encrypted files
#
# To enable this hook, rename this file to "post-commit".
git secret clean
My workspace is clean, however git status
will happily remind me of the encrypted files that have been deleted.