Changeset View
Changeset View
Standalone View
Standalone View
src/parser/ArcanistDiffParser.php
Show First 20 Lines • Show All 211 Lines • ▼ Show 20 Lines | public function parseDiff($diff) { | ||||
// Strip off header comments. While `patch` allows comments anywhere in the | // Strip off header comments. While `patch` allows comments anywhere in the | ||||
// file, `git apply` is more strict. We get these comments in `hg export` | // file, `git apply` is more strict. We get these comments in `hg export` | ||||
// diffs, and Eclipse can also produce them. | // diffs, and Eclipse can also produce them. | ||||
$line = $this->getLineTrimmed(); | $line = $this->getLineTrimmed(); | ||||
while (preg_match('/^#/', $line)) { | while (preg_match('/^#/', $line)) { | ||||
$line = $this->nextLine(); | $line = $this->nextLine(); | ||||
} | } | ||||
if (strlen($message)) { | if ($message !== null && strlen($message)) { | ||||
// If we found a message during pre-parse steps, add it to the resulting | // If we found a message during pre-parse steps, add it to the resulting | ||||
// changes here. | // changes here. | ||||
$change = $this->buildChange(null) | $change = $this->buildChange(null) | ||||
->setType(ArcanistDiffChangeType::TYPE_MESSAGE) | ->setType(ArcanistDiffChangeType::TYPE_MESSAGE) | ||||
->setMetadata('message', $message); | ->setMetadata('message', $message); | ||||
} | } | ||||
do { | do { | ||||
▲ Show 20 Lines • Show All 348 Lines • ▼ Show 20 Lines | if ($is_git) { | ||||
'rename from (?P<old>.*)', | 'rename from (?P<old>.*)', | ||||
'(?P<move>rename) to (?P<cur>.*)', | '(?P<move>rename) to (?P<cur>.*)', | ||||
'copy from (?P<old>.*)', | 'copy from (?P<old>.*)', | ||||
'(?P<copy>copy) to (?P<cur>.*)', | '(?P<copy>copy) to (?P<cur>.*)', | ||||
); | ); | ||||
$ok = false; | $ok = false; | ||||
$match = null; | $match = null; | ||||
if ($line !== null) { | |||||
foreach ($patterns as $pattern) { | foreach ($patterns as $pattern) { | ||||
$ok = preg_match('@^'.$pattern.'@', $line, $match); | $ok = preg_match('@^'.$pattern.'@', $line, $match); | ||||
if ($ok) { | if ($ok) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | |||||
if (!$ok) { | if (!$ok) { | ||||
if ($line === null || | if ($line === null || | ||||
preg_match('/^(diff --git|commit) /', $line)) { | preg_match('/^(diff --git|commit) /', $line)) { | ||||
// In this case, there are ONLY file mode changes, or this is a | // In this case, there are ONLY file mode changes, or this is a | ||||
// pure move. If it's a move, flag these changesets so we can build | // pure move. If it's a move, flag these changesets so we can build | ||||
// synthetic changes later, enabling us to show file contents in | // synthetic changes later, enabling us to show file contents in | ||||
// Differential -- git only gives us a block like this: | // Differential -- git only gives us a block like this: | ||||
▲ Show 20 Lines • Show All 170 Lines • ▼ Show 20 Lines | protected function parseIndexHunk(ArcanistDiffChange $change) { | ||||
// patch. | // patch. | ||||
$is_git_binary_patch = preg_match( | $is_git_binary_patch = preg_match( | ||||
'/^GIT binary patch$/', | '/^GIT binary patch$/', | ||||
rtrim($line)); | rtrim($line)); | ||||
if ($is_git_binary_patch) { | if ($is_git_binary_patch) { | ||||
$this->nextLine(); | $this->nextLine(); | ||||
$this->parseGitBinaryPatch(); | $this->parseGitBinaryPatch(); | ||||
$line = $this->getLine(); | $line = $this->getLine(); | ||||
if (preg_match('/^literal/', $line)) { | if ($line !== null && preg_match('/^literal/', $line)) { | ||||
// We may have old/new binaries (change) or just a new binary (hg add). | // We may have old/new binaries (change) or just a new binary (hg add). | ||||
// If there are two blocks, parse both. | // If there are two blocks, parse both. | ||||
$this->parseGitBinaryPatch(); | $this->parseGitBinaryPatch(); | ||||
} | } | ||||
$this->markBinary($change); | $this->markBinary($change); | ||||
return; | return; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | do { | ||||
"Expected hunk header '%s'.", | "Expected hunk header '%s'.", | ||||
'@@ -NN,NN +NN,NN @@')); | '@@ -NN,NN +NN,NN @@')); | ||||
} | } | ||||
$hunk->setOldOffset($matches[1]); | $hunk->setOldOffset($matches[1]); | ||||
$hunk->setNewOffset($matches[3]); | $hunk->setNewOffset($matches[3]); | ||||
// Cover for the cases where length wasn't present (implying one line). | // Cover for the cases where length wasn't present (implying one line). | ||||
$old_len = idx($matches, 2); | $old_len = idx($matches, 2, ''); | ||||
if (!strlen($old_len)) { | if (!strlen($old_len)) { | ||||
$old_len = 1; | $old_len = 1; | ||||
} | } | ||||
$new_len = idx($matches, 4); | $new_len = idx($matches, 4, ''); | ||||
if (!strlen($new_len)) { | if (!strlen($new_len)) { | ||||
$new_len = 1; | $new_len = 1; | ||||
} | } | ||||
$hunk->setOldLength($old_len); | $hunk->setOldLength($old_len); | ||||
$hunk->setNewLength($new_len); | $hunk->setNewLength($new_len); | ||||
$add = 0; | $add = 0; | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | do { | ||||
$hunk->setDelLines($del); | $hunk->setDelLines($del); | ||||
$change->addHunk($hunk); | $change->addHunk($hunk); | ||||
} | } | ||||
if (!$hit_next_hunk) { | if (!$hit_next_hunk) { | ||||
$line = $this->nextNonemptyLine(); | $line = $this->nextNonemptyLine(); | ||||
} | } | ||||
} while (preg_match('/^@@ /', $line)); | } while (($line !== null) && preg_match('/^@@ /', $line)); | ||||
} | } | ||||
protected function buildChange($path = null) { | protected function buildChange($path = null) { | ||||
$change = null; | $change = null; | ||||
if ($path !== null) { | if ($path !== null) { | ||||
if (!empty($this->changes[$path])) { | if (!empty($this->changes[$path])) { | ||||
return $this->changes[$path]; | return $this->changes[$path]; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 346 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0