Gush\Helper\GitHelper::branchExists PHP Method

branchExists() public method

public branchExists ( string $branch ) : boolean
$branch string
return boolean
    public function branchExists($branch)
    {
        $result = $this->processHelper->runCommand(['git', 'branch', '--list', $branch], true);
        if (1 >= ($exists = preg_match_all('#(?<=\\s)' . preg_quote($branch, '#') . '(?!\\w)$#m', $result))) {
            return 1 === $exists;
        }
        throw new \RuntimeException(sprintf('Invalid list of local branches found while searching for "%s"', $branch));
    }

Usage Example

示例#1
0
    public function testBranchExistsException()
    {
        $sourceBranch = 'my-feat-10';
        $list = <<<'EOL'
  my-feat-10
  my-feat-10
EOL;
        $processHelper = $this->prophesize(ProcessHelper::class);
        $this->unitGit = new GitHelper($processHelper->reveal(), $this->gitConfigHelper->reveal(), $this->filesystemHelper->reveal());
        $processHelper->runCommand(['git', 'branch', '--list', $sourceBranch], true)->willReturn($list);
        $this->setExpectedException('\\RuntimeException', sprintf('Invalid list of local branches found while searching for "%s"', $sourceBranch));
        $this->assertTrue($this->unitGit->branchExists($sourceBranch));
    }