Gitamin\Services\Git\Repository::readDiffLogs PHP Method

readDiffLogs() public method

Read diff logs and generate a collection of diffs.
public readDiffLogs ( array $logs ) : array
$logs array Array of log rows
return array Array of diffs
    public function readDiffLogs(array $logs)
    {
        $diffs = [];
        $lineNumOld = 0;
        $lineNumNew = 0;
        foreach ($logs as $log) {
            // Skip empty lines
            if ($log == '') {
                continue;
            }
            if ('diff' === substr($log, 0, 4)) {
                if (isset($diff)) {
                    $diffs[] = $diff;
                }
                $diff = new Diff();
                if (preg_match('/^diff --[\\S]+ a\\/?(.+) b\\/?/', $log, $name)) {
                    $diff->setFile($name[1]);
                }
                continue;
            }
            if ('index' === substr($log, 0, 5)) {
                $diff->setIndex($log);
                continue;
            }
            if ('---' === substr($log, 0, 3)) {
                $diff->setOld($log);
                continue;
            }
            if ('+++' === substr($log, 0, 3)) {
                $diff->setNew($log);
                continue;
            }
            // Handle binary files properly.
            if ('Binary' === substr($log, 0, 6)) {
                $m = [];
                if (preg_match('/Binary files (.+) and (.+) differ/', $log, $m)) {
                    $diff->setOld($m[1]);
                    $diff->setNew("    {$m[2]}");
                }
            }
            if (!empty($log)) {
                switch ($log[0]) {
                    case '@':
                        // Set the line numbers
                        preg_match('/@@ -([0-9]+)(?:,[0-9]+)? \\+([0-9]+)/', $log, $matches);
                        $lineNumOld = $matches[1] - 1;
                        $lineNumNew = $matches[2] - 1;
                        break;
                    case '-':
                        $lineNumOld++;
                        break;
                    case '+':
                        $lineNumNew++;
                        break;
                    default:
                        $lineNumOld++;
                        $lineNumNew++;
                }
            } else {
                $lineNumOld++;
                $lineNumNew++;
            }
            if (isset($diff)) {
                $diff->addLine($log, $lineNumOld, $lineNumNew);
            }
        }
        if (isset($diff)) {
            $diffs[] = $diff;
        }
        return $diffs;
    }