summaryrefslogtreecommitdiffstats
path: root/acid/acid-source
blob: d03076fa6015ecc3d52f05ea2f42e2e113672d32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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 commit hash is printed to stdout (or the previous
# hash, if no update occurred).  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, the null hash is
# printed, and we exit with 2.

gethash() {
    obj=$(git for-each-ref \
        --count=1 \
        --format='%(objectname)' \
        --sort='-creatordate' \
        "$1") # pattern

    git rev-parse --quiet --verify "$obj^{commit}"
}

fail() {
    echo "$NULL_HASH"
    exit 2
}

cd "$CYCHE_SERVICE_DIR"

# For <name>, limit to a-z, 0-9, -, _
if ! echo "$1" | grep -Eq '^[-_a-z0-9]*$'; then
    echo "Bad service name: $1" >&2
    fail
fi

if ! [ -d "$1" ]; then
    # New repository
    git clone "$2" "$1" >&2 || fail
    cd "$1"

    # Strip down to tags and remote refs only
    git remote set-head origin --delete >/dev/null 2>&1
    git checkout HEAD~0 >/dev/null 2>&1
    git for-each-ref --format='delete %(refname)' refs/heads \
        | git update-ref --stdin >/dev/null 2>&1

    # Init cyche metadata
    echo "$NULL_HASH" >".git/previous_hash"
    touch ".git/previous_slug"
else
    # Existing repository
    cd "$1"
fi

git remote set-url origin "$2" >&2 # pick up url changes
git fetch --all --prune >&2 || fail

prev=$(cat '.git/previous_hash')
next=$(gethash "$3")

if [ -z "$next" ]; then
    echo "No refs match $3" >&2
    fail
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