Platformsh\Cli\Helper\GitHelper::branchExists PHP Method

branchExists() public method

Check whether a branch exists.
public branchExists ( string $branchName, string $dir = null, boolean $mustRun = false ) : boolean
$branchName string The branch name.
$dir string The path to a Git repository.
$mustRun boolean Enable exceptions if the Git command fails.
return boolean
    public function branchExists($branchName, $dir = null, $mustRun = false)
    {
        // The porcelain command 'git branch' is less strict about character
        // encoding than (otherwise simpler) plumbing commands such as
        // 'git show-ref'.
        $result = $this->execute(['branch'], $dir, $mustRun);
        $branches = array_map(function ($line) {
            return trim(ltrim($line, '* '));
        }, explode("\n", $result));
        return in_array($branchName, $branches, TRUE);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Test GitHelper::branchExists().
  */
 public function testBranchExists()
 {
     $repository = $this->getRepositoryDir();
     chdir($repository);
     // Create a branch.
     shell_exec('git checkout -q -b new');
     // Add required Git config before committing.
     shell_exec('git config user.email [email protected]');
     shell_exec('git config user.name "Platform.sh CLI Test"');
     // Make a dummy commit so that there is a HEAD.
     touch($repository . '/README.txt');
     shell_exec('git add -A && git commit -qm "Initial commit"');
     $this->assertTrue($this->gitHelper->branchExists('new', $repository));
 }
All Usage Examples Of Platformsh\Cli\Helper\GitHelper::branchExists