diff --git a/VersionControl/Git.php b/VersionControl/Git.php index abc4541..8c06865 100644 --- a/VersionControl/Git.php +++ b/VersionControl/Git.php @@ -185,6 +185,36 @@ public function getBranches() return $result; } + /** + * Get an array of remote branch names + * + * @param string $name The name of remote repository + * + * @return array + */ + public function getRemoteBranches($name = 'origin') + { + $result = array(); + + $commandResult = $this->getCommand('branch') + ->setOption('r') + ->execute(); + $commandResult = explode(PHP_EOL, rtrim($commandResult)); + + foreach ($commandResult as $v) { + $v = trim($v); + + $prefix = $name.'/'; + if (0 !== strpos($v, $prefix)) { + continue; + } + + $result[] = substr($v, strlen($prefix)); + } + + return $result; + } + /** * Get a current branch name * diff --git a/test/VersionControl_GitTest.php b/test/VersionControl_GitTest.php index 9dbdaa2..d95b2ab 100644 --- a/test/VersionControl_GitTest.php +++ b/test/VersionControl_GitTest.php @@ -106,6 +106,19 @@ public function testGetBranches() $this->assertEquals($branches[7], 'master'); } + public function testGetRemoteBranches() + { + $instance = new VersionControl_Git('./fixtures/001_VersionControl_Git'); + + $branches = $instance->getRemoteBranches(); + + $this->assertEquals(count($branches), 8); + $this->assertEquals($branches[0], 'branch1'); + $this->assertEquals($branches[7], 'master'); + + $this->assertEquals(count($instance->getRemoteBranches('undefined-repository')), 0); + } + public function testGetCurrentBranch() { $instance = new VersionControl_Git('./fixtures/001_VersionControl_Git'); diff --git a/test/fixtures.tar.gz b/test/fixtures.tar.gz index 5da190f..61ffb9a 100644 Binary files a/test/fixtures.tar.gz and b/test/fixtures.tar.gz differ