PowerShell/docs/clone-repos.md

122 lines
4.1 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *clone-repos.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2023-12-07 20:24:45 +01:00
This PowerShell script clones popular Git repositories into a common target directory.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/clone-repos.ps1 [[-targetDir] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-12-07 20:24:45 +01:00
-targetDir <String>
2023-05-26 12:20:18 +02:00
Specifies the file path to the target directory (current working directory by default)
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.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-12-07 20:24:45 +01:00
PS> ./clone-repos C:\MyRepos
2024-11-08 12:35:11 +01:00
⏳ (1) Searching for Git executable... git version 2.46.0.windows.1
⏳ (2) Reading data/popular-repos.csv... 29 repos
⏳ (3) Checking target folder... 📂Repos
⏳ (4/32) Cloning 📂base256 (dev tool) from git@github.com:fleschutz/talk2windows.git (shallow main branch)...
2023-08-06 11:42:46 +02:00
...
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-08-06 11:42:46 +02:00
Clones Git repos
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-12-07 20:24:45 +01:00
This PowerShell script clones popular Git repositories into a common target directory.
2023-05-26 12:20:18 +02:00
.PARAMETER targetDir
Specifies the file path to the target directory (current working directory by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-12-07 20:24:45 +01:00
PS> ./clone-repos C:\MyRepos
2024-11-08 12:35:11 +01:00
⏳ (1) Searching for Git executable... git version 2.46.0.windows.1
⏳ (2) Reading data/popular-repos.csv... 29 repos
⏳ (3) Checking target folder... 📂Repos
⏳ (4/32) Cloning 📂base256 (dev tool) from git@github.com:fleschutz/talk2windows.git (shallow main branch)...
2023-08-06 11:42:46 +02:00
...
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-12-07 20:24:45 +01:00
param([string]$targetDir = "$PWD")
2022-11-17 20:02:26 +01:00
try {
2023-12-07 20:24:45 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-11-17 20:02:26 +01:00
2024-08-15 09:51:46 +02: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" }
2024-08-15 09:51:46 +02:00
Write-Host "⏳ (2) Reading data/popular-repos.csv... " -noNewline
$table = Import-CSV "$PSScriptRoot/../data/popular-repos.csv"
2023-12-07 20:24:45 +01:00
$total = $table.count
Write-Host "$total repos"
2022-11-17 20:02:26 +01:00
2023-12-07 20:24:45 +01:00
$targetDirName = (Get-Item "$targetDir").Name
2024-08-15 09:51:46 +02:00
Write-Host "⏳ (3) Checking target folder... 📂$targetDirName"
2023-12-07 20:24:45 +01:00
if (-not(Test-Path "$targetDir" -pathType container)) { throw "Can't access directory: $targetDir" }
2022-11-17 20:02:26 +01:00
2023-12-07 20:24:45 +01:00
[int]$step = 3
[int]$cloned = 0
[int]$skipped = 0
foreach($row in $table) {
[string]$folderName = $row.FOLDERNAME
[string]$category = $row.CATEGORY
[string]$URL = $row.URL
[string]$branch = $row.BRANCH
[string]$shallow = $row.SHALLOW
$step++
if (Test-Path "$targetDir/$folderName" -pathType container) {
2024-11-08 12:35:11 +01:00
"⏳ ($step/$($total + 3)) Skipping 📂$folderName ($category): exists already"
2023-12-07 20:24:45 +01:00
$skipped++
} elseif ($shallow -eq "yes") {
2024-11-08 12:35:11 +01:00
"⏳ ($step/$($total + 3)) Cloning 📂$folderName ($category) from $URL (shallow $branch branch)..."
2023-12-07 20:24:45 +01:00
& git clone --branch "$branch" --single-branch --recurse-submodules "$URL" "$targetDir/$folderName"
if ($lastExitCode -ne "0") { throw "'git clone --branch $branch $URL' failed with exit code $lastExitCode" }
$cloned++
2023-09-01 17:53:03 +02:00
} else {
2024-11-08 12:35:11 +01:00
"⏳ ($step/$($total + 3)) Cloning 📂$folderName ($category) from $URL (full $branch branch)..."
2023-12-07 20:24:45 +01:00
& git clone --branch "$branch" --recurse-submodules "$URL" "$targetDir/$folderName"
if ($lastExitCode -ne "0") { throw "'git clone --branch $branch $URL' failed with exit code $lastExitCode" }
$clone++
2022-11-17 20:02:26 +01:00
}
}
2023-12-07 20:24:45 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-11-08 12:35:11 +01:00
"✅ Cloned $cloned additional Git repos into 📂$targetDirName in $($elapsed)s."
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
2024-11-08 12:40:31 +01:00
*(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:16)*