Created
February 1, 2019 11:43
-
-
Save sluc23/83bf7d05f06cfb17bbbbeee61c1fd39c to your computer and use it in GitHub Desktop.
Gitlab update hook to rejct tag if extension <version> is different than tag
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 | |
# 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