]> oss.titaniummirror.com Git - git-utils.git/commitdiff
Add the update-mirror script.
authorR. Steve McKown <rsmckown@gmail.com>
Wed, 3 Nov 2010 16:10:29 +0000 (10:10 -0600)
committerR. Steve McKown <rsmckown@gmail.com>
Wed, 3 Nov 2010 16:10:29 +0000 (10:10 -0600)
update-mirror can be ran via cron to periodically update a local mirror from
a source URL pointing to either an SVN or git upstream repository.

update-mirror [new file with mode: 0755]

diff --git a/update-mirror b/update-mirror
new file mode 100755 (executable)
index 0000000..477b32a
--- /dev/null
@@ -0,0 +1,101 @@
+#!/bin/bash
+#
+# Usage: update-mirror <url of upstream> <local project dirname>
+#
+# Derived from girocco's taskd/clone.sh script.  See
+# http://repo.or.cz/w/girocco.git
+
+set -e
+
+cfg_gitweburl=http://$(hostname)/gitweb
+cfg_reporoot=/var/lib/git/mirrors
+
+url=$1
+projdir=$2
+
+proj="${projdir%.git}"
+
+echo "[$proj] update mirror"
+echo "[$proj] $url -> $projdir"
+
+if [ ! -d "$cfg_reporoot/$projdir" ]; then
+  echo "[$proj] $projdir is a new project"
+  mkdir -p "$cfg_reporoot/$projdir"
+  touch "$cfg_reporoot/$projdir/.new_clone"
+fi
+touch "$cfg_reporoot/$projdir/.clone_in_progress"
+
+cd "$cfg_reporoot/$projdir"
+trap "echo \"[$proj] clone failed\"; touch .clone_failed" EXIT
+
+mail="sysadmin"
+
+# Initial mirror
+echo "[$proj] mirroring..."
+case "$url" in
+  svn://* | svn+http://* | svn+https://*)
+    # we just remove svn+ here, so svn+http://... becomes http://...
+    svnurl="${url#svn+}"
+    if [ -f "$cfg_reporoot/$projdir/.new_clone" ]; then
+      echo "[$proj] initial git-svn setup"
+      git --git-dir . --bare init --shared=true
+      git --git-dir . svn init -s --prefix=svn-origin/ "$svnurl"
+      # have git-svn store branches under svn-origin/heads/* not svn-origin/*
+      # FIXME: may need to do similar when new branches are added
+      git --git-dir . config svn-remote.svn.branches \
+         "$(git config --get svn-remote.svn.branches | \
+         sed 's|:refs/remotes/svn-origin/\*$|:refs/remotes/svn-origin/heads/*|')"
+      git --git-dir . svn fetch
+      # Neat Trick suggested by Miklos Vajna
+      git --git-dir . config remote.origin.url .
+      git --git-dir . config remote.origin.fetch \
+         '+refs/remotes/svn-origin/heads/*:refs/heads/*'
+      git --git-dir . config --add remote.origin.fetch \
+         '+refs/remotes/svn-origin/trunk:refs/heads/master'
+      git --git-dir . config --add remote.origin.fetch \
+         '+refs/remotes/svn-origin/tags/*:refs/tags/*'
+      rm -f "$cfg_reporoot/$projdir/.new_clone"
+    fi
+    echo "[$proj] fetching new commits from upstream"
+    git --git-dir . fetch
+    ;;
+
+  darcs://*)
+    echo "[$proj] darcs mirrors not yet supported"
+    exit 1
+    httpurl="${url/darcs:\/\//http://}"
+    /usr/bin/darcs-fast-export --export-marks=$(pwd)/dfe-marks "$httpurl" | \
+       git fast-import --export-marks=$(pwd)/gfi-marks
+    # This is here because by default only the exit code of
+    # git fast-import is checked
+    [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
+    ;;
+
+  bzr://*)
+      echo "[$proj] bazaar mirrors not yet supported"
+      exit 1
+      # we just remove bzr:// here, a typical bzr url is just
+      # "lp:foo"
+      bzrurl="${url#bzr://}"
+      bzr fast-export --export-marks=$(pwd)/bfe-marks "$bzrurl" | \
+         git fast-import --export-marks=$(pwd)/gfi-marks
+      [ ${PIPESTATUS[0]} = 0 -a ${PIPESTATUS[1]} = 0 ]
+      ;;
+
+  *)
+    if [ -f "$cfg_reporoot/$projdir/.new_clone" ]; then
+      echo "[$proj] initial git-svn setup"
+      git remote rm origin >/dev/null 2>&1 || :
+      git remote add --mirror origin "$url"
+    fi
+    echo "[$proj] fetching new commits from upstream"
+    git remote update
+    git remote prune origin
+    ;;
+esac
+
+# The rest
+git update-server-info
+trap "" EXIT
+rm .clone_in_progress
+echo "[$proj] clone updated.  See $cfg_gitweburl/$projdir"