protected $charToLine = array();
protected $lineToFirstChar = array();
+ private $cachedResults;
+ private $cacheVersion;
private $results = array();
private $minimumSeverity = ArcanistLintSeverity::SEVERITY_DISABLED;
$stopped = array();
$linters = $this->buildLinters();
+ if (!$linters) {
+ throw new ArcanistNoEffectException("No linters to run.");
+ }
+
+ $have_paths = false;
+ foreach ($linters as $linter) {
+ if ($linter->getPaths()) {
+ $have_paths = true;
+ break;
+ }
+ }
+
+ if (!$have_paths) {
+ throw new ArcanistNoEffectException("No paths are lintable.");
+ }
+
+ $versions = array($this->getCacheVersion());
+ foreach ($linters as $linter) {
+ $versions[] = get_class($linter).':'.$linter->getCacheVersion();
+ }
+ $this->cacheVersion = crc32(implode("\n", $versions));
+
$exceptions = array();
foreach ($linters as $linter_name => $linter) {
try {
if (isset($stopped[$path])) {
unset($paths[$key]);
}
+ // TODO: Some linters work with the whole directory.
+ if (isset($this->cachedResults[$path][$this->cacheVersion])) {
+ unset($paths[$key]);
+ }
}
$paths = array_values($paths);
}
}
+ if ($this->cachedResults) {
+ foreach ($this->cachedResults as $path => $messages) {
+ foreach (idx($messages, $this->cacheVersion, array()) as $message) {
+ $this->getResultForPath($path)->addMessage(
+ ArcanistLintMessage::newFromDictionary($message));
+ }
+ }
+ }
+
foreach ($this->results as $path => $result) {
$disk_path = $this->getFilePathOnDisk($path);
$result->setFilePathOnDisk($disk_path);
}
}
- if (!$linters) {
- throw new ArcanistNoEffectException("No linters to run.");
- }
-
- $have_paths = false;
- foreach ($linters as $linter) {
- if ($linter->getPaths()) {
- $have_paths = true;
- break;
- }
- }
-
- if (!$have_paths) {
- throw new ArcanistNoEffectException("No paths are lintable.");
- }
-
if ($exceptions) {
throw new PhutilAggregateException('Some linters failed:', $exceptions);
}
return $this->results;
}
+ /**
+ * @param dict<string path, dict<int version, list<dict message>>>
+ * @return this
+ */
+ public function setCachedResults(array $results) {
+ $this->cachedResults = $results;
+ return $this;
+ }
+
public function getResults() {
return $this->results;
}
return false;
}
- public function getResultForPath($path) {
+ protected function getResultForPath($path) {
if (empty($this->results[$path])) {
$result = new ArcanistLintResult();
$result->setPath($path);
+ $result->setCacheVersion($this->cacheVersion);
$this->results[$path] = $result;
}
return $this->results[$path];
return $this;
}
- public function getCacheVersion() {
+ protected function getCacheVersion() {
return 0;
}
const DEFAULT_SEVERITY = ArcanistLintSeverity::SEVERITY_ADVICE;
private $unresolvedMessages;
+ private $shouldLintAll;
private $shouldAmendChanges = false;
private $shouldAmendWithoutPrompt = false;
private $shouldAmendAutofixesWithoutPrompt = false;
return implode("\n", array(
get_class($this->engine),
$this->getArgument('severity', self::DEFAULT_SEVERITY),
- $this->getArgument('lintall'),
- $this->engine->getCacheVersion(),
+ $this->shouldLintAll,
));
}
throw new ArcanistUsageException("Specify either --rev or paths.");
}
- $should_lint_all = $this->getArgument('lintall');
+ $this->shouldLintAll = $this->getArgument('lintall');
if ($paths) {
// NOTE: When the user specifies paths, we imply --lintall and show all
// warnings for the paths in question. This is easier to deal with for
// us and less confusing for users.
- $should_lint_all = true;
+ $this->shouldLintAll = true;
}
$paths = $this->selectPathsForWorkflow($paths, $rev);
$engine->setMinimumSeverity(
$this->getArgument('severity', self::DEFAULT_SEVERITY));
- $cached = false;
if ($this->getArgument('cache')) {
- $paths = array_combine($paths, $paths);
$cache = $this->readScratchJSONFile('lint-cache.json');
$cache = idx($cache, $this->getCacheKey(), array());
+ $cached = array();
foreach ($cache as $path => $messages) {
$messages = idx($messages, md5_file($engine->getFilePathOnDisk($path)));
- // TODO: Some linters work with the whole directory.
if ($messages !== null) {
- foreach ($messages as $message) {
- $engine->getResultForPath($path)->addMessage(
- ArcanistLintMessage::newFromDictionary($message));
- }
- $cached = true;
- unset($paths[$path]);
+ $cached[$path] = $messages;
}
}
+ $engine->setCachedResults($cached);
}
// Propagate information about which lines changed to the lint engine.
// This is used so that the lint engine can drop warning messages
// concerning lines that weren't in the change.
$engine->setPaths($paths);
- if (!$should_lint_all) {
+ if (!$this->shouldLintAll) {
foreach ($paths as $path) {
// Note that getChangedLines() returns null to indicate that a file
// is binary or a directory (i.e., changed lines are not relevant).
try {
$engine->run();
} catch (Exception $ex) {
- if ($ex instanceof ArcanistNoEffectException && $cached) {
- // Swallow.
- } else {
- $failed = $ex;
- }
+ $failed = $ex;
}
$results = $engine->getResults();
}
$this->unresolvedMessages = $unresolved;
- if ($this->getArgument('cache')) {
- $cached = array();
+ $cache = $this->readScratchJSONFile('lint-cache.json');
+ $cached = idx($cache, $this->getCacheKey(), array());
+ if ($cached || $this->getArgument('cache')) {
foreach ($results as $result) {
$path = $result->getPath();
+ if (!$this->getArgument('cache')) {
+ unset($cached[$path]);
+ continue;
+ }
$hash = md5_file($engine->getFilePathOnDisk($path));
- $cached[$path] = array($hash => array());
+ $version = $result->getCacheVersion();
+ $cached[$path] = array($hash => array($version => array()));
foreach ($result->getMessages() as $message) {
if (!$message->isPatchApplied()) {
- $cached[$path][$hash][] = $message->toDictionary();
+ $cached[$path][$hash][$version][] = $message->toDictionary();
}
}
}
- $cache = $this->readScratchJSONFile('lint-cache.json');
$cache[$this->getCacheKey()] = $cached;
// TODO: Garbage collection.
$this->writeScratchJSONFile('lint-cache.json', $cache);