PowerShell/docs/list-repos.md

107 lines
3.3 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *list-repos.ps1*
========================
2021-11-08 21:36:42 +01:00
2024-05-19 10:25:56 +02:00
This PowerShell script lists all Git repositories in a folder with details such as latest tag/branch/status/URL.
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-03-27 17:36:59 +01:00
PS> ./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-05-19 10:25:56 +02:00
PS> ./list-repos.ps1 C:\Repos
2021-11-08 21:36:42 +01:00
2024-05-19 10:25:56 +02:00
Local Repo Latest Tag Branch Status Remote Repo
---------- ---------- ------ ------ -----------
📂cmake v3.23.0 main ✔clean ↓0 git@github.com:Kitware/CMake
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-05-19 10:25:56 +02:00
This PowerShell script lists all Git repositories in a folder with details such as latest tag/branch/status/URL.
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-05-19 10:25:56 +02:00
PS> ./list-repos.ps1 C:\Repos
2022-11-17 20:02:26 +01:00
2024-05-19 10:25:56 +02:00
Local Repo Latest Tag Branch Status Remote Repo
---------- ---------- ------ ------ -----------
📂cmake v3.23.0 main ✔clean ↓0 git@github.com:Kitware/CMake
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-03-27 17:36:59 +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-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-03-27 17:36:59 +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" }
2024-05-19 10:25:56 +02:00
elseif ("$status" -like " M *") { $status = "⚠changed" }
New-Object PSObject -property @{'Local Repo'="📂$folderName";'Latest Tag'="$latestTag";'Branch'="$branch";'Status'="$status";'Remote Repo'="↓$numCommits $remoteURL"}
2022-11-17 20:02:26 +01:00
}
}
try {
2024-03-27 17:36:59 +01:00
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access directory: $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-05-19 10:25:56 +02:00
ListRepos | Format-Table -property @{e='Local Repo';width=19},@{e='Latest Tag';width=16},@{e='Branch';width=19},@{e='Status';width=10},'Remote Repo'
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-05-19 10:25:56 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of list-repos.ps1 as of 05/19/2024 10:25:22)*