mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-14 02:30:45 +01:00
107 lines
3.4 KiB
Markdown
107 lines
3.4 KiB
Markdown
The *list-repos.ps1* Script
|
||
===========================
|
||
|
||
This PowerShell script lists all Git repositories under a folder with details such as latest tag, branch, remote URL, and status.
|
||
|
||
Parameters
|
||
----------
|
||
```powershell
|
||
/home/markus/Repos/PowerShell/scripts/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:\MyRepos
|
||
|
||
|
||
|
||
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
|
||
---------- ---------- ------ ---------- ------
|
||
📂cmake v3.30.2 master https://github.com/Kitware/CMake ✅clean ↓0
|
||
...
|
||
|
||
```
|
||
|
||
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 under a folder with details such as latest tag, branch, remote URL, and status.
|
||
.PARAMETER parentDir
|
||
Specifies the path to the parent directory (current working directory by default)
|
||
.EXAMPLE
|
||
PS> ./list-repos.ps1 C:\MyRepos
|
||
|
||
REPOSITORY LATEST TAG BRANCH REMOTE URL STATUS
|
||
---------- ---------- ------ ---------- ------
|
||
📂cmake v3.30.2 master https://github.com/Kitware/CMake ✅clean ↓0
|
||
...
|
||
.LINK
|
||
https://github.com/fleschutz/PowerShell
|
||
.NOTES
|
||
Author: Markus Fleschutz | License: CC0
|
||
#>
|
||
|
||
param([string]$parentDir = "$PWD")
|
||
|
||
function ListRepos {
|
||
$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)
|
||
} else {
|
||
$latestTag = ""
|
||
}
|
||
$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" }
|
||
elseif ("$status" -like " M *") { $status = "⚠️changed" }
|
||
New-Object PSObject -property @{'REPOSITORY'="📂$dirName";'LATEST TAG'="$latestTag";'BRANCH'="$branch";'REMOTE URL'="$remoteURL";'STATUS'="$status ↓$numCommits"}
|
||
}
|
||
}
|
||
|
||
try {
|
||
if (-not(Test-Path "$parentDir" -pathType container)) { throw "Can't access parent directory at: $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='REPOSITORY';width=19},@{e='LATEST TAG';width=16},@{e='BRANCH';width=19},@{e='REMOTE URL';width=47},@{e='STATUS';width=12}
|
||
exit 0 # success
|
||
} catch {
|
||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||
exit 1
|
||
}
|
||
```
|
||
|
||
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:56)*
|