Configure Drush in Your Git Repository
One of the neat features of Drush is it’s configurability. Setting the defaults for various behaviors and command options allows you to build really simplified, specialized workflows. You can create personal settings defaults by dropping a `drushrc.php` file in one of the places Drush will look, such as inside a .drush folder of your home directory.
It turns out this little capability has provided some very simple wins for developers at GoingOn. We pass around our projects and sites as a subdirectory of a Git repository that contains various things, such as documentation, deployment tools, and drush commands. Unfortunately, this slightly irregular practice disconnects Drush from the Drupal site and excludes those drush commands from the standard directories drush scans.
We fix this by making Drush aware of the Git Repo. There is a command-line script you can use to check if you are anywhere in a Git repository. By configuring Drush to check for a Git Repo on every drush bootstrapping, we give Drush a chance to pick up a bonus, repository-specific Drush config file.
Add this snippet to your drushrc.php file:
$output = array();
exec('git rev-parse --show-toplevel 2> /dev/null', $output);
if (!empty($output)) {
$repo = $output[0];
$options['config'] = $repo . '/drush/drushrc.php';
$options['include'] = $repo . '/drush/commands';
$options['alias-path'] = $repo . '/drush/aliases';
}
This script checks the current directory for participation in a git repository. If there is one, it looks at the root of the git repository for a ‘.drushrc.php’ file to provide repo-specific instructions to Drush. We use this to configure paths to external resources that can vary by code version, as well as setting the Drupal site root for the Drupal site sihpped with that particular repository.
The last line also seeks out a `scripts/` directory in the repository root, which it will scan recursively for any files ending in `.drush.inc`. This allows us to ship versioned drush commands with each repo instead of documenting a complex individual installation process.
There are many other things you can do with a little poking about in the options for a drushrc file, to learn more start with the drushrc.php example.
Update 7/16/2012: Updated code example to the same provided in Drush docs. You can now leverage this trick outside the root directory of your code repository. Also removed not applicable git context resulting in invalid configuration paths.