Skip to content

Instantly share code, notes, and snippets.

@Gimly
Last active May 16, 2023 23:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Gimly/90df046dc38181bb18de to your computer and use it in GitHub Desktop.
Save Gimly/90df046dc38181bb18de to your computer and use it in GitHub Desktop.
Import a SVN repository into a Git repository, complete with branches and tags. Script inspired by the method described by StackOverflow answer http://stackoverflow.com/a/3972103/123597
Param(
[Parameter(Mandatory=$true, Position=1)]
[string]$SvnFolderPath,
[Parameter(Mandatory=$true, Position=2)]
[string]$TargetFolder,
[Parameter(Mandatory=$true, Position=3)]
[string]$GitUrl
)
git svn clone --stdlayout --no-metadata -A users.txt $SvnFolderPath "$TargetFolder-tmp"
cd "$TargetFolder-tmp"
$remoteBranches = git branch -r
foreach($remoteBranch in $remoteBranches)
{
$remoteBranch = $remoteBranch.Trim()
if($remoteBranch.StartsWith("tags/"))
{
$tagName = $remoteBranch.Substring(5)
git checkout -b "tag-$tagName" $remoteBranch
git checkout master
git tag $tagName "tag-$tagName"
git branch -D "tag-$tagName"
}
elseif($remoteBranch -notlike "trunk")
{
git checkout -b $remoteBranch $remoteBranch
}
}
cd ..
git clone "$TargetFolder-tmp" $TargetFolder
rm -Recurse -Force "$TargetFolder-tmp"
cd $TargetFolder
$remoteBranches = git branch -r
foreach($remoteBranch in $remoteBranches)
{
$remoteBranch = $remoteBranch.Trim()
if($remoteBranch -notcontains "HEAD" -and $remoteBranch -notcontains "master")
{
$branchName = $remoteBranch.Substring(7)
git checkout -b $branchName $remoteBranch
}
}
git checkout master
git remote rm origin
git remote add origin $GitUrl
git push --all
@JamesSkemp
Copy link

This is way easier than https://john.albin.net/git/convert-subversion-to-git, which is what I was trying to follow before.

I did have to remove --stdlayout from line 10 to deal with Subversion repos that weren't setup with the standard trunk/branches/tags structure. I was otherwise getting an error about the Git repository being empty.

@tvgm2
Copy link

tvgm2 commented Aug 26, 2019

This is way easier than https://john.albin.net/git/convert-subversion-to-git, which is what I was trying to follow before.

I did have to remove --stdlayout from line 10 to deal with Subversion repos that weren't setup with the standard trunk/branches/tags structure. I was otherwise getting an error about the Git repository being empty.

Thanks for the tip @JamesSkemp

@Polosaty70
Copy link

Polosaty70 commented Nov 22, 2019

I'm afraid the condition $remoteBranch -notlike "trunk" in line 28 is always false. Did you mean $remoteBranch -notlike "*trunk"?
The same issue is in line 44. $remoteBranch -notcontains "HEAD" is always false because -contains and -notcontains don't mean "string contains/not contains substring", they mean "if a collection of objects includes ('contains') a particular object". I suppose the condition in line 44 should be $remoteBranch -notlike "*HEAD" -and $remoteBranch -notlike "*master".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment