summaryrefslogtreecommitdiffstats
path: root/acid/check_source
blob: b86e32d8e0c8995b6112321e5a91c29a69eb31f4 (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
#!/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