You are here

public function SiteAuditCheckCodebaseGitContributions::calculateScore in Site Audit 8.2

Implements \SiteAudit\Check\Abstract\calculateScore().

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/Codebase/GitContributions.php, line 90
Contains \SiteAudit\Check\Codebase\GitContributions.

Class

SiteAuditCheckCodebaseGitContributions
Class SiteAuditCheckCodebaseGitContributions.

Code

public function calculateScore() {

  // Check if site is a git repository.
  $is_git = exec('git rev-parse --is-inside-work-tree 2> /dev/null');
  if ($is_git !== 'true') {
    $this->registry['not_git'] = TRUE;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
  }

  // Check if the repository is from drupal.org.
  $output = array();
  $remote_url = array(
    'http://git.drupal.org/project/drupal',
    'github.com/drupal/drupal',
    'github.com/pantheon-systems/drops-8',
  );
  exec('git remote -v', $output);
  foreach ($output as $line) {
    $line = explode("\t", $line);
    foreach ($remote_url as $url) {
      if (strpos($line, $url) !== FALSE) {
        $this->registry['git_remote'] = TRUE;
        return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
      }
    }
  }

  // Get all users.
  $users = array();
  exec("git log --format='%aN' | sort -u", $users);

  // Get the lines of code for each user.
  $total = 0;
  foreach ($users as $user) {
    $command = "git log --no-merges --shortstat --author '{$user}' 2> /dev/null ";
    $command .= "| grep 'files\\? changed' 2> /dev/null ";
    $command .= "| awk '{if (\$5==\"insertions(+)\" || \$5==\"insertion(+)\") inserted+=\$4; else deleted+=\$4; deleted+=\$6} END {print inserted, deleted}' 2> /dev/null";
    $output = explode(' ', exec($command));
    $this->registry['git_contribution'][$user] = array(
      'inserted' => $output[0],
      'deleted' => $output[1],
    );
    $total += intval($output[0]) + intval($output[1]);
  }
  foreach ($users as $user) {
    $inserted = intval($this->registry['git_contribution'][$user]['inserted']);
    $deleted = intval($this->registry['git_contribution'][$user]['deleted']);
    $percentage = number_format(($inserted + $deleted) / $total * 100, 2);
    $this->registry['git_contribution_percentage'][$user] = $percentage;
  }
  arsort($this->registry['git_contribution_percentage']);
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
}