bzr.inc

  1. 8.0.x commands/pm/version_control/bzr.inc
  2. 6.x commands/pm/version_control/bzr.inc
  3. 7.x commands/pm/version_control/bzr.inc
  4. 3.x commands/pm/version_control/bzr.inc
  5. 4.x commands/pm/version_control/bzr.inc
  6. 5.x commands/pm/version_control/bzr.inc
  7. master commands/pm/version_control/bzr.inc

Drush pm Bazaar extension

Classes

File

commands/pm/version_control/bzr.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush pm Bazaar extension
  5. */
  6. use Drush\Log\LogLevel;
  7. class drush_version_control_bzr implements drush_version_control {
  8. /**
  9. * Implementation of pre_update().
  10. *
  11. * Check that the project or drupal core directory looks clean
  12. */
  13. public function pre_update(&$project, $items_to_test = array()) {
  14. // Bazaar needs a list of items to test within the given project.
  15. // If $items_to_test is empty we need to force it to test the project
  16. // directory itself --once we've cd'ed to it.
  17. if (empty($items_to_test)) {
  18. $items_to_test = array('.' => '.');
  19. }
  20. $args = array_keys($items_to_test);
  21. array_unshift($args, 'bzr status --short' . str_repeat(' %s', count($args)));
  22. array_unshift($args, $project['full_project_path']);
  23. if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
  24. $output = preg_grep('/^[\sRCP][\sNDKM][\s\*]/', drush_shell_exec_output());
  25. if (!empty($output)) {
  26. 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))));
  27. }
  28. }
  29. else {
  30. 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()))));
  31. }
  32. return TRUE;
  33. }
  34. /**
  35. * Implementation of rollback().
  36. */
  37. public function rollback($project) {
  38. if (drush_shell_exec('bzr revert %s', $project['full_project_path'])) {
  39. $output = drush_shell_exec_output();
  40. if (!empty($output)) {
  41. 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))));
  42. }
  43. }
  44. else {
  45. 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()))));
  46. }
  47. }
  48. /**
  49. * Implementation of post_update().
  50. */
  51. public function post_update($project) {
  52. if ($this->sync($project)) {
  53. // Only attempt commit on a sucessful sync
  54. $this->commit($project);
  55. }
  56. }
  57. /**
  58. * Implementation of post_download().
  59. */
  60. public function post_download($project) {
  61. if ($this->sync($project)) {
  62. // Only attempt commit on a sucessful sync
  63. $this->commit($project);
  64. }
  65. }
  66. /**
  67. * Automatically add any unversioned files to Bazaar and remove any files
  68. * that have been deleted on the file system
  69. */
  70. private function sync($project) {
  71. if (drush_get_option('bzrsync')) {
  72. $errors = '';
  73. $root = array();
  74. if (drush_shell_exec('bzr status --short %s', $project['full_project_path'])) {
  75. $output = drush_shell_exec_output();
  76. // All paths returned by bzr status are relative to the repository root.
  77. if (drush_shell_exec('bzr root %s', $project['full_project_path'])) {
  78. $root = drush_shell_exec_output();
  79. }
  80. foreach ($output as $line) {
  81. if (preg_match('/^\?\s+(.*)/', $line, $matches)) {
  82. $path = $root[0] .'/'. $matches[1];
  83. if (!drush_shell_exec('bzr add --no-recurse %s', $path)) {
  84. $errors .= implode("\n", drush_shell_exec_output());
  85. }
  86. }
  87. else if (preg_match('/^\s+D\s+(.*)/', $line, $matches)) {
  88. $path = $root[0] .'/'. $matches[1];
  89. if (!drush_shell_exec('bzr remove %s', $path)) {
  90. $errors .= implode("\n", drush_shell_exec_output());
  91. }
  92. }
  93. }
  94. if (!empty($errors)) {
  95. 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)));
  96. }
  97. }
  98. else {
  99. 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()))));
  100. }
  101. return TRUE;
  102. }
  103. }
  104. /**
  105. * Automatically commit changes to the repository
  106. */
  107. private function commit($project) {
  108. if (drush_get_option('bzrcommit')) {
  109. $message = drush_get_option('bzrmessage');
  110. if (empty($message)) {
  111. $message = dt("Drush automatic commit.\nProject: @name @type\nCommand: @arguments", array('@name' => $project['name'], '@type' => $project['project_type'], '@arguments' => implode(' ', $_SERVER['argv'])));
  112. }
  113. if (drush_shell_exec('bzr commit --message=%s %s', $message, $project['full_project_path'])) {
  114. drush_log(dt('Project committed to Bazaar successfully'), LogLevel::OK);
  115. }
  116. else {
  117. 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()))));
  118. }
  119. }
  120. else {
  121. drush_print(dt("You should consider committing the new code to your Bazaar repository.\nIf this version becomes undesireable, use Bazaar to roll back."));
  122. }
  123. }
  124. public static function reserved_files() {
  125. return array('.bzr', '.bzrignore', '.bzrtags');
  126. }
  127. }