Skip to content

Instantly share code, notes, and snippets.

@ErichDonGubler
Last active August 10, 2022 14:28
Show Gist options
  • Save ErichDonGubler/67d10db49cdab4c373b239e468a95509 to your computer and use it in GitHub Desktop.
Save ErichDonGubler/67d10db49cdab4c373b239e468a95509 to your computer and use it in GitHub Desktop.
Shallow-cloning an SVN repository in Git
function git_svn_shallow_clone() {
# Shallowly clones an SVN repository using `git svn`.
#
# Example: Clone directly into the CWD.
# Note: if your connection fails midway through the cloning, you can pick up from
# where you left off by simply invoking the clone function again. Neat, right?
# $ git_svn_shallow_clone <url> .
#
# You may need to run this in your cloned folder if you need to resume cloning from
# an interrupted connection.
# $ git checkout master
local repo_url="${1:?error: expected invocation of the form '<command> <repo_url> [<clone_directory> <other options for `git clone`>]'}"
shift
local target_dir="${1:-${repo_url##*/}}"
shift
local repo_trunk="$repo_url/trunk"
echo "Making a shallow clone of $repo_trunk into folder $target_dir..."
local revision="$(svn info "$repo_trunk" | awk '/^Last Changed Rev:/ { print $4 }')"
echo " Cloning last changed revision $revision"
git svn clone --stdlayout -r"$revision:HEAD" "$repo_url" "$target_dir" "$@" && pushd "$target_dir" > /dev/null && git reset --hard && popd > /dev/null
}
# TROUBLESHOOTING
# * I'm not sure why -s hasn't worked for me at some points, but it HAS given
# different (failing) behavior than --stdlayout in my most recent testing.
# * The repo_url MUST be the root of the standard SVN repo layout.
# * The revision MUST be part of the history of the repo_url you specify.
#
# For ALL of the issues above, the only indication that something is wrong is
# that git will check out an empty repository. To start over, you have to rm -r
# .git.
# TROUBLESHOOTING
# Same rules as above apply:
# * The repo_url MUST be the root of the standard SVN repo layout.
# * The revision MUST be part of the history of the repo_url you specify.
git svn init --stdlayout "$repo_url"
# You can simply rerun this command if your connection fails, for some reason.
git svn fetch -r"$revision:HEAD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment