PowerShell/Scripts/list-branches.ps1

38 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-02-28 18:26:20 +01:00
#!/bin/powershell
<#
2021-02-28 19:05:36 +01:00
.SYNTAX ./list-branches.ps1 [<repo-dir>] [<pattern>]
2021-03-01 12:05:26 +01:00
.DESCRIPTION lists all branches of the current/given Git repository
2021-02-28 18:26:20 +01:00
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
2021-02-28 19:05:36 +01:00
param($RepoDir = "$PWD", $Pattern = "*")
2021-02-28 18:26:20 +01:00
try {
write-progress "Fetching changes in repository $RepoDir ..."
2021-02-28 18:26:20 +01:00
if (-not(test-path "$RepoDir")) { throw "Can't access Git repository directory: $RepoDir" }
2021-02-28 18:26:20 +01:00
set-location $RepoDir
2021-02-28 19:05:36 +01:00
& git --version
2021-03-10 10:51:59 +01:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
& git fetch --all --recurse-submodules
2021-03-10 16:00:16 +01:00
if ($lastExitCode -ne "0") { throw "'git fetch --all --recurse-submodules' failed" }
2021-03-10 10:51:59 +01:00
write-output "List of Git branches in repository $($RepoDir):"
2021-03-01 12:22:11 +01:00
$Branches = $(git branch --list --remotes --no-color --no-column)
2021-02-28 19:05:36 +01:00
if ($lastExitCode -ne "0") { throw "'git branch --list' failed" }
2021-02-28 18:26:20 +01:00
2021-03-01 12:05:26 +01:00
foreach($Branch in $Branches) {
if ("$Branch" -match "origin/HEAD") { continue }
2021-03-01 12:22:11 +01:00
$BranchName = $Branch.substring(9)
if ("$BranchName" -notlike "$Pattern") { continue }
write-output "$BranchName"
2021-03-01 12:05:26 +01:00
}
2021-02-28 18:26:20 +01:00
exit 0
} catch {
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}