From f649837785a7f1b797ac08a5b4b17bc2d6e2be37 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 17 Jul 2012 16:16:18 -0700 Subject: [PATCH] Dump problematic parser patches to temp files in "arc diff" Summary: I'm trying to get a repro for a Windows + SVN patch issue. Dump patches which fail to a temp file so there's less bewilderment in getting the right patch handed over for analysis. Test Plan: Forced a parse failure, ran "arc diff", inspected temp file. Reviewers: btrahan, vrana Reviewed By: btrahan CC: aran Differential Revision: https://secure.phabricator.com/D2997 --- src/parser/ArcanistDiffParser.php | 30 +++++++++++++++++++++++---- src/workflow/ArcanistBaseWorkflow.php | 6 ++++++ src/workflow/ArcanistDiffWorkflow.php | 9 ++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/parser/ArcanistDiffParser.php b/src/parser/ArcanistDiffParser.php index f95f12fe..441a170b 100644 --- a/src/parser/ArcanistDiffParser.php +++ b/src/parser/ArcanistDiffParser.php @@ -30,6 +30,8 @@ final class ArcanistDiffParser { protected $isMercurial; protected $detectBinaryFiles = false; protected $tryEncoding; + protected $rawDiff; + protected $writeDiffOnFailure; protected $changes = array(); private $forcePath; @@ -957,6 +959,7 @@ final class ArcanistDiffParser { } protected function didStartParse($text) { + $this->rawDiff = $text; // Eat leading whitespace. This may happen if the first change in the diff // is an SVN property change. @@ -1026,9 +1029,15 @@ final class ArcanistDiffParser { $this->text = null; } + public function setWriteDiffOnFailure($write) { + $this->writeDiffOnFailure = $write; + return $this; + } + protected function didFailParse($message) { - $min = max(0, $this->line - 3); - $max = min($this->line + 3, count($this->text) - 1); + $context = 3; + $min = max(0, $this->line - $context); + $max = min($this->line + $context, count($this->text) - 1); $context = ''; for ($ii = $min; $ii <= $max; $ii++) { @@ -1039,8 +1048,21 @@ final class ArcanistDiffParser { $this->text[$ii]); } - $message = "Parse Exception: {$message}\n\n{$context}\n"; - throw new Exception($message); + $out = array(); + $out[] = "Diff Parse Exception: {$message}"; + + if ($this->writeDiffOnFailure) { + $temp = new TempFile(); + $temp->setPreserveFile(true); + + Filesystem::writeFile($temp, $this->rawDiff); + $out[] = "Raw input file was written to: ".(string)$temp; + } + + $out[] = $context; + $out = implode("\n\n", $out); + + throw new Exception($out); } /** diff --git a/src/workflow/ArcanistBaseWorkflow.php b/src/workflow/ArcanistBaseWorkflow.php index 4fe084f8..301bd4c7 100644 --- a/src/workflow/ArcanistBaseWorkflow.php +++ b/src/workflow/ArcanistBaseWorkflow.php @@ -1287,4 +1287,10 @@ abstract class ArcanistBaseWorkflow { return $editor; } + protected function newDiffParser() { + $parser = new ArcanistDiffParser(); + $parser->setWriteDiffOnFailure(true); + return $parser; + } + } diff --git a/src/workflow/ArcanistDiffWorkflow.php b/src/workflow/ArcanistDiffWorkflow.php index f45548f0..ac71aca3 100644 --- a/src/workflow/ArcanistDiffWorkflow.php +++ b/src/workflow/ArcanistDiffWorkflow.php @@ -667,7 +667,7 @@ EOTEXT protected function generateChanges() { - $parser = new ArcanistDiffParser(); + $parser = $this->newDiffParser(); $is_raw = $this->isRawDiffSource(); if ($is_raw) { @@ -908,7 +908,8 @@ EOTEXT // textual data. if ($change->getNeedsSyntheticGitHunks()) { $diff = $repository_api->getRawDiffText($path, $moves = false); - $parser = new ArcanistDiffParser(); + $parser = $this->newDiffParser(); + $raw_changes = $parser->parseDiff($diff); foreach ($raw_changes as $raw_change) { if ($raw_change->getCurrentPath() == $path) { @@ -1008,7 +1009,7 @@ EOTEXT $conduit = $this->getConduit(); $repository_api = $this->getRepositoryAPI(); - $parser = new ArcanistDiffParser(); + $parser = $this->newDiffParser(); $history_messages = $repository_api->getGitHistoryLog(); if (!$history_messages) { // This can occur on the initial commit. @@ -1842,7 +1843,7 @@ EOTEXT private function getGitUpdateMessage() { $repository_api = $this->getRepositoryAPI(); - $parser = new ArcanistDiffParser(); + $parser = $this->newDiffParser(); $commit_messages = $repository_api->getGitCommitLog(); $commit_messages = $parser->parseDiff($commit_messages); -- 2.43.0