Skip to content

Instantly share code, notes, and snippets.

@martinjlowm
Created May 21, 2019 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinjlowm/e51db64795b71d7dedcc19b191340bc0 to your computer and use it in GitHub Desktop.
Save martinjlowm/e51db64795b71d7dedcc19b191340bc0 to your computer and use it in GitHub Desktop.
Migrate a repository to a mono repo
monofyRepo() {
repository=$(basename $PWD)
if [[ ! -d ".git" ]]; then
echo "! $repository is not a git repository."
return
fi
echo "» Monofying repository ($repository)...\n"
echo "» Make sure all branches are merged into master"
git checkout master > /dev/null 2>&1
if [[ "$?" != "0" ]]; then
echo "! Failed to checkout master"
return
fi
while true; do
echo "» Enter the new name of the subdirectory for this project:"
printf "> "
read newName
if [[ "$newName" =~ ^[0-9a-zA-Z_-]+ ]]; then
mkdir -p "$newName"
break
else
echo "! The `$newName` must be a valid directory name.\n"
fi
done
echo "» Moving all files into $newName/"
ls -a1 | grep -v "^$newName" | xargs -I{} git mv {} "$newName"
git commit -m "Moved repository into $newName/"
echo "» Change directory to the monorepo and run:"
echo " git remote add $newName $PWD"
echo " git fetch $newName"
echo " git merge -m \"[Mono] Added $newName\" --allow-unrelated-histories $newName/master"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment