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