22bfb7a412ae8d97ddfd0206fa34d7368e114842
[yaffs-website] / vendor / drush / drush / commands / pm / version_control / bzr.inc
1 <?php
2
3 /**
4  * @file
5  * Drush pm Bazaar extension
6  */
7
8 use Drush\Log\LogLevel;
9
10 class drush_version_control_bzr implements drush_version_control {
11
12   /**
13    * Implementation of pre_update().
14    *
15    * Check that the project or drupal core directory looks clean
16    */
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('.' => '.');
23     }
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))));
31       }
32     }
33     else {
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()))));
35     }
36     return TRUE;
37   }
38
39   /**
40    * Implementation of rollback().
41    */
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))));
47       }
48     }
49     else {
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()))));
51     }
52   }
53
54   /**
55    * Implementation of post_update().
56    */
57   public function post_update($project) {
58     if ($this->sync($project)) {
59       // Only attempt commit on a sucessful sync
60       $this->commit($project);
61     }
62   }
63
64   /**
65    * Implementation of post_download().
66    */
67   public function post_download($project) {
68     if ($this->sync($project)) {
69       // Only attempt commit on a sucessful sync
70       $this->commit($project);
71     }
72   }
73
74   /**
75    * Automatically add any unversioned files to Bazaar and remove any files
76    * that have been deleted on the file system
77    */
78   private function sync($project) {
79     if (drush_get_option('bzrsync')) {
80       $errors = '';
81       $root = array();
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();
87         }
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());
93             }
94           }
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());
99             }
100           }
101         }
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)));
104         }
105       }
106       else {
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()))));
108       }
109       return TRUE;
110     }
111   }
112
113   /**
114    * Automatically commit changes to the repository
115    */
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'])));
121       }
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);
124       }
125       else {
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()))));
127       }
128     }
129     else {
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."));
131     }
132   }
133
134   public static function reserved_files() {
135     return array('.bzr', '.bzrignore', '.bzrtags');
136   }
137 }