PowerShell/Docs/list-repos.md

100 lines
2.8 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *list-repos.ps1* Script
2021-11-08 21:36:42 +01:00
2022-11-17 19:46:02 +01:00
This PowerShell script lists the details of all Git repositories in a folder.
2021-11-08 21:36:42 +01:00
## Parameters
```powershell
2023-05-26 12:20:18 +02:00
/home/mf/Repos/PowerShell/Scripts/list-repos.ps1 [[-ParentDir] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-ParentDir <String>
2022-12-04 10:40:18 +01:00
Specifies the path to the parent directory.
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.
```
## Example
```powershell
PS> ./list-repos C:\MyRepos
2022-12-04 10:40:18 +01:00
No Repository Branch LatestTag Status
-- ---------- ------ --------- ------
1 cmake main v3.23.0 clean
2 opencv main 4.5.5 modified
2021-11-08 21:36:42 +01:00
...
```
## Notes
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
## Related Links
https://github.com/fleschutz/PowerShell
2022-11-17 20:02:26 +01:00
## Source Code
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists Git repositories
.DESCRIPTION
This PowerShell script lists the details of all Git repositories in a folder.
.PARAMETER ParentDir
2022-12-04 10:40:18 +01:00
Specifies the path to the parent directory.
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./list-repos C:\MyRepos
2022-12-04 10:40:18 +01:00
No Repository Branch LatestTag Status
-- ---------- ------ --------- ------
1 cmake main v3.23.0 clean
2 opencv main 4.5.5 modified
2022-11-17 20:02:26 +01:00
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$ParentDir = "$PWD")
function ListRepos {
2022-12-04 10:40:18 +01:00
[int]$No = 1
2022-11-17 20:02:26 +01:00
$Folders = (Get-ChildItem "$ParentDir" -attributes Directory)
foreach ($Folder in $Folders) {
2022-12-04 10:40:18 +01:00
$FolderName = (Get-Item "$Folder").Name
2022-11-17 20:02:26 +01:00
$Branch = (git -C "$Folder" branch --show-current)
$LatestTagCommitID = (git -C "$Folder" rev-list --tags --max-count=1)
$LatestTag = (git -C "$Folder" describe --tags $LatestTagCommitID)
$Status = (git -C "$Folder" status --short)
if ("$Status" -eq "") { $Status = "clean" }
if ("$Status" -like " M *") { $Status = "modified" }
2022-12-04 10:40:18 +01:00
New-Object PSObject -property @{ 'No'="$No"; 'Repository'="$FolderName"; 'Branch'="$Branch"; 'LatestTag'="$LatestTag"; 'Status'="$Status"; }
$No++
2022-11-17 20:02:26 +01:00
}
}
try {
2022-12-04 10:40:18 +01:00
if (-not(Test-Path "$ParentDir" -pathType container)) { throw "Can't access directory: $ParentDir" }
2022-11-17 20:02:26 +01:00
$Null = (git --version)
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
ListRepos | Format-Table -property @{e='No';width=3},@{e='Repository';width=25},@{e='Branch';width=20},LatestTag,Status
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
2021-11-08 21:36:42 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of list-repos.ps1*