<commit_before><?php

namespace Contentacle\Resources;

/**
 * @uri /users/:username/repos/:repo/branches/:branch/commits/([0-9a-f]{40})/undo
 */
class Undo extends Resource {

    /**
     * Undo a commit by creating a new commit that undoes the actions of the first commit.
     *
     * @method post
     * @accepts application/hal+yaml
     * @accepts application/hal+json
     * @accepts application/yaml
     * @accepts application/json
     * @accepts application/x-www-form-urlencoded
     * @accepts text/plain
     * @accepts
     * @secure
     */
    function post($username, $repoName, $branchName, $sha)
    {
        try {
            $repo = $this->repoRepository->getRepo($username, $repoName);
            $commit = $repo->commit($branchName, $sha);
        } catch (\Git\Exception $e) {
            throw new \Tonic\NotFoundException;
        }

        if (isset($this->request->data['message']) && is_string($this->request->data['message'])) {
            $commitMessage = $this->request->data['message'];
        } elseif (is_string($this->request->data)) {
            $commitMessage = $this->request->data;
        } else {
            $commitMessage = 'Undo change '.$commit['sha'];
        }

        if ($revertSha = $repo->revert($commit['sha'], $commitMessage)) {
            $response = new \Tonic\Response(201);
            $response->location = $this->buildUrl($username, $repoName, $branchName, 'commits', $revertSha);
            return $response;
        } else {
            return new \Tonic\Response(400);
        }
        
    }

}<commit_msg>Improve error condition of undo resource<commit_after><?php

namespace Contentacle\Resources;

/**
 * @uri /users/:username/repos/:repo/branches/:branch/commits/([0-9a-f]{40})/undo
 */
class Undo extends Resource {

    /**
     * Undo a commit by creating a new commit that undoes the actions of the first commit.
     *
     * @method post
     * @accepts application/hal+yaml
     * @accepts application/hal+json
     * @accepts application/yaml
     * @accepts application/json
     * @accepts application/x-www-form-urlencoded
     * @accepts text/plain
     * @accepts
     * @secure
     */
    function post($username, $repoName, $branchName, $sha)
    {
        try {
            $repo = $this->repoRepository->getRepo($username, $repoName);
            $commit = $repo->commit($branchName, $sha);
        } catch (\Git\Exception $e) {
            throw new \Tonic\NotFoundException;
        }

        if (isset($this->request->data['message']) && is_string($this->request->data['message'])) {
            $commitMessage = $this->request->data['message'];
        } elseif (is_string($this->request->data)) {
            $commitMessage = $this->request->data;
        } else {
            $commitMessage = 'Undo change “'.$commit['message'].'”';
        }

        if ($revertSha = $repo->revert($commit['sha'], $commitMessage)) {
            $response = $this->response(201);
            $response->location = $this->buildUrl($username, $repoName, $branchName, 'commits', $revertSha);
        } else {
            $response = $this->response(409, 'error');
            $response->addVar('message', 'Conflict');
            $response->addError('conflict', 'Could not undo this commit, it may already be undone or future commits may prevent this commit from being removed.');
            $response->addLink('exit', $this->buildUrl($username, $repoName, $branchName, 'commits'));
        }

        return $response;
    }

}
{'fraction_non_alphanumeric': 0.15642775881683732, 'fraction_numerical': 0.005688282138794084, 'mean_word_length': 2.454813359528487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2957708', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1025, 'n_words': 216}