Skip to content

Instantly share code, notes, and snippets.

@mj-sakellaropoulos
Created July 10, 2024 02:12
Show Gist options
  • Save mj-sakellaropoulos/c09222f33038435999e373df1a25814a to your computer and use it in GitHub Desktop.
Save mj-sakellaropoulos/c09222f33038435999e373df1a25814a to your computer and use it in GitHub Desktop.
param (
[string]$frontendRepoUrl,
[string]$backendRepoUrl,
[string]$monorepoName = "monorepo"
)
# Step 1: Clone the repos
Write-Output "Cloning frontend repo..."
git clone $frontendRepoUrl frontend
Write-Output "Cloning backend repo..."
git clone $backendRepoUrl backend
# Step 2: Use git-filter-repo to create subdirectories
Write-Output "Processing frontend repo with git-filter-repo..."
cd frontend
& "C:\Program Files\Git\bin\bash.exe" -c "git-filter-repo --to-subdirectory-filter frontend"
cd ..
Write-Output "Processing backend repo with git-filter-repo..."
cd backend
& "C:\Program Files\Git\bin\bash.exe" -c "git-filter-repo --to-subdirectory-filter backend"
cd ..
# Step 3: Create a new empty repo called "monorepo"
Write-Output "Creating a new empty monorepo..."
mkdir $monorepoName
cd $monorepoName
git init
# Step 4: Add the previously processed repos as remotes to this monorepo
Write-Output "Adding remotes to monorepo..."
git remote add frontend ../frontend
git remote add backend ../backend
# Step 5: Fetch all branches from the remotes
Write-Output "Fetching all branches from frontend..."
$fetchFrontendOutput = git fetch frontend 'refs/heads/*:refs/heads/*' 2>&1
Write-Output "Frontend fetch output:"
Write-Output $fetchFrontendOutput
Write-Output "Fetching all branches from backend..."
$fetchBackendOutput = git fetch backend 'refs/heads/*:refs/heads/*' 2>&1
Write-Output "Backend fetch output:"
Write-Output $fetchBackendOutput
# Step 6: Keep a list of the rejected branches
Write-Output "Processing fetch results..."
$rejectedBranches = @()
$regex = [regex]::new('(?<rej>\[rejected\])\W*(?<branch>\S*)(\W*->)')
$fetchFrontendOutput -split "`n" | ForEach-Object {
if ($regex.IsMatch($_)) {
$matches = $regex.Match($_)
$rejectedBranches += $matches.Groups["branch"].Value
}
}
$fetchBackendOutput -split "`n" | ForEach-Object {
if ($regex.IsMatch($_)) {
$matches = $regex.Match($_)
$rejectedBranches += $matches.Groups["branch"].Value
}
}
Write-Output "Rejected branches:"
$rejectedBranches | ForEach-Object {
Write-Output $_
}
# Step 7: Merge all rejected branches
Write-Output "Merging rejected branches..."
foreach ($branch in $rejectedBranches) {
Write-Output "Merging branch $branch..."
git checkout $branch
git merge frontend/$branch --allow-unrelated-histories -m "Merged frontend/$branch into $branch"
git merge backend/$branch --allow-unrelated-histories -m "Merged backend/$branch into $branch"
}
Write-Output "Monorepo creation complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment