HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/work.naniguide.com/vendor/gitonomy/gitlib/src/Gitonomy/Git/WorkingCopy.php
<?php

/**
 * This file is part of Gitonomy.
 *
 * (c) Alexandre Salomé <[email protected]>
 * (c) Julien DIDIER <[email protected]>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gitonomy\Git;

use Gitonomy\Git\Diff\Diff;
use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\LogicException;

/**
 * @author Alexandre Salomé <[email protected]>
 */
class WorkingCopy
{
    /**
     * @var Repository
     */
    protected $repository;

    public function __construct(Repository $repository)
    {
        $this->repository = $repository;

        if ($this->repository->isBare()) {
            throw new LogicException('Can\'t create a working copy on a bare repository');
        }
    }

    public function getUntrackedFiles()
    {
        $lines = explode("\0", $this->run('status', ['--porcelain', '--untracked-files=all', '-z']));
        $lines = array_filter($lines, function ($l) {
            return substr($l, 0, 3) === '?? ';
        });
        $lines = array_map(function ($l) {
            return substr($l, 3);
        }, $lines);

        return $lines;
    }

    public function getDiffPending()
    {
        $diff = Diff::parse($this->run('diff', ['-r', '-p', '-m', '-M', '--full-index']));
        $diff->setRepository($this->repository);

        return $diff;
    }

    public function getDiffStaged()
    {
        $diff = Diff::parse($this->run('diff', ['-r', '-p', '-m', '-M', '--full-index', '--staged']));
        $diff->setRepository($this->repository);

        return $diff;
    }

    /**
     * @return WorkingCopy
     */
    public function checkout($revision, $branch = null)
    {
        $args = [];
        if ($revision instanceof Commit) {
            $args[] = $revision->getHash();
        } elseif ($revision instanceof Reference) {
            $args[] = $revision->getFullname();
        } elseif (is_string($revision)) {
            $args[] = $revision;
        } else {
            throw new InvalidArgumentException(sprintf('Unknown type "%s"', gettype($revision)));
        }

        if (null !== $branch) {
            $args = array_merge($args, ['-b', $branch]);
        }

        $this->run('checkout', $args);

        return $this;
    }

    protected function run($command, array $args = [])
    {
        return $this->repository->run($command, $args);
    }
}