aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorziggys <ziggys@autistici.org>2018-06-10 18:17:06 -0400
committerziggys <ziggys@autistici.org>2018-06-10 18:17:06 -0400
commite718d4f6a8109a6fd693fefba61254192755cd8f (patch)
tree3da892cc180d29aa1b43536e094a4b111df9b0d7
parent71e885dc526f928e37795c0e2fcf4d7238154cc6 (diff)
parent7eff9f324d32590b64f91d5576ce0f981af910a8 (diff)
downloadgc2gs-e718d4f6a8109a6fd693fefba61254192755cd8f.zip
gc2gs-e718d4f6a8109a6fd693fefba61254192755cd8f.tar
Merge branch 'dev'
-rwxr-xr-xgc2gs.sh142
-rwxr-xr-xsnippet/snippet2
2 files changed, 87 insertions, 57 deletions
diff --git a/gc2gs.sh b/gc2gs.sh
index 67006ab..0f0de70 100755
--- a/gc2gs.sh
+++ b/gc2gs.sh
@@ -1,22 +1,23 @@
#!/bin/sh
# -*- ENCODING: UTF-8 -*-
-# gc2gs - Git-project Commit to Gnu-Social
+# gc2gs - Git 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)
+# License: The Drunken BEER License v 1.1 (https://git.p4g.club/git/beer/about)
# scriptinfo
SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME%%.sh}"
-VERSION="1.0.0" # Release version 1.0.0
+VERSION="1.0.2" # Add some basic tests
AUTHOR="ziggys"
-LICENSE="The Drunken BEER License v 1.0 (https://git.p4g.club/git/beer/about)"
+LICENSE="The Drunken BEER License v 1.1 (https://git.p4g.club/git/beer/about)"
# show help
show_help () {
test $# = 0 || echo "$@"
echo "${SCRIPTNAME} version $VERSION"
-echo " License: "${LICENSE}""
+echo " by ${AUTHOR}"
+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"
@@ -31,18 +32,22 @@ 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 " -h HELP Print this help and exit"
echo
+echo "For more details about this script go to"
+echo "Official Repository: https://git.p4g.club/git/gc2gs"
+echo "Mirror Repository: https://gitgud.io/ziggys/gc2gs"
exit $?
}
# configure new project.config
configure () {
- printf "Configuring a new project.config file for %s\\n\\n" "${CONFNAME}"
+ printf "Configuring a new project.config file for %s\\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 \
+ && printf "[yes to create a new one] (yes/no): " \
+ && read -r OVERANS \
|| echo "" > "${RCFILE}"
test "${OVERANS}" = "no" \
@@ -51,30 +56,37 @@ configure () {
|| 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
+ printf "'username' (i.e. 'me@myno.de', 'username=me'): " \
+ && read -r CONFUNAME
+ printf "'password' (your 'plain-text password'): " \
+ && read -r CONFPASS
+ printf "'node' (url w/o trainling slash 'https://myno.de'): " \
+ && read -r 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 "git project (the name of your git repository): " \
+ && read -r CONFPROJ
+ printf "project url (https://git.reposito.ry/me/project): " \
+ && read -r CONFPURL
+ printf "project path (i.e. %s/git/myrepo ): " "${HOME}" \
+ && read -r REPOPATH
+ printf "platform (cgit | gitlab | github | gitea | other): " \
+ && read -r CONFPLAT
printf "\\nPopulating %s...\\n" "${RCFILE}"
printf "USER=%s;\\nPASSWORD=%s;\\nNODE=%s;\\n" \
- "${CONFUNAME}" "${CONFPASS}" "${CONFNODE}" >> ${RCFILE}
+ "${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
+ printf "\\ndo you want to configure a 'pre-push' 'git hook'? (yes/no): " \
+ && read -r HOOKANS
test "${HOOKANS}" = "no" \
&& printf "done!\\n" \
&& exit $? \
@@ -100,8 +112,8 @@ hook_config () {
}
# check if the repopath is a git repository
-isgit (){
- cd ${REPOPATH}
+isgit () {
+ cd "${REPOPATH}" || echo "${REPOPATH}" > /dev/null
git rev-parse > /dev/null
test "$?" -ne 0 \
&& printf "\\n%s is not a git repository, exiting..." "${REPOPATH}" \
@@ -109,11 +121,54 @@ isgit (){
|| ISGIT="notnull"
}
+# basic tests
+basic_tests () {
+test ! -d "${CONFIGDIR}" && mkdir -p "${CONFIGDIR}"
+test ! -f "${RCFILE}" \
+ && printf "It seems you don't have a configuration file for %s\\n" \
+ "${CONFNAME}" \
+ && printf "Do you want to configure it know? " \
+ && printf "(answering 'no' will exit script) (yes/no): " \
+ && read -r ANSFORCONFIG \
+ || echo "RCFILE exists" > /dev/null
+
+test "${ANSFORCONFIG}" = "yes" && configure || exit 0
+}
+
+status_config () {
+# project variables
+ cd "${REPOPATH}" || echo "${REPOPATH}" > /dev/null
+ 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}" "${COMMITTER}" "${COMMITDATE}" >> "${TSTATUS}"
+ printf "%s (%s)\\n\\ncommit_url: %s\\n\\nproject_url: %s" \
+ "${COMMITMSG}" "${COMMITID}" "${COMMITURL}" "${PROJECTURL}" >> "${TSTATUS}"
+ STATUS="$(cat "${TSTATUS}")"
+}
+
# post
gs_post () {
+ status_config
# deobfuscate user and pass
- USER="$(echo ${USER} | base64 --decode -)"
- PASSWORD="$(echo ${PASSWORD} | base64 --decode -)"
+ 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}"
@@ -126,7 +181,7 @@ gs_post () {
# read options from commandline
test "$#" -lt 1 && show_help
-while getopts ":c:k:x:p:vh" opt; do
+while getopts ":c:k:p:vh" opt; do
case ${opt} in
c) CONFNAME="${OPTARG}" ; EXECUTE="config" ;;
k) CONFNAME="${OPTARG}" ; EXECUTE="hook" ;;
@@ -134,29 +189,30 @@ while getopts ":c:k:x:p:vh" opt; do
\?) 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 ;;
+ 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})"
+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"
+test ! -d "${CONFIGDIR}" && basic_tests
+test ! -f "${RCFILE}" && basic_tests
SNIPPET="snippet/snippet"
HOOKPATH=".git/hooks/pre-push"
# input variables
-sed '/^#/ d; s/;[ \t].*//g; s/;$//g; s/[ \t]*$//g' ${RCFILE} > "$INPUT"
+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")"
@@ -166,32 +222,6 @@ 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 ;;
diff --git a/snippet/snippet b/snippet/snippet
index f549854..6f38888 100755
--- a/snippet/snippet
+++ b/snippet/snippet
@@ -68,7 +68,7 @@ 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}"
+ "${TTITLE}" "${BRANCH}" "${COMMITTER}" "${COMMITDATE}" >> "${TSTATUS}"
printf "%s (%s)\\n\\ncommit_url: %s\\n\\nproject_url: %s" \
"${COMMITMSG}" "${COMMITID}" "${COMMITURL}" "${PROJECTURL}" >> "${TSTATUS}"
STATUS="$(cat ${TSTATUS})"