PowerShell/Docs/clone-repos.md

111 lines
3.4 KiB
Markdown
Raw Normal View History

2022-12-04 10:40:18 +01:00
## The *clone-repos.ps1* Script
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script clones well-known Git repositories into a folder.
2021-11-08 21:36:42 +01:00
## Parameters
```powershell
2022-02-10 09:01:07 +01:00
clone-repos.ps1 [[-FolderPath] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
-FolderPath <String>
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
2022-02-10 09:01:07 +01:00
PS> ./clone-repos C:\Repos
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
Clones Git repositories
.DESCRIPTION
This PowerShell script clones well-known Git repositories into a folder.
.PARAMETER folder
2022-12-04 10:40:18 +01:00
Specifies the target folder (default is current working directory)
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./clone-repos C:\Repos
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$FolderPath = "$PWD")
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2022-12-04 10:40:18 +01:00
Write-Host "⏳ (1) Searching for Git executable... " -noNewline
2022-11-17 20:02:26 +01:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2022-12-04 10:40:18 +01:00
Write-Host "⏳ (2) Loading database table from Data/git-repos.csv... " -noNewline
2022-11-17 20:02:26 +01:00
$Table = Import-CSV "$PSScriptRoot/../Data/git-repos.csv"
$NumEntries = $Table.count
2022-12-04 10:40:18 +01:00
Write-Host "$NumEntries entries found"
2022-11-17 20:02:26 +01:00
$ParentFolderName = (Get-Item "$FolderPath").Name
2022-12-04 10:40:18 +01:00
"⏳ (3) Checking target folder 📂$ParentFolderName..."
2022-11-17 20:02:26 +01:00
if (-not(Test-Path "$FolderPath" -pathType container)) { throw "Can't access directory: $FolderPath" }
[int]$Step = 3
[int]$Cloned = 0
[int]$Skipped = 0
foreach($Row in $Table) {
2022-12-04 10:40:18 +01:00
[string]$Group = $Row.GROUP
[string]$FolderName = $Row.FOLDERNAME
[string]$Branch = $Row.BRANCH
[string]$Full = $Row.FULL
2022-11-17 20:02:26 +01:00
[string]$URL = $Row.URL
$Step++
2022-12-04 10:40:18 +01:00
if (Test-Path "$FolderPath/$FolderName" -pathType container) {
"⏳ ($Step/$($NumEntries + 4)) Skipping 📂$($FolderName), it exists already..."
2022-11-17 20:02:26 +01:00
$Skipped++
continue
}
if ($Full -eq "yes") {
2022-12-04 10:40:18 +01:00
"⏳ ($Step/$($NumEntries + 4)) Cloning into 📂$($FolderName) ($Branch branch with full history)..."
2022-11-17 20:02:26 +01:00
& git clone --branch "$Branch" --recurse-submodules "$URL" "$FolderPath/$FolderName"
if ($lastExitCode -ne "0") { throw "'git clone --branch $Branch $URL' failed with exit code $lastExitCode" }
} else {
2022-12-04 10:40:18 +01:00
"⏳ ($Step/$($NumEntries + 4)) Cloning $Branch branch into 📂$FolderName..."
2022-11-17 20:02:26 +01:00
& git clone --branch "$Branch" --single-branch --recurse-submodules "$URL" "$FolderPath/$FolderName"
if ($lastExitCode -ne "0") { throw "'git clone --branch $Branch $URL' failed with exit code $lastExitCode" }
}
$Cloned++
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
if ($Cloned -eq 1) {
2022-12-04 10:40:18 +01:00
"✔️ 1 Git repository cloned into 📂$ParentFolderName in $Elapsed sec ($Skipped skipped)."
2022-11-17 20:02:26 +01:00
} else {
2022-12-04 10:40:18 +01:00
"✔️ $Cloned Git repos cloned into 📂$ParentFolderName in $Elapsed sec ($Skipped skipped)."
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
2021-11-08 21:36:42 +01:00
*Generated by convert-ps2md.ps1 using the comment-based help of clone-repos.ps1*