PowerShell/scripts/clone-repos.ps1

72 lines
2.7 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2023-08-02 18:54:14 +02:00
Clones Git repos
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2023-10-29 12:35:39 +01:00
This PowerShell script clones popular Git repositories into a common target directory.
.PARAMETER targetDir
2023-04-28 07:49:54 +02:00
Specifies the file path to the target directory (current working directory by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-12-05 08:40:27 +01:00
PS> ./clone-repos C:\MyRepos
2024-08-25 11:01:51 +02: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-02 18:54:14 +02:00
...
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-04-24 16:24:10 +02:00
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-12-01 12:24:56 +01:00
2023-10-29 12:35:39 +01:00
param([string]$targetDir = "$PWD")
2021-02-28 18:49:53 +01:00
2021-02-13 10:26:14 +01:00
try {
2023-10-29 12:35:39 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2021-04-21 11:28:12 +02:00
2024-07-31 19:51:14 +02:00
Write-Host "⏳ (1) Searching for Git executable... " -noNewline
2022-04-24 16:24:10 +02:00
& git --version
2021-04-17 11:29:32 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2020-12-01 12:24:56 +01:00
2024-07-31 19:51:14 +02:00
Write-Host "⏳ (2) Reading data/popular-repos.csv... " -noNewline
2024-07-31 19:36:09 +02:00
$table = Import-CSV "$PSScriptRoot/../data/popular-repos.csv"
2023-10-29 12:35:39 +01:00
$total = $table.count
Write-Host "$total repos"
2021-01-20 16:11:38 +01:00
2023-10-29 12:35:39 +01:00
$targetDirName = (Get-Item "$targetDir").Name
2024-07-31 19:51:14 +02:00
Write-Host "⏳ (3) Checking target folder... 📂$targetDirName"
2023-10-29 12:35:39 +01:00
if (-not(Test-Path "$targetDir" -pathType container)) { throw "Can't access directory: $targetDir" }
2022-08-29 13:03:53 +02:00
2023-10-29 12:35:39 +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++
2021-06-28 20:58:37 +02:00
2023-10-29 12:35:39 +01:00
if (Test-Path "$targetDir/$folderName" -pathType container) {
2024-08-25 11:01:51 +02:00
"⏳ ($step/$($total + 3)) Skipping 📂$folderName ($category): exists already"
2023-10-29 12:35:39 +01:00
$skipped++
2023-12-05 08:40:27 +01:00
} elseif ($shallow -eq "yes") {
2024-08-25 11:01:51 +02:00
"⏳ ($step/$($total + 3)) Cloning 📂$folderName ($category) from $URL (shallow $branch branch)..."
2023-10-29 12:35:39 +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" }
2023-12-05 08:40:27 +01:00
$cloned++
2023-08-10 11:06:59 +02:00
} else {
2024-08-25 11:01:51 +02:00
"⏳ ($step/$($total + 3)) Cloning 📂$folderName ($category) from $URL (full $branch branch)..."
2023-10-29 12:35:39 +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" }
2023-12-05 08:40:27 +01:00
$clone++
2021-06-30 18:36:58 +02:00
}
2020-12-01 12:24:56 +01:00
}
2023-10-29 12:35:39 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-09-24 21:28:31 +02:00
"✅ Cloned $cloned additional Git repos into 📂$targetDirName in $($elapsed)s."
2021-09-27 10:09:45 +02:00
exit 0 # success
2020-12-09 10:30:55 +01:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}