PowerShell/Docs/list-repos.md
2023-10-19 08:12:00 +02:00

107 lines
3.1 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.

*list-repos.ps1*
================
This PowerShell script lists details of all Git repositories in a folder.
Parameters
----------
```powershell
PS> ./list-repos.ps1 [[-ParentDir] <String>] [<CommonParameters>]
-ParentDir <String>
Specifies the path to the parent directory.
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 C:\MyRepos
Repository Latest Tag Branch Status Remote
---------- ---------- ------ ------ ------
📂cmake v3.23.0 main clean git@github.com:Kitware/CMake 0
...
```
Notes
-----
Author: Markus Fleschutz | License: CC0
Related Links
-------------
https://github.com/fleschutz/PowerShell
Script Content
--------------
```powershell
<#
.SYNOPSIS
Lists Git repos
.DESCRIPTION
This PowerShell script lists details of all Git repositories in a folder.
.PARAMETER ParentDir
Specifies the path to the parent directory.
.EXAMPLE
PS> ./list-repos C:\MyRepos
Repository Latest Tag Branch Status Remote
---------- ---------- ------ ------ ------
📂cmake v3.23.0 main ✔clean git@github.com:Kitware/CMake ↓0
...
.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) {
$Repository = (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 = "⚠modified" }
New-Object PSObject -property @{'Repository'="📂$Repository";'Latest Tag'="$LatestTag";'Branch'="$Branch";'Status'="$Status";'Remote'="$RemoteURL$NumCommits";}
}
}
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='Repository';width=20},@{e='Latest Tag';width=18},@{e='Branch';width=20},@{e='Status';width=10},Remote
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 10/19/2023 08:11:40)*