Skip to content

Instantly share code, notes, and snippets.

@ErgEnn
Last active August 14, 2023 14:14
Show Gist options
  • Save ErgEnn/9b58063b80d0bf1ce7478e267fc9e94e to your computer and use it in GitHub Desktop.
Save ErgEnn/9b58063b80d0bf1ce7478e267fc9e94e to your computer and use it in GitHub Desktop.
Powershell scripts to manage per ticket worktrees of multiple repos

Expects following folder structure

/repo1.git
/repo2.git
/TEMPLATE
         /repo1
               /some_file_you_want_to_overwrite
               /post_new_branch.ps1
/tickets
        /some_ticket_name
                         /repo1
                               /some_file_you_want_to_overwrite
                         /repo2

post_new_branch.ps1 will be run in ticket dir relative to template root after copy

When using bare repos make sure that origin is configured: git config --local --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

try {
$ticketNumber = Read-Host "Enter ticket number to delete"
Get-ChildItem -Filter *.git |
ForEach-Object {
$gitPath = $_.FullName
$gitName = $_.Name.Replace(".git", "")
& git --git-dir=$gitPath worktree remove "tickets/$ticketNumber/$gitName" --force
& git --git-dir=$gitPath branch -d $ticketNumber
}
}
catch {
Read-Host "Error occurred"
}
try {
$ticketNumber = Read-Host "Enter ticket number"
New-Item -ItemType Directory -Force -Path "tickets\$ticketNumber"
$initialLoc = Get-Location
Get-ChildItem -Filter *.git |
ForEach-Object {
$gitPath = $_.FullName
$repoPath = $_.Name.Replace(".git", "")
Write-Output "Fetching master"
& git --git-dir=$gitPath fetch origin +refs/heads/*:refs/heads/*
Write-Output "Creating worktrees for $gitPath"
& git --git-dir=$gitPath worktree add -b $ticketNumber "tickets\$ticketNumber\$repoPath" origin/master
Set-Location "$gitPath\..\tickets\$ticketNumber\$repoPath"
# & git pull origin master
& git submodule update --init --recursive
& git branch --unset-upstream
Set-Location $initialLoc
}
$baseDir = Join-Path -Path $PWD -ChildPath "TEMPLATE"
$ticketDir = Join-Path -Path $PWD -ChildPath "tickets\$ticketNumber"
$itemsToCopy = Get-ChildItem -Path $baseDir -Recurse -Exclude "post_new_branch.ps1"
foreach ($item in $itemsToCopy) {
$destinationPath = Join-Path -Path $ticketDir -ChildPath ($item.FullName.Substring($baseDir.Length + 1))
Write-Host "Copying $($item.Fullname) to $destinationPath"
if ($item.PSIsContainer) {
if (!(Test-Path -Path $destinationPath -PathType Container)) {
New-Item -Path $destinationPath -ItemType Directory | Out-Null
}
}
else {
Copy-Item -Path $item.FullName -Destination $destinationPath -Force
}
}
$scriptsToRun = Get-ChildItem -Path $baseDir -Recurse -Filter "post_new_branch.ps1" -File
foreach ($script in $scriptsToRun) {
$scriptDir = Join-Path -Path $ticketDir -ChildPath ($script.Directory.FullName.Substring($baseDir.Length + 1))
Write-Host "Running script $($script.FullName)"
Set-Location -Path $scriptDir
& $script.FullName
}
Set-Location -Path $PSScriptRoot
}
catch {
Write-Host ($_ | ConvertTo-Json)
Read-Host "Error occurred"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment