annotate vendor/squizlabs/php_codesniffer/src/Reports/Hgblame.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2 /**
Chris@17 3 * Mercurial blame report for PHP_CodeSniffer.
Chris@17 4 *
Chris@17 5 * @author Ben Selby <benmatselby@gmail.com>
Chris@17 6 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@17 7 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@17 8 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@17 9 */
Chris@17 10
Chris@17 11 namespace PHP_CodeSniffer\Reports;
Chris@17 12
Chris@17 13 use PHP_CodeSniffer\Exceptions\DeepExitException;
Chris@17 14
Chris@17 15 class Hgblame extends VersionControl
Chris@17 16 {
Chris@17 17
Chris@17 18 /**
Chris@17 19 * The name of the report we want in the output
Chris@17 20 *
Chris@17 21 * @var string
Chris@17 22 */
Chris@17 23 protected $reportName = 'MERCURIAL';
Chris@17 24
Chris@17 25
Chris@17 26 /**
Chris@17 27 * Extract the author from a blame line.
Chris@17 28 *
Chris@17 29 * @param string $line Line to parse.
Chris@17 30 *
Chris@17 31 * @return mixed string or false if impossible to recover.
Chris@17 32 */
Chris@17 33 protected function getAuthor($line)
Chris@17 34 {
Chris@17 35 $blameParts = [];
Chris@17 36 $line = preg_replace('|\s+|', ' ', $line);
Chris@17 37
Chris@17 38 preg_match(
Chris@17 39 '|(.+[0-9]{2}:[0-9]{2}:[0-9]{2}\s[0-9]{4}\s.[0-9]{4}:)|',
Chris@17 40 $line,
Chris@17 41 $blameParts
Chris@17 42 );
Chris@17 43
Chris@17 44 if (isset($blameParts[0]) === false) {
Chris@17 45 return false;
Chris@17 46 }
Chris@17 47
Chris@17 48 $parts = explode(' ', $blameParts[0]);
Chris@17 49
Chris@17 50 if (count($parts) < 6) {
Chris@17 51 return false;
Chris@17 52 }
Chris@17 53
Chris@17 54 $parts = array_slice($parts, 0, (count($parts) - 6));
Chris@17 55
Chris@17 56 return trim(preg_replace('|<.+>|', '', implode($parts, ' ')));
Chris@17 57
Chris@17 58 }//end getAuthor()
Chris@17 59
Chris@17 60
Chris@17 61 /**
Chris@17 62 * Gets the blame output.
Chris@17 63 *
Chris@17 64 * @param string $filename File to blame.
Chris@17 65 *
Chris@17 66 * @return array
Chris@17 67 */
Chris@17 68 protected function getBlameContent($filename)
Chris@17 69 {
Chris@17 70 $cwd = getcwd();
Chris@17 71
Chris@17 72 $fileParts = explode(DIRECTORY_SEPARATOR, $filename);
Chris@17 73 $found = false;
Chris@17 74 $location = '';
Chris@17 75 while (empty($fileParts) === false) {
Chris@17 76 array_pop($fileParts);
Chris@17 77 $location = implode($fileParts, DIRECTORY_SEPARATOR);
Chris@17 78 if (is_dir($location.DIRECTORY_SEPARATOR.'.hg') === true) {
Chris@17 79 $found = true;
Chris@17 80 break;
Chris@17 81 }
Chris@17 82 }
Chris@17 83
Chris@17 84 if ($found === true) {
Chris@17 85 chdir($location);
Chris@17 86 } else {
Chris@17 87 $error = 'ERROR: Could not locate .hg directory '.PHP_EOL.PHP_EOL;
Chris@17 88 throw new DeepExitException($error, 3);
Chris@17 89 }
Chris@17 90
Chris@17 91 $command = 'hg blame -u -d -v "'.$filename.'" 2>&1';
Chris@17 92 $handle = popen($command, 'r');
Chris@17 93 if ($handle === false) {
Chris@17 94 $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
Chris@17 95 throw new DeepExitException($error, 3);
Chris@17 96 }
Chris@17 97
Chris@17 98 $rawContent = stream_get_contents($handle);
Chris@17 99 fclose($handle);
Chris@17 100
Chris@17 101 $blames = explode("\n", $rawContent);
Chris@17 102 chdir($cwd);
Chris@17 103
Chris@17 104 return $blames;
Chris@17 105
Chris@17 106 }//end getBlameContent()
Chris@17 107
Chris@17 108
Chris@17 109 }//end class