Skip to content

Instantly share code, notes, and snippets.

@kcd83
Forked from Gimly/Git-Import-SVN.ps1
Last active January 17, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcd83/5dcc6a186f8e8290fc4cf5e6a1f234c6 to your computer and use it in GitHub Desktop.
Save kcd83/5dcc6a186f8e8290fc4cf5e6a1f234c6 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)
Write-Host -ForegroundColor Green "Setting tag [${tagName}] from SVN [${remoteBranch}]"
git checkout -b "tag-$tagName" $remoteBranch
git checkout master
git tag $tagName "tag-$tagName"
git branch -D "tag-$tagName"
}
elseif($remoteBranch -notlike "*/trunk")
{
Write-Host -ForegroundColor Green "Checkout local branch for SVN [${remoteBranch}]"
git checkout -b $remoteBranch $remoteBranch
}
else
{
Write-Host -ForegroundColor Green "Skip trunk SVN [${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 -notlike "*/HEAD" -and $remoteBranch -notlike "*/master")
{
Write-Host -ForegroundColor Green "Checkout GIT TMP [${remoteBranch}]"
$branchName = $remoteBranch.Substring(7)
git checkout -b $branchName $remoteBranch
}
else
{
Write-Host -ForegroundColor Green "Skip GIT TMP [${remoteBranch}]"
}
}
git checkout master
git remote rm origin
git remote add origin $GitUrl
git push --all
@kcd83
Copy link
Author

kcd83 commented Jan 17, 2021

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