Skip to content

Commit

Permalink
Reduce to just a singe Remove-GitBranch command
Browse files Browse the repository at this point in the history
Also, update comment help to not use EOLs.   Let the PS help system handle that.
  • Loading branch information
rkeithhill committed Feb 24, 2019
1 parent 14475c3 commit 3c7409c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 200 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

### Added

- Added `Remove-GitBranch` and `Remove-MergedGitBranch` commands. Partially addresses #79.
- Added `Remove-GitBranch` command. Partially addresses #79.
These commands currently only delete local branches.
([#79](https://github.com/dahlbyk/posh-git/issues/79))
([PR #663](https://github.com/dahlbyk/posh-git/pull/663))
Expand Down
271 changes: 74 additions & 197 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -409,133 +409,134 @@ function Get-AliasPattern($exe) {

<#
.SYNOPSIS
Removes the specified Git branch or branches if a wildcard pattern is used.
Deletes the specified Git branches.
.DESCRIPTION
Removes the specified Git branches REGARDLESS of their merge status.
You must either specify a branch name via the Name parameter, which
accepts wildard characters, or via the Pattern parameter, which accepts
a regular expression.
By default, the following branches are always excluded from removal:
the current branch and the develop and master branches.
IMPORTANT: BE VERY CAREFUL using this command. As a consequence of the
downside potential of deleting unmerged branches, this command requires
confirmation for each branch it deletes by default. You can suppress
confirmation prompting by using the Force parameter. In order to get
this command to "delete with force" unmerged branches, you have to
separately specify the DeleteForce parameter.
If you only want to remove *merged branches*, please use the
Remove-MergedGitBranch command instead. The Remove-MergedGitBranch command
limits branch candidates to only those that have been merged. Consequently
Remove-MergedGitBranch does not prompt for confirmation.
Deletes the specified Git branches that have been merged into the commit specified by the Commit parameter (HEAD by default). You must either specify a branch name via the Name parameter, which accepts wildard characters, or via the Pattern parameter, which accepts a regular expression.
The following branches are always excluded from deletion:
* The current branch
* develop
* master
The default set of excluded branches can be overridden with the ExcludePattern parameter.
Consider always running this command FIRST with the WhatIf parameter. This will show you which branches will be deleted. This gives you a chance to adjust your branch name wildcard pattern or regular expression if you are using the Pattern parameter.
IMPORTANT: Be careful using this command. Even though by default this command deletes only merged branches, most, if not all, of your historical branches have been merged. But that doesn't mean you want to delete them. That is why this command excludes `develop` and `master` by default. But you may use different names e.g. `development` or have other historical branches you don't want to delete. In these cases, you can either:
* Specify a narrower branch name wildcard such as "user/$env:USERNAME/*".
* Specify an updated ExcludeParameter e.g. '(^\*)|(^. (develop|master|v\d+)$)' which adds any branch matching the pattern 'v\d+' to the exclusion list.
If necessary, you can delete the specified branches REGARDLESS of their merge status by using the IncludeUnmerged parameter. BE VERY CAREFUL using the IncludeUnmerged parameter together with the Force parameter, since you will not be given the opportunity to confirm each branch deletion.
The following Git commands are executed by this command:
git branch | Where-Object {$_ -notmatch $ExcludePattern} |
Where-Object {$_.Trim() -like $Name} |
Foreach-Object {git branch --delete $_.Trim()}
git branch --merged $Commit |
Where-Object { $_ -notmatch $ExcludePattern } |
Where-Object { $_.Trim() -like $Name } |
Foreach-Object { git branch --delete $_.Trim() }
If the IncludeUnmerged parameter is specified, execution changes to:
git branch |
Where-Object { $_ -notmatch $ExcludePattern } |
Where-Object { $_.Trim() -like $Name } |
Foreach-Object { git branch --delete $_.Trim() }
If the DeleteForce parameter is specified, the Foreach-Object changes to:
If the DeleteForce parameter is specified, this command executes:
Foreach-Object { git branch --delete --force $_.Trim() }
git branch | Where-Object {$_ -notmatch $ExcludePattern} |
Where-Object {$_.Trim() -like $Name} |
Foreach-Object {git branch --delete --force $_.Trim()}
If the Pattern parameter is used instead of the Name parameter, the second Where-Object changes to:
If the Pattern parameter is used instead of the Name parameter, the second
Where-Object changes to: Where-Object {$_ -match $Pattern }.
Where-Object { $_ -match $Pattern }
Recovering Deleted Branches:
If you wind up deleting a branch you didn't intend to, you can easily
recover it with the info provided by Git during the delete. For instance,
let's say you realized you didn't want to delete the branch 'feature/exp1'.
In the output of this command, you should see a deletion entry for this
branch that looks like:
## Recovering Deleted Branches
Deleted branch feature/exp1 (was 08f9000).
If you wind up deleting a branch you didn't intend to, you can easily recover it with the info provided by Git during the delete. For instance, let's say you realized you didn't want to delete the branch 'feature/exp1'. In the output of this command, you should see a deletion entry for this branch that looks like:
Deleted branch feature/exp1 (was 08f9000).
To recover this branch, execute the following Git command:
# git branch <branch-name> <sha1>
git branch feature/exp1 08f9000
# git branch <branch-name> <sha1>
git branch feature/exp1 08f9000
.EXAMPLE
PS> Remove-GitBranch -Name "user/${env:USERNAME}/*" -WhatIf
Shows the branches that would be removed by the specified regular
expression without actually removing them. Remove the WhatIf parameter
when you are happy with the list of branches that will be removed.
Shows the merged branches that would be deleted by the specified branch name without actually deleting. Remove the WhatIf parameter when you are happy with the list of branches that will be deleted.
.EXAMPLE
PS> Remove-GitBranch "feature/*" -Force
Removes the branches that match the specified wildcard. Using -Force skips
all the confirmation prompts. Name is a positional parameter. The
first argument is assumed to be the value of the -Name parameter.
Deletes the merged branches that match the specified wildcard. Using the Force parameter skips all confirmation prompts. Name is a positional parameter. The first argument is assumed to be the value of the Name parameter.
.EXAMPLE
PS> Remove-GitBranch "bugfix/*" -Force -DeleteForce
Removes the branches that match the specified wildcard. Using the Force
parameter skips all the confirmation prompts while the DeleteForce
parameter uses the --force option in the underlying
`git branch --delete <branch-name>` command.
Deletes the merged branches that match the specified wildcard. Using the Force parameter skips all confirmation prompts while the DeleteForce parameter uses the --force option in the underlying Git command.
.EXAMPLE
PS> Remove-GitBranch -Pattern 'user/(dahlbyk|hillr)/.*'
Removes the branches that match the specified regular expression.
Deletes the merged branches that match the specified regular expression.
.EXAMPLE
PS> Remove-GitBranch -Name * -ExcludePattern '(^\*)|(^. (develop|master|v\d+)$)'
Removes ALL branches except the current branch, develop, master and branches
that also match the pattern 'v\d+' e.g. v1, v1.0, v1.x. BE VERY CAREFUL
WHEN SPECIYING SUCH A BROAD -NAME WILDCARD TO THIS COMMAND!
.LINK
Remove-MergedGitBranch
Deletes merged branches except the current branch, develop, master and branches that also match the pattern 'v\d+' e.g. v1, v1.0, v1.x. BE VERY CAREFUL SPECIYING SUCH A BROAD BRANCH NAME WILDCARD!
.EXAMPLE
PS> Remove-GitBranch "feature/*" -IncludeUnmerged -WhatIf
Shows the branches, both merged and unmerged, that match the specified wildcard that would be deleted without actually deleting them. Once you've verified the list of branches looks correct, remove the WhatIf parameter to actually delete the branches.
#>
function Remove-GitBranch {
[CmdletBinding(DefaultParameterSetName="Wildcard", SupportsShouldProcess, ConfirmImpact="Medium")]
param(
# Specifies a regular expression pattern for the branches that will be deleted.
# Certain branches are always excluded from deletion e.g. the current branch
# as well as the develop and master branches. See the ExcludePattern
# parameter to modify that pattern.
# Specifies a regular expression pattern for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
[Parameter(Position=0, Mandatory, ParameterSetName="Wildcard")]
[ValidateNotNullOrEmpty()]
[string]
$Name,

# Specifies a regular expression pattern for the branches that will be deleted.
# Certain branches are always excluded from deletion e.g. the current branch
# as well as the develop and master branches. See the ExcludePattern
# parameter to modify that pattern.
# Specifies a regular expression for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
[Parameter(Position=0, Mandatory, ParameterSetName="Pattern")]
[ValidateNotNull()]
[string]
$Pattern,

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

# Removes the specified branches without prompting for confirmation. By default,
# Remove-GitBranch prompts for confirmation before removing branches.
# Branches whose tips are reachable from the specified commit will be deleted. The default commit is HEAD. This parameter is ignored if the IncludeUnmerged parameter is specified.
[Parameter()]
[string]
$Commit = "HEAD",

# Specifies that unmerged branches are also eligible to be deleted.
[Parameter()]
[switch]
$IncludeUnmerged,

# Deletes the specified branches without prompting for confirmation. By default, Remove-GitBranch prompts for confirmation before deleting branches.
[Parameter()]
[switch]
$Force,

# Removes the specified branches by adding the --force parameter to the
# git branch --delete command e.g. git branch --delete --force <branch-name>.
# This is also the equivalent of using the -D parameter on the git
# branch command.
# Deletes the specified branches by adding the --force parameter to the git branch --delete command e.g. git branch --delete --force <branch-name>. This is also the equivalent of using the -D parameter on the git branch command.
[Parameter()]
[switch]
$DeleteForce
)

$branches = git branch | Where-Object {$_ -notmatch $ExcludePattern }
if ($IncludeUnmerged) {
$branches = git branch
}
else {
$branches = git branch --merged $Commit
}

$filteredBranches = $branches | Where-Object {$_ -notmatch $ExcludePattern }

if ($PSCmdlet.ParameterSetName -eq "Wildcard") {
$branchesToDelete = $branches | Where-Object { $_.Trim() -like $Name }
$branchesToDelete = $filteredBranches | Where-Object { $_.Trim() -like $Name }
}
else {
$branchesToDelete = $branches | Where-Object { $_ -match $Pattern }
$branchesToDelete = $filteredBranches | Where-Object { $_ -match $Pattern }
}

$action = if ($DeleteForce) { "delete with force"} else { "delete" }
Expand All @@ -561,130 +562,6 @@ function Remove-GitBranch {
}
}

<#
.SYNOPSIS
Removes the specified Git branch or branches that have been merged.
.DESCRIPTION
Removes the specified Git branches that have been merged into the commit
specified by the Commit parameter (HEAD by default). You must either
specify a branch name via the Name parameter, which accepts wildard
characters, or via the Pattern parameter, which accepts a regular
expression.
By default, the following branches are always excluded from removal:
the current branch and the `develop` and `master` branches.
IMPORTANT: Be careful using this command. Most, if not all, of your
historical branches have been merged but that doesn't mean you want to
remove them. That is why this command excludes `develop` and `master` by
default. But you may use different names e.g. `development` or have other
historical branches you don't want to delete. In these cases, you can
either:
* Request confirmation by using the Confirm parameter.
* Specify a narrower branch Name wildcard such as "user/$env:USERNAME/*".
* Specify an updated ExcludeParameter e.g. '(^\*)|(^. (develop|master|v\d+)$)'
which adds any branch matching the pattern 'v\d+' to the exclusion list.
The following Git commands are executed by this command:
git branch --merged $Commit | Where-Object {$_ -notmatch $ExcludePattern} |
Where-Object {$_.Trim() -like $Name} |
Foreach-Object {git branch --delete $_.Trim()}
If the Pattern parameter is used instead of the Name parameter, the second
Where-Object changes to: Where-Object {$_ -match $Pattern }.
Recovering Deleted Branches:
If you wind up deleting a branch you didn't intend to, you can easily
recover it with the info provided by Git during the delete. For instance,
let's say you realized you didn't want to delete the branch 'feature/exp1'.
In the output of this command, you should see a deletion entry for this
branch that looks like:
Deleted branch feature/exp1 (was 08f9000).
To recover this branch, execute the following Git command:
# git branch <branch-name> <sha1>
git branch feature/exp1 08f9000
.EXAMPLE
PS> Remove-MergedGitBranch -Name "user/$env:USERNAME/*" -Whatif
Shows which branches would be removed without actually removing them.
Remove the WhatIf parameter when you are happy with the list of branches
that will be removed.
.EXAMPLE
PS> Remove-MergedGitBranch "feature/*"
Removes only merged feature/* branches except for the current branch, if
it matches this wildcard pattern. Note that Name is a positional (first)
parameter.
.EXAMPLE
PS> Remove-MergedGitBranch "*" -Confirm
Removes all merged branches but give you the chance to confirm each and
every branch deletion.
.EXAMPLE
PS> Remove-MergedGitBranch -Pattern "user/(dahlbyk|hillr)/.*"
Removes only merged feature/* branches except the current branch, if it's a
feature branch.
.EXAMPLE
PS> Remove-MergedGitBranch -ExcludePattern '(^\*)|(^. (develop|master|v\d+)$)'
Removes ALL merged branches except the current branch, develop, master and
branches that also match the pattern 'v\d+' e.g. v1, v1.0, v1.x.
.LINK
Remove-GitBranch
#>
function Remove-MergedGitBranch {
[CmdletBinding(DefaultParameterSetName="Wildcard", SupportsShouldProcess)]
param(
# Specifies a regular expression pattern for the merged branches that will be deleted.
# Certain branches are always excluded from deletion e.g. the current branch
# as well as the develop and master branches. See the ExcludePattern
# parameter to modify that pattern.
[Parameter(Position=0, Mandatory, ParameterSetName="Wildcard")]
[ValidateNotNullOrEmpty()]
[string]
$Name,

# Specifies a regular expression pattern for the merged ranches that will be deleted.
# Certain branches are always excluded from deletion e.g. the current branch
# as well as the develop and master branches. See the ExcludePattern
# parameter to modify that pattern.
[Parameter(Position=0, Mandatory, ParameterSetName="Pattern")]
[ValidateNotNull()]
[string]
$Pattern,

# Branches whose tips are reachable from the specified commit will be removed.
# The default commit is HEAD.
[Parameter(Position=1)]
[string]
$Commit = "HEAD",

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

$branches = git branch --merged | Where-Object {$_ -notmatch $ExcludePattern }

if ($PSCmdlet.ParameterSetName -eq "Wildcard") {
$branchesToDelete = $branches | Where-Object { $_.Trim() -like $Name }
}
else {
$branchesToDelete = $branches | Where-Object { $_ -match $Pattern }
}

foreach ($branch in $branchesToDelete) {
$targetBranch = $branch.Trim()
if ($PSCmdlet.ShouldProcess($targetBranch, "delete")) {
Invoke-Utf8ConsoleCommand { git branch --delete $targetBranch }
}
}
}

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

0 comments on commit 3c7409c

Please sign in to comment.