PowerShell/scripts/list-repos.ps1

55 lines
2.1 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02: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-08-22 08:05:00 +02:00
This PowerShell script lists all Git repositories under a folder with details such as latest tag, branch, remote URL, and status.
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-08-22 08:05:00 +02:00
PS> ./list-repos.ps1 C:\MyRepos
2021-09-28 20:06:15 +02:00
2024-08-22 08:05:00 +02:00
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
---------- ---------- ------ ---------- ------
2024-09-27 16:18:55 +02:00
📂cmake v3.30.2 master https://github.com/Kitware/CMake clean 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-09-04 11:47:32 +02:00
$dirs = (Get-ChildItem "$parentDir" -attributes Directory)
foreach($dir in $dirs) {
$dirName = (Get-Item "$dir").Name
$latestTagCommitID = (git -C "$dir" rev-list --tags --max-count=1)
if ("$latestTagCommitID" -ne "") {
$latestTag = (git -C "$dir" 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-09-04 11:47:32 +02:00
$branch = (git -C "$dir" branch --show-current)
$remoteURL = (git -C "$dir" remote get-url origin)
$numCommits = (git -C "$dir" rev-list HEAD...origin/$branch --count)
$status = (git -C "$dir" status --short)
2024-09-27 16:18:55 +02:00
if ("$status" -eq "") { $status = "✅clean" }
2024-05-13 08:21:54 +02:00
elseif ("$status" -like " M *") { $status = "changed" }
2024-09-04 11:47:32 +02:00
New-Object PSObject -property @{'REPOSITORY'="📂$dirName";'LATEST TAG'="$latestTag";'BRANCH'="$branch";'REMOTE URL'="$remoteURL";'STATUS'="$status$numCommits"}
2021-09-28 20:03:03 +02:00
}
}
try {
2024-08-22 08:05:00 +02:00
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access parent directory at: $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-08-22 08:05:00 +02:00
ListRepos | Format-Table -property @{e='REPOSITORY';width=19},@{e='LATEST TAG';width=16},@{e='BRANCH';width=19},@{e='REMOTE URL';width=47},@{e='STATUS';width=12}
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
}