Chris@17: Chris@17: * @author Greg Sherwood Chris@17: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@17: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@17: */ Chris@17: Chris@17: namespace PHP_CodeSniffer\Reports; Chris@17: Chris@17: use PHP_CodeSniffer\Exceptions\DeepExitException; Chris@17: Chris@17: class Hgblame extends VersionControl Chris@17: { Chris@17: Chris@17: /** Chris@17: * The name of the report we want in the output Chris@17: * Chris@17: * @var string Chris@17: */ Chris@17: protected $reportName = 'MERCURIAL'; Chris@17: Chris@17: Chris@17: /** Chris@17: * Extract the author from a blame line. Chris@17: * Chris@17: * @param string $line Line to parse. Chris@17: * Chris@17: * @return mixed string or false if impossible to recover. Chris@17: */ Chris@17: protected function getAuthor($line) Chris@17: { Chris@17: $blameParts = []; Chris@17: $line = preg_replace('|\s+|', ' ', $line); Chris@17: Chris@17: preg_match( Chris@17: '|(.+[0-9]{2}:[0-9]{2}:[0-9]{2}\s[0-9]{4}\s.[0-9]{4}:)|', Chris@17: $line, Chris@17: $blameParts Chris@17: ); Chris@17: Chris@17: if (isset($blameParts[0]) === false) { Chris@17: return false; Chris@17: } Chris@17: Chris@17: $parts = explode(' ', $blameParts[0]); Chris@17: Chris@17: if (count($parts) < 6) { Chris@17: return false; Chris@17: } Chris@17: Chris@17: $parts = array_slice($parts, 0, (count($parts) - 6)); Chris@17: Chris@17: return trim(preg_replace('|<.+>|', '', implode($parts, ' '))); Chris@17: Chris@17: }//end getAuthor() Chris@17: Chris@17: Chris@17: /** Chris@17: * Gets the blame output. Chris@17: * Chris@17: * @param string $filename File to blame. Chris@17: * Chris@17: * @return array Chris@17: */ Chris@17: protected function getBlameContent($filename) Chris@17: { Chris@17: $cwd = getcwd(); Chris@17: Chris@17: $fileParts = explode(DIRECTORY_SEPARATOR, $filename); Chris@17: $found = false; Chris@17: $location = ''; Chris@17: while (empty($fileParts) === false) { Chris@17: array_pop($fileParts); Chris@17: $location = implode($fileParts, DIRECTORY_SEPARATOR); Chris@17: if (is_dir($location.DIRECTORY_SEPARATOR.'.hg') === true) { Chris@17: $found = true; Chris@17: break; Chris@17: } Chris@17: } Chris@17: Chris@17: if ($found === true) { Chris@17: chdir($location); Chris@17: } else { Chris@17: $error = 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL; Chris@17: throw new DeepExitException($error, 3); Chris@17: } Chris@17: Chris@17: $command = 'hg blame -u -d -v "'.$filename.'" 2>&1'; Chris@17: $handle = popen($command, 'r'); Chris@17: if ($handle === false) { Chris@17: $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL; Chris@17: throw new DeepExitException($error, 3); Chris@17: } Chris@17: Chris@17: $rawContent = stream_get_contents($handle); Chris@17: fclose($handle); Chris@17: Chris@17: $blames = explode("\n", $rawContent); Chris@17: chdir($cwd); Chris@17: Chris@17: return $blames; Chris@17: Chris@17: }//end getBlameContent() Chris@17: Chris@17: Chris@17: }//end class