PowerShell/scripts/list-repos.ps1

55 lines
2.0 KiB
PowerShell
Raw Normal View History

2023-10-31 12:33:36 +01:00
<#
2021-09-28 20:03:03 +02:00
.SYNOPSIS
2024-04-19 12:09:00 +02:00
Lists Git repositories
2021-09-28 20:03:03 +02:00
.DESCRIPTION
2024-04-22 12:44:59 +02:00
This PowerShell script lists all Git repositories in a folder with details such as tag/branch/status/URL.
2024-03-20 13:27:17 +01:00
.PARAMETER parentDir
Specifies the path to the parent directory (current working directory by default)
2021-09-28 20:03:03 +02:00
.EXAMPLE
2024-04-19 12:09:00 +02:00
PS> ./list-repos.ps1 C:\Repos
2021-09-28 20:06:15 +02:00
2024-04-19 12:09:00 +02:00
Local Repo Latest Tag Branch Status Remote Repo
---------- ---------- ------ ------ -----------
2023-08-23 17:29:15 +02:00
📂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
#>
2024-03-20 13:27:17 +01:00
param([string]$parentDir = "$PWD")
2021-09-28 20:03:03 +02:00
2022-03-28 12:12:26 +02:00
function ListRepos {
2024-03-20 13:27:17 +01:00
$folders = (Get-ChildItem "$parentDir" -attributes Directory)
foreach($folder in $folders) {
$folderName = (Get-Item "$folder").Name
$latestTagCommitID = (git -C "$folder" rev-list --tags --max-count=1)
if ($latestTagCommitID -ne "") {
$latestTag = (git -C "$folder" describe --tags $latestTagCommitID)
2023-07-30 18:47:46 +02:00
} else {
2024-03-20 13:27:17 +01:00
$latestTag = ""
2023-07-30 18:47:46 +02:00
}
2024-03-20 13:27:17 +01:00
$branch = (git -C "$folder" branch --show-current)
$remoteURL = (git -C "$folder" remote get-url origin)
$numCommits = (git -C "$folder" rev-list HEAD...origin/$branch --count)
$status = (git -C "$folder" status --short)
if ("$status" -eq "") { $status = "clean" }
elseif ("$status" -like " M *") { $status = "modified" }
2024-04-22 12:44:59 +02:00
New-Object PSObject -property @{'Local Repo'="📂$folderName";'Latest Tag'="$latestTag";'Branch'="$branch";'Remote Repo'="$remoteURL";'Status'="$status$numCommits"}
2021-09-28 20:03:03 +02:00
}
}
try {
2024-03-20 13:27:17 +01:00
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access directory: $parentDir" }
2021-09-28 20:03:03 +02:00
2024-03-20 13:27:17 +01:00
$null = (git --version)
2021-09-28 20:03:03 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2024-04-22 12:44:59 +02:00
ListRepos | Format-Table -property @{e='Local Repo';width=19},@{e='Latest Tag';width=18},@{e='Branch';width=15},'Remote Repo',@{e='Status';width=14}
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
}