Skip to content

Commit 8bf8cda

Browse files
committed
Added support for remote branches
1 parent 559390b commit 8bf8cda

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

src/Gitonomy/Git/Reference/Branch.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,42 @@
2121
*/
2222
class Branch extends Reference
2323
{
24+
private $local = null;
25+
2426
/**
2527
* {@inheritdoc}
2628
*/
2729
public function getName()
2830
{
29-
if (!preg_match('#^refs/heads/(.*)$#', $this->fullname, $vars)) {
30-
throw new \RuntimeException(sprintf('Cannot extract branch name from "%s"', $this->fullname));
31+
if (preg_match('#^refs/heads/(?<name>.*)$#', $this->fullname, $vars)) {
32+
return $vars['name'];
33+
}
34+
35+
if (preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $this->fullname, $vars)) {
36+
return $vars['remote'].'/'.$vars['name'];
3137
}
3238

33-
return $vars[1];
39+
throw new \RuntimeException(sprintf('Cannot extract branch name from "%s"', $this->fullname));
40+
}
41+
42+
public function isRemote()
43+
{
44+
$this->detectBranchType();
45+
46+
return !$this->local;
47+
}
48+
49+
public function isLocal()
50+
{
51+
$this->detectBranchType();
52+
53+
return $this->local;
54+
}
55+
56+
private function detectBranchType()
57+
{
58+
if (null === $this->local) {
59+
$this->local = !preg_match('#^refs/remotes/(?<remote>[^/]*)/(?<name>.*)$#', $this->fullname);
60+
}
3461
}
3562
}

src/Gitonomy/Git/ReferenceBag.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ public function hasBranch($name)
143143
return $this->has('refs/heads/'.$name);
144144
}
145145

146+
public function hasRemoteBranch($name)
147+
{
148+
return $this->has('refs/remotes/'.$name);
149+
}
150+
146151
public function hasTag($name)
147152
{
148153
return $this->has('refs/tags/'.$name);
@@ -250,6 +255,40 @@ public function getBranches()
250255
return $result;
251256
}
252257

258+
/**
259+
* Returns all locales branches.
260+
*
261+
* @return array
262+
*/
263+
public function getLocalBranches()
264+
{
265+
$result = array();
266+
foreach ($this->getBranches() as $branch) {
267+
if ($branch->isLocal()) {
268+
$result[] = $branch;
269+
}
270+
}
271+
272+
return $result;
273+
}
274+
275+
/**
276+
* Returns all remote branches.
277+
*
278+
* @return array
279+
*/
280+
public function getRemoteBranches()
281+
{
282+
$result = array();
283+
foreach ($this->getBranches() as $branch) {
284+
if ($branch->isRemote()) {
285+
$result[] = $branch;
286+
}
287+
}
288+
289+
return $result;
290+
}
291+
253292
/**
254293
* @return array An associative array with fullname as key (refs/heads/master, refs/tags/0.1)
255294
*/
@@ -280,6 +319,16 @@ public function getBranch($name)
280319
return $this->get('refs/heads/'.$name);
281320
}
282321

322+
/**
323+
* @return Branch
324+
*/
325+
public function getRemoteBranch($name)
326+
{
327+
$this->initialize();
328+
329+
return $this->get('refs/remotes/'.$name);
330+
}
331+
283332
protected function initialize()
284333
{
285334
if (true === $this->initialized) {
@@ -289,7 +338,7 @@ protected function initialize()
289338

290339
try {
291340
$parser = new Parser\ReferenceParser();
292-
$output = $this->repository->run('show-ref', array('--tags', '--heads'));
341+
$output = $this->repository->run('show-ref');
293342
} catch (\RuntimeException $e) {
294343
$output = $e->getOutput();
295344
$error = $e->getErrorOutput();
@@ -302,11 +351,14 @@ protected function initialize()
302351
foreach ($parser->references as $row) {
303352
list($commitHash, $fullname) = $row;
304353

305-
if (preg_match('#^refs/heads/(.*)$#', $fullname, $vars)) {
354+
if (preg_match('#^refs/(heads|remotes)/(.*)$#', $fullname)) {
355+
if (preg_match('#.*HEAD$#', $fullname)) {
356+
continue;
357+
}
306358
$reference = new Reference\Branch($this->repository, $fullname, $commitHash);
307359
$this->references[$fullname] = $reference;
308360
$this->branches[] = $reference;
309-
} elseif (preg_match('#^refs/tags/(.*)$#', $fullname, $vars)) {
361+
} elseif (preg_match('#^refs/tags/(.*)$#', $fullname)) {
310362
$reference = new Reference\Tag($this->repository, $fullname, $commitHash);
311363
$this->references[$fullname] = $reference;
312364
$this->tags[] = $reference;

tests/Gitonomy/Git/Tests/ReferenceTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ public function testResolveBranches($repository)
113113

114114
$resolved = $repository->getReferences()->resolveBranches($master->getCommitHash());
115115

116-
$this->assertEquals(1, count($resolved), "1 revision resolved");
116+
if ($repository->isBare()) {
117+
$this->assertEquals(1, count($resolved), "1 revision resolved");
118+
} else {
119+
$this->assertEquals(2, count($resolved), "2 revision resolved");
120+
}
121+
117122
$this->assertTrue(reset($resolved) instanceof Branch, "Resolved object is a branch");
118123
}
119124

0 commit comments

Comments
 (0)