<commit_before><?php declare(strict_types=1);

namespace Gitilicious\GitClient\Git;

use Gitilicious\GitClient\Cli\Input\LongFlag;
use Gitilicious\GitClient\Cli\Input\PlainArgument;
use Gitilicious\GitClient\Cli\Input\ShortFlag;
use Gitilicious\GitClient\Client;
use Gitilicious\GitClient\FileSystem\Directory;
use Gitilicious\GitClient\FileSystem\File;

class Repository
{
    private $client;

    private $directory;

    private $name;

    public function __construct(Client $client, Directory $directory, string $name)
    {
        $this->client    = $client;
        $this->directory = $directory;
        $this->name      = $name;
    }

    public static function createBare(Client $client, Directory $directory, string $name): Repository
    {
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);

        $result = $client->run($repositoryDirectory, new PlainArgument('init'), new LongFlag('bare'));

        if (!$result->isSuccess()) {
            throw new Exception($result->getErrorMessage());
        }

        return new self($client, $directory, $name);
    }

    public static function create(Client $client, Directory $directory, string $name): Repository
    {
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);

        $result = $client->run($repositoryDirectory, new PlainArgument('init'));

        if (!$result->isSuccess()) {
            throw new Exception($result->getErrorMessage());
        }

        return new self($client, $directory, $name);
    }

    public function clone(Directory $directory, string $name): Repository
    {
        $repositoryDirectory = $directory->create($directory->getPath() . '/' . $name);

        $result = $this->client->run(
            $repositoryDirectory,
            new PlainArgument('clone'),
            new PlainArgument($this->getPath()),
            new PlainArgument($repositoryDirectory->getPath())
        );

        if (!$result->isSuccess()) {
            throw new Exception($result->getErrorMessage());
        }

        return new self($this->client, $directory, $name);
    }

    public function getPath(): string
    {
        return $this->directory->getPath() . '/' . $this->name;
    }

    public function getBranches(): Branches
    {
        $result = $this->client->run($this->getPath(), 'branch');

        if (!$result->isSuccess()) {
            throw new Exception('Cannot list the branches.');
        }

        $branches = new Branches($this->client);

        foreach ($result->getOutput() as $branch) {
            $branches->add(new Branch($branch));
        }

        return $branches;
    }

    public function add(File $file)<commit_msg>Removed throws because we are handling it in the client now<commit_after><?php declare(strict_types=1);

namespace Gitilicious\GitClient\Git;

use Gitilicious\GitClient\Cli\Input\LongFlag;
use Gitilicious\GitClient\Cli\Input\PlainArgument;
use Gitilicious\GitClient\Cli\Input\ShortFlag;
use Gitilicious\GitClient\Client;
use Gitilicious\GitClient\FileSystem\Directory;
use Gitilicious\GitClient\FileSystem\File;

class Repository
{
    private $client;

    private $directory;

    public function __construct(Client $client, Directory $directory)
    {
        $this->client    = $client;
        $this->directory = $directory;
    }

    public static function createBare(Client $client, Directory $directory): Repository
    {
        $client->run($directory, new PlainArgument('init'), new LongFlag('bare'));

        return new self($client, $directory);
    }

    public static function create(Client $client, Directory $directory): Repository
    {
        $client->run($directory, new PlainArgument('init'));

        return new self($client, $directory);
    }

    public function clone(Directory $directory): Repository
    {
        $this->client->run(
            $directory,
            new PlainArgument('clone'),
            new PlainArgument($this->directory->getPath()),
            new PlainArgument($directory->getPath())
        );

        return new self($this->client, $directory);
    }

    public function getPath(): string
    {
        return $this->directory->getPath();
    }

    public function getActiveBranch(): Branch
    {
        $branches = $this->getBranches();

        if (!count($branches)) {
            return new Branch('* master');
        }

        foreach ($branches as $branch) {
            if ($branch->isActive()) {
                return $branch;
            }
        }
    }

    public function getBranches(): Branches
    {
        $result = $this->client->run($this->directory, new PlainArgument('branch'));

        $branches = new Branches($this->client);

        foreach ($result->getOutput() as $branch) {
            $branches->add(new Branch($branch));
        }

        return $branches;
    }

    public function createBranch(string $name): Branch
    {
        $this->client->run(
            $this->directory,
            new PlainArgument('checkout'),
            new ShortFlag('b'),
            new PlainArgument($name)
        );

        return new Branch($name);
    }

    public function checkout(string $name): Branch
    {
        $this->client->run(
            $this->directory,
            new PlainArgument('checkout'),
            new PlainArgument($name)
        );

        return new Branch('* ' . $name);
    }

    public function add(File $file)
{'fraction_non_alphanumeric': 0.1354071363220494, 'fraction_numerical': 0.00036596523330283625, 'mean_word_length': 2.581913499344692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1167566', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1443, 'n_words': 330}