aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorziggys <ziggys@autistici.org>2018-06-09 17:16:54 -0400
committerziggys <ziggys@autistici.org>2018-06-09 17:16:54 -0400
commitf8baf4f992732fb81d9f7cae9be7f80843b892e0 (patch)
treee617822febdfae6be2fdd782b9bc6bcc5ad81eaf
downloadgc2gs-f8baf4f992732fb81d9f7cae9be7f80843b892e0.zip
gc2gs-f8baf4f992732fb81d9f7cae9be7f80843b892e0.tar
Release version 1.0.0
-rw-r--r--LICENSE16
-rwxr-xr-xgc2gs.sh204
-rwxr-xr-xsnippet/snippet79
3 files changed, 299 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f728a90
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,16 @@
+The Drunken BEER License - v 1.1
+---------------------------------
+
+This shit must be considered throw up into the Fucking Pub[alcoho]lic Domain.
+
+This shit is poured 'AS-IS', with ABSOLUTELY ANY FUCKING EXPRESS OR IMPLIED
+WARRANTY OF ANY FUCKING KIND. May the fellow one who developed be not held liable
+for any trouble or damage caused by anything an stupid one does with this shit.
+
+In place to recognize the effort of the fellow one who poured this, don't behave as
+an asshole and give proper credit.
+
+When some modifications or enhacements are maded on this drunken work, please
+share with the other fellows in the pub, cause everyone get equally drunk.
+
+Drink happy!
diff --git a/gc2gs.sh b/gc2gs.sh
new file mode 100755
index 0000000..67006ab
--- /dev/null
+++ b/gc2gs.sh
@@ -0,0 +1,204 @@
+#!/bin/sh
+# -*- ENCODING: UTF-8 -*-
+# gc2gs - Git-project Commit to Gnu-Social
+# Post last git-project commit info to gnu-social
+# Author: ziggys
+# License: The Drunken BEER License v 1.0 (https://git.p4g.club/git/beer/about)
+
+# scriptinfo
+SCRIPTNAME="${0##*/}"
+SCRIPTNAME="${SCRIPTNAME%%.sh}"
+VERSION="1.0.0" # Release version 1.0.0
+AUTHOR="ziggys"
+LICENSE="The Drunken BEER License v 1.0 (https://git.p4g.club/git/beer/about)"
+
+# show help
+show_help () {
+test $# = 0 || echo "$@"
+echo "${SCRIPTNAME} version $VERSION"
+echo " License: "${LICENSE}""
+echo
+echo "${SCRIPTNAME} is a shellscript that posts a status message to a"
+echo " gnu-social node with the last commit information of a git-project"
+echo
+echo "Usage: $0 [OPTIONS] <args> ..."
+echo "Usage: $0 [-[ckp]vh] projectname"
+echo
+echo "Options"
+echo " -c <projectname> Create a configuration file for 'projectname'"
+echo " -k <projectname> Create a 'pre-push' 'git hook' for 'projectname'"
+echo " -p <projectname> Post a status message with 'projectname'"
+echo " last commit information. Usefull when running"
+echo " the script manually"
+echo " -v VERBOSE Increase verbosity"
+echo " -h HELP Print this help"
+echo
+exit $?
+}
+
+# configure new project.config
+configure () {
+ printf "Configuring a new project.config file for %s\\n\\n" "${CONFNAME}"
+
+ test -f "${RCFILE}" \
+ && printf "%s already exists...\\ndo you want to overwrite? " "${RCFILE}" \
+ && read -p "[yes to create a new one] (yes/no): " OVERANS \
+ || echo "" > "${RCFILE}"
+
+ test "${OVERANS}" = "no" \
+ && printf "you will be using old project.config file\\n" \
+ && exit $? \
+ || echo "" > "${RCFILE}"
+
+ printf "\\nPlease enter your gnu-social account information\\n"
+ read -p "'username' (i.e. 'me@myno.de', 'username=me'): " CONFUNAME
+ read -p "'password' (your 'plain-text password'): " CONFPASS
+ read -p "'node' (url w/o trainling slash 'https://myno.de'): " CONFNODE
+ # obfuscate user and password
+ CONFUNAME="$(echo "$CONFUNAME" | base64 -)"
+ CONFPASS="$(echo "$CONFPASS" | base64 -)"
+
+ printf "\\nPlease enter your git project information\\n"
+ read -p "git project (the name of your git repository): " CONFPROJ
+ read -p "project url (https://git.reposito.ry/me/project): " CONFPURL
+ read -p "project path (i.e. ${HOME}/git/myrepo ): " REPOPATH
+ read -p "platform (cgit | gitlab | github | gitea | other): " CONFPLAT
+
+ printf "\\nPopulating %s...\\n" "${RCFILE}"
+ printf "USER=%s;\\nPASSWORD=%s;\\nNODE=%s;\\n" \
+ "${CONFUNAME}" "${CONFPASS}" "${CONFNODE}" >> ${RCFILE}
+ printf "PROJECT=%s;\\nPROJECTURL=%s;\\nREPOPATH=%s;\\nPLATFORM=%s;\\n" \
+ "${CONFPROJ}" "${CONFPURL}" "${REPOPATH}" "${CONFPLAT}" >> "${RCFILE}"
+
+ # this file stores passwords, securize permissions
+ chmod 600 "${RCFILE}"
+
+ printf "\\ndo you want to configure an automatic 'pre-push' 'git hook'? "
+ read -p "(yes/no): " HOOKANS
+ test "${HOOKANS}" = "no" \
+ && printf "done!\\n" \
+ && exit $? \
+ || hook_config
+}
+
+# configure a pre-push git hook
+hook_config () {
+ sed 's/nullconfname/'"${CONFNAME}"'/' "${SNIPPET}" > "$CPREPUSH"
+ printf "\\nConfiguring a 'pre-push' 'git hook' for %s\\n" "${CONFNAME}"
+
+ test ! -d "${REPOPATH}" \
+ && printf "\\n%s is not a directory... exiting" "${REPOPATH}" \
+ && exit $? \
+ || isgit
+
+ test ! -z "${ISGIT}" \
+ && chmod +x "${CPREPUSH}" \
+ && cp "${CPREPUSH}" "${REPOPATH}/${HOOKPATH}" \
+ && printf "\\ndone!"
+
+ exit 0
+}
+
+# check if the repopath is a git repository
+isgit (){
+ cd ${REPOPATH}
+ git rev-parse > /dev/null
+ test "$?" -ne 0 \
+ && printf "\\n%s is not a git repository, exiting..." "${REPOPATH}" \
+ && exit $? \
+ || ISGIT="notnull"
+}
+
+# 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" > "${VOUTPUT}"
+
+ test "$?" -lt 1 && printf "%s\\n" "$PME" || printf "%s\\n" "$NPME"
+ test ! -z "${VERBOSE}" && cat "${VOUTPUT}"
+}
+
+# read options from commandline
+test "$#" -lt 1 && show_help
+while getopts ":c:k:x:p:vh" opt; do
+ case ${opt} in
+ c) CONFNAME="${OPTARG}" ; EXECUTE="config" ;;
+ k) CONFNAME="${OPTARG}" ; EXECUTE="hook" ;;
+ p) CONFNAME="${OPTARG}" ; EXECUTE="post" ;;
+ \?) printf "Invalid: -%s" "${OPTARG}" 1>&2 ; exit 3 ;;
+ :) printf "Invalid: -%s requires an argument" "${OPTARG}" 1>&2 ; exit 3 ;;
+ v) VERBOSE="notnull" ;;
+ h) show_help ;;
+ esac
+done
+shift $((OPTIND -1))
+
+# create temp files for operation
+TMPDIR="$(mktemp -d)"
+CPREPUSH="$(mktemp -p ${TMPDIR})"
+INPUT="$(mktemp -p ${TMPDIR})"
+TSTATUS="$(mktemp -p ${TMPDIR})"
+VOUTPUT="$(mktemp -p ${TMPDIR})"
+
+# configure variables
+test -z "${CONFNAME}" && show_help
+XDGCONF="${XDG_HOME_CONFIG:=${HOME}/.config}"
+CONFIGDIR="${XDGCONF}/gc2gs"
+test ! -d "${CONFIGDIR}" && mkdir -p "${CONFIGDIR}"
+RCFILE="${CONFIGDIR}/${CONFNAME}.config"
+SNIPPET="snippet/snippet"
+HOOKPATH=".git/hooks/pre-push"
+
+# 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})"
+
+# check options and execute
+case "${EXECUTE}" in
+ config) configure ;;
+ hook) hook_config ;;
+ post) gs_post ;;
+esac
+
+# clean and exit
+rm -rf "${TMPDIR}"
+exit 0
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