GitRepository.php 1.27 KiB
<?php declare(strict_types=1);
/*
 * irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer.
 * Copyright (C) 2018-2020 IRSTEA
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Irstea\CS\Git;
use Assert\Assertion;
/**
 * Class GitRepository.
final class GitRepository implements GitRepositoryInterface
    /**
     * @var string
    private $repositoryPath;
    /**
     * GitRepository constructor.
    public function __construct(string $repositoryPath)
        Assertion::directory($repositoryPath . '/.git');
        $this->repositoryPath = $repositoryPath;
    /**
     * {@inheritdoc}
    public function getHeadCommit(): string
        return trim(shell_exec('git -C ' . escapeshellarg($this->repositoryPath) . ' rev-parse HEAD'));
    /**
     * {@inheritdoc}
    public function getYearRange(): string
        $last = date('Y');
        $first = exec('git -C ' . escapeshellarg($this->repositoryPath) . ' log --format=%cd --date=format:%Y --date-order | tail -n1');
        if (!$first) {
            $first = '???';
        return ($last !== null && $last !== $first) ? "$first-$last" : $first;