Shell script to simplify Drupal CVS module checkouts

Feb 24 2009

I am finally fed up enough with the multi-step process for checking out a Drupal contrib module that I created a simple shell script to do the dirty work for me. This script is named cvscheckoutmodule.sh (imaginative, huh?). In hopes that this might be of interest/use to others... here it is. <!--break-->

#!/bin/bash
#####################################################################
# This script checks out a contrib module.
# It sets up the environment first, and then does a CVS login and 
# a CVS checkout.
#####################################################################

if [[ $# == 0 ]]; then
  echo "Usage: $0 module_name [branch_or_tag]"
  echo " "
  echo "  module_name: "
  echo "    Module name must be the name of the module  in Drupal's CVS repo."
  echo "  branch_or_tag: "
  echo "    If no branch/tag is specified, HEAD will be used."
  echo " "
  echo "Example: "
  echo "  $0 cacheexclude DRUPAL-6--2"
  echo " "
  exit 1;
fi

# This directory will be created, and then used to house the 
# new code.
dest="./$1/$2"

mkdir -p $dest
cd $dest

export CVSROOT=:pserver:$DRUPAL_USERNAME@cvs.drupal.org:/cvs/drupal-contrib
cvs login

TAG='HEAD';
if [[ $# > 1 ]]; then
  TAG=$2;
fi

echo "Checking out $1 with revision $TAG"
cvs -z6 checkout -r $TAG -d $1 contributions/modules/$1

(The code above is public domain. Use it however.)

Make sure to set $DRUPAL_USERNAME to your username.

Running this script is as easy as this:

$ ./cvs_checkout_module.sh cacheexclude DRUPAL-6--2

This will check out the DRUPAL-6--2 branch of the cacheexclude module.

How the code works

The code above does the following:

  • It creates a new directory of the form ./MODULE_NAME/TAG. For example, ./cacheexclude/DRUPAL-6--2. This allows you to conveniently keep multiple versions of a module in the same place.
  • It logs into the Drupal CVS server. Again, make sure that $DRUPAL_USERNAME is set.
  • It checks out the new module into the appropriate directory.

When this is done, you should have a directory layout looking like this:

./cacheexclude/
  DRUPAL-6--2/
    cacheexclude/
      cacheexclude.module
      cacheexclude.info
      # etc.

Checking out multiple branches of the same project Need to grab another branch? You can run the same command again, passing a different branch:

$ ./cvs_checkout_module.sh cacheexclude DRUPAL-5--2

That will give you a directory layout that looks like this:

./cacheexclude/
  DRUPAL-6--2/
    cacheexclude/
      cacheexclude.module
      cacheexclude.info
      # etc.
  DRUPAL-5--2/
    cacheexclude/
      cacheexclude.module
      cacheexclude.info
      # etc.

From inside of each of branch directories, you can use CVS as usual.