Last active
December 17, 2015 11:29
-
-
Save udoprog/5602616 to your computer and use it in GitHub Desktop.
Git Hook
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
#!/bin/bash | |
# | |
# Hook suitable for maintaining hiera directories. | |
# | |
# --- Command line | |
refname="$1" | |
oldrev="$2" | |
newrev="$3" | |
# --- Safety check | |
if [ -z "$GIT_DIR" ]; then | |
echo "Don't run this script from the command line." >&2 | |
echo " (if you want, you could supply GIT_DIR then run" >&2 | |
echo " $0 <ref> <oldrev> <newrev>)" >&2 | |
exit 1 | |
fi | |
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then | |
echo "Usage: $0 <ref> <oldrev> <newrev>" >&2 | |
exit 1 | |
fi | |
# --- Check types | |
# if $newrev is 0000...0000, it's a commit to delete a ref. | |
zero="0000000000000000000000000000000000000000" | |
if [ "$newrev" = "$zero" ]; then | |
newrev_type=delete | |
else | |
newrev_type=$(git cat-file -t $newrev) | |
fi | |
checkout_directory="/tmp/checkout_directory" | |
checkout_lock="/tmp/checkout_directory/.lock" | |
blobs_directory="${checkout_directory}/blobs" | |
function build_path() { | |
branch="$1" | |
case "$branch" in | |
hiera/*/*) | |
hiera_name=${branch##hiera/} | |
echo "${checkout_directory}/hiera/${hiera_name}" | |
;; | |
*) | |
echo "${checkout_directory}/branch/${branch}" | |
;; | |
esac | |
} | |
function main() { | |
case "$refname","$newrev_type" in | |
refs/tags/*,commit) ;; | |
refs/tags/*,delete) ;; | |
refs/tags/*,tag) ;; | |
refs/remotes/*,commit) ;; | |
refs/remotes/*,delete) ;; | |
refs/heads/*,commit) | |
branch=${refname##refs/heads/} | |
link=$(build_path "$branch") | |
link_directory=$(dirname "$link") | |
blob_name=$(base64 <<< "$branch") | |
checkout="${blobs_directory}/${blob_name}" | |
mkdir -p "$link_directory" | |
mkdir -p "$checkout" | |
echo "creating blob: $checkout" | |
GIT_WORK_TREE=$checkout git checkout --force --quiet $newrev | |
echo "creating link: $link -> $checkout" | |
ln -nsf "$checkout" "$link" | |
;; | |
refs/heads/*,delete) | |
branch=${refname##refs/heads/} | |
link=$(build_path "$branch") | |
if [[ -L "$link" ]]; then | |
blob_path=$(readlink "$link") | |
echo "removing blob: $blob_path" | |
rm -rf "$blob_path" | |
fi | |
echo "removing link: $link" | |
rm -f "$link" | |
link_directory=$(dirname "$link") | |
while rmdir "$link_directory" 2> /dev/null; do | |
echo "removed directory: $link_directory" | |
link_directory=$(dirname "$link_directory") | |
done | |
;; | |
*) | |
# Anything else (is there anything else?) | |
echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 | |
return 1 | |
;; | |
esac | |
return 0 | |
} | |
( | |
flock -e 200 | |
main | |
exit $? | |
) 200> $checkout_lock | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment