PowerShell/docs/clone-repos.md

123 lines
4.0 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*clone-repos.ps1*
================
2021-11-08 21:36:42 +01:00
2023-05-26 12:20:18 +02:00
This PowerShell script clones popular Git repositories into a target directory.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./clone-repos.ps1 [[-TargetDir] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-05-26 12:20:18 +02:00
-TargetDir <String>
Specifies the file path to the target directory (current working directory by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2022-02-10 09:01:07 +01:00
PS> ./clone-repos C:\Repos
2023-08-06 11:42:46 +02:00
⏳ (1) Searching for Git executable... git version 2.41.0.windows.3
2023-09-01 17:53:03 +02:00
⏳ (2) Reading Data/popular-repositories.csv... 28 repos
2023-08-06 11:42:46 +02:00
⏳ (3) Checking target folder... 📂repos
⏳ (4/32) Cloning into 📂base256unicode (dev tool)...
...
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-08-06 11:42:46 +02:00
Clones Git repos
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-05-26 12:20:18 +02:00
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)
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./clone-repos C:\Repos
2023-08-06 11:42:46 +02:00
⏳ (1) Searching for Git executable... git version 2.41.0.windows.3
2023-09-01 17:53:03 +02:00
⏳ (2) Reading Data/popular-repositories.csv... 28 repos
2023-08-06 11:42:46 +02:00
⏳ (3) Checking target folder... 📂repos
⏳ (4/32) Cloning into 📂base256unicode (dev tool)...
...
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-05-26 12:20:18 +02:00
param([string]$TargetDir = "$PWD")
2022-11-17 20:02:26 +01:00
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (1) Searching for Git executable... " -noNewline
2022-11-17 20:02:26 +01:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2023-09-01 17:53:03 +02:00
Write-Host "⏳ (2) Reading Data/popular-repositories.csv... " -noNewline
$Table = Import-CSV "$PSScriptRoot/../Data/popular-repositories.csv"
2022-11-17 20:02:26 +01:00
$NumEntries = $Table.count
2023-08-06 11:42:46 +02:00
Write-Host "$NumEntries repos"
2022-11-17 20:02:26 +01:00
2023-05-26 12:20:18 +02: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-11-17 20:02:26 +01:00
[int]$Step = 3
[int]$Cloned = 0
[int]$Skipped = 0
foreach($Row in $Table) {
2022-12-04 10:40:18 +01:00
[string]$FolderName = $Row.FOLDERNAME
2023-05-26 12:20:18 +02:00
[string]$Category = $Row.CATEGORY
2022-12-04 10:40:18 +01:00
[string]$Branch = $Row.BRANCH
2023-09-01 17:53:03 +02:00
[string]$Shallow = $Row.SHALLOW
2022-11-17 20:02:26 +01:00
[string]$URL = $Row.URL
$Step++
2023-05-26 12:20:18 +02:00
if (Test-Path "$TargetDir/$FolderName" -pathType container) {
"⏳ ($Step/$($NumEntries + 4)) Skipping existing 📂$FolderName ($Category)..."
2022-11-17 20:02:26 +01:00
$Skipped++
continue
}
2023-09-01 17:53:03 +02:00
if ($Shallow -eq "yes") {
2023-05-26 12:20:18 +02:00
"⏳ ($Step/$($NumEntries + 4)) Cloning into 📂$FolderName ($Category) - $Branch branch only..."
& git clone --branch "$Branch" --single-branch --recurse-submodules "$URL" "$TargetDir/$FolderName"
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git clone --branch $Branch $URL' failed with exit code $lastExitCode" }
2023-09-01 17:53:03 +02:00
} 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" }
2022-11-17 20:02:26 +01:00
}
$Cloned++
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-08-06 11:42:46 +02:00
"✔️ Cloning $Cloned of $NumEntries Git repos into folder 📂$TargetDirName took $Elapsed sec"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-10-19 08:12:00 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of clone-repos.ps1 as of 10/19/2023 08:11:36)*