svn.inc

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

Drush pm SVN extension

Classes

File

commands/pm/version_control/svn.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush pm SVN extension
  5. */
  6. use Drush\Log\LogLevel;
  7. class drush_version_control_svn implements drush_version_control {
  8. /**
  9. * Implementation of pre_update().
  10. */
  11. public function pre_update(&$project, $items_to_test = array()) {
  12. // If items to test is empty, test everything; otherwise, pass just
  13. // the list of files to test to svn status.
  14. $args = array_keys($items_to_test);
  15. array_unshift($args, 'svn status '. drush_get_option('svnstatusparams') . str_repeat('%s ', count($args)));
  16. array_unshift($args, $project['full_project_path']);
  17. if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
  18. $output = preg_grep('/^[ ACDMRX?!~][ CM][ L][ +][ SX][ K]/', drush_shell_exec_output());
  19. if (!empty($output)) {
  20. return drush_set_error('DRUSH_PM_SVN_LOCAL_CHANGES', dt("The SVN 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))));
  21. }
  22. }
  23. else {
  24. return drush_set_error('DRUSH_PM_SVN_NOT_FOUND', dt("Drush was unable to get the svn status on !path.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!errors' => implode("\n", drush_shell_exec_output()))));
  25. }
  26. // Check for incoming updates
  27. $args = array_keys($items_to_test);
  28. array_unshift($args, 'svn status -u '. drush_get_option('svnstatusparams') . str_repeat('%s ', count($args)));
  29. array_unshift($args, $project['full_project_path']);
  30. if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
  31. $output = preg_grep('/\*/', drush_shell_exec_output());
  32. if (!empty($output)) {
  33. return drush_set_error('DRUSH_PM_SVN_REMOTE_CHANGES', dt("The SVN working copy at !path appears to be out of date with the repository (see below). Please run 'svn update' to pull down changes before continuing:\n!output", array('!path' => $project['full_project_path'], '!output' => implode("\n", $output))));
  34. }
  35. }
  36. else {
  37. return drush_set_error('DRUSH_PM_SVN_NOT_FOUND', dt("Drush was unable to get the svn remote status on !path. Check that you have connectivity to the repository.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!errors' => implode("\n", drush_shell_exec_output()))));
  38. }
  39. return TRUE;
  40. }
  41. /**
  42. * Implementation of rollback().
  43. */
  44. public function rollback($project) {
  45. if (drush_shell_exec('svn revert '. drush_get_option('svnrevertparams') .' '. $project['full_project_path'])) {
  46. $output = drush_shell_exec_output();
  47. if (!empty($output)) {
  48. return drush_set_error('DRUSH_PM_SVN_LOCAL_CHANGES', dt("The SVN 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))));
  49. }
  50. }
  51. else {
  52. return drush_set_error('DRUSH_PM_SVN_NOT_FOUND', dt("Drush was unable to get the svn status on !path. Check that you have Subversion \ninstalled and that this directory is a subversion working copy.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!errors' => implode("\n", drush_shell_exec_output()))));
  53. }
  54. }
  55. /**
  56. * Implementation of post_update().
  57. */
  58. public function post_update($project) {
  59. if ($this->sync($project)) {
  60. // Only attempt commit on a sucessful sync
  61. $this->commit($project);
  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. * Automatically add any unversioned files to Subversion and remove any files
  75. * that have been deleted on the file system
  76. */
  77. private function sync($project) {
  78. if (drush_get_option('svnsync')) {
  79. $errors = '';
  80. if (drush_shell_exec('svn status '. drush_get_option('svnstatusparams') .' '. $project['full_project_path'])) {
  81. $output = drush_shell_exec_output();
  82. foreach ($output as $line) {
  83. if (preg_match('/^\? *(.*)/', $line, $matches)) {
  84. if (!drush_shell_exec('svn add '. drush_get_option('svnaddparams') .' '. $matches[1])) {
  85. $errors .= implode("\n", drush_shell_exec_output());
  86. }
  87. }
  88. if (preg_match('/^\! *(.*)/', $line, $matches)) {
  89. if (!drush_shell_exec('svn remove '. drush_get_option('svnremoveparams') .' '. $matches[1])) {
  90. $errors .= implode("\n", drush_shell_exec_output());
  91. }
  92. }
  93. }
  94. if (!empty($errors)) {
  95. return drush_set_error('DRUSH_PM_SVN_SYNC_PROBLEMS', dt("Problems were encountered adding or removing files to/from this SVN working copy.\nThe specific errors are below:\n!errors", array('!errors' => $errors)));
  96. }
  97. }
  98. else {
  99. return drush_set_error('DRUSH_PM_SVN_NOT_FOUND', dt("Drush was unable to get the svn status on !path. Check that you have Subversion \ninstalled and that this directory is a subversion working copy.\nThe specific errors are below:\n!errors", array('!path' => $project['full_project_path'], '!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('svncommit')) {
  109. $message = drush_get_option('svnmessage');
  110. if (empty($message)) {
  111. $message = dt("Drush automatic commit: \n") . implode(' ', $_SERVER['argv']);
  112. }
  113. if (drush_shell_exec('svn commit '. drush_get_option('svncommitparams') .' -m "'. $message .'" '. $project['full_project_path'])) {
  114. drush_log(dt('Project committed to Subversion successfully'), LogLevel::OK);
  115. }
  116. else {
  117. drush_set_error('DRUSH_PM_SVN_COMMIT_PROBLEMS', dt("Problems were encountered committing your changes to Subversion.\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 Subversion repository.\nIf this version becomes undesireable, use Subversion to roll back."));
  122. }
  123. }
  124. public static function reserved_files() {
  125. return array('.svn');
  126. }
  127. }