How to migrate svn to gitlab with full history logs

When migrating the source codes from svn to gitlab, we also would like to consider the svn logs migration. Recently we are working on the migration from svn with full history logs, and i would like to share some of the experiences.

Prerequisite

This article only applies to the migrations that, each project in svn will be migrated to gitlab with specified project space, take an example, the projectA in svn will be migrated to http://your/gitlab/url/yourgr...
Otherwise, the operations described here may not be served for your purpose.

Typical scenarios

There are three typical scenarios with tools to complete.

Standard svn layout

If you are not quite familiar with the svn standard layout, come to this page for reference http://svnbook.red-bean.com/e...

In summary, it should have similar form as:
$repo/paint/trunk
$repo/paint/branches
$repo/paint/tags
$repo/calc/trunk
$repo/calc/branches
$repo/calc/tags

No standard layout but the path never changed

For example, the svn repo has consistent hierarchy
$repo/pkg/paint
$repo/pkg/calc
$repo/branches/paint-skeleton
$repo/branches/paint-hotfix

$repo/tags/paint/v0.1.0
$repo/tags/paint/v0.1.2
$repo/tags/calc/v0.1.1

No standard layout and the path was changed before

For example, before the svn repo has hierarchy as:
$repo/pkg/paint
$repo/pkg/calc suite/calc
$repo/branches/paint-skeleton
$repo/branches/paint-hotfix

Later on, the svn repo changed the organization:
$repo/trunk/paint
$repo/trunk/calc suite/calc
$repo/tags/paint-skeleton
$repo/tags/paint-hotfix

In summary, the original ‘pkg’ was renamed to ‘trunk’; the original ‘branches’ was renamed to ‘tags’; and no ‘branches’ exists any more.

Standard layout Migration

Standard layout migration is relatively simple; here I recommend using subgit.
The subgit is a tool, which is portable and no installation steps. Its official download site is https://subgit.com/download.html

  1. Choose any clean folder as your operation root directory; we call this dir as $root
  2. In your local svn project directory which you want to migrated, execute command , to get the authors

    svn log | awk '($0 ~ /^r/) {print $3}' | sort |uniq
  3. Switch back to $root, add a new file authormap.txt; this file should contain all mappings between svn(before the equal sign) and gitlab(after the equal sign) authors you listed in step2, example:

    Nicolas wang = xiaoqiang.wang [email protected]
  4. Execute command in $root

    path/to/subgit-3.2.2/bin/subgit import --non-interactive --default-domain YOUR_DOMAIN --authors-file authormap.txt --trunk trunk --tags tags --branches branches --username SVN_USERNAME --password SVN_PASSWORD --svn-url http://svn.example.com/path/to/repo repo.git

    If any error occurred during the migration and the process was interrupted, execute ‘subgit import repo.git’ to recover

  5. Clone a bare repo and remove the useless svn metadata

    git clone --bare repo.git repo-clone.git
  6. Change to directory repo-clone.git

    cd repo-clone.git
  7. Push local git repo to remote gitlab repository

    git remote add gitlab  http://gitlab.example.com/path/to/repo.git
    git push gitlab --all
    git push gitlab --tags

No standard layout migration and the path was changed before

This is the key part of this article. Here we could not use subgit anymore, because this tool will only migrate the logs after renaming pkg->trunk and branches->tags. The logs generated in pkg or branches before, will not be migrated.
What we used here is git svn commands.

  1. Choose any clean folder as your operation root directory; we call this dir as $root
  2. In your local svn project directory which you want to migrated, take the calc project mentioned above as an example, execute command below to get the authors

    svn log | awk '($0 ~ /^r/) {print $3}' | sort –u
  3. Switch back to $root, add a new file authormap.txt; this file should contain all mappings between svn(before the equal sign) and gitlab(after the equal sign) authors you listed in step2, example:

    Nicolas wang = xiaoqiang.wang [email protected]
  4. Also in $root, execute the command

    git svn clone --no-minimize-url --trunk "trunk/calc suite/calc" --authors-file authormap.txt "http://svn/repo/path/" new.git
  5. Then execute the command

    git svn clone --no-minimize-url --trunk "pkg/calc suite/calc" --authors-file authormap.txt "http://svn/repo/path/" old.git

    The only difference compared to step4, is step5 points to the clone of objects before renaming.

  6. Change to directory old.git, execute command and get the last commit’s SHA in log history, such as 56a0f2578fce65a85436453f37535f416a3c6c07

    git log
  7. Change to directory new.git, execute command and get the first commit’s SHA in log history, such as 63c91bddc647588a3eccf6102df2a4146d447629

    git log
  8. Also in new.git, execute commands

    git remote add oldstuff ../old.git
    git fetch oldstuff
  9. Then execute commands

    git replace [first-commit-sha-from-new.git] [last-commit-sha-from-old.git]
  10. Rewrite the SHA hashes so everything is kosher and you no longer need the replace ref

    git filter-branch
  11. Clean up the no-longer-needed references

    git remote rm oldstuff
    rm -rf .git/refs/replace
  12. Removing git-svn-id messages

    git filter-branch -f --msg-filter 'sed -e "/git-svn-id:/d"'

    Explanation: When migrating from Subversion, every commit messages has a git-svn-id line appended to it like this one: "git-svn-id: http://svn/repo/url/trunk@9837 1eab27b1-3bc6-4acd-4026-59d9a2a3569e". If you are planning on migrating away from your old Subversion repository entirely, there’s no need to keep these.

  13. Removing empty commit messages

    git filter-branch -f --msg-filter '
    read msg
    if [ -n "$msg" ] ; then
        echo "$msg"
    else
        echo "<empty commit message>"
    fi'

    Explanation: Subversion allows empty commit messages, but Git does not. Any empty commit messages in your newly migrated git repository should be replaced so commands like git rebase will work on these commits.

  14. Push local git repo to remote gitlab repository

    git remote add gitlab http://gitlab.example.com/path/to/repo.git
    git push gitlab --al
    git push gitlab --tags

    The only drawback of this solution, is that it will overwrite the first commit of new.git using the last commit of old.git, so the first commit of new.git will lose.

No standard layout migration and the path was never changed

Either of the above two scenarios could cover this topic, it depends on the actual situation which tool (subgit or git svn) you should use, and it will not be extended here

References

http://treyhunner.com/2011/11...
http://blog.woobling.org/2009...
https://stackoverflow.com/que...
http://www.jianshu.com/p/29f7...
https://subgit.com/import-boo...
https://groups.google.com/for...

相关推荐