Skip to content

Commit

Permalink
Cache svn previous file by last changed version (#46)
Browse files Browse the repository at this point in the history
* Use last changed revision for svn files rather than repo revision

* Use file revision for old file cache hash of svn files

* Remove revisionId from caches

* Update tests to use revision Id for svn file hashes
  • Loading branch information
sirbrillig authored Mar 22, 2021
1 parent d67c70f commit c89ab3d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 99 deletions.
16 changes: 0 additions & 16 deletions PhpcsChanged/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public function save(): void {
$this->hasBeenModified = false;
}

public function getRevision(): ?string {
return $this->cacheObject->revisionId;
}

public function getCacheVersion(): string {
return $this->cacheObject->cacheVersion;
}
Expand Down Expand Up @@ -131,17 +127,6 @@ public function setCacheVersion(string $cacheVersion): void {
$this->cacheObject->cacheVersion = $cacheVersion;
}

public function setRevision(string $revisionId): void {
if (! $this->cacheObject->revisionId || $this->cacheObject->revisionId === $revisionId) {
$this->cacheObject->revisionId = $revisionId;
return;
}
($this->debug)("Revision has changed ('{$this->cacheObject->revisionId}' -> '{$revisionId}'). Clearing cache.");
$this->hasBeenModified = true;
$this->clearCache();
$this->cacheObject->revisionId = $revisionId;
}

public function getCacheForFile(string $filePath, string $type, string $hash, string $phpcsStandard): ?string {
$entry = $this->fileDataByPath[$filePath][$type][$hash][$phpcsStandard] ?? null;
if (! $entry) {
Expand Down Expand Up @@ -203,7 +188,6 @@ public function removeCacheEntry(CacheEntry $entry): void {
public function clearCache(): void {
($this->debug)("Cache cleared");
$this->hasBeenModified = true;
$this->cacheObject->revisionId = '';
$this->fileDataByPath = [];
$this->cacheObject->entries = [];
}
Expand Down
5 changes: 0 additions & 5 deletions PhpcsChanged/CacheObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ class CacheObject {
*/
public $entries = [];

/**
* @var string
*/
public $revisionId;

/**
* @var string
*/
Expand Down
11 changes: 2 additions & 9 deletions PhpcsChanged/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,15 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s

$oldFilePhpcsOutput = '';
if ( ! $isNewFile ) {
if (isCachingEnabled($options)) {
$cache->setRevision($revisionId);
}

$oldFilePhpcsOutput = isCachingEnabled($options) ? $cache->getCacheForFile($svnFile, 'old', '', $phpcsStandard ?? '') : null;
$oldFilePhpcsOutput = isCachingEnabled($options) ? $cache->getCacheForFile($svnFile, 'old', $revisionId, $phpcsStandard ?? '') : null;
if ($oldFilePhpcsOutput) {
$debug("Using cache for old file '{$svnFile}' at revision '{$revisionId}' and standard '{$phpcsStandard}'");
}
if (! $oldFilePhpcsOutput) {
$debug("Not using cache for old file '{$svnFile}' at revision '{$revisionId}' and standard '{$phpcsStandard}'");
$oldFilePhpcsOutput = getSvnBasePhpcsOutput($svnFile, $svn, $phpcs, $phpcsStandardOption, [$shell, 'executeCommand'], $debug);
if (isCachingEnabled($options)) {
$cache->setCacheForFile($svnFile, 'old', '', $phpcsStandard ?? '', $oldFilePhpcsOutput);
$cache->setCacheForFile($svnFile, 'old', $revisionId, $phpcsStandard ?? '', $oldFilePhpcsOutput);
}
}
}
Expand Down Expand Up @@ -333,9 +329,6 @@ function runGitWorkflowForFile(string $gitFile, array $options, ShellOperator $s
$isNewFile = isNewGitFile($gitFile, $git, [$shell, 'executeCommand'], $options, $debug);
$oldFilePhpcsOutput = '';
if (! $isNewFile) {
if (isCachingEnabled($options)) {
$cache->setRevision(''); // git files are all protected by a hash key; there is no need to invalidate the cache if the version changes
}
$oldFileHash = getOldGitFileHash($gitFile, $git, $cat, [$shell, 'executeCommand'], $options, $debug);
$oldFilePhpcsOutput = isCachingEnabled($options) ? $cache->getCacheForFile($gitFile, 'old', $oldFileHash, $phpcsStandard ?? '') : null;
if ($oldFilePhpcsOutput) {
Expand Down
6 changes: 0 additions & 6 deletions PhpcsChanged/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function load(): CacheObject {
}
$cacheObject = new CacheObject();
$cacheObject->cacheVersion = $decoded['cacheVersion'];
$cacheObject->revisionId = $decoded['revisionId'];
foreach($decoded['entries'] as $entry) {
if (! $this->isDecodedEntryValid($entry)) {
throw new \Exception('Invalid cache file entry: ' . $entry);
Expand All @@ -42,7 +41,6 @@ public function load(): CacheObject {
public function save(CacheObject $cacheObject): void {
$data = [
'cacheVersion' => $cacheObject->cacheVersion,
'revisionId' => $cacheObject->revisionId,
'entries' => $cacheObject->entries,
];
$result = file_put_contents($this->cacheFilePath, json_encode($data));
Expand All @@ -57,7 +55,6 @@ public function save(CacheObject $cacheObject): void {
private function isDecodedDataValid($decoded): bool {
if (! is_array($decoded) ||
! array_key_exists('cacheVersion', $decoded) ||
! array_key_exists('revisionId', $decoded) ||
! array_key_exists('entries', $decoded) ||
! is_array($decoded['entries'])
) {
Expand All @@ -66,9 +63,6 @@ private function isDecodedDataValid($decoded): bool {
if (! is_string($decoded['cacheVersion'])) {
return false;
}
if (! is_string($decoded['revisionId'])) {
return false;
}
// Note that this does not validate the entries to avoid iterating over
// them twice. That should be done by isDecodedEntryValid.
return true;
Expand Down
2 changes: 1 addition & 1 deletion PhpcsChanged/SvnWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function isNewSvnFile(string $svnFileInfo): bool {
}

function getSvnRevisionId(string $svnFileInfo): string {
preg_match('/\bRevision:\s([^\n]+)/', $svnFileInfo, $matches);
preg_match('/\bLast Changed Rev:\s([^\n]+)/', $svnFileInfo, $matches);
$version = $matches[1] ?? null;
if (! $version) {
// New files will not have a revision
Expand Down
Loading

0 comments on commit c89ab3d

Please sign in to comment.