Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# This script takes a remote repository and merges it into
# the current one as a subdirectory
set -e
# REPOS=('design-system-components' 'design-system-tokens' 'design-system-figma-scripting')
# REPOS=('flight')
REPOS=('design-system-tokens')
for repo in "${REPOS[@]}"
do
REPO_REMOTE="https://github.com/hashicorp/${repo}"
REPO_DIR_TMP="$(mktemp -d -t "${repo##*/}.XXXX")"
REPO_NAME=$repo
echo "REPO REMOTE: $REPO_REMOTE"
echo "REPO NAME: $REPO_NAME"
echo "REPO TMP DIR: $REPO_DIR_TMP"
echo
# clone other repo
git clone "$REPO_REMOTE" "$REPO_DIR_TMP"
# rewrite the entire history into sub-directory
export REPO_NAME
(
cd $REPO_DIR_TMP &&
git filter-repo --to-subdirectory-filter "packages/"
)
# merge the rewritten repo
git remote add "$REPO_NAME" "$REPO_DIR_TMP" -f
git fetch "$REPO_NAME"
# if you're running an older version of git, remove --allow-unrelated-histories
git merge --allow-unrelated-histories "$REPO_NAME/main" -m "Merge ${REPO_NAME} into design system monorepo"
# loop through all remote branches and create local copies
for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/${REPO_NAME} --no-merged); do
git checkout -b "${branch}" "${branch}"
done
# return to main
git checkout main
# delete the rewritten repo
rm -rf "$REPO_DIR_TMP"
git remote rm "$REPO_NAME"
done
@amyrlam
Copy link

amyrlam commented Mar 4, 2022

git filter-repo isn't a git command out of the box. Install brew install git-filter-repo via Homebrew re: https://superuser.com/a/1646987. Another reference: https://github.com/newren/git-filter-repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment