Update list-repos.ps1

This commit is contained in:
Markus Fleschutz 2022-03-28 11:44:20 +02:00
parent 7a11e4705a
commit 6840cd3fbb

View File

@ -1,35 +1,38 @@
<# <#
.SYNOPSIS .SYNOPSIS
Lists the details of all Git repositories in a folder Lists Git repositories
.DESCRIPTION .DESCRIPTION
This PowerShell script lists the details of all Git repositories in the given folder. This PowerShell script lists the details of all Git repositories in a folder.
.PARAMETER ParentDir .PARAMETER ParentDir
Specifies the path to the parent folder. Specifies the path to the parent folder.
.EXAMPLE .EXAMPLE
PS> ./list-repos C:\MyRepos PS> ./list-repos C:\MyRepos
Repository Branch Status No Repository Branch Status
------ ------ ------ -- ---------- ------ ------
cmake main clean 1 cmake main clean
opencv master clean 2 opencv main modified
... ...
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz / License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$ParentDir = "$PWD") param([string]$ParentDir = "$PWD")
function ListRepos { param([string]$ParentDir) function ListRepos { param([string]$ParentDir)
[int]$No = 0
$Folders = (get-childItem "$ParentDir" -attributes Directory) $Folders = (get-childItem "$ParentDir" -attributes Directory)
foreach ($Folder in $Folders) { foreach ($Folder in $Folders) {
$No++
$Repository = (get-item "$Folder").Name $Repository = (get-item "$Folder").Name
$Branch = (git -C "$Folder" branch --show-current) $Branch = (git -C "$Folder" branch --show-current)
$Status = (git -C "$Folder" status --short) $Status = (git -C "$Folder" status --short)
if ("$Status" -eq "") { $Status = "clean" } if ("$Status" -eq "") { $Status = "clean" }
if ("$Status" -like " M *") { $Status = "modified" }
New-Object PSObject -property @{ 'Repository'="$Repository"; 'Branch'="$Branch"; 'Status'="$Status"; } New-Object PSObject -property @{ 'No'="$No"; 'Repository'="$Repository"; 'Branch'="$Branch"; 'Status'="$Status"; }
} }
} }
@ -39,7 +42,7 @@ try {
$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" }
ListRepos | format-table -property Repository,Branch,Status ListRepos | format-table -property @{e='No';width=3},@{e='Repository';width=25},@{e='Branch';width=20},Status
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))" "⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"