summaryrefslogtreecommitdiff
path: root/web-server/update-scripts/update-recent-gnu-sv-www-repo.sh
blob: 65d6bf3a22f902acb255a7c137bd7d1504ad3adf (plain)
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
89
90
91
92
93
94
#!/bin/sh

# Copyright (C) 2014 Assaf Gordon (assafgordon@gmail.com)
#
# 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/>.

##
## This script checks which projects were recently updated,
## by visiting http://gitweb.housegordon.org/recent/ ,
## and comparing the timestamp of the listed projects to the cached
## timestamp in '/var/run/savannah-checkouts/recent.timestamp'
##
## Project with newer timestamp (or all listed projects, if no timestamp file
## exists) will be git-pulled .
##

die()
{
    BASE=$(basename "$0")
    echo "$BASE: error: $@" >&2
    exit 1
}

log()
{
    DATE=$(date +%F-%H%M%S)
    echo "$DATE: $@" >&2
}

# Directory containing each repository
GITDIR=/var/lib/savannah-checkouts/

# URL of a CGI script reporting the recently updated projects
RECENT_URL=http://gitweb.housegordon.org/recent/

# Timestamp file
TIMESTAMP_FILE=/var/run/savannah-checkouts/recent.timestamp

# Fetch The list of recently updated projects
LIST=$(wget --quiet -O - "$RECENT_URL") \
    || die "failed to fetch recently updated projects from '$RECENT_URL'"
LIST=$(echo "$LIST" | sort -k2n,2)

# Check if we have a record of the last timestamp we've updated.
# If so, don't fetch older updates
TIMESTAMP=0
if test -e "$TIMESTAMP_FILE" ; then
    TIMESTAMP=$(cat "$TIMESTAMP_FILE")
    expr "$TIMESTAMP" : "^[0-9][0-9]*$" >/dev/null || TIMESTAMP=0
    LIST=$(echo "$LIST" | awk -v TS="$TIMESTAMP" '$2>TS') \
        || die "failed to filter recent entries with awk"
fi

# No projects are newer than the latest timestamp, exit
test -z "$LIST" && exit 0

echo "$LIST" \
 | while read PROJ TS DATE TIME ; do

    # Input validation
    expr "$PROJ" : '^[A-Za-z0-9_\-][A-Za-z0-9_\-]*$' >/dev/null \
        || die "invalid repository/project name ($PROJ) in recent-updates list"
    expr "$TS" : "^[0-9][0-9]*$" >/dev/null \
        || die "invalid timesramp ($TS) for repository ($PROJ) in recent-updates list"

    # TOOD: auto-create new projects?
    test -d "$GITDIR/$PROJ" \
        || die "Found update record for non existing repository ($PROJ) " \
                "consider creating the project setup."

    cd "$GITDIR/$PROJ" \
        || die "failed to CD to $GITDIR/$PROJ"

    git pull --quiet \
        || die "failed to git-pull in '$GITDIR/$PROJ'"

    echo "$TS" > "$TIMESTAMP_FILE" \
        || die "failed to update timestamp file $TIMESTAMP_FILE"
done

## vim: set shiftwidth=4:
## vim: set tabstop=4:
## vim: set expandtab: