Skip to content

Instantly share code, notes, and snippets.

@amcgowanca
Last active December 30, 2015 09:09
Show Gist options
  • Save amcgowanca/7807340 to your computer and use it in GitHub Desktop.
Save amcgowanca/7807340 to your computer and use it in GitHub Desktop.
A rough prototype of an example of how drush can recursively do a git pull on origin remote for the current branch.
<?php
/**
* @file
*/
function imagex_installkit_ops_drush_command() {
$commands = array();
$commands['installkit-ops-update'] = array(
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
'aliases' => array('ops-update'),
);
return $commands;
}
function drush_imagex_installkit_ops_installkit_ops_update() {
$curdir = drush_cwd();
_imagex_installkit_ops_git_pull_latest($curdir);
_imagex_installkit_ops_installkit_ops_update_recursive($curdir);
}
function _imagex_installkit_ops_installkit_ops_update_recursive($directory) {
if (FALSE !== ($dir = dir($directory))) {
while (FALSE !== ($entry = $dir->read())) {
if ('..' === $entry || '.' == $entry) {
continue;
}
$entry = $dir->path . '/' . $entry;
if (is_dir($entry)) {
_imagex_installkit_ops_git_pull_latest($entry);
_imagex_installkit_ops_installkit_ops_update_recursive($entry);
}
}
$dir->close();
}
}
function _imagex_installkit_ops_git_pull_latest($dir) {
if (!is_dir($dir . '/.git')) {
return;
}
drush_shell_cd_and_exec($dir, 'git rev-parse --abbrev-ref HEAD');
$shell_output = drush_shell_exec_output();
$current_branch = array_shift($shell_output);
if (!drush_shell_cd_and_exec($dir, 'git pull origin %s', $current_branch)) {
return drush_log(dt('Unable to fetch latest in !dir.', array('!dir' => $dir)), 'notice');
}
drush_log(dt('Fetched latest for !dir.', array('!dir' => $dir)), 'ok');
}
@amcgowanca
Copy link
Author

This code should be placed within the following directory ~/.drush/imagex_installkit_ops and in a file named imagex_installkit_ops.drush.inc.

Prior to running drush cc drush.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment