5 * Drush pm Bazaar extension
8 use Drush\Log\LogLevel;
10 class drush_version_control_bzr implements drush_version_control {
13 * Implementation of pre_update().
15 * Check that the project or drupal core directory looks clean
17 public function pre_update(&$project, $items_to_test = array()) {
18 // Bazaar needs a list of items to test within the given project.
19 // If $items_to_test is empty we need to force it to test the project
20 // directory itself --once we've cd'ed to it.
21 if (empty($items_to_test)) {
22 $items_to_test = array('.' => '.');
24 $args = array_keys($items_to_test);
25 array_unshift($args, 'bzr status --short' . str_repeat(' %s', count($args)));
26 array_unshift($args, $project['full_project_path']);
27 if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
28 $output = preg_grep('/^[\sRCP][\sNDKM][\s\*]/', drush_shell_exec_output());
29 if (!empty($output)) {
30 return drush_set_error('DRUSH_PM_BZR_LOCAL_CHANGES', dt("The Bazaar working copy at !path appears to have uncommitted changes (see below). Please commit or revert these changes before continuing:\n!output", array('!path' => $project['full_project_path'], '!output' => implode("\n", $output))));
34 return drush_set_error('DRUSH_PM_BZR_NOT_FOUND', dt("Drush was unable to get the bzr status on !path. Check that you have Bazaar \ninstalled and that this directory is a Bazaar working copy.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!errors' => implode("\n", drush_shell_exec_output()))));
40 * Implementation of rollback().
42 public function rollback($project) {
43 if (drush_shell_exec('bzr revert %s', $project['full_project_path'])) {
44 $output = drush_shell_exec_output();
45 if (!empty($output)) {
46 return drush_set_error('DRUSH_PM_BZR_LOCAL_CHANGES', dt("The Bazaar working copy at !path appears to have uncommitted changes (see below). Please commit or revert these changes before continuing:\n!output", array('!path' => $project['full_project_path'], '!output' => implode("\n", $output))));
50 return drush_set_error('DRUSH_PM_BZR_NOT_FOUND', dt("Drush was unable to get the Bazaar status on !path. Check that you have Bazaar \ninstalled and that this directory is a Bazaar working copy.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!errors' => implode("\n", drush_shell_exec_output()))));
55 * Implementation of post_update().
57 public function post_update($project) {
58 if ($this->sync($project)) {
59 // Only attempt commit on a sucessful sync
60 $this->commit($project);
65 * Implementation of post_download().
67 public function post_download($project) {
68 if ($this->sync($project)) {
69 // Only attempt commit on a sucessful sync
70 $this->commit($project);
75 * Automatically add any unversioned files to Bazaar and remove any files
76 * that have been deleted on the file system
78 private function sync($project) {
79 if (drush_get_option('bzrsync')) {
82 if (drush_shell_exec('bzr status --short %s', $project['full_project_path'])) {
83 $output = drush_shell_exec_output();
84 // All paths returned by bzr status are relative to the repository root.
85 if (drush_shell_exec('bzr root %s', $project['full_project_path'])) {
86 $root = drush_shell_exec_output();
88 foreach ($output as $line) {
89 if (preg_match('/^\?\s+(.*)/', $line, $matches)) {
90 $path = $root[0] .'/'. $matches[1];
91 if (!drush_shell_exec('bzr add --no-recurse %s', $path)) {
92 $errors .= implode("\n", drush_shell_exec_output());
95 else if (preg_match('/^\s+D\s+(.*)/', $line, $matches)) {
96 $path = $root[0] .'/'. $matches[1];
97 if (!drush_shell_exec('bzr remove %s', $path)) {
98 $errors .= implode("\n", drush_shell_exec_output());
102 if (!empty($errors)) {
103 return drush_set_error('DRUSH_PM_BZR_SYNC_PROBLEMS', dt("Problems were encountered adding or removing files to/from Bazaar.\nThe specific errors are below:\n!errors", array('!errors' => $errors)));
107 return drush_set_error('DRUSH_PM_BZR_NOT_FOUND', dt("Drush was unable to get the bzr status. Check that you have Bazaar \ninstalled and that the site is a Bazaar working copy.\nThe specific errors are below:\n!errors", array('!errors' => implode("\n", drush_shell_exec_output()))));
114 * Automatically commit changes to the repository
116 private function commit($project) {
117 if (drush_get_option('bzrcommit')) {
118 $message = drush_get_option('bzrmessage');
119 if (empty($message)) {
120 $message = dt("Drush automatic commit.\nProject: @name @type\nCommand: @arguments", array('@name' => $project['name'], '@type' => $project['project_type'], '@arguments' => implode(' ', $_SERVER['argv'])));
122 if (drush_shell_exec('bzr commit --message=%s %s', $message, $project['full_project_path'])) {
123 drush_log(dt('Project committed to Bazaar successfully'), LogLevel::OK);
126 drush_set_error('DRUSH_PM_BZR_COMMIT_PROBLEMS', dt("Problems were encountered committing your changes to Bazaar.\nThe specific errors are below:\n!errors", array('!errors' => implode("\n", drush_shell_exec_output()))));
130 drush_print(dt("You should consider committing the new code to your Bazaar repository.\nIf this version becomes undesireable, use Bazaar to roll back."));
134 public static function reserved_files() {
135 return array('.bzr', '.bzrignore', '.bzrtags');