PowerShell/docs/list-repos.md

107 lines
3.4 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *list-repos.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2024-11-08 12:35:11 +01:00
This PowerShell script lists all Git repositories under a folder with details such as latest tag, branch, remote URL, and status.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/list-repos.ps1 [[-parentDir] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-03-27 17:36:59 +01:00
-parentDir <String>
Specifies the path to the parent directory (current working directory by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
PS> ./list-repos.ps1 C:\MyRepos
2021-11-08 21:36:42 +01:00
2024-11-08 12:35:11 +01:00
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
---------- ---------- ------ ---------- ------
📂cmake v3.30.2 master https://github.com/Kitware/CMake ✅clean ↓0
2021-11-08 21:36:42 +01:00
...
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2024-05-19 10:25:56 +02:00
Lists Git repositories
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2024-11-08 12:35:11 +01:00
This PowerShell script lists all Git repositories under a folder with details such as latest tag, branch, remote URL, and status.
2024-03-27 17:36:59 +01:00
.PARAMETER parentDir
Specifies the path to the parent directory (current working directory by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-11-08 12:35:11 +01:00
PS> ./list-repos.ps1 C:\MyRepos
2022-11-17 20:02:26 +01:00
2024-11-08 12:35:11 +01:00
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
---------- ---------- ------ ---------- ------
📂cmake v3.30.2 master https://github.com/Kitware/CMake ✅clean ↓0
2022-11-17 20:02:26 +01:00
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +01:00
param([string]$parentDir = "$PWD")
2022-11-17 20:02:26 +01:00
function ListRepos {
2024-11-08 12:35:11 +01: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-08-06 11:42:46 +02:00
} else {
2024-03-27 17:36:59 +01:00
$latestTag = ""
2023-08-06 11:42:46 +02:00
}
2024-11-08 12:35:11 +01: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)
if ("$status" -eq "") { $status = "✅clean" }
2024-05-19 10:25:56 +02:00
elseif ("$status" -like " M *") { $status = "⚠changed" }
2024-11-08 12:35:11 +01:00
New-Object PSObject -property @{'REPOSITORY'="📂$dirName";'LATEST TAG'="$latestTag";'BRANCH'="$branch";'REMOTE URL'="$remoteURL";'STATUS'="$status ↓$numCommits"}
2022-11-17 20:02:26 +01:00
}
}
try {
2024-11-08 12:35:11 +01:00
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access parent directory at: $parentDir" }
2022-11-17 20:02:26 +01:00
2024-03-27 17:36:59 +01:00
$null = (git --version)
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2024-11-08 12:35:11 +01: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}
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:56)*