|
13 | 13 | namespace StaticReview\VersionControl; |
14 | 14 |
|
15 | 15 | use StaticReview\VersionControl\VersionControlInterface; |
| 16 | +use StaticReview\File\FileInterface; |
16 | 17 | use StaticReview\File\File; |
17 | 18 | use StaticReview\Collection\FileCollection; |
18 | 19 |
|
@@ -45,26 +46,37 @@ public function getStagedFiles() |
45 | 46 | $output = array_filter(explode(PHP_EOL, $process->getOutput())); |
46 | 47 |
|
47 | 48 | foreach($output as $file) { |
48 | | - |
49 | 49 | list($status, $path) = explode("\t", $file); |
50 | 50 |
|
51 | | - $cachedPath = sys_get_temp_dir() . '/sjparkinson.static-review/cached/' . $path; |
52 | | - |
53 | | - if (! is_dir(dirname($cachedPath))) { |
54 | | - mkdir(dirname($cachedPath), 0700, true); |
55 | | - } |
56 | | - |
57 | | - $cmd = sprintf('git show :%s > %s', $path, $cachedPath); |
58 | | - $process = new Process($cmd); |
59 | | - $process->run(); |
60 | | - |
61 | 51 | $file = new File($status, $path, $base); |
62 | | - $file->setCachedPath($cachedPath); |
| 52 | + $this->saveFileToCache($file); |
63 | 53 |
|
64 | 54 | $files->append($file); |
65 | | - |
66 | 55 | } |
67 | 56 |
|
68 | 57 | return $files; |
69 | 58 | } |
| 59 | + |
| 60 | + /** |
| 61 | + * Saves a copy of the cached version of the given file to a temp directory. |
| 62 | + * |
| 63 | + * @param FileInterface $file |
| 64 | + * @return FileInterface |
| 65 | + */ |
| 66 | + private function saveFileToCache(FileInterface $file) |
| 67 | + { |
| 68 | + $cachedPath = sys_get_temp_dir() . '/sjparkinson.static-review/cached/' . $file->getRelativePath(); |
| 69 | + |
| 70 | + if (! is_dir(dirname($cachedPath))) { |
| 71 | + mkdir(dirname($cachedPath), 0700, true); |
| 72 | + } |
| 73 | + |
| 74 | + $cmd = sprintf('git show :%s > %s', $file->getRelativePath(), $cachedPath); |
| 75 | + $process = new Process($cmd); |
| 76 | + $process->run(); |
| 77 | + |
| 78 | + $file->setCachedPath($cachedPath); |
| 79 | + |
| 80 | + return $file; |
| 81 | + } |
70 | 82 | } |
0 commit comments