drush_pm_svn.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/drush/drush_pm_svn/drush_pm_svn.module

Functions & methods

NameDescription
drush_pm_svn_commitAutomatically commit changes to the repository
drush_pm_svn_drush_pm_post_installImplementation of hook_drush_pm_post_install().
drush_pm_svn_drush_pm_post_updateImplementation of hook_drush_pm_post_update().
drush_pm_svn_drush_pm_skip_backupImplementation of hook_drush_pm_skip_backup().
drush_pm_svn_helpImplementation of hook_help().
drush_pm_svn_syncAutomatically add any unversioned files to Subversion and remove any files that have been deleted on the file system

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_help().
  4. */
  5. function drush_pm_svn_help($section) {
  6. $help = '';
  7. switch ($section) {
  8. case 'drush:pm update':
  9. case 'drush:pm install':
  10. $help .= t("--svnsync - automatically add new files to the Subversion repository and
  11. remove deleted files. Use this with caution, especially if you leave files in
  12. project directories that you don't want to be marked for addition, or you
  13. have deleted a non-project file that you don't want to be marked in svn as a
  14. deletion.\n
  15. --svncommit - automatically commit project install or update changes to the
  16. sites Subversion repository. You can only use this option if you are also
  17. using the --svnsync option, because committing without first adding new and
  18. removing deleted files will cause unpredictable results.\n
  19. --svnmessage - Override the commit message from the default, which is:
  20. 'Drush automatic commit: ', and the drush command line used.\n
  21. --svnstatusparams - add options to the 'svn status' command
  22. --svnaddparams - add options to the 'svn add' command
  23. --svnremoveparams - add options to the 'svn remove' command
  24. --svncommitparams - add options to the 'svn commit' command
  25. e.g. --svncommitparams=\"--username joe\" (note that quotes are required) will
  26. commit changes as the user 'joe'.\n");
  27. return $help;
  28. }
  29. }
  30. /**
  31. * Implementation of hook_drush_pm_skip_backup().
  32. */
  33. function drush_pm_svn_drush_pm_skip_backup($source) {
  34. if (file_exists($source. '/.svn')) {
  35. // Skip backup since we need all .svn directories. Assuming admin can use svn revert if new project is bad.
  36. drush_verbose(t('Backup skipped because .svn directory was detected. Use Subversion to revert if needed.'));
  37. return TRUE;
  38. }
  39. }
  40. /**
  41. * Implementation of hook_drush_pm_post_update().
  42. */
  43. function drush_pm_svn_drush_pm_post_update($project, $info, $path = '.') {
  44. if (drush_pm_svn_sync($project, $info, $path)) {
  45. // Only attempt commit on a sucessful sync
  46. drush_pm_svn_commit($project, $info, $path);
  47. }
  48. }
  49. /**
  50. * Implementation of hook_drush_pm_post_install().
  51. */
  52. function drush_pm_svn_drush_pm_post_install($project, $info, $path = '.') {
  53. if (drush_pm_svn_sync($project, $info, $path)) {
  54. // Only attempt commit on a sucessful sync
  55. drush_pm_svn_commit($project, $info, $path);
  56. }
  57. }
  58. /**
  59. * Automatically add any unversioned files to Subversion and remove any files
  60. * that have been deleted on the file system
  61. */
  62. function drush_pm_svn_sync($project, $info, $path = '.') {
  63. if (drush_get_option('svnsync')) {
  64. $errors = '';
  65. if (drush_shell_exec('svn status '. drush_get_option('svnstatusparams') .' '. $project)) {
  66. $output = drush_shell_exec_output();
  67. foreach ($output as $line) {
  68. if (preg_match('/^\? *(.*)/', $line, $matches)) {
  69. if (!drush_shell_exec('svn add '. drush_get_option('svnaddparams') .' '. $matches[1])) {
  70. $errors .= implode("\n", drush_shell_exec_output());
  71. }
  72. }
  73. if (preg_match('/^\! *(.*)/', $line, $matches)) {
  74. if (!drush_shell_exec('svn remove '. drush_get_option('svnremoveparams') .' '. $matches[1])) {
  75. $errors .= implode("\n", drush_shell_exec_output());
  76. }
  77. }
  78. }
  79. if (!empty($errors)) {
  80. drush_print(t("Problems were encountered adding or removing files to/from subversion.\nThe specific errors are below:"));
  81. drush_print($errors);
  82. return FALSE;
  83. }
  84. }
  85. else {
  86. drush_print(t("Drush was unable to get the svn status. Check that you have Subversion \ninstalled and that the site is a subversion working copy.\nThe specific errors are below:"));
  87. drush_print(implode("\n", drush_shell_exec_output()));
  88. return FALSE;
  89. }
  90. return TRUE;
  91. }
  92. }
  93. /**
  94. * Automatically commit changes to the repository
  95. */
  96. function drush_pm_svn_commit($project, $info, $path = '.') {
  97. if (drush_get_option('svncommit')) {
  98. $message = drush_get_option('svnmessage');
  99. if (empty($message)) {
  100. $message = t("Drush automatic commit: \n") . implode(' ', $_SERVER['argv']);
  101. }
  102. if (drush_shell_exec('svn commit '. drush_get_option('svncommitparams') .' -m "'. $message .'" '. $project)) {
  103. drush_print(t('Project committed to Subversion successfully'));
  104. }
  105. else {
  106. drush_print(t("'Problems were encountered committing your changes to Subversion.\nThe specific errors are below:"));
  107. drush_print(implode("\n", drush_shell_exec_output()));
  108. }
  109. }
  110. else {
  111. drush_print(t("You should consider committing the new code to your Subversion repository.\nIf this version becomes undesireable, use Subversion to roll back."));
  112. }
  113. }