protected $api;
protected $text;
protected $line;
+ protected $lineSaved;
protected $isGit;
protected $isMercurial;
protected $detectBinaryFiles = false;
'(?P<type>diff -r) (?P<hgrev>[a-f0-9]+) (?:-r [a-f0-9]+ )?(?P<cur>.+)',
);
- $ok = false;
$line = $this->getLine();
$match = null;
- foreach ($patterns as $pattern) {
- $ok = preg_match('@^'.$pattern.'$@', $line, $match);
- if ($ok) {
- break;
+ $ok = $this->tryMatchHeader($patterns, $line, $match);
+
+ if (!$ok && $this->isFirstNonEmptyLine()) {
+ // 'hg export' command creates so called "extended diff" that
+ // contains some meta information and comment at the beginning
+ // (isFirstNonEmptyLine() to check for beginning). Actual mercurial
+ // code detects where comment ends and unified diff starts by
+ // searching "diff -r" in the text.
+ $this->saveLine();
+ $line = $this->nextLineThatLooksLikeDiffStart();
+ if (!$this->tryMatchHeader($patterns, $line, $match)) {
+ // Restore line before guessing to display correct error.
+ $this->restoreLine();
+ $this->didFailParse(
+ "Expected a hunk header, like 'Index: /path/to/file.ext' (svn), ".
+ "'Property changes on: /path/to/file.ext' (svn properties), ".
+ "'commit 59bcc3ad6775562f845953cf01624225' (git show), ".
+ "'diff --git' (git diff), '--- filename' (unified diff), or " .
+ "'diff -r' (hg diff or patch).");
}
}
- if (!$ok) {
- $this->didFailParse(
- "Expected a hunk header, like 'Index: /path/to/file.ext' (svn), ".
- "'Property changes on: /path/to/file.ext' (svn properties), ".
- "'commit 59bcc3ad6775562f845953cf01624225' (git show), ".
- "'diff --git' (git diff), or '--- filename' (unified diff).");
- }
-
if (isset($match['type'])) {
if ($match['type'] == 'diff --git') {
if (isset($match['old'])) {
return $this->changes;
}
+ protected function tryMatchHeader($patterns, $line, &$match) {
+ foreach ($patterns as $pattern) {
+ if (preg_match('@^'.$pattern.'$@', $line, $match)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
protected function parseCommitMessage(ArcanistDiffChange $change) {
$change->setType(ArcanistDiffChangeType::TYPE_MESSAGE);
return $this->getLine();
}
+ protected function nextLineThatLooksLikeDiffStart() {
+ while (($line = $this->nextLine()) !== null) {
+ if (preg_match('/^\s*diff\s+-r/', $line)) {
+ break;
+ }
+ }
+ return $this->getLine();
+ }
+
+ protected function saveLine() {
+ $this->lineSaved = $this->line;
+ }
+
+ protected function restoreLine() {
+ $this->line = $this->lineSaved;
+ }
+
+ protected function isFirstNonEmptyLine() {
+ $count = count($this->text);
+ for ($i = 0; $i < $count; $i++) {
+ if (strlen(trim($this->text[$i])) != 0) {
+ return ($i == $this->line);
+ }
+ }
+ // Entire file is empty.
+ return false;
+ }
+
protected function didFinishParse() {
$this->text = null;
}