]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/arcanist_util/cpp_linter/BaseDirectoryScopedFormatLinter.php
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / arcanist_util / cpp_linter / BaseDirectoryScopedFormatLinter.php
1 <?php
2 // Copyright 2004-present Facebook. All Rights Reserved.
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree. An additional grant
5 // of patent rights can be found in the PATENTS file in the same directory.
6
7 abstract class BaseDirectoryScopedFormatLinter extends ArcanistLinter {
8
9 const LINT_FORMATTING = 1;
10
11 private $changedLines = array();
12 private $rawLintOutput = array();
13
14 abstract protected function getPathsToLint();
15
16 protected function shouldLintPath($path) {
17 foreach ($this->getPathsToLint() as $p) {
18 // check if $path starts with $p
19 if (strncmp($path, $p, strlen($p)) === 0) {
20 return true;
21 }
22 }
23 return false;
24 }
25
26 // API to tell this linter which lines were changed
27 final public function setPathChangedLines($path, $changed) {
28 $this->changedLines[$path] = $changed;
29 }
30
31 final public function willLintPaths(array $paths) {
32 $futures = array();
33 foreach ($paths as $path) {
34 if (!$this->shouldLintPath($path)) {
35 continue;
36 }
37
38 $changed = $this->changedLines[$path];
39 if (!isset($changed)) {
40 // do not run linter if there are no changes
41 continue;
42 }
43
44 $futures[$path] = $this->getFormatFuture($path, $changed);
45 }
46
47 foreach (id(new FutureIterator($futures))->limit(8) as $p => $f) {
48 $this->rawLintOutput[$p] = $f->resolvex();
49 }
50 }
51
52 abstract protected function getFormatFuture($path, array $changed);
53 abstract protected function getLintMessage($diff);
54
55 final public function lintPath($path) {
56 if (!isset($this->rawLintOutput[$path])) {
57 return;
58 }
59
60 list($new_content) = $this->rawLintOutput[$path];
61 $old_content = $this->getData($path);
62
63 if ($new_content != $old_content) {
64 $diff = ArcanistDiffUtils::renderDifferences($old_content, $new_content);
65 $this->raiseLintAtOffset(
66 0,
67 self::LINT_FORMATTING,
68 $this->getLintMessage($diff),
69 $old_content,
70 $new_content);
71 }
72 }
73
74 }