PowerShell/scripts/list-repos.ps1

55 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-10-31 12:33:36 +01:00
<#
2021-09-28 20:03:03 +02:00
.SYNOPSIS
2023-07-30 11:00:07 +02:00
Lists Git repos
2021-09-28 20:03:03 +02:00
.DESCRIPTION
2023-07-30 11:00:07 +02:00
This PowerShell script lists details of all Git repositories in a folder.
2021-10-15 23:09:08 +02:00
.PARAMETER ParentDir
2022-11-28 08:41:17 +01:00
Specifies the path to the parent directory.
2021-09-28 20:03:03 +02:00
.EXAMPLE
PS> ./list-repos C:\MyRepos
2021-09-28 20:06:15 +02:00
2023-08-23 17:29:15 +02:00
Repository Latest Tag Branch Status Remote
---------- ---------- ------ ------ ------
📂cmake v3.23.0 main clean git@github.com:Kitware/CMake 0
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 {
$Folders = (Get-ChildItem "$ParentDir" -attributes Directory)
2023-06-19 12:56:24 +02:00
foreach($Folder in $Folders) {
2023-07-30 11:00:07 +02:00
$Repository = (Get-Item "$Folder").Name
2023-07-30 18:47:46 +02:00
$LatestTagCommitID = (git -C "$Folder" rev-list --tags --max-count=1)
if ($LatestTagCommitID -ne "") {
$LatestTag = (git -C "$Folder" describe --tags $LatestTagCommitID)
} else {
$LatestTag = ""
}
2023-07-30 20:10:30 +02:00
$Branch = (git -C "$Folder" branch --show-current)
2023-08-23 17:29:15 +02:00
$RemoteURL = (git -C "$Folder" remote get-url origin)
2023-07-30 11:00:07 +02:00
$NumCommits = (git -C "$Folder" rev-list HEAD...origin/$Branch --count)
2021-09-28 20:03:03 +02:00
$Status = (git -C "$Folder" status --short)
2023-07-30 18:47:46 +02:00
if ("$Status" -eq "") { $Status = "clean" }
elseif ("$Status" -like " M *") { $Status = "modified" }
2023-08-23 17:29:15 +02:00
New-Object PSObject -property @{'Repository'="📂$Repository";'Latest Tag'="$LatestTag";'Branch'="$Branch";'Status'="$Status";'Remote'="$RemoteURL$NumCommits";}
2021-09-28 20:03:03 +02:00
}
}
try {
2022-11-28 08:41:17 +01:00
if (-not(Test-Path "$ParentDir" -pathType container)) { throw "Can't access directory: $ParentDir" }
2021-09-28 20:03:03 +02:00
$Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2023-08-23 17:29:15 +02:00
ListRepos | Format-Table -property @{e='Repository';width=20},@{e='Latest Tag';width=18},@{e='Branch';width=20},@{e='Status';width=10},Remote
2021-09-28 20:03:03 +02:00
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-09-28 20:03:03 +02:00
exit 1
2023-07-30 11:00:07 +02:00
}