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
drush_pm_git_drupalorg_compute_rebuild_version Helper function to compute the rebulid version string for a project.
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. // We need to check for a git executable and then make sure version is >=1.7
  13. // (avoid drush_shell_exec because we want to run this even in --simulated mode.)
  14. $success = exec('git --version', $git);
  15. $git_version_array = explode(" ", $git[0]);
  16. $git_version = $git_version_array[2];
  17. drush_set_context('DRUSH_DEBUG', $debug);
  18. if (!$success) {
  19. return drush_set_error('DRUSH_SHELL_COMMAND_NOT_FOUND', dt('git executable not found.'));
  20. } elseif ($git_version < '1.7') {
  21. return drush_set_error('GIT_VERSION_UNSUPPORTED', dt('Your git version !git_version is not supported; please upgrade to git 1.7 or later.', array('!git_version' => $git_version)));
  22. }
  23. // Check git_deploy is enabled. Only for bootstrapped sites.
  24. if (drush_get_context('DRUSH_BOOTSTRAP_PHASE') >= DRUSH_BOOTSTRAP_DRUPAL_FULL) {
  25. if (!drush_get_option('gitinfofile') && !module_exists('git_deploy')) {
  26. drush_log(dt('git package handler needs git_deploy module enabled to work properly.'), 'warning');
  27. }
  28. }
  29. return TRUE;
  30. }
  31. /**
  32. * Download a project.
  33. *
  34. * @param $request
  35. * The project array with name, base and full (final) paths.
  36. * @param $release
  37. * The release details array from drupal.org.
  38. */
  39. function package_handler_download_project(&$request, $release) {
  40. if ($username = drush_get_option('gitusername')) {
  41. // Uses SSH, which enables pushing changes back to git.drupal.org.
  42. $repository = $username . '@git.drupal.org:project/' . $request['name'] . '.git';
  43. }
  44. else {
  45. $repository = 'git://git.drupal.org/project/' . $request['name'] . '.git';
  46. }
  47. $request['repository'] = $repository;
  48. $tag = $release['tag'];
  49. // If the --cache option was given, create a new git reference cache of the
  50. // remote repository, or update the existing cache to fetch recent changes.
  51. if (drush_get_option('cache') && ($cachedir = drush_directory_cache())) {
  52. $gitcache = $cachedir . '/git';
  53. $projectcache = $gitcache . '/' . $request['name'] . '.git';
  54. drush_mkdir($gitcache);
  55. // Setup a new cache, if we don't have this project yet.
  56. if (!file_exists($projectcache)) {
  57. // --mirror works similar to --bare, but retrieves all tags, local
  58. // branches, remote branches, and any other refs (notes, stashes, etc).
  59. // @see http://stackoverflow.com/questions/3959924
  60. $command = 'git clone --mirror';
  61. if (drush_get_context('DRUSH_VERBOSE')) {
  62. $command .= ' --verbose --progress';
  63. }
  64. $command .= ' %s %s';
  65. drush_shell_cd_and_exec($gitcache, $command, $repository, $request['name'] . '.git');
  66. }
  67. // If we already have this project, update it to speed up subsequent clones.
  68. else {
  69. // A --mirror clone is fully synchronized with `git remote update` instead
  70. // of `git fetch --all`.
  71. // @see http://stackoverflow.com/questions/6150188
  72. drush_shell_cd_and_exec($projectcache, 'git remote update');
  73. }
  74. $gitcache = $projectcache;
  75. }
  76. // Clone the repo into its appropriate target location.
  77. $command = 'git clone';
  78. $command .= ' ' . drush_get_option('gitcloneparams');
  79. if (drush_get_option('cache')) {
  80. $command .= ' --reference ' . drush_escapeshellarg($gitcache);
  81. }
  82. if (drush_get_context('DRUSH_VERBOSE')) {
  83. $command .= ' --verbose --progress';
  84. }
  85. $command .= ' ' . drush_escapeshellarg($repository);
  86. $command .= ' ' . drush_escapeshellarg($request['full_project_path']);
  87. if (!drush_shell_exec($command)) {
  88. return drush_set_error('DRUSH_PM_GIT_CHECKOUT_PROBLEMS', dt('Unable to clone project !name from git.drupal.org.', array('!name' => $request['name'])));
  89. }
  90. // Check if the 'tag' from the release feed is a tag or a branch.
  91. // If the tag exists, git will return it
  92. if (!drush_shell_cd_and_exec($request['full_project_path'], 'git tag -l ' . drush_escapeshellarg($tag))) {
  93. return drush_set_error('DRUSH_PM_GIT_CHECKOUT_PROBLEMS', dt('Unable to clone project !name from git.drupal.org.', array('!name' => $request['name'])));
  94. }
  95. $output = drush_shell_exec_output();
  96. if (isset($output[0]) && ($output[0] == $tag)) {
  97. // If we want a tag, simply checkout it. The checkout will end up in
  98. // "detached head" state.
  99. $command = 'git checkout ' . drush_get_option('gitcheckoutparams');
  100. $command .= ' ' . drush_escapeshellarg($tag);
  101. if (!drush_shell_cd_and_exec($request['full_project_path'], $command)) {
  102. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  103. }
  104. }
  105. else {
  106. // Else, we want to checkout a branch.
  107. // First check if we are not already in the correct branch.
  108. if (!drush_shell_cd_and_exec($request['full_project_path'], 'git symbolic-ref HEAD')) {
  109. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  110. }
  111. $output = drush_shell_exec_output();
  112. $current_branch = preg_replace('@^refs/heads/@', '', $output[0]);
  113. // If we are not on the correct branch already, switch to the correct one.
  114. if ($current_branch != $tag) {
  115. $command = 'git checkout';
  116. $command .= ' ' . drush_get_option('gitcheckoutparams');
  117. $command .= ' --track ' . drush_escapeshellarg('origin/' . $tag) . ' -b ' . drush_escapeshellarg($tag);
  118. if (!drush_shell_cd_and_exec($request['full_project_path'], $command)) {
  119. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to retrieve ' . $request['name'] . ' from git.drupal.org.');
  120. }
  121. }
  122. }
  123. return TRUE;
  124. }
  125. /**
  126. * Update a project (so far, only modules are supported).
  127. *
  128. * @param $request
  129. * The project array with name, base and full (final) paths.
  130. * @param $release
  131. * The release details array from drupal.org.
  132. */
  133. function package_handler_update_project($request, $release) {
  134. drush_log('Updating project ' . $request['name'] . ' ...');
  135. $commands = array();
  136. if ($release['version_extra'] == 'dev') {
  137. // Update the branch of the development repository.
  138. $commands[] = 'git pull';
  139. $commands[] = drush_get_option('gitpullparams');
  140. }
  141. else {
  142. // Use a stable repository.
  143. $commands[] = 'git fetch';
  144. $commands[] = drush_get_option('gitfetchparams');
  145. $commands[] = ';';
  146. $commands[] = 'git checkout';
  147. $commands[] = drush_get_option('gitcheckoutparams');
  148. $commands[] = $release['version'];
  149. }
  150. if (!drush_shell_cd_and_exec($request['full_project_path'], implode(' ', $commands))) {
  151. return drush_set_error('DRUSH_PM_UNABLE_CHECKOUT', 'Unable to update ' . $request['name'] . ' from git.drupal.org.');
  152. }
  153. return TRUE;
  154. }
  155. /**
  156. * Post download action.
  157. *
  158. * This action take place once the project is placed in its final location.
  159. *
  160. * Here we add the project as a git submodule.
  161. */
  162. function package_handler_post_download($project, $release) {
  163. if (drush_get_option('gitsubmodule', FALSE)) {
  164. // Obtain the superproject path, then add as submodule.
  165. if (drush_shell_cd_and_exec(dirname($project['full_project_path']), 'git rev-parse --show-toplevel')) {
  166. $output = drush_shell_exec_output();
  167. $superproject = $output[0];
  168. // Add the downloaded project as a submodule of its git superproject.
  169. $command = array();
  170. $command[] = 'git submodule add';
  171. $command[] = drush_get_option('gitsubmoduleaddparams');
  172. $command[] = $project['repository'];
  173. // We need the submodule relative path.
  174. $command[] = substr(realpath($project['full_project_path']), strlen(realpath($superproject)) + 1);
  175. if (!drush_shell_cd_and_exec($superproject, implode(' ', $command))) {
  176. 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)));
  177. }
  178. }
  179. else {
  180. 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']))));
  181. }
  182. }
  183. if (drush_get_option('gitinfofile', FALSE)) {
  184. $matches = array();
  185. if (preg_match('/^(.+).x-dev$/', $release['version'], $matches)) {
  186. $full_version = drush_pm_git_drupalorg_compute_rebuild_version($project['project_install_location'], $matches[1]);
  187. }
  188. else {
  189. $full_version = $release['version'];
  190. }
  191. drush_pm_inject_info_file_metadata($project['project_install_location'], $project['name'], $full_version);
  192. }
  193. }
  194. /**
  195. * Helper function to compute the rebulid version string for a project.
  196. *
  197. * This does some magic in Git to find the latest release tag along
  198. * the branch we're packaging from, count the number of commits since
  199. * then, and use that to construct this fancy alternate version string
  200. * which is useful for the version-specific dependency support in Drupal
  201. * 7 and higher.
  202. *
  203. * NOTE: A similar function lives in git_deploy and in the drupal.org
  204. * packaging script (see DrupalorgProjectPackageRelease.class.php inside
  205. * drupalorg/drupalorg_project/plugins/release_packager). Any changes to the
  206. * actual logic in here should probably be reflected in the other places.
  207. *
  208. * @param string $project_dir
  209. * The full path to the root directory of the project to operate on.
  210. * @param string $branch
  211. * The branch that we're using for -dev. This should only include the
  212. * core version, the dash, and the branch's major version (eg. '7.x-2').
  213. *
  214. * @return string
  215. * The full 'rebuild version string' in the given Git checkout.
  216. */
  217. function drush_pm_git_drupalorg_compute_rebuild_version($project_dir, $branch) {
  218. $rebuild_version = '';
  219. $branch_preg = preg_quote($branch);
  220. if (drush_shell_cd_and_exec($project_dir, 'git describe --tags')) {
  221. $shell_output = drush_shell_exec_output();
  222. $last_tag = $shell_output[0];
  223. // Make sure the tag starts as Drupal formatted (for eg.
  224. // 7.x-1.0-alpha1) and if we are on a proper branch (ie. not master)
  225. // then it's on that branch.
  226. if (preg_match('/^(?<drupalversion>' . $branch_preg . '\.\d+(?:-[^-]+)?)(?<gitextra>-(?<numberofcommits>\d+-)g[0-9a-f]{7})?$/', $last_tag, $matches)) {
  227. // If we found additional git metadata (in particular, number of commits)
  228. // then use that info to build the version string.
  229. if (isset($matches['gitextra'])) {
  230. $rebuild_version = $matches['drupalversion'] . '+' . $matches['numberofcommits'] . 'dev';
  231. }
  232. // Otherwise, the branch tip is pointing to the same commit as the
  233. // last tag on the branch, in which case we use the prior tag and
  234. // add '+0-dev' to indicate we're still on a -dev branch.
  235. else {
  236. $rebuild_version = $last_tag . '+0-dev';
  237. }
  238. }
  239. }
  240. return $rebuild_version;
  241. }