summaryrefslogtreecommitdiff
path: root/libgithook.sh
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2019-06-15 15:13:12 -0600
committerAaron Ball <nullspoon@oper.io>2019-06-15 15:13:12 -0600
commite3c2e8e5d51a9a858fecee318b87a35c207741f4 (patch)
tree17a63a5c1bdd4296fa9dfcbc30b92bc8c01f3b4b /libgithook.sh
downloadlibgithook-master.tar.gz
libgithook-master.tar.xz
Initial commitHEADmaster
This commit mostly includes functions that parse parts a git hook session (like postreceive). Functions included are load_refs, load_tags, load_file_actions, and dump_sess. Also contains the GPLv3 license.
Diffstat (limited to 'libgithook.sh')
-rwxr-xr-xlibgithook.sh101
1 files changed, 101 insertions, 0 deletions
diff --git a/libgithook.sh b/libgithook.sh
new file mode 100755
index 0000000..30ca43f
--- /dev/null
+++ b/libgithook.sh
@@ -0,0 +1,101 @@
+#!/usr/bin/env bash
+# Library for easier Git hook scripting
+# Copyright (C) 2019 Aaron Ball <nullspoon@oper.io>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+set -eou pipefail
+
+export OLDREV NEWREV REFNAME
+declare -a TAGS
+declare -a REFS
+declare -a FILES_A
+declare -a FILES_M
+declare -a FILES_D
+declare -a FILES_R
+
+export IFS=$' \n\t'
+read OLDREV NEWREV REFNAME
+export IFS=$'\n'
+[ ${OLDREV} = '0000000000000000000000000000000000000000' ] && OLDREV=${NEWREV}
+[ ${NEWREV} = '0000000000000000000000000000000000000000' ] && NEWREV=${OLDREV}
+
+
+# load_tags:
+# Loads any tags into the TAGS array that were created within the changed refs.
+load_tags() {
+ if [ "${REFNAME:0:10}" = 'refs/tags/' ]; then
+ if [ -f "${REFNAME}" ]; then
+ printf '[tag] created %s\n' "$(basename ${REFNAME})"
+ TAGS+=("$(basename ${REFNAME})")
+ elif [ ! -f "${REFNAME}" ]; then
+ printf '[tag] deleted %s\n' "$(basename ${REFNAME})"
+ else
+ printf '[tag] Something went wrong with "%s"\n' "${REFNAME}"
+ fi
+ fi
+}
+
+
+# load_refs:
+# Loads the list of new refs into the REFS array
+load_refs() {
+ REFS=($(git rev-list "^${OLDREV}" "${NEWREV}"))
+}
+
+
+# load_file_actions:
+# Reads the changed refs list and loads file paths into their respective files
+# arrays (eg: FILES_A (added), FILES_D (deleted), FILES_M (modified), and
+# FILES_R (renamed)).
+load_file_actions() {
+ local changes=($(git diff --name-status "^${OLDREV}" "${NEWREV}"))
+ for change in ${changes[@]}; do
+ if [ "${change:0:1}" = 'A' ]; then
+ # File is added
+ FILES_A+=(${change#* })
+ elif [ "${change:0:1}" = 'M' ]; then
+ # File is modified
+ FILES_M+=(${change#* })
+ elif [ "${change:0:1}" = 'D' ]; then
+ # File is deleted
+ FILES_D+=(${change#* })
+ elif [ "${change:0:4}" = 'R100' ]; then
+ # File is moved/renamed
+ FILES_R+=(${change#* })
+ fi
+ done
+}
+
+
+# dump_sess:
+# Dumps all gathered info about the git operation to stdout so clients can see
+# the changes that were detected. This includes dumping the files arrays, tags
+# arrays and the ref information.
+dump_sess() {
+ printf '\n'
+ printf 'OLD: %s\n' "${OLDREV}"
+ printf 'NEW: %s\n' "${NEWREV}"
+ printf 'NAME: %s\n\n' "${REFNAME}"
+ printf 'Files added:\n'
+ printf ' %s\n' "${FILES_A[@]}"
+ printf 'Files modified:\n'
+ printf ' %s\n' "${FILES_M[@]}"
+ printf 'Files deleted:\n'
+ printf ' %s\n' "${FILES_D[@]}"
+ printf 'Files renamed:\n'
+ printf ' %s\n' "${FILES_R[@]}"
+ printf 'Tags created:\n'
+ printf ' %s\n' "${TAGS[@]}"
+ printf '\n'
+}

Generated by cgit