Creating Lib_ALL repository from other repos

So it was decided to have all of TeamMentor libraries in separate Github repositories. While it may keep organization and issue tracking for each library at a manageable state, quickly we realized that you need one repository that would contain all the libraries. With power of git and some shell scripting I came up with a single script that takes a list of libraries and creates a single repository called Lib_ALL. It uses git subtree merge and combines all the libraries into the single repository preserving all commit history. Here is how it works.

1. You need to setup ssh access to github. It is possible to do this without ssh access, however since there is no easy way of supplying a password to git on the command line, there would be an awful lot of typing of the password. Use these instructions to set up ssh access, run the test at the end of the instructions to make sure it works. https://help.github.com/articles/generating-ssh-keys

2. Create a directory somewhere, call it mud, or foo, the repository will be created as “Lib_ALL” subdirectory under the directory you create.

3. Copy and paste all the gitub repositories you are going to merge and save them into a Libs.txt file in the directory you created.

4. Copy this script to a file with a meaningful name in the same directory

if [[ -d Lib_ALL ]]
then
   echo "Git repository exists, no good dude(te)."
   exit
fi
mkdir Lib_ALL
cd Lib_ALL

git init
touch .gitignore
git add .gitignore
git commit -m "initial commit" 
awk -F_ '{print $2}' ../Libs.txt | while read LINE
do
git remote add -f $LINE git@github.com:TMContent/Lib_$LINE.git
if [[ $? -ne 0 ]]
then
   echo "Failed to add remote for repo $LINE"
   exit
fi
git merge -s ours --no-commit $LINE/master
if [[ $? -ne 0 ]]
then
   echo "Failed to merge repo $LINE"
   exit
fi
git read-tree --prefix=$LINE/ -u $LINE/master
if [[ $? -ne 0 ]]
then
   echo "Failed to read-tree for repo $LINE"
   exit
fi
git commit -m "$LINE Library merged"
if [[ $? -ne 0 ]]
then
   echo "Failed to commit repo $LINE"
   exit
fi
done

5. Open git bash in that directory and run the script.

WARNING: If the script fails for any reason, it will abort the whole process leaving you with a bunch of useless files everywhere. If that happens it is recommended to start over.

There will be a boatload of messages going through

.

But it would be all cool at the end.


6. That’s about. Next is to create a script that pulls all the changes and updates the repo and pushes the updates to github.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s