PowerShell/scripts/clone-repos.ps1

70 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
2022-01-21 20:04:05 +01:00
PS> ./clone-repos C:\Repos
2023-08-02 18:54:14 +02:00
...
2023-10-29 12:35:39 +01:00
Cloned 29 of 29 Git repos into 📂C:\Repos in 123 sec
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
2022-12-12 19:23:10 +01: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
2023-08-10 11:06:59 +02:00
Write-Host "⏳ (2) Reading Data/popular-repositories.csv... " -noNewline
2023-10-31 11:25:11 +01:00
$table = Import-CSV "$PSScriptRoot/../data/popular-repositories.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
Write-Host "⏳ (3) Checking target folder... 📂$targetDirName"
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) {
"⏳ ($step/$($total + 4)) Skipping existing 📂$folderName ($category)..."
$skipped++
2021-01-21 15:19:24 +01:00
continue
}
2023-10-29 12:35:39 +01:00
if ($shallow -eq "yes") {
"⏳ ($step/$($total + 4)) Cloning into 📂$folderName (a $category, $branch branch, shallow)..."
& 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-08-10 11:06:59 +02:00
} else {
2023-10-29 12:35:39 +01:00
"⏳ ($step/$($total + 4)) Cloning into 📂$folderName (a $category, $branch branch, full history)..."
& git clone --branch "$branch" --recurse-submodules "$URL" "$targetDir/$folderName"
if ($lastExitCode -ne "0") { throw "'git clone --branch $branch $URL' failed with exit code $lastExitCode" }
2021-06-30 18:36:58 +02:00
}
2023-10-29 12:35:39 +01:00
$cloned++
2020-12-01 12:24:56 +01:00
}
2023-10-29 12:35:39 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Cloned $cloned of $total Git repos into 📂$targetDirName in $Elapsed sec"
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
}