You are here

function _drush_coder_review_git in Coder 7.2

Install coder as a git pre-commit hook.

1 call to _drush_coder_review_git()
drush_coder_review in coder_review/coder_review.drush.inc
Performs the actual review for drush.

File

coder_review/coder_review.drush.inc, line 95
Command line utility support for Coder_review module.

Code

function _drush_coder_review_git($uninstall = FALSE) {
  $gitpath = _drush_coder_git_path();
  $realroot = realpath(DRUPAL_ROOT) . '/';
  $realgit = realpath($gitpath . '/../');
  $gitrelative = substr($realgit, strlen($realroot));

  // @ignore sniffer_strings_unnecessarystringconcat_found
  $doxygen_file = '@' . 'file';

  // @ignore sniffer_squiz_whitespace_operatorspacing_nospaceafter
  $pre_commit_code = <<<__PRE_COMMIT__
#!/usr/bin/php
<?php
/**
 * {<span class="php-variable">$doxygen_file</span>}
 * Generic .git pre-commit hook runs all pre-commit hooks
 * following the naming convention of pre-commit_example.
 *
 * This script was created by drush coder-review --git.
 * Do not modify.
 */
\$GITPATH = "{<span class="php-variable">$gitpath</span>}";

/**
 * Return array of pre-commit_ files with their full relative paths.
 */
function find_pre_commit(\$dir = '.') {
  \$files = array();
  foreach (scandir(\$dir) as \$file) {
    if (substr(\$file, 0, 11) == 'pre-commit_' && is_executable("\$dir/\$file")) {
      \$files[] = "\$dir/\$file";
    }
    elseif (\$file != '.' && \$file != '..' && is_dir("\$dir/\$file")) {
      \$files = array_merge(\$files, find_pre_commit("\$dir/\$file"));
    }
  }
  return \$files;
}

// Run each pre-commit script.
foreach (find_pre_commit(\$GITPATH) as \$pre_commit) {
  system(\$pre_commit, \$ret);
  if (\$ret != 0) {
    // Exit as soon as one of the scripts returns an error.
    exit(\$ret);
  }
}
__PRE_COMMIT__;
  _drush_coder_write_file("{$gitpath}/hooks/pre-commit", $pre_commit_code, 0755, $uninstall);
  $args = $_SERVER['argv'];
  foreach ($args as $delta => $arg) {
    if ($delta == 0 || $delta == 1 || $arg == '--y' || $arg == '--git' || $arg == '-y') {
      unset($args[$delta]);
    }
  }
  $coder_args = implode(' ', $args);

  // @ignore sniffer_squiz_whitespace_operatorspacing_nospaceafter
  $coder_commit_code = <<<__CODER_COMMIT__
#!/usr/bin/php
<?php
/**
 * {<span class="php-variable">$doxygen_file</span>}
 * Coder pre-commit hook.
 *
 * This script was created by drush coder-review --git.
 * Do not modify.
 */
\$GITPATH = '{<span class="php-variable">$gitpath</span>}';
\$GITRELATIVE = '{<span class="php-variable">$gitrelative</span>}';
\$CODER_ARGS = '--empty=no {<span class="php-variable">$coder_args</span>}';

// Find the files that are ready to checkin.
// The magic revision is for an initial commit: diff against an empty tree object.
// See .git/pre-commit.sample.
exec('git rev-parse --verify HEAD 2> /dev/null 2>&1', \$dummy, \$ret);
\$against = (\$ret == 0) ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
exec("git diff --cached --name-only \$against", \$files);

// Convert .git relative path to DRUPAL_ROOT relative.
foreach (\$files as &\$file) {
  \$file = "\$GITRELATIVE/\$file";
}

// Run coder against the .git files.
\$cmd = "drush \$CODER_ARGS";
print "Coder pre-commit: \$cmd\\n - " . implode("\\n - ", \$files) ."\\n\\n";
system(\$cmd . ' ' . implode(' ', \$files), \$ret);
if (\$ret) {
  exit(\$ret);
}
__CODER_COMMIT__;
  _drush_coder_write_file("{$gitpath}/hooks/pre-commit_coder", $coder_commit_code, 0755, $uninstall);
}