git and cvs turnaround ‘guerilla style’

2 Jul
2009

sorrycharlie

This is about some real-life experience using git (for developing) and cvs (because you have to). Why? Because git is faster, easier, you are no longer dependent on a server and it’s just cool to leave stone age. I’m so sorry, Charlie ;)

Preparations

I assume you have a working environment where git, cvs, cvsps is installed. (use cygwin, macports, apt-get, …) If you access your cvs repository over ssh (:ext:), you have to set-up a public key for ssh-agent (git-cvsimport wants to log in without password request, and you’ll love it, too).

Create a git repository from cvs

If you are like me and connect via ssh, you should add this line to your ~/.bashrc – you’ll need it every time you make a rebase. (Or just export the variable for now)

export CVS_RSH=ssh

Next, cd into the directory where you want your git repo.

export CVSROOT=:ext:username@server:/directory/dir/dir
mkdir myproject.git
git cvsimport -v -r cvs -k -d $CVSROOT myproject

You can read all git cvsimport-options on the manpage. The initial import may take a while, so get a coffee while it’s working.

Tweaking the Config

Setup some more cvs infos for git. (Type this in the console and it goes straight to .git/config – you can edit there anytime)

git config cvsimport.module cvs_module_to_checkout
git config cvsimport.r cvs
git config cvsimport.d $CVSROOT

Incremential Imports

Make sure you are in the git root dir and on the master branch.

git cvsimport

Code in git!

Work in your git repo. Always use a branch & checkin often. (see: The Git development cycle)

Get the changes back into CVS

You wanna be nice to Charlie? git cvsexportcommit is your friend. It exports A SINGLE commit back to cvs.

Before you export commits, you need a fresh cvs directory of your project. So find a convenient location and checkout in cvs:

cvs init
cvs co myproject

Then tell git where this checkout can be found (use pwd to get the full dir)

# within your git project directory
git config cvsexportcommit.cvsdir /path/to/your/cvs/checkout/withmodule/

Now we prepared everything. Note, as of git 1.5.6 you can combine both your git and your cvs directory with the option -W. Read this for more info.

To export a single commit from git->cvs you can use this:

git cvsexportcommit -u -p -c 987654 (your sha-1 commit id)

But you committed often, didn’t you? If you developed your feature in a branch and are about to merge, you can do it this way:

git checkout master
git merge --no-ff --log -m "Optional commit message here" topic/branch/name
git cvsexportcommit -u -p -c ORIG_HEAD HEAD

Your branch is now merged and you can delete it with

git branch -D new_branch

Need more Information? Stackoverflow has an excellent article!

Update: You can automate this with a script. I put it in /opt/local/bin/gce

#!/bin/sh
#
# Export a branch from git to CVS.
if test -z "$1"; then
echo Incorrect number of arguments
exit 1
fi

git checkout master
git merge --no-ff --log $1
git cvsexportcommit -u -p -c ORIG_HEAD HEAD

Make it executable:

sudo chmod /opt/local/bin/gce +x

Now you can commit that way:

gce my_branch

Related posts:

  1. Git Aliases for bash

Comment Form

top

Switch to our mobile site