Skip to content

Instantly share code, notes, and snippets.

@guiajlopes
Created October 21, 2015 11:33
Show Gist options
  • Select an option

  • Save guiajlopes/dbd8d42b975fa0753644 to your computer and use it in GitHub Desktop.

Select an option

Save guiajlopes/dbd8d42b975fa0753644 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;
}
// 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