Last active
November 1, 2016 17:18
-
-
Save chestercodes/d62f7172c997825b81ee to your computer and use it in GitHub Desktop.
Gitflow delete merged branches
This file contains 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
$lastCommitsFile = "C:\LastCommitsOfDeletedBranches.txt" | |
if((Test-Path $lastCommitsFile ) -eq $false){"" > $lastCommitsFile} | |
"Lets delete stuff..." | |
$repoLocations = @("C:\Dev\SomeRepo") | |
$pullTheTrigger = $false | |
$branchRegex = "^[a-zA-Z0-9\-_/]+$" | |
function GetReposAndBranches($repoLocations, $deleteBranchesMergedInto = "origin/develop"){ | |
$reposAndBranches = @() | |
foreach ($location in $repoLocations){ | |
cd $location | |
Write-Host "Repo - $location" | |
# make sure is it up to date | |
git remote prune origin | |
git fetch | |
Write-Host "Fetched" | |
$allMergedBranches = git branch --merged $deleteBranchesMergedInto -r | |
$regexExcludingNonFeatureBranchesAndImportantOnes = "origin\/master|origin\/HEAD|origin\/hotfix|origin\/spike|origin\/release|origin\/develop|master|develop" | |
$branchesToDelete = $allMergedBranches | Where-Object {($_ -match $regexExcludingNonFeatureBranchesAndImportantOnes) -eq $false} | |
$count = $branchesToDelete.Length | |
#Write-Host "I plan to delete $count branches from $location`:" | |
ForEach($branchToDelete in $branchesToDelete) | |
{ | |
$branchToDelete = $branchToDelete.Trim() | |
if($branchToDelete.StartsWith("* ")){ | |
$branchToDelete = $branchToDelete.Substring(2) | |
} | |
if($branchToDelete -notmatch $branchRegex){ | |
Write-Error "Branch name is bad $branchToDelete" | |
continue | |
} | |
$props = @{} | |
$props.RepoPath=$location.Trim() | |
$props.RepoPathLength=$location.Trim().Length | |
$props.BranchName=$branchToDelete | |
$object = New-Object -TypeName PSObject -Prop $props | |
#Write-Host "- $branchToDelete" | |
$reposAndBranches += $object | |
} | |
} | |
return $reposAndBranches | |
} | |
function DeleteRemoteBranches($reposAndBranchesToDelete){ | |
if($pullTheTrigger){"The trigger IS being pulled, branches WILL be deleted... Read the branches below carefully"}else{"Simulation of deleting branches"} | |
$numBranches = $reposAndBranchesToDelete.Count | |
Write-Host "I plan to delete $numBranches branches" | |
# print out branches to be deleted | |
$locationLengths = $reposAndBranchesToDelete | Sort-Object -Property RepoPathLength -Descending | |
$padTo = $locationLengths[0].RepoPathLength + 5 | |
ForEach($repoAndBranchToDelete in $reposAndBranchesToDelete) | |
{ | |
$branchToDelete = $repoAndBranchToDelete.BranchName | |
$repoPath = $repoAndBranchToDelete.RepoPath.PadRight($padTo) | |
Write-Host "$repoPath - $branchToDelete" | |
} | |
$yesOrSomethingElse = Read-Host -Prompt "Press 'y' to continue or anything else to quit." | |
if($yesOrSomethingElse.ToLower() -ne "y") | |
{ | |
Write-Host "Exiting..." | |
exit 0 | |
} | |
ForEach($repoAndBranchToDelete in $reposAndBranchesToDelete) | |
{ | |
$branchToDelete = $repoAndBranchToDelete.BranchName | |
$repoPath = $repoAndBranchToDelete.RepoPath | |
cd $repoPath | |
# Adds to spaces on the front for some reason | |
$branchToDelete = $branchToDelete.Trim() | |
if($branchToDelete -notmatch $branchRegex){ | |
Write-Error "Branch name is bad $branchToDelete" | |
continue | |
} | |
if($branchToDelete -eq "" -or $branchToDelete -eq $null) | |
{ | |
exit 0 | |
} | |
$lastCommitInfo = git log -1 $branchToDelete | |
"About to delete branch - $branchToDelete" | |
if($branchToDelete -match "origin\/") | |
{ | |
# Need to delete remote branch | |
$branchToDelete = $branchToDelete.Replace("remotes/", "") | |
$branchToDelete = $branchToDelete.Replace("origin/", "") | |
$remoteBranchName = "refs/heads/" + $branchToDelete | |
if($pullTheTrigger) | |
{ | |
git push origin --delete $remoteBranchName | |
"Deleted" | |
} | |
else | |
{ | |
"Pretending to delete branch $remoteBranchName" | |
} | |
} | |
else | |
{ | |
# want to delete local branch | |
if($pullTheTrigger) | |
{ | |
# don't want to force Delete | |
git branch -d $branchToDelete | |
} | |
else | |
{ | |
"Pretending to delete branch $branchToDelete" | |
} | |
} | |
$infoToAdd = "$repoPath - $branchToDelete - $lastCommitInfo" | |
if($pullTheTrigger) | |
{ | |
$infoToAdd >> $lastCommitsFile | |
"Info logged to file" | |
} | |
else | |
{ | |
"pretending to log info $infoToAdd" | |
} | |
} | |
} | |
# Run it! | |
$reposAndBranchesToDelete = GetReposAndBranches $repoLocations | |
DeleteRemoteBranches $reposAndBranchesToDelete |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment