TQ\Git\Repository\Repository::open PHP Method

open() public static method

Opens a Git repository on the file system, optionally creates and initializes a new repository
public static open ( string $repositoryPath, Binary | string | null $git = null, boolean | integer $createIfNotExists = false, array | null $initArguments = null, boolean $findRepositoryRoot = true ) : Repository
$repositoryPath string The full path to the repository
$git TQ\Git\Cli\Binary | string | null The Git binary
$createIfNotExists boolean | integer False to fail on non-existing repositories, directory creation mode, such as 0755 if the command should create the directory and init the repository instead
$initArguments array | null Arguments to be passed to git-init if initializing a repository
$findRepositoryRoot boolean False to use the repository path as the root directory.
return Repository
    public static function open($repositoryPath, $git = null, $createIfNotExists = false, $initArguments = null, $findRepositoryRoot = true)
    {
        $git = Binary::ensure($git);
        $repositoryRoot = null;
        if (!is_string($repositoryPath)) {
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid path', $repositoryPath));
        }
        if ($findRepositoryRoot) {
            $repositoryRoot = self::findRepositoryRoot($repositoryPath);
        }
        if ($repositoryRoot === null) {
            if (!$createIfNotExists) {
                throw new \InvalidArgumentException(sprintf('"%s" is not a valid path', $repositoryPath));
            } else {
                if (!file_exists($repositoryPath) && !mkdir($repositoryPath, $createIfNotExists, true)) {
                    throw new \RuntimeException(sprintf('"%s" cannot be created', $repositoryPath));
                } else {
                    if (!is_dir($repositoryPath)) {
                        throw new \InvalidArgumentException(sprintf('"%s" is not a valid path', $repositoryPath));
                    }
                }
                self::initRepository($git, $repositoryPath, $initArguments);
                $repositoryRoot = $repositoryPath;
            }
        }
        if ($repositoryRoot === null) {
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid Git repository', $repositoryPath));
        }
        return new static($repositoryRoot, $git);
    }

Usage Example

Example #1
0
 public function givenRepositoryWithConfiguration()
 {
     $this->git = Repository::open($this->repository, '/usr/bin/git', 0755);
     $result = $this->git->transactional(function (TQ\Vcs\Repository\Transaction $t) {
         copy(realpath(dirname(__FILE__) . "/resources/{$this->configFileName}"), $this->repository . '/phploy.ini');
         $t->setCommitMsg('Add configuration');
     });
 }
All Usage Examples Of TQ\Git\Repository\Repository::open