środa, 23 lipca 2014

Convert SVN repo to GIT repos. Ready-to-use scripts.


Finally, I managed move all my micro-projects from one huge SVN repo to multiple GIT repos. My micro-projects (about 100+) are generally SQL/Crystal reports, SQL procedures and triggers, ad-hoc SQL queries (maybe useful in future) and some of C# and MVC3 projects also.

I have spent a lot of time to make my mind how should all those micro-projects be organized as a GIT repo or repos.

There were arguments to have all of those projects in one monolith GIT repo (as it was in SVN):
  • When I clone my monolith GIT repo I have all my micro-projects cloned (and backed up this way);
  • It's easier to track my past activity reading one log than hundred of them;
  • Some of micro-projects depends on other (they were linked in SVN using externals).
And there were arguments not to have those project in one monolith GIT repo:
  • Some of projects should be moved to an attic (I like CVS idea of having an attic);
  • SVN repo was not a regular structure (some of micro-projects had trunk/tags/branches structure, some of them had not, some of them was organized into common folder which had trunk/tags/branches structure) - converting this irregular may be a headache ass-ache.
Finally, I decided to have multiple GIT repositories. And also to break sets of micro-projects with common trunk/tags/branches structure into individual GIT repos. Then I moved all GIT bare repos to company shared folder where I clone from. Shared folder is backed onto Ultrium tape everyday.

So I have following cases in one monolith SVN repo:

Case 1 - SVN structure per project

https://svn.mycompany.local/svn/main/client1/project1/trunk --> project1.git
https://svn.mycompany.local/svn/main/client1/project2/trunk --> project2.git

Case 2 - common SVN structure for set of projects

https://svn.mycompany.local/svn/main/client1/set-of-projects1/trunk/project3 --> project3.git
https://svn.mycompany.local/svn/main/client2/set-of-projects2/trunk/project4 --> project4.git

Case 3 - no SVN structure

https://svn.mycompany.local/svn/main/client1/project5 --> project5.git
https://svn.mycompany.local/svn/main/client1/project6 --> project6.git

I did succeed to convert all above cases using linux scripts I want to share you with.

Download / clone the tool

You may download / clone them from my GitHub: https://github.com/htrzewik/svn-repos-to-git-repos

Description of the tool

There are two scripts. They are converting one project at once.
rebuild-repos-svn-layout.sh is for projects with SVN trunk/tags/branches structure (like case 1 described earlier)
rebuild-repos-flat-layout.sh is for projects with no SVN trunk/tags/branches structure (like case 3 and case 2 also)

and three config files:

authors - it's dictionary of developers who worked over SVN repos. IT'S VERY IMPORTANT THAT AUTHORS DICTIONARY IS COMPLETE. Other case history of project may be truncated - you will lost most recent commits, not the oldest. Ask uncle Google for SVN trick to list all authors in SVN repo.

default_svnbase - put there an URL of your repo with subdirs (or put that URL into argument 2 every time you run script)

default_workir - put there a path to workdir created for this script (temporary repos will be stored there)

Examples of use

Example for case 1

Put https://svn.mycompany.local/svn/main/client1 into default_svnbase

Run ./rebuild-repos-svn-layout.sh project1

If script succeeded move svn2git_workdir/git-bare/project1.git to your GIT server / GIT shared folder.
BE CAREFUL OF WARNING: You appear to have cloned an empty repository. Review you URL, project name, authors list.

Run ./rebuild-repos-svn-layout.sh project2

Move svn2git_workdir/git-bare/project2.git

Example of case 2

Put https://svn.mycompany.local/svn/main/client1/set-of-projects1/trunk into default_svnbase. Notice: tags and branches will be lost (not converted).

Run ./rebuild-repos-flat-layout.sh project3
Move svn2git_workdir/git-bare/project3.git
Run ./rebuild-repos-flat-layout.sh project4
Move svn2git_workdir/git-bare/project4.git

Example of case 3

Put https://svn.mycompany.local/svn/main/client1 into default_svnbase.

Run ./rebuild-repos-flat-layout.sh project5
Move svn2git_workdir/git-bare/project5.git
Run ./rebuild-repos-flat-layout.sh project6
Move svn2git_workdir/git-bare/project6.git

How it works

What scripts exactly do?

1. Script clones SVN repo into local GIT repo using git svn clone command.
2. Creates target GIT bare repo and does refs trick.
3. Configures local GIT repo where SVN was cloned into and add target GIT bare repo as remote.
4. Pushes SVN cloned repo into target GIT bare repo.
5. Renames trunk branch into master trunk (only script for SVN style repo).
6. It does a test clone into temporary GIT repo.

After script complete you may check svn2git_workdir/git-test-workdir/project.git if project files exists and if commit log seems complete. 

Now, you have no excuse for not switching to GIT.