PowerShell/docs/list-repos.md
2024-05-19 10:25:56 +02:00

107 lines
3.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Script: *list-repos.ps1*
========================
This PowerShell script lists all Git repositories in a folder with details such as latest tag/branch/status/URL.
Parameters
----------
```powershell
PS> ./list-repos.ps1 [[-parentDir] <String>] [<CommonParameters>]
-parentDir <String>
Specifies the path to the parent directory (current working directory by default)
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.
```
Example
-------
```powershell
PS> ./list-repos.ps1 C:\Repos
Local Repo Latest Tag Branch Status Remote Repo
---------- ---------- ------ ------ -----------
📂cmake v3.23.0 main clean 0 git@github.com:Kitware/CMake
...
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Lists Git repositories
.DESCRIPTION
This PowerShell script lists all Git repositories in a folder with details such as latest tag/branch/status/URL.
.PARAMETER parentDir
Specifies the path to the parent directory (current working directory by default)
.EXAMPLE
PS> ./list-repos.ps1 C:\Repos
Local Repo Latest Tag Branch Status Remote Repo
---------- ---------- ------ ------ -----------
📂cmake v3.23.0 main ✔clean ↓0 git@github.com:Kitware/CMake
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$parentDir = "$PWD")
function ListRepos {
$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)
} else {
$latestTag = ""
}
$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 = "⚠changed" }
New-Object PSObject -property @{'Local Repo'="📂$folderName";'Latest Tag'="$latestTag";'Branch'="$branch";'Status'="$status";'Remote Repo'="↓$numCommits $remoteURL"}
}
}
try {
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access directory: $parentDir" }
$null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
ListRepos | Format-Table -property @{e='Local Repo';width=19},@{e='Latest Tag';width=16},@{e='Branch';width=19},@{e='Status';width=10},'Remote Repo'
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
*(generated by convert-ps2md.ps1 using the comment-based help of list-repos.ps1 as of 05/19/2024 10:25:22)*