summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2023-08-08 05:04:50 -0400
committerMalfurious <m@lfurio.us>2023-08-08 05:04:50 -0400
commit2a5c618158dc9a3c583e5ae054bb59b5870955af (patch)
tree550d8dc585aa61af140dd228d088a2fae4d02948
downloadcychedelic-2a5c618158dc9a3c583e5ae054bb59b5870955af.tar.gz
cychedelic-2a5c618158dc9a3c583e5ae054bb59b5870955af.zip
acid: Add helper script for monitoring source git repositories
Signed-off-by: Malfurious <m@lfurio.us>
-rwxr-xr-xacid/check_source47
1 files changed, 47 insertions, 0 deletions
diff --git a/acid/check_source b/acid/check_source
new file mode 100755
index 0000000..b86e32d
--- /dev/null
+++ b/acid/check_source
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+# check_source <name> <url> <refspec>
+#
+# Check a git source for new updates. A local cache repository is cloned if
+# <name> doesn't exist. If updates are found, we attempt to checkout the
+# newest version according to <refspec> (see git-for-each-ref(1)).
+#
+# The new (or current) git commit hash is printed to stdout. If the file tree
+# is updated, this file exits non-zero.
+
+gethash() {
+ git for-each-ref \
+ --count=1 \
+ --format='%(objectname)' \
+ --sort='-creatordate' \
+ "$1" #ref
+}
+
+cd '/git'
+
+if ! [ -d "$1" ]; then
+ git clone "$2" "$1" >&2
+ touch "$1/.git/previous_hash"
+ touch "$1/.git/previous_slug"
+fi
+
+cd "$1"
+
+git fetch --all --prune >&2
+prev=$(cat '.git/previous_hash')
+next=$(gethash "$3")
+echo "$next"
+
+if [ "$prev" != "$next" ]; then
+ git submodule deinit --all -f >/dev/null 2>&1
+ git reset . >/dev/null 2>&1
+ git checkout . >/dev/null 2>&1
+ git clean -xffd >/dev/null 2>&1
+
+ git checkout "$next" >/dev/null 2>&1
+ git submodule update --init --recursive >/dev/null 2>&1
+
+ exit 1
+fi
+
+exit 0