function DrushMakeProject::applyPatches

8.0.x make.project.inc DrushMakeProject::applyPatches($project_directory)
6.x make.project.inc DrushMakeProject::applyPatches($project_directory)
7.x make.project.inc DrushMakeProject::applyPatches($project_directory)
5.x make.project.inc DrushMakeProject::applyPatches($project_directory)
master make.project.inc DrushMakeProject::applyPatches($project_directory)

Retrieve and apply any patches specified by the makefile to this project.

1 call to DrushMakeProject::applyPatches()
DrushMakeProject::make in commands/make/make.project.inc
Build a project.

File

commands/make/make.project.inc, line 242
Drush Make processing classes.

Class

DrushMakeProject
The base project class.

Code

function applyPatches($project_directory) {
  if (empty($this->patch)) {
    return TRUE;
  }

  $patches_txt = '';
  $local_patches = array();
  $ignore_checksums = drush_get_option('ignore-checksums');
  foreach ($this->patch as $info) {
    $this->preprocessLocalFileUrl($info);

    // Download the patch.
    if ($filename = _make_download_file($info['url'])) {
      $patched = FALSE;
      $output = '';
      // Test each patch style; -p1 is the default with git. See
      // http://drupal.org/node/1054616
      $patch_levels = array('-p1', '-p0');
      foreach ($patch_levels as $patch_level) {
        $checked = drush_shell_exec('cd %s && GIT_DIR=. git apply --check %s %s --verbose', $project_directory, $patch_level, $filename);
        if ($checked) {
          // Apply the first successful style.
          $patched = drush_shell_exec('cd %s && GIT_DIR=. git apply %s %s --verbose', $project_directory, $patch_level, $filename);
          break;
        }
      }

      // In some rare cases, git will fail to apply a patch, fallback to using
      // the 'patch' command.
      if (!$patched) {
        foreach ($patch_levels as $patch_level) {
          // --no-backup-if-mismatch here is a hack that fixes some
          // differences between how patch works on windows and unix.
          if ($patched = drush_shell_exec("patch %s --no-backup-if-mismatch -d %s < %s", $patch_level, $project_directory, $filename)) {
            break;
          }
        }
      }

      if ($output = drush_shell_exec_output()) {
        // Log any command output, visible only in --verbose or --debug mode.
        drush_log(implode("\n", $output));
      }

      // Set up string placeholders to pass to dt().
      $dt_args = array(
        '@name' => $this->name,
        '@filename' => basename($filename),
      );

      if ($patched) {
        if (!$ignore_checksums && !_make_verify_checksums($info, $filename)) {
          return FALSE;
        }
        $patch_url = $info['url'];

        // If this is a local patch, copy that into place as well.
        if ($info['local']) {
          $local_patches[] = $info['url'];
          // Use a local path for the PATCHES.txt file.
          $pathinfo = pathinfo($patch_url);
          $patch_url = $pathinfo['basename'];
        }
        $patches_txt .= '- ' . $patch_url . "\n";

        drush_log(dt('@name patched with @filename.', $dt_args), 'ok');
      }
      else {
        make_error('PATCH_ERROR', dt("Unable to patch @name with @filename.", $dt_args));
      }
      drush_op('unlink', $filename);
    }
    else {
      make_error('DOWNLOAD_ERROR', 'Unable to download ' . $info['url'] . '.');
      return FALSE;
    }
  }
  if (!empty($patches_txt) && !drush_get_option('no-patch-txt') && !file_exists($project_directory . '/PATCHES.txt')) {
    $patches_txt = "The following patches have been applied to this project:\n" . $patches_txt . "\nThis file was automatically generated by Drush Make (http://drupal.org/project/drush).";
    file_put_contents($project_directory . '/PATCHES.txt', $patches_txt);
    drush_log('Generated PATCHES.txt file for ' . $this->name, 'ok');

    // Copy local patches into place.
    foreach ($local_patches as $url) {
      $pathinfo = pathinfo($url);
      drush_copy_dir($url, $project_directory . '/' . $pathinfo['basename']);
    }
  }
  return TRUE;
}