git_drupalorg.inc

  1. 8.0.x commands/pm/package_handler/git_drupalorg.inc
  2. 6.x commands/pm/package_handler/git_drupalorg.inc
  3. 7.x commands/pm/package_handler/git_drupalorg.inc
  4. 4.x commands/pm/package_handler/git_drupalorg.inc
  5. 5.x commands/pm/package_handler/git_drupalorg.inc
  6. master commands/pm/package_handler/git_drupalorg.inc

Drush PM drupal.org Git extension.

Functions

Namesort descending Description
package_handler_download_project Download a project.
package_handler_post_download Post download action.
package_handler_update_project Update a project (so far, only modules are supported).
package_handler_validate Validate this package handler can run.

File

commands/pm/package_handler/git_drupalorg.inc
View source
  1. <?php
  2. /**
  3. * @file Drush PM drupal.org Git extension.
  4. */
  5. /**
  6. * Validate this package handler can run.
  7. */
  8. function package_handler_validate() {
  9. // Check git command exists. Disable possible output.
  10. $debug = drush_get_context('DRUSH_DEBUG');
  11. drush_set_context('DRUSH_DEBUG', FALSE);
  12. $success = drush_shell_exec('git --version');
  13. drush_set_context('DRUSH_DEBUG', $debug);
  14. if (!$success) {
  15. return drush_set_error('DRUSH_SHELL_COMMAND_NOT_FOUND', dt('git executable not found.'));
  16. }
  17. // Check git_deploy is enabled. Only for bootstrapped sites.
  18. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  19. if (!module_exists('git_deploy')) {
  20. drush_log(dt('git package handler needs git_deploy module enabled to work properly.'), 'warning');
  21. }
  22. }
  23. }
  24. /**
  25. * Download a project.
  26. *
  27. * @param $request
  28. * The project array with name, base and full (final) paths.
  29. * @param $release
  30. * The release details array from drupal.org.
  31. */
  32. function package_handler_download_project(&$request, $release) {
  33. if ($username = drush_get_option('gitusername')) {
  34. // Uses SSH, which enables pushing changes back to git.drupal.org.
  35. $repository = $username . '@git.drupal.org:project/' . $request['name'] . '.git';
  36. }
  37. else {
  38. $repository = 'git://git.drupal.org/project/' . $request['name'] . '.git';
  39. }
  40. $request['repository'] = $repository;
  41. $tag = $release['tag'];
  42. // If the --cache option was given, create a new git reference cache of the
  43. // remote repository, or update the existing cache to fetch recent changes.
  44. if (drush_get_option('cache') && ($cachedir = drush_directory_cache())) {
  45. $gitcache = $cachedir . '/git';
  46. $projectcache = $gitcache . '/' . $request['name'] . '.git';
  47. drush_mkdir($gitcache);
  48. // Setup a new cache, if we don't have this project yet.
  49. if (!file_exists($projectcache)) {
  50. // --mirror works similar to --bare, but retrieves all tags, local
  51. // branches, remote branches, and any other refs (notes, stashes, etc).
  52. // @see http://stackoverflow.com/questions/3959924
  53. $command = 'git clone --mirror';
  54. if (drush_get_context('DRUSH_VERBOSE')) {
  55. $command .= ' --verbose --progress';
  56. }
  57. $command .= ' %s %s';
  58. drush_shell_cd_and_exec($gitcache, $command, $repository, $request['name'] . '.git');
  59. }
  60. // If we already have this project, update it to speed up subsequent clones.
  61. else {
  62. // A --mirror clone is fully synchronized with `git remote update` instead
  63. // of `git fetch --all`.
  64. // @see http://stackoverflow.com/questions/6150188
  65. drush_shell_cd_and_exec($projectcache, 'git remote update');
  66. }
  67. $gitcache = $projectcache;
  68. }
  69. // Clone the repo into its appropriate target location.
  70. $command = 'git clone';
  71. $command .= ' ' . drush_get_option('gitcloneparams');
  72. if (drush_get_option('cache')) {
  73. $command .= ' --reference ' . drush_escapeshellarg($gitcache);
  74. }
  75. if (drush_get_context('DRUSH_VERBOSE')) {
  76. $command .= ' --verbose --progress';
  77. }
  78. $command .= ' ' . drush_escapeshellarg($repository);
  79. $command .= ' ' . drush_escapeshellarg($request['full_project_path']);
  80. if (!drush_shell_exec($command)) {
  81. return drush_set_error('DRUSH_PM_GIT_CHECKOUT_PROBLEMS', dt('Unable to clone project !name from git.drupal.org.', array('!name' => $request['name'])));
  82. }
  83. // Check if the 'tag' from the release feed is a tag or a branch.
  84. // If the tag exists, git will return it
  85. if (!drush_shell_cd_and_exec($request['full_project_path'], 'git tag -l ' . drush_escapeshellarg($tag))) {
  86. return drush_set_error('DRUSH_PM_GIT_CHECKOUT_PROBLEMS', dt('Unable to clone project !name from git.drupal.org.', array('!name' => $request['name'])));
  87. }
  88. $output = drush_shell_exec_output();
  89. if (isset($output[0]) && ($output[0] == $tag)) {
  90. // If we want a tag, simply checkout it. The checkout will end up in
  91. // "detached head" state.
  92. $command = 'git checkout ' . drush_get_option('gitcheckoutparams');
  93. $command .= ' ' . drush_escapeshellarg($tag);
  94. if (!drush_shell_cd_and_exec($request['full_project_path'], $command)) {
  95. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  96. }
  97. }
  98. else {
  99. // Else, we want to checkout a branch.
  100. // First check if we are not already in the correct branch.
  101. if (!drush_shell_cd_and_exec($request['full_project_path'], 'git symbolic-ref HEAD')) {
  102. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  103. }
  104. $output = drush_shell_exec_output();
  105. $current_branch = preg_replace('@^refs/heads/@', '', $output[0]);
  106. // If we are not on the correct branch already, switch to the correct one.
  107. if ($current_branch != $tag) {
  108. $command = 'git checkout';
  109. $command .= ' ' . drush_get_option('gitcheckoutparams');
  110. $command .= ' --track ' . drush_escapeshellarg('origin/' . $tag) . ' -b ' . drush_escapeshellarg($tag);
  111. if (!drush_shell_cd_and_exec($request['full_project_path'], $command)) {
  112. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  113. }
  114. }
  115. }
  116. return TRUE;
  117. }
  118. /**
  119. * Update a project (so far, only modules are supported).
  120. *
  121. * @param $request
  122. * The project array with name, base and full (final) paths.
  123. * @param $release
  124. * The release details array from drupal.org.
  125. */
  126. function package_handler_update_project($request, $release) {
  127. drush_log('Updating project ' . $request['name'] . ' ...');
  128. $commands = array();
  129. if ($release['version_extra'] == 'dev') {
  130. // Update the branch of the development repository.
  131. $commands[] = 'git pull';
  132. $commands[] = drush_get_option('gitpullparams');
  133. }
  134. else {
  135. // Use a stable repository.
  136. $commands[] = 'git fetch';
  137. $commands[] = drush_get_option('gitfetchparams');
  138. $commands[] = ';';
  139. $commands[] = 'git checkout';
  140. $commands[] = drush_get_option('gitcheckoutparams');
  141. $commands[] = $release['version'];
  142. }
  143. if (!drush_shell_cd_and_exec($request['full_project_path'], implode(' ', $commands))) {
  144. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to update ' . $request['name'] . ' from git.drupal.org.');
  145. }
  146. return TRUE;
  147. }
  148. /**
  149. * Post download action.
  150. *
  151. * This action take place once the project is placed in its final location.
  152. *
  153. * Here we add the project as a git submodule.
  154. */
  155. function package_handler_post_download($project) {
  156. if (drush_get_option('gitsubmodule', FALSE)) {
  157. // Obtain the superproject path, then add as submodule.
  158. if (drush_shell_cd_and_exec(dirname($project['full_project_path']), 'git rev-parse --show-toplevel')) {
  159. $output = drush_shell_exec_output();
  160. $superproject = $output[0];
  161. // Add the downloaded project as a submodule of its git superproject.
  162. $command = array();
  163. $command[] = 'git submodule add';
  164. $command[] = drush_get_option('gitsubmoduleaddparams');
  165. $command[] = $project['repository'];
  166. // We need the submodule relative path.
  167. $command[] = substr($project['full_project_path'], strlen($superproject) + 1);
  168. if (!drush_shell_cd_and_exec($superproject, implode(' ', $command))) {
  169. return drush_set_error('DRUSH_PM_GIT_CHECKOUT_PROBLEMS', dt('Unable to add !name as a git submodule of !super.', array('!name' => $project['name'], '!super' => $superproject)));
  170. }
  171. }
  172. else {
  173. return drush_set_error('DRUSH_PM_GIT_SUBMODULE_PROBLEMS', dt('Unable to create !project as a git submodule: !dir is not in a Git repository.', array('!project' => $project['name'], '!dir' => dirname($project['full_project_path']))));
  174. }
  175. }
  176. }