Skip to content
This repository was archived by the owner on Dec 8, 2017. It is now read-only.

Commit 038d0a5

Browse files
author
Samuel Parkinson
committed
Refactored getStagedFiles for git.
1 parent 32a4acb commit 038d0a5

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/VersionControl/GitVersionControl.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace StaticReview\VersionControl;
1414

1515
use StaticReview\VersionControl\VersionControlInterface;
16+
use StaticReview\File\FileInterface;
1617
use StaticReview\File\File;
1718
use StaticReview\Collection\FileCollection;
1819

@@ -45,26 +46,37 @@ public function getStagedFiles()
4546
$output = array_filter(explode(PHP_EOL, $process->getOutput()));
4647

4748
foreach($output as $file) {
48-
4949
list($status, $path) = explode("\t", $file);
5050

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-
6151
$file = new File($status, $path, $base);
62-
$file->setCachedPath($cachedPath);
52+
$this->saveFileToCache($file);
6353

6454
$files->append($file);
65-
6655
}
6756

6857
return $files;
6958
}
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+
}
7082
}

0 commit comments

Comments
 (0)