Skip to content

Commit 3c9a1e8

Browse files
committed
Changed grep to use -P instead of -E and tidied up logic
1 parent 13b231c commit 3c9a1e8

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/PhpcsDiff.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,35 +248,32 @@ protected function getChangedFiles()
248248
protected function getChangedLinesPerFile(array $files)
249249
{
250250
$extract = [];
251-
$pattern = '@@ -[0-9]+(?:,[0-9]+)? \+([0-9]+)(,([0-9]+))? @@';
251+
$pattern = '@@ -[0-9]+(?:,[0-9]+)? \+([0-9]+)(?:,([0-9]+))? @@';
252252

253253
foreach ($files as $file => $data) {
254-
$lineDiff = shell_exec(
255-
'git diff -U0 ' . $this->baseBranch . ' ' . $this->currentBranch . ' ' . $file .
256-
' | grep -E \'' . $pattern . '\''
257-
);
258-
254+
$command = 'git diff -U0 ' . $this->baseBranch . ' ' . $this->currentBranch . ' ' . $file .
255+
' | grep -P ' . escapeshellarg($pattern);
256+
$lineDiff = shell_exec($command);
259257
$lines = array_filter(explode(PHP_EOL, $lineDiff));
260258
$linesChanged = [];
261259

262260
foreach ($lines as $line) {
263261
preg_match('/' . $pattern . '/', $line, $matches);
264-
$start = (int)$matches[1];
265262

266-
if (!isset($matches[2])) {
267-
// We only have a single line that was changed
268-
$linesChanged[] = $start;
269-
continue;
270-
}
263+
$start = $end = (int)$matches[1];
271264

272-
// Multiple lines were changed, so we need to create a range
273-
$length = (int)$matches[2];
274-
$end = $start + $length;
265+
// Multiple lines were changed, so we need to calculate the end line
266+
if (isset($matches[2])) {
267+
$length = (int)$matches[2];
268+
$end = $start + $length - 1;
269+
}
275270

276-
array_merge($linesChanged, range($start, $end));
271+
foreach (range($start, $end) as $l) {
272+
$linesChanged[$l] = null;
273+
}
277274
}
278275

279-
$extract[$file] = $linesChanged;
276+
$extract[$file] = array_keys($linesChanged);
280277
}
281278

282279
return $extract;

0 commit comments

Comments
 (0)