Skip to content

Instantly share code, notes, and snippets.

@sluc23
Created Feb 1, 2019
Embed
What would you like to do?
Gitlab update hook to rejct tag if extension <version> is different than tag
#!/bin/bash
# Git arguments
refname="$1"
oldrev="$2"
newrev="$3"
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$*"; exit 1; }
get_repo_name(){
if [ $(git rev-parse --is-bare-repository) = true ]
then
REPOSITORY=$(basename "$PWD")
REPOSITORY=${REPOSITORY%.git}
else
REPOSITORY=$(basename $(readlink -nf "$PWD"/..))
fi
echo "REPOSITORY is $REPOSITORY"
}
check_version(){
TYPE="$1"
if [ "$TYPE" == "civicrm" ]; then
FILENAME="info.xml"
elif [ "$TYPE" == "drupal" ]; then
FILENAME="${REPOSITORY}.info"
fi
OUTPUT=$(git ls-tree --name-only $newrev ${FILENAME})
if [ "$OUTPUT" == "$FILENAME" ]; then
echo "${TYPE} component found. Checking version <==> tag"
TAG=${refname##refs/tags/}
if [ "$TYPE" == "civicrm" ]; then
VERSION=$(git cat-file -p $newrev:${FILENAME} | grep -oP '(?<=<version>)[^<]*')
elif [ "$TYPE" == "drupal" ]; then
VERSION=$(git cat-file -p $newrev:${FILENAME} | awk -F "=" '/version/ {print $2}' | tr -d ' ' | tr -d \'\")
fi
if [ "$VERSION" != "$TAG" ]; then
fatal "${FILENAME} version <${VERSION}> doesn't match with tag <${TAG}>"
else
echo "Check OK"
exit 0
fi
fi
}
# --- 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
case "$refname" in
refs/tags/*)
echo " iXiam Git Hooks"
echo "=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~"
get_repo_name
check_version "civicrm"
check_version "drupal"
;;
esac
# --- Finished
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment