/** * @test */ public function committerIsThreadSafe() { $gitRepository = new GitRepository(self::$repositoryDir, __DIR__); $gitRepository->init(); $numberOfParallelCommits = 50; $numberOfFilesInEachCommit = 10; /** @var Process[] $runningProcesses */ $runningProcesses = []; for ($i = 0; $i < $numberOfParallelCommits; $i++) { $from = $i * $numberOfFilesInEachCommit; $to = ($i + 1) * $numberOfFilesInEachCommit; $process = new Process("php generate-files-and-commit.php --from={$from} --to={$to}", __DIR__); $process->start(); $runningProcesses[] = $process; } foreach ($runningProcesses as $process) { $process->wait(); } $log = $gitRepository->log(); $this->assertCount($numberOfParallelCommits, $log); foreach ($log as $commit) { $files = $commit->getChangedFiles(); $this->assertCount($numberOfFilesInEachCommit, $files, "Files: \n" . join("\n", array_column($files, 'path'))); } }
/** * Create CommitAsserter to start tracking the git repo for future asserts. Should generally * be called after a test setup (if there is any) and before all the actual work. Asserts follow * after it. * * @param \VersionPress\Git\GitRepository $gitRepository * @param DbSchemaInfo $dbSchema * @param ActionsInfoProvider $actionsInfoProvider * @param string[] $pathPlaceholders */ public function __construct($gitRepository, DbSchemaInfo $dbSchema, ActionsInfoProvider $actionsInfoProvider, $pathPlaceholders = []) { $this->gitRepository = $gitRepository; $this->pathPlaceholders = $pathPlaceholders; $this->dbSchema = $dbSchema; $this->actionsInfoProvider = $actionsInfoProvider; if ($gitRepository->isVersioned()) { $this->startCommit = $gitRepository->getCommit($gitRepository->getLastCommitHash()); } }
/** * Calls Git `add -A` on files that are related to the given $changeInfo. * The "exchange format" is an array documented in {@see TrackedChangedInfo::getChangedFiles()}. * * @param TrackedChangeInfo|ChangeInfoEnvelope $changeInfo */ private function stageRelatedFiles($changeInfo) { if ($changeInfo instanceof ChangeInfoEnvelope) { /** @var TrackedChangeInfo $subChangeInfo */ foreach ($changeInfo->getChangeInfoList() as $subChangeInfo) { $this->stageRelatedFiles($subChangeInfo); } return; } $changes = $changeInfo->getChangedFiles(); foreach ($changes as $change) { if ($change["type"] === "storage-file") { $entityName = $change["entity"]; $entityId = $change["id"]; $parentId = $change["parent-id"]; $path = $this->storageFactory->getStorage($entityName)->getEntityFilename($entityId, $parentId); } elseif ($change["type"] === "all-storage-files") { $entityName = $change["entity"]; $path = $this->storageFactory->getStorage($entityName)->getPathCommonToAllEntities(); } elseif ($change["type"] === "path") { $path = $change["path"]; } else { continue; } $this->repository->stageAll($path); } }
private function convertChangeInfo($changeInfo) { $change = array(); if ($changeInfo instanceof TrackedChangeInfo) { $change['type'] = $changeInfo->getEntityName(); $change['action'] = $changeInfo->getAction(); $change['tags'] = $changeInfo->getCustomTags(); } if ($changeInfo instanceof EntityChangeInfo) { $change['name'] = $changeInfo->getEntityId(); } if ($changeInfo instanceof PluginChangeInfo) { $pluginTags = $changeInfo->getCustomTags(); $pluginName = $pluginTags[PluginChangeInfo::PLUGIN_NAME_TAG]; $change['name'] = $pluginName; } if ($changeInfo instanceof ThemeChangeInfo) { $themeTags = $changeInfo->getCustomTags(); $themeName = $themeTags[ThemeChangeInfo::THEME_NAME_TAG]; $change['name'] = $themeName; } if ($changeInfo instanceof WordPressUpdateChangeInfo) { $change['name'] = $changeInfo->getNewVersion(); } if ($changeInfo instanceof RevertChangeInfo) { $commit = $this->gitRepository->getCommit($changeInfo->getCommitHash()); $change['tags']['VP-Commit-Details'] = array('message' => $commit->getMessage()->getSubject(), 'date' => $commit->getDate()->format(\DateTime::ISO8601)); } return $change; }
private function revertToCommit($commitHash) { $this->repository->revertAll($commitHash); if (!$this->repository->willCommit()) { return RevertStatus::NOTHING_TO_COMMIT; } return RevertStatus::OK; }
/** * @return string */ private function getInitialCommitHash() { $preActivationHash = trim(file_get_contents(VERSIONPRESS_ACTIVATION_FILE)); if (empty($preActivationHash)) { return $this->gitRepository->getInitialCommit()->getHash(); } return $this->gitRepository->getChildCommit($preActivationHash); }
public function prepare_undoNonDbChange() { $newFile = 'vp-file.txt'; file_put_contents($this->testConfig->testSite->path . '/' . $newFile, ''); $this->repository->stageAll($newFile); $this->repository->commit('Manual commit', 'John Tester', '*****@*****.**'); return [['D', $newFile]]; }
public function restoreAllDefinitionFilesFromHistory() { FileSystem::removeContent($this->directory); $definitionFilesWildcard = WP_PLUGIN_DIR . '/*/.versionpress/actions.yml'; $modifications = $this->gitRepository->getFileModifications($definitionFilesWildcard); $modifications = array_filter($modifications, function ($modification) { return $modification['status'] !== 'D'; }); $lastModifications = ArrayUtils::unique($modifications, function ($modification) { return $modification['path']; }); foreach ($lastModifications as $modification) { $fileContent = $this->gitRepository->getFileInRevision($modification['path'], $modification['commit']); $plugin = basename(dirname(dirname($modification['path']))); $targetFile = $this->getDefinitionFileName($plugin); file_put_contents($targetFile, $fileContent); } $this->saveDefinitionForPlugin('versionpress/versionpress.php'); }
private function adjustGitProcessTimeout() { $maxExecutionTime = intval(ini_get('max_execution_time')); if ($maxExecutionTime === 0) { $this->repository->setGitProcessTimeout(0); return; } $currentTime = microtime(true); $alreadyConsumedTime = $currentTime - $this->executionStartTime; $remainingTime = $maxExecutionTime - $alreadyConsumedTime; $this->checkTimeout(); $processTimeout = $remainingTime - self::TIME_FOR_ABORTION; $this->repository->setGitProcessTimeout($processTimeout); }
/** * @test */ public function allCommitsContainListOfChangedFiles() { touch(self::$repositoryPath . '/somefile'); self::$repository->stageAll(); self::$repository->commit(new CommitMessage("Some commit"), "Author name", "*****@*****.**"); touch(self::$repositoryPath . '/otherfile'); self::$repository->stageAll(); self::$repository->commit(new CommitMessage("Other commit"), "Author name", "*****@*****.**"); $log = self::$repository->log(); $lastCommit = $log[0]; $expectedChangedFilesInLastCommit = [['status' => 'A', 'path' => 'otherfile']]; $this->assertEquals($expectedChangedFilesInLastCommit, $lastCommit->getChangedFiles()); $previousCommit = $log[1]; $expectedChangedFilesInPreviousCommit = [['status' => 'A', 'path' => 'somefile']]; $this->assertEquals($expectedChangedFilesInPreviousCommit, $previousCommit->getChangedFiles()); }
private function commitEverything() { self::$repository->stageAll(); self::$repository->commit(new CommitMessage("Some commit"), "Author name", "*****@*****.**"); }
private function commitFile($file, $commitMessage, $fileContent = '') { file_put_contents(self::$repositoryPath . '/' . $file, $fileContent); self::$repository->stageAll(); $this->commit($commitMessage); }
public static function commit($message = 'Default commit message') { self::$gitRepository->stageAll(); self::$gitRepository->commit($message, GitConfig::$wpcliUserName, GitConfig::$wpcliUserEmail); }