Skip to content

Instantly share code, notes, and snippets.

@kostajh
Created September 11, 2014 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kostajh/10c9c555e2c165db4587 to your computer and use it in GitHub Desktop.
Save kostajh/10c9c555e2c165db4587 to your computer and use it in GitHub Desktop.
Use with Jenkins. Add this as a build step.
<?php
// This script analyzes the files affected between GIT_PREVIOUS_COMMIT and
// GIT_COMMIT. See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin for
// more information on these parameters.
// If the files affected are in the /features directory, then we will call
// `drush features-revert {feature}` for each feature affected.
// Why is this necessary? Because `features-revert-all` will revert every
// feature on your site; we only want to revert features that are overridden.
/**
* Usage: `drush scr /path/to/smart-features-revert.php GIT_COMMIT GIT_PREVIOUS_COMMIT /path/to/repo @example.prod`
*/
$args = array();
while ($arg = drush_shift()) {
$args[] = $arg;
}
$git_commit = $args[0];
$git_previous_commit = $args[1];
$dir = $args[2];
$alias_name = $args[3];
$git = trim(shell_exec('which git'));
$cmd = sprintf('%s diff --name-only %s %s', $git, $git_previous_commit, $git_commit);
if (!drush_shell_cd_and_exec($dir, $cmd)) {
// An error occurred.
$output = drush_shell_exec_output();
$out = implode("\n", $output);
return drush_set_error(dt("An error occurred when running command !cmd.\n !msg",
array('!cmd' => $cmd, '!msg' => $out)));
}
$output = drush_shell_exec_output();
if (!count($output)) {
// No files affected, return.
drush_log(dt('No files affected.'), 'ok');
return TRUE;
}
// Check if any of the files are features.
$features_revert = array();
foreach ($output as $file) {
if (strpos(trim($file), 'docroot/sites/all/modules/features') === 0) {
$parts = explode('/', $file);
$feature_name = $parts[5];
$features_revert[$feature_name] = $feature_name;
}
}
// If no features to revert, return.
if (!count($features_revert)) {
return TRUE;
}
else {
$result = drush_invoke_process($alias_name, 'features-revert', $features_revert);
if ($result['error_status'] == 0) {
drush_log(dt('Successfully revert features !features', array('!features' => implode(' ', $features_revert))), 'success');
return TRUE;
}
else {
return drush_set_error(dt('An error occurred when reverting features !features', array('!features' => implode(' ', $features_revert))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment