summaryrefslogtreecommitdiff
path: root/web-server/update-scripts/update-recent-gnu-sv-www-repo.sh
diff options
context:
space:
mode:
Diffstat (limited to 'web-server/update-scripts/update-recent-gnu-sv-www-repo.sh')
-rwxr-xr-xweb-server/update-scripts/update-recent-gnu-sv-www-repo.sh94
1 files changed, 94 insertions, 0 deletions
diff --git a/web-server/update-scripts/update-recent-gnu-sv-www-repo.sh b/web-server/update-scripts/update-recent-gnu-sv-www-repo.sh
new file mode 100755
index 0000000..65d6bf3
--- /dev/null
+++ b/web-server/update-scripts/update-recent-gnu-sv-www-repo.sh
@@ -0,0 +1,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: