/**
* @param WP_REST_Request $request
* @return WP_REST_Response|\WP_Error
*/
public function getCommits(WP_REST_Request $request)
{
$gitLogPaginator = new GitLogPaginator($this->gitRepository);
$gitLogPaginator->setCommitsPerPage(25);
$page = intval($request['page']);
$commits = $gitLogPaginator->getPage($page);
if (empty($commits)) {
return new \WP_Error('notice', 'No more commits to show.', array('status' => 403));
}
$preActivationHash = trim(file_get_contents(VERSIONPRESS_ACTIVATION_FILE));
if (empty($preActivationHash)) {
$initialCommitHash = $this->gitRepository->getInitialCommit()->getHash();
} else {
$initialCommitHash = $this->gitRepository->getChildCommit($preActivationHash);
}
$isChildOfInitialCommit = $this->gitRepository->wasCreatedAfter($commits[0]->getHash(), $initialCommitHash);
$isFirstCommit = $page === 0;
$result = array();
foreach ($commits as $commit) {
$isChildOfInitialCommit = $isChildOfInitialCommit && $commit->getHash() !== $initialCommitHash;
$canUndoCommit = $isChildOfInitialCommit && !$commit->isMerge();
$canRollbackToThisCommit = !$isFirstCommit && ($isChildOfInitialCommit || $commit->getHash() === $initialCommitHash);
$changeInfo = ChangeInfoMatcher::buildChangeInfo($commit->getMessage());
$isEnabled = $isChildOfInitialCommit || $canRollbackToThisCommit || $commit->getHash() === $initialCommitHash;
$fileChanges = $this->getFileChanges($commit);
$changeInfoList = $changeInfo instanceof ChangeInfoEnvelope ? $changeInfo->getChangeInfoList() : array();
$result[] = array("hash" => $commit->getHash(), "date" => $commit->getDate()->format('c'), "message" => $changeInfo->getChangeDescription(), "canUndo" => $canUndoCommit, "canRollback" => $canRollbackToThisCommit, "isEnabled" => $isEnabled, "isInitial" => $commit->getHash() === $initialCommitHash, "isMerge" => $commit->isMerge(), "changes" => array_merge($this->convertChangeInfoList($changeInfoList), $fileChanges));
$isFirstCommit = false;
}
return new WP_REST_Response(array('pages' => $gitLogPaginator->getPrettySteps($page), 'commits' => $result));
}