<# .SYNOPSIS Clones Git repos .DESCRIPTION This PowerShell script clones popular Git repositories into a target directory. .PARAMETER targetDir Specifies the file path to the target directory (current working directory by default) .EXAMPLE PS> ./clone-repos C:\Repos β³ (1) Searching for Git executable... git version 2.41.0.windows.3 β³ (2) Reading Data/popular-repositories.csv... 28 repos β³ (3) Checking target folder... πrepos β³ (4/32) Cloning into πbase256unicode (dev tool)... ... .LINK https://github.com/fleschutz/PowerShell .NOTES Author: Markus Fleschutz | License: CC0 #> param([string]$TargetDir = "$PWD") try { $StopWatch = [system.diagnostics.stopwatch]::startNew() Write-Host "β³ (1) Searching for Git executable... " -noNewline & git --version if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } Write-Host "β³ (2) Reading Data/popular-repositories.csv... " -noNewline $Table = Import-CSV "$PSScriptRoot/../Data/popular-repositories.csv" $NumEntries = $Table.count Write-Host "$NumEntries repos" $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" } [int]$Step = 3 [int]$Cloned = 0 [int]$Skipped = 0 foreach($Row in $Table) { [string]$FolderName = $Row.FOLDERNAME [string]$Category = $Row.CATEGORY [string]$Branch = $Row.BRANCH [string]$Shallow = $Row.SHALLOW [string]$URL = $Row.URL $Step++ if (Test-Path "$TargetDir/$FolderName" -pathType container) { "β³ ($Step/$($NumEntries + 4)) Skipping existing π$FolderName ($Category)..." $Skipped++ continue } if ($Shallow -eq "yes") { "β³ ($Step/$($NumEntries + 4)) Cloning into π$FolderName ($Category) - $Branch branch only..." & 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" } } else { "β³ ($Step/$($NumEntries + 4)) Cloning into π$FolderName ($Category) - $Branch branch with 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" } } $Cloned++ } [int]$Elapsed = $StopWatch.Elapsed.TotalSeconds "βοΈ Cloning $Cloned of $NumEntries Git repos into folder π$TargetDirName took $Elapsed sec" exit 0 # success } catch { "β οΈ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" exit 1 }