Last active
February 1, 2020 03:43
-
-
Save mhenrixon/9686828 to your computer and use it in GitHub Desktop.
How we merged multiple github repos into a monolithic project repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This script merges all the repos we have into subfolders in a new repo | |
# | |
# Clones or copies a repository from either disc or remote repository. | |
cloppy() { | |
local dir=$1 | |
if [[ -z $2 ]]; then | |
local repo=$dir | |
else | |
local repo=$2 | |
fi | |
if [[ -d ${dir}_bkp ]]; then | |
cp -R ${dir}_bkp $dir | |
else | |
git clone [email protected]:casinosaga/casino-saga-$repo.git $dir | |
cp -R $dir ${dir}_bkp | |
fi | |
} | |
# Merges all the remote branches into sub folders | |
merge_repository() { | |
local project=$1 | |
reorganize_branch | |
cd ~/code/casino-saga | |
git remote add $project ~/code/$project | |
merge_repository_branches | |
} | |
# Merge all branches from one repository into the same branch name in the destination repository | |
merge_repository_branches() { | |
cd ~/code/$project | |
local -a branches=( $(git branch | sed "s/\*/ /" ) ) | |
echo "XXX Operate in $project on branches: ${branches[@]}" | |
cd ~/code/casino-saga | |
git fetch $project | |
for branch in ${branches[@]} | |
do | |
create_branch $branch | |
echo "XXX reset" | |
git reset --hard | |
echo "XXX git fetch $project" | |
git fetch $project | |
echo "XXX git merge $project/$branch" | |
git merge $project/$branch -m "Merge history for $project/$branch into new repo" | |
done | |
} | |
# Create a new EMPTY branch or checkout existing | |
create_branch() { | |
echo "XXX Try creating orphaned branch $branch" | |
git checkout --orphan $branch | |
if [[ $? != 0 ]]; then | |
echo "XXX $branch existed. Switch to it." | |
git checkout $branch | |
fi | |
} | |
# Moves all files in an existing repository into a new folder structure | |
reorganize_branch() { | |
cd ~/code/$project | |
git fetch --all | |
local -a repo_branches=( $(git branch -r | tail -n+2 ) ) | |
for repo_branch in ${repo_branches[@]} | |
do | |
real_b=${repo_branch/origin\//} | |
echo "XXX moving $project/$real_b into new structure" | |
git checkout $real_b | |
mkdir -p $project | |
move_files_into_subfolder | |
git commit -am "Prepare $project/$real_b for new structure (move files)" | |
done | |
} | |
# Moves all files in an existing repository into a new subfolder named | |
# the same thing as the project we are working on. | |
move_files_into_subfolder() { | |
local -a files=( $(ls -a) ) | |
for file in ${files[@]} | |
do | |
if [[ "$file" != "$project" && "$file" != ".git" && "$file" != ".." && "$file" != "." ]]; then | |
git mv $file ./$project/ | |
fi | |
done | |
} | |
# 1. Remove all old working directories | |
rm -rf ~/code/casino-saga/ | |
rm -rf ~/code/api/ | |
rm -rf ~/code/core/ | |
rm -rf ~/code/web/ | |
rm -rf ~/code/admin/ | |
rm -rf ~/code/docs/ | |
# 2. Clone the destination repository that will hold all the merged repos. | |
cd ~/code | |
git clone [email protected]:casinosaga/casino-saga.git | |
# 3. Clone or copy all the projects to merge into the master repo | |
cloppy core shared | |
cloppy api | |
cloppy admin backoffice | |
cloppy web | |
cloppy docs | |
# 4. Loop through all the projects and merge them one by one | |
projects=(core api admin web docs) | |
for project in ${projects[@]} | |
do | |
merge_repository $project | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment