comparison vendor/squizlabs/php_codesniffer/src/Reports/Hgblame.php @ 17:129ea1e6d783

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