|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script synchronizes a Git repository with a given Subversion repository. |
| 4 | +# Originally specific to ImageJ2 (before that project switched over to Git), |
| 5 | +# it accepts the repository URLs via the command-line, e.g. for use in a |
| 6 | +# Jenkins job. |
| 7 | +# |
| 8 | +# It takes the Subversion URL as first parameter and an arbitrary number of |
| 9 | +# Git push URLs after that. |
| 10 | +# |
| 11 | +# Example: |
| 12 | +# |
| 13 | +# git-svn-synchronizer.sh \ |
| 14 | +# https://valelab.ucsf.edu/svn/micromanager2/ \ |
| 15 | +# github.com:openspim/micromanager \ |
| 16 | +# fiji.sc:/srv/git/micromanager1.4/.git \ |
| 17 | + |
| 18 | +if test $# -lt 2 |
| 19 | +then |
| 20 | + echo "Usage: $0 <Subversion-URL> <Git-push-URL>..." >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +set -e |
| 25 | + |
| 26 | +SVN_URL="$1" |
| 27 | +shift |
| 28 | + |
| 29 | +# initialize repository |
| 30 | +if ! test -d .git |
| 31 | +then |
| 32 | + git init && |
| 33 | + git config core.bare true && |
| 34 | + git remote add -f tmp "$1" && |
| 35 | + # read from remote repository to prevent unnecessary git-svn cloning |
| 36 | + git for-each-ref --format '%(refname)' refs/remotes/tmp/svn/ | |
| 37 | + while read ref |
| 38 | + do |
| 39 | + git push . $ref:refs/remotes/${ref#refs/remotes/tmp/svn/} |
| 40 | + done && |
| 41 | + git remote rm tmp |
| 42 | +fi |
| 43 | + |
| 44 | +# initialize the Subversion URL |
| 45 | +if ! test -d .git/svn || test "a$SVN_URL" != "$(git config svn-remote.svn.url)" |
| 46 | +then |
| 47 | + # Try standard trunk/branches/tags setup first |
| 48 | + if ! git svn init -s "$SVN_URL" && |
| 49 | + ! git svn fetch && |
| 50 | + ! git rev-parse refs/remotes/trunk |
| 51 | + then |
| 52 | + git svn init "$SVN_URL" && |
| 53 | + git svn fetch |
| 54 | + fi |
| 55 | +else |
| 56 | + git svn fetch |
| 57 | +fi |
| 58 | + |
| 59 | +# push refs/remote/* to refs/heads/svn/* |
| 60 | +args="$(git for-each-ref --shell --format '%(refname)' refs/remotes/ | |
| 61 | + sed -e "s/^'\(refs\/remotes\/\(.*\)\)'$/'\1:refs\/heads\/svn\/\2'/" \ |
| 62 | + -e 's/:refs\/heads\/svn\/tags\//:refs\/tags\//')" |
| 63 | + |
| 64 | +for remote |
| 65 | +do |
| 66 | + eval git push \"$remote\" $args |
| 67 | +done |
0 commit comments