Chris@17
|
1 <?php
|
Chris@17
|
2 /**
|
Chris@17
|
3 * SVN blame report for PHP_CodeSniffer.
|
Chris@17
|
4 *
|
Chris@17
|
5 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@17
|
8 */
|
Chris@17
|
9
|
Chris@17
|
10 namespace PHP_CodeSniffer\Reports;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Exceptions\DeepExitException;
|
Chris@17
|
13
|
Chris@17
|
14 class Svnblame extends VersionControl
|
Chris@17
|
15 {
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * The name of the report we want in the output
|
Chris@17
|
19 *
|
Chris@17
|
20 * @var string
|
Chris@17
|
21 */
|
Chris@17
|
22 protected $reportName = 'SVN';
|
Chris@17
|
23
|
Chris@17
|
24
|
Chris@17
|
25 /**
|
Chris@17
|
26 * Extract the author from a blame line.
|
Chris@17
|
27 *
|
Chris@17
|
28 * @param string $line Line to parse.
|
Chris@17
|
29 *
|
Chris@17
|
30 * @return mixed string or false if impossible to recover.
|
Chris@17
|
31 */
|
Chris@17
|
32 protected function getAuthor($line)
|
Chris@17
|
33 {
|
Chris@17
|
34 $blameParts = [];
|
Chris@17
|
35 preg_match('|\s*([^\s]+)\s+([^\s]+)|', $line, $blameParts);
|
Chris@17
|
36
|
Chris@17
|
37 if (isset($blameParts[2]) === false) {
|
Chris@17
|
38 return false;
|
Chris@17
|
39 }
|
Chris@17
|
40
|
Chris@17
|
41 return $blameParts[2];
|
Chris@17
|
42
|
Chris@17
|
43 }//end getAuthor()
|
Chris@17
|
44
|
Chris@17
|
45
|
Chris@17
|
46 /**
|
Chris@17
|
47 * Gets the blame output.
|
Chris@17
|
48 *
|
Chris@17
|
49 * @param string $filename File to blame.
|
Chris@17
|
50 *
|
Chris@17
|
51 * @return array
|
Chris@17
|
52 */
|
Chris@17
|
53 protected function getBlameContent($filename)
|
Chris@17
|
54 {
|
Chris@17
|
55 $command = 'svn blame "'.$filename.'" 2>&1';
|
Chris@17
|
56 $handle = popen($command, 'r');
|
Chris@17
|
57 if ($handle === false) {
|
Chris@17
|
58 $error = 'ERROR: Could not execute "'.$command.'"'.PHP_EOL.PHP_EOL;
|
Chris@17
|
59 throw new DeepExitException($error, 3);
|
Chris@17
|
60 }
|
Chris@17
|
61
|
Chris@17
|
62 $rawContent = stream_get_contents($handle);
|
Chris@17
|
63 fclose($handle);
|
Chris@17
|
64
|
Chris@17
|
65 $blames = explode("\n", $rawContent);
|
Chris@17
|
66
|
Chris@17
|
67 return $blames;
|
Chris@17
|
68
|
Chris@17
|
69 }//end getBlameContent()
|
Chris@17
|
70
|
Chris@17
|
71
|
Chris@17
|
72 }//end class
|