svn.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file Drush pm SVN extension
  4. */
  5. class drush_pm_version_control_svn implements drush_pm_version_control {
  6. /**
  7. * Implementation of pre_update().
  8. */
  9. public function pre_update(&$project, $items_to_test = array()) {
  10. // If items to test is empty, test everything; otherwise, pass just
  11. // the list of files to test to svn status.
  12. $args = array_keys($items_to_test);
  13. array_unshift($args, 'svn status '. drush_get_option('svnstatusparams') . str_repeat('%s ', count($args)));
  14. array_unshift($args, $project['full_project_path']);
  15. if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
  16. $output = preg_grep('/^[ ACDMRX?!~][ CM][ L][ +][ SX][ K]/', drush_shell_exec_output());
  17. if (!empty($output)) {
  18. 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))));
  19. }
  20. }
  21. else {
  22. 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()))));
  23. }
  24. // Check for incoming updates
  25. $args = array_keys($items_to_test);
  26. array_unshift($args, 'svn status -u '. drush_get_option('svnstatusparams') . str_repeat('%s ', count($args)));
  27. array_unshift($args, $project['full_project_path']);
  28. if (call_user_func_array('drush_shell_cd_and_exec', $args)) {
  29. $output = preg_grep('/\*/', drush_shell_exec_output());
  30. if (!empty($output)) {
  31. 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))));
  32. }
  33. }
  34. else {
  35. 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()))));
  36. }
  37. return TRUE;
  38. }
  39. /**
  40. * Implementation of rollback().
  41. */
  42. public function rollback($project) {
  43. if (drush_shell_exec('svn revert '. drush_get_option('svnrevertparams') .' '. $project['full_project_path'])) {
  44. $output = drush_shell_exec_output();
  45. if (!empty($output)) {
  46. 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))));
  47. }
  48. }
  49. else {
  50. 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()))));
  51. }
  52. }
  53. /**
  54. * Implementation of post_update().
  55. */
  56. public function post_update($project) {
  57. if ($this->sync($project)) {
  58. // Only attempt commit on a sucessful sync
  59. $this->commit($project);
  60. }
  61. }
  62. /**
  63. * Implementation of post_download().
  64. */
  65. public function post_download($project) {
  66. if ($this->sync($project)) {
  67. // Only attempt commit on a sucessful sync
  68. $this->commit($project);
  69. }
  70. }
  71. /**
  72. * Automatically add any unversioned files to Subversion and remove any files
  73. * that have been deleted on the file system
  74. */
  75. private function sync($project) {
  76. if (drush_get_option('svnsync')) {
  77. $errors = '';
  78. if (drush_shell_exec('svn status '. drush_get_option('svnstatusparams') .' '. $project['full_project_path'])) {
  79. $output = drush_shell_exec_output();
  80. foreach ($output as $line) {
  81. if (preg_match('/^\? *(.*)/', $line, $matches)) {
  82. if (!drush_shell_exec('svn add '. drush_get_option('svnaddparams') .' '. $matches[1])) {
  83. $errors .= implode("\n", drush_shell_exec_output());
  84. }
  85. }
  86. if (preg_match('/^\! *(.*)/', $line, $matches)) {
  87. if (!drush_shell_exec('svn remove '. drush_get_option('svnremoveparams') .' '. $matches[1])) {
  88. $errors .= implode("\n", drush_shell_exec_output());
  89. }
  90. }
  91. }
  92. if (!empty($errors)) {
  93. 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)));
  94. }
  95. }
  96. else {
  97. 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()))));
  98. }
  99. return TRUE;
  100. }
  101. }
  102. /**
  103. * Automatically commit changes to the repository
  104. */
  105. private function commit($project) {
  106. if (drush_get_option('svncommit')) {
  107. $message = drush_get_option('svnmessage');
  108. if (empty($message)) {
  109. $message = dt("Drush automatic commit: \n") . implode(' ', $_SERVER['argv']);
  110. }
  111. if (drush_shell_exec('svn commit '. drush_get_option('svncommitparams') .' -m "'. $message .'" '. $project['full_project_path'])) {
  112. drush_log(dt('Project committed to Subversion successfully'), 'ok');
  113. }
  114. else {
  115. 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()))));
  116. }
  117. }
  118. else {
  119. drush_print(dt("You should consider committing the new code to your Subversion repository.\nIf this version becomes undesireable, use Subversion to roll back."));
  120. }
  121. }
  122. public static function reserved_files() {
  123. return array('.svn');
  124. }
  125. }