Merging Two Bitbucket Repositories

Posted on January 15, 2015

0


I have been using Atlassian Bitbucket for quite some time now, just because that it is free for private repos and up to 5 people. But i had a requirements to copy/move contents of one repo to another repo in bitbucket itself. Doing this was not straight-forward and this post is to capture what I had done to accomplish this:

  1. A bitbucket repo consists of Wiki repo + Source code repo + Tickets + Configurations + Change-sets. Not sure how the tickets are stored, may be in a database.
  2. First the source repo is a Mercurial repo (looks like I had chosen it to be mercurial (while creating the repo) and the target repo is git. So, I had to look for how to do the conversion from Mercurial to Git without losing the versioning & history information. There are a few ways ( here and here ) to do this which are nicely explained in this post, and I chose to use the hg-git extension of Mercurial, by using virtualenv & following commands:

    pip install hg-git
    pip install hg-mercurial
    echo “hgext.bookmarks =
    hggit =”>>~/.hgrc
    hg bookmark -r default master
    ../bin/hg push git+https://<url to hg repo>

  3. So, now we have a git repo, I had to rename some files to avoid clobbering. Git renames only links the new file name with the old file name history, you need to get the complete history by following the link (using –follow param to log). This was not showing up in the history tab of Bitbucket wiki page, but I wanted the history to show up in the wiki page tab. Hence, I used the instructions in this blog post which led me to use this script, and the following commands

    git-mv-with-history.sh <old_file_name>=<new_file_name>
    git log
    git commit -m “Renamed with history using git-mv-with-history.sh”
    git push
    git status
    git push –force origin master

  4. Now, the source git repo is ready with the changed file names, you need to use the add-fetch-branch-merge, as explained in this post, to merge the source repo to the target git repo. Specifically the commands are:

    git remote add src-repo
    git fetch src-repo
    git branch src-repo remotes/src-repo/master
    git merge src-repo

  5. If there are source code in the bitbucket source repo, you should repeat the above steps for the source code repos.
  6. Unlike github which uses the login username information as the committer details, bitbucket git repo takes from the local git configuration settings. If you have committed some changes without proper username, you can correct this using this script. There are other ways of doing this also.
  7. Tickets also need to be move. But there is no direct way of merging tickets without losing the creation/modification history of the issues in the source bitbucket repo. There is an open ticket (BB759) for this, but you can use this script which uses the bitbucket remote API.

If you want to retain the tickets history also, it should be possible to export just the issues from the source repo (in the admin screen: https://bitbucket.org/<username>/<source repo name>/admin/issues/import-export).  Do the same step of export issues for the destination repo. Then write a script which can merge the two issues file. The the merged file can be used to import the combined set of issues. If this works (I have not tried it yet) this is just a work-around until a permanent solution is provided for ticker BB759.

 

Posted in: Process