Yes, the regular expression used in the largerepo-tree.ps1 script will work correctly with repository names containing hyphens (-) and underscores (_).
Character Classes: The regular expression uses [^/]+ to match one or more characters that are not a forward slash (/). This character class includes hyphens and underscores, as well as letters, numbers, and most other symbols.
Specific Parts: The relevant part of the regex is (https://github.com/[^/]+/[^/]+). This is designed to capture the https://github.com/owner/repo portion of the URL. The [^/]+ parts will match any character except a forward slash, which is exactly what you need to handle hyphens and underscores in the repository owner and repository name.
Example Scenarios:
Let's say you have these repository URLs:
https://github.com/my-org/my_repo/tree/main/frontend
https://github.com/my_org-test/my-repo_project/tree/develop/backend/api
The script will successfully parse these URLs and extract the correct repository URL, branch, and sparse path.
Testing:
To verify, you can create a test repository with hyphens and underscores in the name and try running the script with a tree URL from that repository.
In conclusion, the largerepo-tree.ps1 script is robust enough to handle repository names containing hyphens and underscores.
largerepo-tree.ps1
param (
[Parameter(Mandatory=$true, HelpMessage="Repository tree URL (e.g., https://github.com/largeorg/monorepo/tree/main/frontend)")]
[string]$TreeUrl
)
try {
# Extract repository URL, branch, and sparse path from the tree URL
if ($TreeUrl -match "^(https://github.com/[^/]+/[^/]+)/tree/([^/]+)/(.+)$") {
$repoUrl = $Matches[1] + ".git"
$branch = $Matches[2]
$sparsePath = $Matches[3]
} else {
throw "Invalid tree URL format. Expected: https://github.com/largeorg/monorepo/tree/main/frontend"
}
# Extract repository name from URL
$repoName = Split-Path -Leaf $repoUrl
if ($repoName -match "\.git$") {
$repoName = $repoName.Substring(0, $repoName.Length - 4)
}
# Clone the repository with sparse checkout
Write-Host "Cloning $repoUrl..."
git clone --depth 1 --sparse -b $branch $repoUrl $repoName
# Navigate to the repository directory
Write-Host "Navigating to $repoName..."
cd $repoName
# Initialize sparse checkout
Write-Host "Initializing sparse checkout..."
git sparse-checkout init --cone
# Set the sparse checkout path
Write-Host "Setting sparse checkout path to $sparsePath..."
git sparse-checkout set $sparsePath
Write-Host "Sparse checkout of $sparsePath from $repoUrl (branch: $branch) completed successfully."
} catch {
Write-Error "An error occurred: $($_.Exception.Message)"
exit 1 # Indicate an error occurred
}
exit 0 # Indicate success