2021-09-27 10:38:12 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2021-10-04 21:29:23 +02:00
|
|
|
|
Lists all branches in a Git repository
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.DESCRIPTION
|
2022-01-29 12:47:46 +01:00
|
|
|
|
This PowerShell script lists all branches in a Git repository.
|
2021-10-15 23:09:08 +02:00
|
|
|
|
.PARAMETER RepoDir
|
|
|
|
|
Specifies the path to the Git repository (current working directory by default)
|
|
|
|
|
.PARAMETER SearchPattern
|
|
|
|
|
Specifies the search patter (anything by default)
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2021-09-24 17:19:49 +02:00
|
|
|
|
PS> ./list-branches
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2022-01-29 12:47:46 +01:00
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz / License: CC0
|
2021-02-28 18:26:20 +01:00
|
|
|
|
#>
|
|
|
|
|
|
2021-10-04 21:29:23 +02:00
|
|
|
|
param([string]$RepoDir = "$PWD", [string]$SearchPattern = "*")
|
2021-02-28 18:26:20 +01:00
|
|
|
|
|
|
|
|
|
try {
|
2021-03-16 08:45:53 +01:00
|
|
|
|
if (-not(test-path "$RepoDir" -pathType container)) { throw "Can't access directory: $RepoDir" }
|
2021-02-28 19:05:36 +01:00
|
|
|
|
|
2021-04-17 11:29:32 +02:00
|
|
|
|
$Null = (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" }
|
|
|
|
|
|
2021-05-10 16:31:13 +02:00
|
|
|
|
& git -C "$RepoDir" fetch
|
|
|
|
|
if ($lastExitCode -ne "0") { throw "'git fetch' failed" }
|
2021-03-10 10:51:59 +01:00
|
|
|
|
|
2021-05-10 16:31:13 +02:00
|
|
|
|
$Branches = $(git -C "$RepoDir" 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-05-10 16:31:13 +02:00
|
|
|
|
""
|
|
|
|
|
"List of Git Branches"
|
|
|
|
|
"--------------------"
|
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)
|
2021-10-04 21:29:23 +02:00
|
|
|
|
if ("$BranchName" -notlike "$SearchPattern") { continue }
|
2021-05-10 16:31:13 +02:00
|
|
|
|
"$BranchName"
|
2021-03-01 12:05:26 +01:00
|
|
|
|
}
|
2021-05-10 16:31:13 +02:00
|
|
|
|
""
|
2021-09-27 10:09:45 +02:00
|
|
|
|
exit 0 # success
|
2021-02-28 18:26:20 +01:00
|
|
|
|
} catch {
|
2021-09-16 20:19:10 +02:00
|
|
|
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
2021-02-28 18:26:20 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|