Skip to content

Instantly share code, notes, and snippets.

@davereid
Last active August 29, 2015 14:03
Show Gist options
  • Save davereid/45df5478b5154c73f737 to your computer and use it in GitHub Desktop.
Save davereid/45df5478b5154c73f737 to your computer and use it in GitHub Desktop.
<?php
// Load a drushrc.php file from the 'drush' folder at the root of the current
// git repository.
if ($repo_root = _drushrc_find_repo_root()) {
drush_set_context('DRUSH_REPO_ROOT', $repo_root);
if (is_dir($repo_root . '/drush')) {
if (is_file($repo_root . '/drush/drushrc.php')) {
$options['config'] = $repo_root . '/drush/drushrc.php';
}
if (is_dir($repo_root . '/drush/commands')) {
$options['include'] = $repo_root . '/drush/commands';
}
if (is_dir($repo_root . '/drush/aliases')) {
$options['alias-path'] = $repo_root . '/drush/aliases';
}
}
}
/**
* Attempt to find the root directory of a Git clone.
*
* @param string $directory
* The directory that may be inside a Git checkout.
*
* @return string|bool
* The Git root directory if found, or FALSE otherwise.
*/
function _drushrc_find_repo_root($directory = NULL) {
if (!isset($directory)) {
$directory = _drushrc_find_drupal_root();
}
if (!is_dir($directory)) {
return FALSE;
}
$success = drush_shell_cd_and_exec($directory, 'git rev-parse --show-toplevel 2> ' . drush_bit_bucket());
if ($success) {
$output = drush_shell_exec_output();
return $output[0];
}
return FALSE;
}
/**
* Attempt to find the current Drupal root directory.
*/
function _drushrc_find_drupal_root() {
if ($root = drush_get_option('root')) {
return $root;
}
// Drush has not yet bootstrapped to detect the current site if an alias was
// provided. The side effects of calling this early here are not yet known.
// For example, if you are using a Drush alias that is defined inside your
// project's drush directory, that alias will not yet have been loaded, and
// the the following will not get executed. In this case, however, the
// behavior of falling back to the current directory still works I think.
/*if (drush_sitealias_check_arg() === TRUE) {
// If the alias points to a remote host, just stop here.
if (drush_get_option('remote-host')) {
return FALSE;
}
elseif ($root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT')) {
return $root;
}
}*/
// Fallback to the current directory.
return drush_cwd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment