Update list-branches.ps1

This commit is contained in:
Markus Fleschutz 2024-03-18 11:32:35 +01:00
parent 885abeb385
commit 7ee714e3a9

View File

@ -2,11 +2,11 @@
.SYNOPSIS .SYNOPSIS
Lists Git branches Lists Git branches
.DESCRIPTION .DESCRIPTION
This PowerShell script lists all branches in a Git repository. This PowerShell script lists branches in a Git repository - either all (default) or by a search pattern.
.PARAMETER RepoDir .PARAMETER pathToRepo
Specifies the path to the Git repository (current working directory by default) Specifies the path to the Git repository (current working directory by default)
.PARAMETER SearchPattern .PARAMETER searchPattern
Specifies the search patter (anything by default) Specifies the search pattern ("*", anything by default)
.EXAMPLE .EXAMPLE
PS> ./list-branches.ps1 PS> ./list-branches.ps1
@ -19,28 +19,28 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$RepoDir = "$PWD", [string]$SearchPattern = "*") param([string]$pathToRepo = "$PWD", [string]$searchPattern = "*")
try { try {
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" } if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access repo folder '$pathToRepo' - maybe a typo or missing folder permissions?" }
$Null = (git --version) $null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
& git -C "$RepoDir" fetch & git -C "$pathToRepo" fetch
if ($lastExitCode -ne "0") { throw "'git fetch' failed" } if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
$Branches = $(git -C "$RepoDir" branch --list --remotes --no-color --no-column) $branches = $(git -C "$pathToRepo" branch --list --remotes --no-color --no-column)
if ($lastExitCode -ne "0") { throw "'git branch --list' failed" } if ($lastExitCode -ne "0") { throw "'git branch --list' failed" }
"" ""
"List of Git Branches" "List of Git Branches"
"--------------------" "--------------------"
foreach($Branch in $Branches) { foreach($branch in $branches) {
if ("$Branch" -match "origin/HEAD") { continue } if ("$branch" -match "origin/HEAD") { continue }
$BranchName = $Branch.substring(9) $branchName = $branch.substring(9)
if ("$BranchName" -notlike "$SearchPattern") { continue } if ("$branchName" -notlike "$searchPattern") { continue }
"$BranchName" "$branchName"
} }
"" ""
exit 0 # success exit 0 # success