PowerShell/Scripts/clone-repos.ps1

71 lines
2.4 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
Clones Git repositories
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script clones well-known Git repositories into a folder.
2021-10-16 16:50:10 +02:00
.PARAMETER folder
Specifies the target folder
2021-07-13 21:10:02 +02:00
.EXAMPLE
2022-01-21 20:04:05 +01:00
PS> ./clone-repos C:\Repos
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
2022-01-21 20:04:05 +01:00
param([string]$FolderPath = "$PWD")
2021-02-28 18:49:53 +01:00
2021-02-13 10:26:14 +01:00
try {
2021-04-21 11:28:12 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2022-08-06 14:20:35 +02:00
"⏳ Step 1 - Searching for Git executable..."
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
2022-08-29 13:03:53 +02:00
"⏳ Step 2 - Loading database table in Data/git-repos.csv..."
2022-08-06 14:20:35 +02:00
$Table = Import-CSV "$PSScriptRoot/../Data/git-repos.csv"
2022-01-21 20:04:05 +01:00
$NumEntries = $Table.count
"Found $NumEntries entries."
2021-01-20 16:11:38 +01:00
2022-08-29 13:03:53 +02:00
$ParentFolderName = (Get-Item "$FolderPath").Name
"⏳ Step 3 - Checking folder 📂$ParentFolderName..."
if (-not(Test-Path "$FolderPath" -pathType container)) { throw "Can't access directory: $FolderPath" }
[int]$Step = 3
2022-01-21 20:04:05 +01:00
[int]$Cloned = 0
[int]$Skipped = 0
2021-01-20 16:11:38 +01:00
foreach($Row in $Table) {
2021-06-30 18:36:58 +02:00
[string]$FolderName = $Row.FolderName
[string]$Branch = $Row.Branch
2022-04-27 15:29:06 +02:00
[string]$Full = $Row.Full
2021-06-30 18:36:58 +02:00
[string]$URL = $Row.URL
2022-01-21 20:04:05 +01:00
$Step++
2021-06-28 20:58:37 +02:00
2022-01-21 20:04:05 +01:00
if (test-path "$FolderPath/$FolderName" -pathType container) {
"⏳ Step $Step/$($NumEntries + 4) - Skipping 📂$($FolderName) (exists already)..."
2022-01-21 20:04:05 +01:00
$Skipped++
2021-01-21 15:19:24 +01:00
continue
}
2022-04-27 15:29:06 +02:00
if ($Full -eq "yes") {
"⏳ Step $Step/$($NumEntries + 4) - Cloning into 📂$($FolderName) ($Branch branch with full history)..."
2022-01-21 20:04:05 +01:00
& git clone --branch "$Branch" --recurse-submodules "$URL" "$FolderPath/$FolderName"
2022-04-27 15:29:06 +02:00
if ($lastExitCode -ne "0") { throw "'git clone --branch $Branch $URL' failed with exit code $lastExitCode" }
} else {
"⏳ Step $Step/$($NumEntries + 4) - Cloning into 📂$FolderName ($Branch branch only)..."
2022-04-27 15:29:06 +02: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" }
2021-06-30 18:36:58 +02:00
}
2022-01-21 20:04:05 +01:00
$Cloned++
2020-12-01 12:24:56 +01:00
}
2021-04-21 16:43:08 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2022-04-24 16:24:10 +02:00
if ($Cloned -eq 1) {
"✔️ $Cloned repo cloned into 📂$ParentFolderName ($Skipped skipped) in $Elapsed sec"
} else {
"✔️ $Cloned repos cloned into 📂$ParentFolderName ($Skipped skipped) 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
2022-07-29 15:21:39 +02:00
}