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 BZR extension

Classes

File

commands/pm/version_control/bzr.inc
View source
  1. <?php
  2. /**
  3. * @file Drush pm BZR extension
  4. */
  5. class drush_pm_version_control_bzr implements drush_pm_version_control {
  6. /**
  7. * Helper function: get the root of a repository path.
  8. *
  9. * @param $path
  10. * The path to check.
  11. * @return
  12. * An absolute path to the repository root.
  13. */
  14. public function bzr_get_repository_root($path) {
  15. if (drush_shell_exec('bzr root %s', $path)) {
  16. $output = drush_shell_exec_output();
  17. return reset($output);
  18. }
  19. }
  20. /**
  21. * Implementation of pre_update().
  22. */
  23. public function pre_update(&$project, $items_to_test = array()) {
  24. // TODO: Add similar checks to SVN?
  25. }
  26. /**
  27. * Implementation of post_update().
  28. */
  29. public function post_update($project) {
  30. if (is_versioned($project['full_project_path']) && sync($project)) {
  31. // Only attempt commit on a sucessful sync
  32. commit($project);
  33. }
  34. }
  35. /**
  36. * Implementation of post_download().
  37. */
  38. public function post_download($project) {
  39. if (is_versioned($project['full_project_path']) && sync($project)) {
  40. // Only attempt commit on a sucessful sync
  41. commit($project);
  42. }
  43. }
  44. /**
  45. * Automatically add any unversioned files to Bzr and remove any files
  46. * that have been deleted on the file system
  47. */
  48. private function sync($project) {
  49. if (drush_get_option('bzrsync')) {
  50. $errors = '';
  51. // All paths returned by bzr status are relative to the repository root.
  52. $root = bzr_get_repository_root($project['full_project_path']);
  53. if (drush_shell_exec('bzr status -S %s', $project['full_project_path'])) {
  54. $output = drush_shell_exec_output();
  55. // Parse the output of bzr status to get the list of added and removed
  56. // files.
  57. $new_files = array();
  58. $removed_files = array();
  59. foreach ($output as $line) {
  60. if (preg_match('/^\? *(.*)/', $line, $matches)) {
  61. $new_files[] = $root . '/' . $matches[1];
  62. }
  63. if (preg_match('/^\ D *(.*)/', $line, $matches)) {
  64. $removed_files[] = $root . '/' . $matches[1];
  65. }
  66. }
  67. // Proceed adding new files.
  68. foreach (array_chunk($new_files, 100) as $chunk) {
  69. $chunk = array_map('escapeshellarg', $chunk);
  70. if (!drush_shell_exec('bzr add ' . implode(' ', $chunk))) {
  71. $errors .= implode("\n", drush_shell_exec_output());
  72. }
  73. }
  74. // Proceed removing old files.
  75. foreach (array_chunk($removed_files, 100) as $chunk) {
  76. $chunk = array_map('escapeshellarg', $chunk);
  77. if (!drush_shell_exec('bzr remove ' . implode(' ', $chunk))) {
  78. $errors .= implode("\n", drush_shell_exec_output());
  79. }
  80. }
  81. if (!empty($errors)) {
  82. 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)));
  83. }
  84. }
  85. else {
  86. 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()))));
  87. }
  88. return TRUE;
  89. }
  90. }
  91. /**
  92. * Automatically commit changes to the repository
  93. */
  94. private function commit($project) {
  95. if (drush_get_option('bzrcommit')) {
  96. $message = drush_get_option('bzrmessage');
  97. if (empty($message)) {
  98. $message = dt("Drush automatic commit: \n") . implode(' ', $_SERVER['argv']);
  99. }
  100. if (drush_shell_exec('bzr commit -m %s %s', $message, $project['full_project_path'])) {
  101. drush_log(dt('Project committed to Bazaar successfully'), 'ok');
  102. }
  103. else {
  104. 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()))));
  105. }
  106. }
  107. else {
  108. drush_print(dt("You should consider committing the new code to your Bazaar repository.\nIf this version becomes undesireable, use Bazaar to roll back."));
  109. }
  110. }
  111. }