Skip to content

Instantly share code, notes, and snippets.

@JimboFromLimbo
Last active April 17, 2023 01:41
Show Gist options
  • Save JimboFromLimbo/21a5eaec6de50cf250b26e0154029063 to your computer and use it in GitHub Desktop.
Save JimboFromLimbo/21a5eaec6de50cf250b26e0154029063 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
set -euo pipefail ${DEBUGSH+-x}
# List your repositories' URLs here, one per line
repositories=(
"https://github.com/user/repo1.git"
"https://github.com/user/repo2.git"
)
# Ensure the "packages" directory exists
mkdir -p packages
# Loop through the repositories and pull them into the MONOREPO
for repo_url in $repositories; do
repo_name="$(basename "$repo_url" .git)"
target_dir="./packages/$repo_name"
if [[ -d "$target_dir" ]]; then
>&2 echo "Target directory $target_dir already exists. Skipping."
continue
fi
# Clone the repository into the target directory, preserving history
git clone "$repo_url" "$target_dir"
# Enter the target directory
cd "$target_dir"
# Move all repository files and folders into a new "packages" folder
git filter-branch --prune-empty --tree-filter '
mkdir -p packages
find . -maxdepth 1 ! -name ".git" ! -name "packages" -exec mv {} packages/ \;
'
# Move back to the main MONOREPO root
cd - > /dev/null
# Add the cloned repository as a remote
git remote add "$repo_name" "$target_dir"
# Fetch the remote repository, preserving tags
git fetch "$repo_name" --tags
# Merge the remote repository into the main MONOREPO
git merge --allow-unrelated-histories -m "Merge $repo_name into MONOREPO" "$repo_name/master"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment