aboutsummaryrefslogtreecommitdiff
path: root/snippet/snippet
diff options
context:
space:
mode:
Diffstat (limited to 'snippet/snippet')
-rwxr-xr-xsnippet/snippet79
1 files changed, 79 insertions, 0 deletions
diff --git a/snippet/snippet b/snippet/snippet
new file mode 100755
index 0000000..f549854
--- /dev/null
+++ b/snippet/snippet
@@ -0,0 +1,79 @@
+#!/bin/sh
+# -*- ENCODING: UTF-8 -*-
+# Snippet to create a pre-push git hook using gc2gs
+# Author: ziggys
+# License: The Drunken BEER License v 1.0 (https://git.p4g.club/git/beer/about)
+
+# scriptinfo
+SCRIPTNAME="${0##*/}"
+SCRIPTNAME="${SCRIPTNAME%%.sh}"
+
+# post
+gs_post () {
+ # deobfuscate user and pass
+ USER="$(echo ${USER} | base64 --decode -)"
+ PASSWORD="$(echo ${PASSWORD} | base64 --decode -)"
+ PME="A status message was posted on ${NODE} by ${USER}"
+ NPME="An error ocurred posting on ${NODE}"
+
+ curl -s -u "$USER:$PASSWORD" --data-urlencode status="${STATUS}" \
+ -d source="${SCRIPTNAME}" "${NODE}/api/statuses/update.xml" > /dev/null
+
+ test "$?" -lt 1 && printf "%s\\n" "$PME" || printf "%s\\n" "$NPME"
+}
+
+# create temp files for operation
+TMPDIR="$(mktemp -d)"
+INPUT="$(mktemp -p ${TMPDIR})"
+TSTATUS="$(mktemp -p ${TMPDIR})"
+
+# configure definition
+CONFNAME="nullconfname"
+test -z "${CONFNAME}" && "no argument for configuration file"
+XDGCONF="${XDG_HOME_CONFIG:=${HOME}/.config}"
+CONFIGDIR="${XDGCONF}/gc2gs"
+test ! -d "${CONFIGDIR}" && mkdir -p "${CONFIGDIR}"
+RCFILE="${CONFIGDIR}/${CONFNAME}.config"
+test ! -f "${RCFILE}" && printf "%s notexistent" "${RCFILE}" && exit $?
+
+# input variables
+sed '/^#/ d; s/;[ \t].*//g; s/;$//g; s/[ \t]*$//g' ${RCFILE} > "$INPUT"
+
+USER="$(sed '/USER=/ !d; s/USER=//' "$INPUT")"
+PASSWORD="$(sed '/PASSWORD=/ !d; s/PASSWORD=//' "$INPUT")"
+NODE="$(sed '/NODE=/ !d; s/NODE=//' "$INPUT")"
+PROJECT="$(sed '/PROJECT=/ !d; s/PROJECT=//' "$INPUT")"
+PROJECTURL="$(sed '/PROJECTURL=/ !d; s/PROJECTURL=//' "$INPUT")"
+REPOPATH="$(sed '/REPOPATH=/ !d; s/REPOPATH=//' "$INPUT")"
+PLATFORM="$(sed '/PLATFORM=/ !d; s/PLATFORM=//' "$INPUT")"
+
+# project variables
+cd "${REPOPATH}"
+BRANCH="$(git branch | sed '/^*/ !d; s/* //')"
+COMMITMSG="$(git log -1 --pretty=%B)"
+COMMITTER="$(git log -1 | sed '/Author:/ !d; s/Author: //; s/<*[ \t].*//')"
+COMMITDATE="$(git log -1 | sed '/Date/ !d; s/Date:.[ \t].//; s/-.*//')"
+COMMITDATE="$(date --date "${COMMITDATE}" "+%d-%m %H:%M")"
+COMMITID="$(git log -1 | sed '/commit/ !d; s/commit //')"
+
+# url variables
+GITURI="commit/${COMMITID}"
+CGITURI="commit/?h=${BRANCH}"
+case "${PLATFORM}" in
+ cgit) COMMITURI="${CGITURI}" ;;
+ *) COMMITURI="${GITURI}" ;;
+esac
+COMMITURL="${PROJECTURL}/${COMMITURI}"
+
+# status message
+TTITLE="${TTITTLE:="The $PROJECT project"}"
+printf "%s\\n\\n | New commit on branch %s by %s (at %s):\\n\\n" \
+ "${TTITLE}" "${BRANCH}" "${COMMITER}" "${COMMITDATE}" >> "${TSTATUS}"
+printf "%s (%s)\\n\\ncommit_url: %s\\n\\nproject_url: %s" \
+ "${COMMITMSG}" "${COMMITID}" "${COMMITURL}" "${PROJECTURL}" >> "${TSTATUS}"
+STATUS="$(cat ${TSTATUS})"
+
+# post, clean and exit
+gs_post
+rm -rf "${TMPDIR}"
+exit 0