Skip to content

Instantly share code, notes, and snippets.

@Gimly
Last active October 16, 2024 03:09
Show Gist options
  • 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
@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