function make_download_git

8.0.x make.download.inc make_download_git($name, $type, $download, $download_location)
6.x make.download.inc make_download_git($name, $type, $download, $download_location)
7.x make.download.inc make_download_git($name, $type, $download, $download_location)
5.x make.download.inc make_download_git($name, $type, $download, $download_location)
master make.download.inc make_download_git($name, $type, $download, $download_location)

Checks out a git repository to the specified download location.

Allowed parameters in $download, in order of precedence:

  • 'tag'
  • 'revision'
  • 'branch'

This will also attempt to write out release information to the .info file if the 'no-gitinfofile' option is FALSE. If $download['full_version'] is present, this will be used, otherwise, version will be set in this order of precedence:

  • 'tag'
  • 'branch'
  • 'revision'

Return value

mixed The download location on success, FALSE otherwise.

1 call to make_download_git()
_make_parse_info_file in commands/make/make.utilities.inc
Parse makefile recursively.

File

commands/make/make.download.inc, line 251
Download-specific functions for Drush Make.

Code

function make_download_git($name, $type, $download, $download_location) {
  $tmp_path = make_tmp();
  $wc = _get_working_copy_option($download);
  $checkout_after_clone = TRUE;
  // If no download URL specified, assume anonymous clone from git.drupal.org.
  $download['url'] = isset($download['url']) ? $download['url'] : "http://git.drupal.org/project/$name.git";
  // If no working-copy download URL specified, assume it is the same.
  $download['wc_url'] = isset($download['wc_url']) ? $download['wc_url'] : $download['url'];

  // If not a working copy, and if --no-cache has not been explicitly
  // declared, create a new git reference cache of the remote repository,
  // or update the existing cache to fetch recent changes.
  // @see package_handler_download_project()
  $cache = !$wc && !drush_get_option('no-cache', FALSE);
  if ($cache && ($git_cache = drush_directory_cache('git'))) {
    $project_cache = $git_cache . '/' . $name . '-' . md5($download['url']);
    // Set up a new cache, if it doesn't exist.
    if (!file_exists($project_cache)) {
      $command = 'git clone --mirror';
      if (drush_get_context('DRUSH_VERBOSE')) {
        $command .= ' --verbose --progress';
      }
      $command .= ' %s %s';
      drush_shell_cd_and_exec($git_cache, $command, $download['url'], $project_cache);
    }
    else {
      // Update the --mirror clone.
      drush_shell_cd_and_exec($project_cache, 'git remote update');
    }
    $git_cache = $project_cache;
  }

  // Use working-copy download URL if --working-copy specified.
  $url = $wc ? $download['wc_url'] : $download['url'];

  $tmp_location = drush_tempdir() . '/' . basename($download_location);

  $command = 'git clone %s %s';
  if (drush_get_context('DRUSH_VERBOSE')) {
    $command .= ' --verbose --progress';
  }
  if ($cache) {
    $command .= ' --reference ' . drush_escapeshellarg($git_cache);
  }

  // the shallow clone option is only applicable to git entries which reference a tag or a branch
  if (drush_get_option('shallow-clone', FALSE) && (!empty($download['tag']) || !empty($download['branch']))) {

    $branch = (!empty($download['branch']) ? $download['branch'] : $download['tag']);
    $command .= " --depth=1 --branch=${branch}";

    // since the shallow copy option automatically "checks out" the requested branch, no further
    // actions are needed after the clone command
    $checkout_after_clone = FALSE;
  }

  // Before we can checkout anything, we need to clone the repository.
  if (!drush_shell_exec($command, $url, $tmp_location)) {
    make_error('DOWNLOAD_ERROR', dt('Unable to clone @project from @url.', array('@project' => $name, '@url' => $url)));
    return FALSE;
  }

  drush_log(dt('@project cloned from @url.', array('@project' => $name, '@url' => $url)), LogLevel::OK);

  if ($checkout_after_clone) {
    // Get the current directory (so we can move back later).
    $cwd = getcwd();
    // Change into the working copy of the cloned repo.
    chdir($tmp_location);

    // We want to use the most specific target possible, so first try a refspec.
    if (!empty($download['refspec'])) {
      if (drush_shell_exec("git fetch %s %s", $url, $download['refspec'])) {
        drush_log(dt("Fetched refspec !refspec.", array('!refspec' => $download['refspec'])), LogLevel::OK);

        if (drush_shell_exec("git checkout FETCH_HEAD")) {
          drush_log(dt("Checked out FETCH_HEAD."), LogLevel::INFO);
        }
      }
      else {
        make_error('DOWNLOAD_ERROR', dt("Unable to fetch the refspec @refspec from @project.", array('@refspec' => $download['refspec'], '@project' => $name)));
      }
    }

    // If there wasn't a refspec, try a tag.
    elseif (!empty($download['tag'])) {
      // @TODO: change checkout to refs path.
      if (drush_shell_exec("git checkout %s", 'refs/tags/' . $download['tag'])) {
        drush_log(dt("Checked out tag @tag.", array('@tag' => $download['tag'])), LogLevel::OK);
      }
      else {
        make_error('DOWNLOAD_ERROR', dt("Unable to check out tag @tag.", array('@tag' => $download['tag'])));
      }
    }

    // If there wasn't a tag, try a specific revision hash.
    elseif (!empty($download['revision'])) {
      if (drush_shell_exec("git checkout %s", $download['revision'])) {
        drush_log(dt("Checked out revision @revision.", array('@revision' => $download['revision'])), LogLevel::OK);
      }
      else {
        make_error('DOWNLOAD_ERROR', dt("Unable to checkout revision @revision", array('@revision' => $download['revision'])));
      }
    }

    // If not, see if we at least have a branch.
    elseif (!empty($download['branch'])) {
      if (drush_shell_exec("git checkout %s", $download['branch']) && (trim(implode(drush_shell_exec_output())) != '')) {
        drush_log(dt("Checked out branch @branch.", array('@branch' => $download['branch'])), LogLevel::OK);
      }
      elseif (drush_shell_exec("git checkout -b %s %s", $download['branch'], 'origin/' . $download['branch'])) {
        drush_log(dt('Checked out branch origin/@branch.', array('@branch' => $download['branch'])), LogLevel::OK);
      }
      else {
        make_error('DOWNLOAD_ERROR', dt('Unable to check out branch @branch.', array('@branch' => $download['branch'])));
      }
    }

    if (!empty($download['submodule'])) {
      $command = 'git submodule update';
      foreach ($download['submodule'] as $option) {
        $command .= ' --%s';
      }
      if (call_user_func_array('drush_shell_exec', array_merge(array($command), $download['submodule']))) {
        drush_log(dt('Initialized registered submodules.'), LogLevel::OK);
      }
      else {
        make_error('DOWNLOAD_ERROR', dt('Unable to initialize submodules.'));
      }
    }

    // Move back to last current directory (first line).
    chdir($cwd);
  }

  // Move the directory into the final resting location.
  drush_copy_dir($tmp_location, $download_location, FILE_EXISTS_OVERWRITE);

  return dirname($tmp_location);
}