Skip to content

Commit

Permalink
Merge pull request drush-ops#812 from mattacular/shallow-clone-opt
Browse files Browse the repository at this point in the history
Shallow clone opt for git download targets
  • Loading branch information
jhedstrom committed Nov 8, 2014
2 parents 4c154cd + 47f68b0 commit fd1dc99
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 55 deletions.
125 changes: 70 additions & 55 deletions commands/make/make.download.inc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ function make_download_get($name, $type, $download, $download_location) {
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.
Expand Down Expand Up @@ -257,6 +258,18 @@ function make_download_git($name, $type, $download, $download_location) {
$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)));
Expand All @@ -265,74 +278,76 @@ function make_download_git($name, $type, $download, $download_location) {

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

// Get the current directory (so we can move back later).
$cwd = getcwd();
// Change into the working copy of the cloned repo.
chdir($tmp_location);
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'])), 'ok');
// 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'])), 'ok');

if (drush_shell_exec("git checkout FETCH_HEAD")) {
drush_log(dt("Checked out FETCH_HEAD."), 'info');
if (drush_shell_exec("git checkout FETCH_HEAD")) {
drush_log(dt("Checked out FETCH_HEAD."), 'info');
}
}
else {
make_error('DOWNLOAD_ERROR', dt("Unable to fetch the refspec @refspec from @project.", array('@refspec' => $download['refspec'], '@project' => $name)));
}
}
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'])), 'ok');
}
else {
make_error('DOWNLOAD_ERROR', dt("Unable to check out tag @tag.", array('@tag' => $download['tag'])));
// 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'])), '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'])), 'ok');
}
else {
make_error('DOWNLOAD_ERROR', dt("Unable to checkout revision @revision", array('@revision' => $download['revision'])));
// 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'])), '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'])), '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'])), 'ok');
}
else {
make_error('DOWNLOAD_ERROR', dt('Unable to check out branch @branch.', array('@branch' => $download['branch'])));
// 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'])), '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'])), '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.'), 'ok');
}
else {
make_error('DOWNLOAD_ERROR', dt('Unable to initialize submodules.'));
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.'), 'ok');
}
else {
make_error('DOWNLOAD_ERROR', dt('Unable to initialize submodules.'));
}
}
}

// Move back to last current directory (first line).
chdir($cwd);
// 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, TRUE);
Expand Down
11 changes: 11 additions & 0 deletions commands/make/make.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ function make_drush_command() {
'drush make example.make example --lock-update=example.lock' => 'Write the updated version of example.make to example.lock.',
),
),
'shallow-clone' => array(
'description' => 'For makefile entries which use git for downloading, this option will utilize shallow clones where possible (ie. by using the git-clone\'s depth=1 option). If the "working-copy" option is not desired, this option will significantly speed up makes which involve modules stored in very large git repos. In fact, if "working-copy" option is enabled, this option cannot be used.',
'examples' => array(
'drush make example.make example --shallow-clone',
),
),
),
'engines' => array('release_info'),
'topics' => array('docs-make', 'docs-make-example'),
Expand Down Expand Up @@ -200,6 +206,11 @@ function drush_make($makefile = NULL, $build_path = NULL) {
return FALSE;
}

if (drush_get_option('shallow-clone', FALSE) && drush_get_option('working-copy', FALSE)) {
drush_set_error('MAKE_SHALLOW_CLONE_WORKING_COPY_CONFLICT', dt('You cannot use "--shallow-copy" and "--working-copy" options together.'));
return FALSE;
}

$info = _make_parse_info_file($makefile);

if (!drush_get_option('no-build', FALSE)) {
Expand Down

0 comments on commit fd1dc99

Please sign in to comment.