« Previous | Next»

Bzr2Git -- make a git repositpry out of a bazaar one

Posted by coldtobi | 1 Nov, 2009, 15:25

I have used bazaar on my Thecus for my private projects. At that time, git was not available on Windows platforms.

However, during the development of solarpowerlog,  I learned that is not longer true, and after some time of usage I decided to finally switch everything to git. (bzr also showed some "hangs" on my thecus when commiting which were really annoying....

Anzeige

 

Whatever, what i missed was a small script that will do the transfer of the repositories while keeping the history.

The script does:

  • copy all revisions from the bzr repository to git
  • keeps author and commit date
  • keeps the original commit message
  • extracts all set tags and set these to on the new git repository

It does NOT

  • handle branches and their merges.


Here is it: (download here: bzr2git)

 

	#!/bin/bash
	# bzr2git
	# (c) Tobias Frost 2009. Released under the GPL3. 
	# http://blog.coldtobi.de
	# This script takes a bazaar (bzr) repository and makes a git repository out of
	# it. The current dir should be the bzw one, the git one is created in 
	# new_git_repo.
	#
	# To commit to a different git branch, just remove the exit 1 below and before
	# invoking the script, create the branch manually in git and change to it. 
	# (note: this is untested, but should work)
	# NOTE: !!!ALWAYS USE BACKUPS!!! THIS SCRIPT MIGHT HAVE AN ERROR
	# WHICH TRIGGERS DOOMSDAY
	if [[ -e new_git_repo ]] 
	then
	echo "new_git_repo exits. Please remove it or edit this script"
	echo "alternativly, comment out the exit 1 below and go on on your own risk"
	echo "(you can then add branches to git...)"
	exit 1
	else
	# generate a git repository staging directory
	# make a empty git repository	
	mkdir new_git_repo || exit 1
	(cd new_git_repo && git init) || exit 1
	fi
	if [[ -e rev_to_merge ]] 
	then
	echo "rev_to_merge exits. Please remove it or edit this script"
	exit 1
	fi
	# ok, so lets start extracting the revs from bzr.
	for (( j=1; j<=99999 ; j++ )) 
	do	
	# Here you can pimp the revision to be checked out in order to manually merge 
	# some branch...
	# e.g i="32.1.$j" to get the branch 32.1.x 
	i=$j
	# check for log and determine if rev exists....
	bzr log --revision $i 2>/dev/null | tail -n +7 | cut -c3- > /tmp/revlog  || exit 1
	echo >>/tmp/revlog
	echo "original bzr status:" >>/tmp/revlog
	bzr log -r $i | tail -n +2 | head -n 4 >>/tmp/revlog
	#extract author and date
	AUTHOR=$(bzr log -r $i | grep ^committer: | cut -c12-)
	export GIT_COMMITTER_DATE=""
	export GIT_COMMITTER_DATE=$(bzr log -r $i | grep ^timestamp: | cut -c12-) 
	export GIT_AUTHOR_DATE=$GIT_COMMITTER_DATE
	echo $GIT_COMMITTER_DATE
	[[ "x$AUTHOR" == "x" ]] && AUTHOR="unkown"
	echo "Extracting rev $i"
	# see if there is a tag associated with this revision
	# sed: expr1 -> remove revision number
	#      expr2 -> remove trailing blanks
	#	   expr3 -> replace blanks with underscores 
	TAGS=$(bzr tags -r $i | sed -e "s/\(.*\).$i/\1/g" -e 's/[ \t]*$//' -e "s/\(.*\)/\'\1\'/g" -e "s/[[:blank:]]/_/g")
	if [[ "x$TAGS" != "x" ]] 
	then
	echo "TAGS detected."
	echo TAGS $TAGS || hexdump -C 
	fi
	bzr branch --revision $i . rev_to_merge || exit 1
	# make sure that we update the gitignore as well.
	rm -rf new_git_repo/* # remove old files or mv will complain
	mv -f rev_to_merge/.bzrignore new_git_repo/.gitignore
	mv -f rev_to_merge/* new_git_repo/ || exit 1
	# do the commit
	( cd new_git_repo && git add -A ) || exit 1
	( cd new_git_repo && git commit --author "$AUTHOR" -F /tmp/revlog ) || exit 1
	if [[ "x$TAGS" != "x" ]] 
	then
	for t in $TAGS
	do
	echo "CREATING TAG $t"
	( cd new_git_repo && git tag -a $t -m "Autoimported tag $t from bzr repository" ) || exit 1
	done
	fi
	# remove the staging dir 
	rm -rf rev_to_merge || exit 1
	done
	
	

Blog and Website | Comments (2) | Trackbacks (0)

Related Articles:

2 Comments | "Bzr2Git -- make a git repositpry out of a bazaar one" »

  1. Lukáš Lalinský :

    02/11/2009, at 11:07 [ Reply ]

    You could have used bzr-git to convert the repository. It would correctly handle merges, without any work on writing the script.

    It's as simple as (after installing dulwich and bzr-git):

    cd /tmp/git-repo
    git init
    cd /path/to/bzr/branch
    bzr dpush /tmp/git-repo

  2. coldtobi : Re:

    02/11/2009, at 21:32 [ Reply ]

    Thanks for the info...

    Tried it but bzr-git fails to do the job an fails with a fatal error...
    The repository is created until, but the branches are not expanded.

    bzr dpush ../git/
    bzr: ERROR: exceptions.AttributeError: 'InterRepository' object has no attribute 'fetch_objects' (..)

    *** Bazaar has encountered an internal error. This probably indicates a
    bug in Bazaar. You can help us fix it by filing a bug report at
    https://bugs.launchpad.net/bzr/+filebug
    including this traceback and a description of the problem.

Add comment

 

 This is the ReCaptcha Plugin for Lifetype

Due to German legislation, all comments are moderated. If you get NO error message, your comment is accepted by the system and will be released at the earliest opportunity. Sorry for the inconvenience this might cause.

Inappropiate comments might be edited or not accepted.