Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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