Skip to content

Instantly share code, notes, and snippets.

@Graham42
Last active August 29, 2015 14:15

Revisions

  1. Graham42 revised this gist Feb 13, 2015. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions git-svn-clone-from-existing-clone.sh
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,13 @@ type svn >/dev/null 2>&1 || { echo >&2 "svn needs to be installed for this scrip
    type git >/dev/null 2>&1 || { echo >&2 "git needs to be installed for this script to run."; exit 1; }
    git svn --help >/dev/null 2>&1 || { echo >&2 "git-svn needs to be installed for this script to run."; exit 1; }

    GIT_VERSION=$( git --version | sed 's/.*\([0-9]\.[0-9]\.[0-9]\)/\1/' | sed 's/\.//g' )
    if [ $GIT_VERSION -ge 200 ]; then
    REMOTE_PREFIX="origin/"
    else
    REMOTE_PREFIX=""
    fi

    # check args
    args_err="Requires 2 arguments
    @arg 1 - the git repository on someone elses machine or a server
    @@ -34,7 +41,7 @@ mkdir $folder
    cd $folder
    git init
    git remote add origin $1
    git config --replace-all remote.origin.fetch '+refs/remotes/*:refs/remotes/*'
    git config --replace-all remote.origin.fetch '+refs/remotes/${REMOTE_PREFIX}*:refs/remotes/${REMOTE_PREFIX}*'
    git fetch
    # Prevent fetch/pull from remote git server in the future,
    # we only want to use git svn for future updates
    @@ -43,15 +50,17 @@ git config --remove-section remote.origin
    svn list $2 | grep "trunk/" -q
    if [ $? -eq 0 ]; then
    git svn init --stdlayout $2
    remote_branch="remotes/trunk"
    remote_branch="remotes/${REMOTE_PREFIX}trunk"
    else
    git svn init $2
    remote_branch="remotes/git-svn"
    remote_branch="remotes/${REMOTE_PREFIX}git-svn"
    fi
    # Create branch master and point to trunk
    git checkout -b master $remote_branch
    # this line may fail in later versions
    git branch --set-upstream-to=$remote_branch
    # Git version 1.9.x doesn't support setting an upstream branch
    if $(echo $GIT_VERSION | grep '^19' -q) ; then
    git branch --set-upstream-to=$remote_branch
    fi
    # Pull the latest changes from Subversion
    git svn fetch
    git rebase $remote_branch
  2. Graham42 revised this gist Feb 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-svn-clone-from-existing-clone.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # @arg 1 - the git repository on someone elses machine or a server
    # ie. root@192.168.1.337:/home/gmcgregor/MODULE_NAME
    # ie. git@192.168.1.337:/home/gmcgregor/MODULE_NAME
    # @arg 2 - the svn url to point to
    # ie. https://svn.my-company.com/MODULE_NAME

  3. Graham42 created this gist Feb 13, 2015.
    64 changes: 64 additions & 0 deletions git-svn-clone-from-existing-clone.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/bin/bash
    # @arg 1 - the git repository on someone elses machine or a server
    # ie. root@192.168.1.337:/home/gmcgregor/MODULE_NAME
    # @arg 2 - the svn url to point to
    # ie. https://svn.my-company.com/MODULE_NAME

    # check if required programs are installed
    type svn >/dev/null 2>&1 || { echo >&2 "svn needs to be installed for this script to run."; exit 1; }
    type git >/dev/null 2>&1 || { echo >&2 "git needs to be installed for this script to run."; exit 1; }
    git svn --help >/dev/null 2>&1 || { echo >&2 "git-svn needs to be installed for this script to run."; exit 1; }

    # check args
    args_err="Requires 2 arguments
    @arg 1 - the git repository on someone elses machine or a server
    @arg 2 - the svn url to point to
    "
    if [ $# -ne 2 ]; then
    echo >&2 "$args_err"
    exit 1
    fi

    git ls-remote $1 >/dev/null 2>&1 || { echo >&2 "Not a valid git repo."; exit 1; }
    svn list $2 >/dev/null 2>&1 || { echo >&2 "Not a valid svn url."; exit 1; }

    # Clone locally - make sure the refs/remotes/ space matches the server
    folder=${1%.git*}
    folder=${folder%/}
    folder=${folder##*/}
    if [ -e $folder ]; then
    echo >&2 "$folder already exists. Aborting."
    exit 1
    fi
    mkdir $folder
    cd $folder
    git init
    git remote add origin $1
    git config --replace-all remote.origin.fetch '+refs/remotes/*:refs/remotes/*'
    git fetch
    # Prevent fetch/pull from remote git server in the future,
    # we only want to use git svn for future updates
    git config --remove-section remote.origin
    # check the svn remote if standard layout
    svn list $2 | grep "trunk/" -q
    if [ $? -eq 0 ]; then
    git svn init --stdlayout $2
    remote_branch="remotes/trunk"
    else
    git svn init $2
    remote_branch="remotes/git-svn"
    fi
    # Create branch master and point to trunk
    git checkout -b master $remote_branch
    # this line may fail in later versions
    git branch --set-upstream-to=$remote_branch
    # Pull the latest changes from Subversion
    git svn fetch
    git rebase $remote_branch
    # if .gitignore does not exist, create it
    if [[ ! -e .gitignore ]]; then
    echo "# Migrated from svn:ignore" > .gitignore
    git svn show-ignore >> .gitignore
    fi

    cd ..