PowerShell/Scripts/list-repos.ps1

53 lines
1.7 KiB
PowerShell
Raw Normal View History

2021-09-28 20:03:03 +02:00
<#
.SYNOPSIS
2022-03-28 12:16:50 +02:00
Lists Git repositories
2021-09-28 20:03:03 +02:00
.DESCRIPTION
2022-03-28 11:44:20 +02:00
This PowerShell script lists the details of all Git repositories in a folder.
2021-10-15 23:09:08 +02:00
.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
2022-03-28 12:12:26 +02:00
No Repository Branch LatestTag Status
-- ---------- ------ --------- ------
1 cmake main v3.23.0 clean
2 opencv main 4.5.5 modified
2021-09-28 20:06:15 +02:00
...
2021-09-28 20:03:03 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-03-28 11:44:20 +02:00
Author: Markus Fleschutz | License: CC0
2021-09-28 20:03:03 +02:00
#>
param([string]$ParentDir = "$PWD")
2022-03-28 12:12:26 +02:00
function ListRepos {
2022-03-28 11:44:20 +02:00
[int]$No = 0
2022-03-28 12:12:26 +02:00
$Folders = (Get-ChildItem "$ParentDir" -attributes Directory)
2021-09-28 20:03:03 +02:00
foreach ($Folder in $Folders) {
2022-03-28 11:44:20 +02:00
$No++
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)
2022-03-28 12:12:26 +02:00
$LatestTagCommitID = (git -C "$Folder" rev-list --tags --max-count=1)
$LatestTag = (git -C "$Folder" describe --tags $LatestTagCommitID)
2021-09-28 20:03:03 +02:00
$Status = (git -C "$Folder" status --short)
if ("$Status" -eq "") { $Status = "clean" }
2022-03-28 11:44:20 +02:00
if ("$Status" -like " M *") { $Status = "modified" }
2021-09-28 20:03:03 +02:00
2022-03-28 12:12:26 +02:00
New-Object PSObject -property @{ 'No'="$No"; 'Repository'="$Repository"; 'Branch'="$Branch"; 'LatestTag'="$LatestTag"; '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" }
2022-03-28 12:12:26 +02:00
ListRepos | Format-Table -property @{e='No';width=3},@{e='Repository';width=25},@{e='Branch';width=20},LatestTag,Status
2021-09-28 20:03:03 +02:00
exit 0 # success
} catch {
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
exit 1
2022-03-28 12:16:50 +02:00
}