getLastCommitHash() public method

Gets last (most recent) commit hash in the repository, or an empty string is there are no commits.
public getLastCommitHash ( string $options = "" ) : string
$options string Options passed to git log
return string Empty string or SHA1
 public function undoLastCommit()
 {
     $lastCommit = $this->repository->getLastCommitHash();
     try {
         $this->wpAutomation->runWpCliCommand('vp', 'undo', [$lastCommit]);
     } catch (\Exception $e) {
     }
     // Intentionally empty catch. It may throw an expcetion if the status code is not 0.
 }
 private function doInitializationCommit($isUpdate)
 {
     $this->checkTimeout();
     // Since WP-217 the `.active` file contains not the SHA1 of the first commit that VersionPress
     // created but the one before that (which may be an empty string if VersionPress's commit
     // was the first one in the repository).
     $lastCommitHash = $this->repository->getLastCommitHash();
     file_put_contents(VERSIONPRESS_ACTIVATION_FILE, $lastCommitHash);
     $this->reportProgressChange(InitializerStates::CREATING_INITIAL_COMMIT);
     $action = $isUpdate ? 'update' : 'activate';
     $committedFiles = [["type" => "path", "path" => "*"]];
     $changeInfo = $this->changeInfoFactory->createTrackedChangeInfo('versionpress', $action, VersionPress::getVersion(), [], $committedFiles);
     $currentUser = wp_get_current_user();
     /** @noinspection PhpUndefinedFieldInspection */
     $authorName = $currentUser->display_name;
     /** @noinspection PhpUndefinedFieldInspection */
     $authorEmail = $currentUser->user_email;
     if (defined('WP_CLI') && WP_CLI) {
         $authorName = GitConfig::$wpcliUserName;
         $authorEmail = GitConfig::$wpcliUserEmail;
     }
     try {
         $this->adjustGitProcessTimeout();
         $this->repository->stageAll();
         $this->adjustGitProcessTimeout();
         $this->repository->commit($changeInfo->getCommitMessage(), $authorName, $authorEmail);
     } catch (ProcessTimedOutException $ex) {
         $this->abortInitialization();
     }
 }
 /**
  * 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());
     }
 }
 /**
  * @param WP_REST_Request $request
  * @return WP_REST_Response|WP_Error
  */
 public function rollbackToCommit(WP_REST_Request $request)
 {
     $commitHash = $request['commit'];
     $initialCommitHash = $this->getInitialCommitHash();
     $log = $this->gitRepository->log($commitHash);
     if (!preg_match('/^[0-9a-f]+$/', $commitHash) || count($log) === 0) {
         return new WP_Error('error', 'Invalid commit hash', ['status' => 404]);
     }
     if (!$this->gitRepository->wasCreatedAfter($commitHash, $initialCommitHash) && $log[0]->getHash() !== $initialCommitHash) {
         return new WP_Error('error', 'Cannot roll back before initial commit', ['status' => 403]);
     }
     if ($log[0]->getHash() === $this->gitRepository->getLastCommitHash()) {
         return new WP_Error('error', 'Nothing to commit. Current state is the same as the one you want rollback to.', ['status' => 403]);
     }
     return $this->revertCommits('rollback', [$commitHash]);
 }
Example #5
0
 /**
  * @test
  */
 public function revertAllCanRevertToCommitInParallelBranch()
 {
     VPCommandUtils::exec('git branch test', self::$repositoryPath);
     $this->commitFile('some-file', 'Some commit');
     VPCommandUtils::exec('git checkout test', self::$repositoryPath);
     $this->commitFile('other-file', 'Other commit');
     $hash = self::$repository->getLastCommitHash();
     VPCommandUtils::exec('git checkout master', self::$repositoryPath);
     VPCommandUtils::exec('git merge test', self::$repositoryPath);
     $commitAsserter = $this->createCommitAsserter();
     self::$repository->revertAll($hash);
     $this->commit('Revert all');
     $commitAsserter->assertNumCommits(1);
     $commitAsserter->assertCleanWorkingDirectory();
     $commitAsserter->assertCountOfAffectedFiles(1);
     $commitAsserter->assertCommitPath('D', 'some-file');
 }