Skip to content

Commit

Permalink
Fix #79 - add Remove-MergedGitBranch command
Browse files Browse the repository at this point in the history
Not sure if this command should require confirmation by default.

Perhaps we also need a Remove-GitBranch command which would need
confirmation.
  • Loading branch information
rkeithhill committed Feb 20, 2019
1 parent 6370c19 commit 2af13ed
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,76 @@ function Get-AliasPattern($exe) {
"($($aliases -join '|'))"
}

<#
.SYNOPSIS
Removes all Git branches merged into the sepcified commit (HEAD by default).
.DESCRIPTION
Removes all Git branches that have been merged into the sepcified commit
(HEAD by default).
By default, several potentially merged branches are always excluded. This
includes the current branch as well as develop and master.
.EXAMPLE
PS> Remove-MergedGitBranch
Removes all merged branches except the current, develop and master.
.EXAMPLE
PS> Remove-MergedGitBranch -Force
Removes all merged branches except the current, develop and master.
Using -Force, skips the confirmation prompts.
.EXAMPLE
PS> Remove-MergedGitBranch -Pattern 'feature/.*'
Removes all merged feature/* branches except the current branch.
#>
function Remove-MergedGitBranch {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact="Medium")]
param(
# Branches whose tips are reachable from the specified commit will be removed.
# The default commit is HEAD.
[Parameter(Position=0)]
[string]
$Commit = "HEAD",

# Removes merged branches without prompting for confirmation. By default,
# Remove-MergedGitBranch prompts for confirmation before removing merged branches.
[Parameter()]
[switch]
$Force,

# Specifies a regular expression used to exclude merged branches from being removed.
# The default pattern excludes the current branch, develop and master branches.
[Parameter()]
[string]
$ExcludeBranchPattern = '(^\*)|(^. (develop|master)$)',

# Specifies a regular expression to limit the non-excluded merged branches to be removed.
# The default pattern '.*' allows all non-excluded merged branches.
[Parameter()]
[string]
$Pattern = ".*"
)

$branchesToDelete = git branch --merged $Commit |
Where-Object {$_ -notmatch $ExcludeBranchPattern } |
Where-Object {$_ -match $Pattern}

$action = "remove merged branch"
$yesToAll = $noToAll = $false

foreach ($branch in $branchesToDelete) {
$targetBranch = $branch.Trim()
if ($PSCmdlet.ShouldProcess($targetBranch, $action)) {
if ($Force -or $yesToAll -or
$PSCmdlet.ShouldContinue("Are you sure you want to ${action} `"$targetBranch`"?",
"Confirm removing branch", [ref]$yesToAll, [ref]$noToAll)) {

if ($noToAll) { return }

Invoke-Utf8ConsoleCommand { git branch -d $targetBranch }
}
}
}
}

function Update-AllBranches($Upstream = 'master', [switch]$Quiet) {
$head = git rev-parse --abbrev-ref HEAD
git checkout -q $Upstream
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ FunctionsToExport = @(
'Get-PromptConnectionInfo',
'Get-PromptPath',
'New-GitPromptSettings',
'Remove-MergedGitBranch',
'Update-AllBranches',
'Write-GitStatus',
'Write-GitBranchName',
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ $exportModuleMemberParams = @{
'Get-PromptConnectionInfo',
'Get-PromptPath',
'New-GitPromptSettings',
'Remove-MergedGitBranch',
'Update-AllBranches',
'Write-GitStatus',
'Write-GitBranchName',
Expand Down

0 comments on commit 2af13ed

Please sign in to comment.