PowerShell/Scripts/list-repos.ps1

48 lines
1.4 KiB
PowerShell
Raw Normal View History

2021-09-28 20:03:03 +02:00
<#
.SYNOPSIS
2021-10-04 21:29:23 +02:00
Lists the details of all Git repositories in a folder
2021-09-28 20:03:03 +02:00
.DESCRIPTION
2021-10-15 23:09:08 +02:00
This script lists the details of all Git repositories in the given folder.
.PARAMETER ParentDir
Specifies the path to the parent folder.
2021-09-28 20:03:03 +02:00
.EXAMPLE
PS> ./list-repos C:\MyRepos
2021-09-28 20:06:15 +02:00
2021-09-28 20:07:51 +02:00
Repository Branch Status
------ ------ ------
cmake main clean
opencv master clean
2021-09-28 20:06:15 +02:00
...
2021-09-28 20:03:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
.LINK
https://github.com/fleschutz/PowerShell
#>
param([string]$ParentDir = "$PWD")
function ListRepos { param([string]$ParentDir)
$Folders = (get-childItem "$ParentDir" -attributes Directory)
foreach ($Folder in $Folders) {
2021-09-28 20:07:51 +02:00
$Repository = (get-item "$Folder").Name
2021-09-28 20:03:03 +02:00
$Branch = (git -C "$Folder" branch --show-current)
$Status = (git -C "$Folder" status --short)
if ("$Status" -eq "") { $Status = "clean" }
2021-09-28 20:07:51 +02:00
New-Object PSObject -property @{ 'Repository'="$Repository"; 'Branch'="$Branch"; 'Status'="$Status"; }
2021-09-28 20:03:03 +02:00
}
}
try {
if (-not(test-path "$ParentDir" -pathType container)) { throw "Can't access directory: $ParentDir" }
$Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2021-09-28 20:07:51 +02:00
ListRepos | format-table -property Repository,Branch,Status
2021-09-28 20:03:03 +02:00
exit 0 # success
} catch {
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
exit 1
}