Created
March 20, 2012 15:33
-
-
Save WilbertOnGithub/2137097 to your computer and use it in GitHub Desktop.
Cleans up git repositories that have been imported with 'git svn clone'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script that cleans up an entire directory with Git repositories | |
# that have been imported from Subversion using the 'git svn clone' | |
# command. | |
# It does the following: | |
# - Recurses through all subdirectory in the directory you specify. | |
# For each git repository in a subdirectory: | |
# - Parses all tags from the remote branches and creates explicit | |
# git tags. | |
# - Parses all other remote branches and creates local branches | |
# from them. | |
# Basically, it follows the workflow as described here on StackOverflow | |
# http://stackoverflow.com/a/3972103/33671 | |
# This script assumes that git is in your path. | |
Param | |
( | |
[Parameter(Mandatory = $True)] | |
[string]$DirectoryWithGitRepositories | |
) | |
# Strip all leading whitespace from the 'git branch -r' command. | |
function StripWhiteSpace([array]$RemoteBranches) | |
{ | |
$Stripped=@() | |
foreach ($RemoteBranch in $RemoteBranches) | |
{ | |
$RemoteBranch = $RemoteBranch -replace "^\s*", "" | |
$Stripped = $Stripped + $RemoteBranch | |
} | |
return $Stripped | |
} | |
# Retrieve all branches that should become tags. | |
function GetAllTags ([array]$RemoteBranches) | |
{ | |
$Tags = @{} | |
foreach ($RemoteBranch in $RemoteBranches) | |
{ | |
if ($RemoteBranch -match "tags/(.*)") | |
{ | |
# Remove %20 characters and make everything lowercase | |
$FilteredValue = $matches[1] -replace "%20", "-" | |
$Tags[$RemoteBranch] = $FilteredValue.ToLower() | |
} | |
} | |
return $Tags | |
} | |
# Retrieve all branches that are *not* tags. | |
# Discard default 'trunk' branch | |
function GetAllNonTags ([array]$RemoteBranches) | |
{ | |
$Branches = @{} | |
foreach ($RemoteBranch in $RemoteBranches) | |
{ | |
if ($RemoteBranch -match "^tags/" -or $RemoteBranch -eq "trunk") | |
{ | |
# Nothing | |
} | |
else | |
{ | |
$Key = "remotes/" + $RemoteBranch | |
$Branches[$Key] = $RemoteBranch | |
} | |
} | |
return $Branches | |
} | |
# Actually create the local branches | |
function CreateLocalBranches($Branches) | |
{ | |
Write-Host "Found" $Branches.Count "branches in remote." | |
if ($Branches.Count -gt 0) | |
{ | |
Write-Host "Creating local branches..." | |
$Branches.GetEnumerator() | Foreach-Object { | |
Write-Host "git branch" $_.Value $_.Key | |
git branch $_.Value $_.Key | |
Write-Host | |
} | |
} | |
Write-Host | |
} | |
# Actually create the local tags | |
function CreateTags ($Tags) | |
{ | |
Write-Host "Found" $Tags.Count "tagbranches in remote" | |
if ($Tags.Count -gt 0) | |
{ | |
Write-Host "Creating local tags..." | |
$Tags.GetEnumerator() | Foreach-Object { | |
Write-Host "git branch" $_.Value $_.Key | |
git branch $_.Value $_.Key | |
Write-Host "git tag" $_.Value $_.Value | |
git tag $_.Value $_.Value | |
Write-Host "git branch -D" $_.Value | |
git branch -D $_.Value | |
Write-Host | |
} | |
} | |
} | |
# The fun starts here... | |
$items = Get-ChildItem -Path $DirectoryWithGitRepositories | |
foreach ($item in $items) | |
{ | |
if ($item.Attributes -eq "Directory") | |
{ | |
Write-Host "Switching to" $item.FullName | |
pushd $item.FullName | |
[array]$RemoteBranches = git branch -r | |
$RemoteBranches = StripWhiteSpace ($RemoteBranches) | |
CreateTags (GetAllTags ($RemoteBranches)) | |
CreateLocalBranches (GetAllNonTags ($RemoteBranches)) | |
popd | |
} | |
} | |
Write-Host "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment