User:Dantman/Migrating from SVN to Git

From mediawiki.org

Some drafts for changes to Migrating from SVN to Git.

Safely migrating your release checkout (branches/REL#_## or tags/REL#_##_*)[edit]

Make a backup of your files (doing your database aswell wouldn't be a bad thing)!

Before starting with your conversion you should run svn status and review how your wiki's checkout differs from the codebase.

You can ignore all the lines starting with a ?, especially the ones about things like extensions/, .htaccess files, and sitemaps. However if you see any M (modified) lines you will probably want to run svn diff and review your modifications. If you have any modifications you will likely later either be dropping them or making them part of your git repo.

At this point we want to make sure that we are at a common point between svn and git that we can safely leap between expecting the whole file tree to be exactly the same. Check your svn info; If you are on tags/ you are in a good state. However for the many of us who have checkout out branches/ it's time to switch to a tag. branches are variable, and it is liable that the git branch we check out will have a different file tree which will cause migration issues.

If you're on a branches/ checkout follow these steps to switch to a tag:

  1. Take a look at your wiki's Special:Version and check precisely which version you have checked out.
  2. Look through http://svn.wikimedia.org/svnroot/mediawiki/tags/ and find a REL tag that matches your version. (note that you will be switching to the phase3/ inside of that) eg: If you're on 1.18.0 you'll want to check out tags/REL1_18_0/phase
  3. Run a svn switch to the phase3 that tag. eg: If you're using anonymous http checkout an running 1.18.0 you'll use svn switch http://svn.wikimedia.org/svnroot/mediawiki/tags/REL1_18_0/phase3/.
  4. If all things went well theoretically svn won't even bother having to modify any files. Whatever the case as long as you used the right tag things should turn out fine.

Now that you're on a tag we can start migration to git.

  1. Turn your wiki into a blank git repository using git init
  2. To avoid .svn cluttering your git status add .svn to your repo's private ignore/exclude list using echo '.svn/' >> .git/info/exclude
  3. Add mediawiki/core.git as a the remote 'origin' for your repository: (use -f --tags to have git fetch the commits and tags)
    • Anonymously: git remote add -f --tags origin https://gerrit.wikimedia.org/r/mediawiki/core.git
    • Labs account (only if you plan to commit and have ssh ready): git remote add -f --tags origin ssh://<USERNAME>@gerrit.wikimedia.org:29418/mediawiki/core.git
  4. Wait for git to download all the commits. This will take a few minutes but you will only need to wait for this once.
  5. Do a soft reset to the tag that matches your svn tag:
    • Our git tags use the normal '.' format, so for tags/REL1_18_0 you'll want the tag '1.18.0'
    • Soft reset to that tag: git reset --soft <tag>
    • eg: For tags/REL1_18_0/phase3 you would use git reset --soft 1.18.0

At this point we have a repository with a working tree tied to a specific release but with a working copy that contains any modifications left over. At this point our working tree undoubtedly contains modifications from the git version we are paired with. At the very minimum there are many $Id$ changes from your svn checkout inside includes/api/ which you need to have dropped.

  1. Clear the svn '$Id: ...$'s from MW using the following command:
    • find includes/ maintenance/ \( -path '*/.svn' -prune \) -o \( -type f -exec sed 's/\$\(Id\|LastChanged\(Revision\|Date\)\):.\+\$/$\1$/' -i '{}' ';' \)
  2. You will have some leftover modifications to clear out:
    • Run git checkout -p HEAD you will be asked what modifications you wish to discard:
      • If you see a diff comparing something like $Id: ApiUpload.php 51812 2009-06-12 23:45:20Z dale $ and $Id$ use "y" to discard it (if you're asked "Apply them to the worktree anyway?" use another "y")
      • Same if you are sure you do not want the modification anymore.
      • If it's a modification you wish to keep answer "n".