PowerShell/scripts/clone-repos.ps1

69 lines
2.5 KiB
PowerShell
Raw Normal View History

2023-10-31 12:12:58 +01: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
2023-08-02 18:54:14 +02:00
...
2024-07-31 19:51:14 +02:00
Cloned 29 additional Git repos into 📂MyRepos in 123s.
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-02 17:30:43 +02:00
"⏳ ($step/$($total + 3)) Skipping 📂$folderName - the $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-07-31 19:51:14 +02:00
"⏳ ($step/$($total + 3)) Cloning into 📂$folderName (a $category, $branch branch, shallow)..."
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-07-31 19:51:14 +02:00
"⏳ ($step/$($total + 3)) Cloning into 📂$folderName (a $category, $branch branch, full history)..."
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-07-31 19:51:14 +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
}