diff options
author | Malfurious <m@lfurio.us> | 2023-10-24 22:05:05 -0400 |
---|---|---|
committer | Malfurious <m@lfurio.us> | 2023-10-26 20:41:40 -0400 |
commit | fe1abd055998e171b92d0e42df8063cab83a40ed (patch) | |
tree | f6c117db24b71a8cfbbe4cc5518b1cf5678f0ac4 /acid/acid-source | |
parent | f87fb62563b34e34505021ee3f25fe53cbc5beb1 (diff) | |
download | cychedelic-fe1abd055998e171b92d0e42df8063cab83a40ed.tar.gz cychedelic-fe1abd055998e171b92d0e42df8063cab83a40ed.zip |
acid: Don't use 'cychedelic' in file names
As I add new components to the cychedelic system, it would be best to
use more specific naming. 'cychedelic' is the name of the project,
whereas 'acid' is the name of this program.
Signed-off-by: Malfurious <m@lfurio.us>
Diffstat (limited to 'acid/acid-source')
-rwxr-xr-x | acid/acid-source | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/acid/acid-source b/acid/acid-source new file mode 100755 index 0000000..2d507d6 --- /dev/null +++ b/acid/acid-source @@ -0,0 +1,71 @@ +#!/bin/bash + +# acid-source <name> <url> <ref-pattern> +# +# 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 <ref-pattern> (see <pattern> from git-for-each-ref(1)). +# +# On a successful run, the new object hash is printed to stdout (or the previous +# hash, if no update occurred). This may be a commit hash. However, if +# <ref-pattern> matches an annotated tag, then the hash refers to the tag object. +# The caller should cache this hash value upon successful deployment. +# +# If a new version of the file tree is checked out, this file exits with code 1, +# otherwise a stale success exits with 0. On any error, no hash is printed, and +# we exit with 2. + +gethash() { + git for-each-ref \ + --count=1 \ + --format='%(objectname)' \ + --sort='-creatordate' \ + "$1" # pattern +} + +cd '/services' + +# For <name>, limit to a-z, 0-9, -, _ +if ! echo "$1" | grep -Eq '^[-_a-z0-9]*$'; then + echo "Bad service name: $1" >&2 + exit 2 +fi + +if ! [ -d "$1" ]; then + git clone "$2" "$1" >&2 || exit 2 + touch "$1/.git/previous_hash" + touch "$1/.git/previous_slug" +fi + +cd "$1" + +git remote set-url origin "$2" >&2 # pick up url changes +git fetch --all --prune >&2 || exit 2 + +prev=$(cat '.git/previous_hash') +next=$(gethash "$3") + +if [ -z "$next" ]; then + echo "No refs match $3" >&2 + exit 2 +fi + +echo "$next" + +if [ "$prev" != "$next" ]; then + # update file tree + git submodule deinit --all --force >/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 >&2 + + echo -n "Checked out " >&2 + git log -1 --oneline --no-abbrev-commit --no-decorate >&2 + + exit 1 +fi + +exit 0 |