Skip to content

Commit b413b0b

Browse files
committed
Added support for Commit::getIncludingBranches
1 parent 8bf8cda commit b413b0b

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,56 @@ public function getShortMessage($length = 50, $preserve = false, $separator = '.
302302
}
303303
}
304304

305+
/**
306+
* Find branch containing the commit
307+
*
308+
* @param boolean $local set true to try to locate a commit on local repository
309+
* @param boolean $remote set true to try to locate a commit on remote repository
310+
*
311+
* @return array An array of Reference\Branch
312+
*/
313+
public function getIncludingBranches($local = true, $remote = true)
314+
{
315+
$arguments = array('--contains', $this->hash);
316+
317+
if ($local && $remote) {
318+
$arguments[] = '-a';
319+
} elseif (!$local && $remote) {
320+
$arguments[] = '-r';
321+
} elseif (!$local && !$remote) {
322+
throw new \InvalidArgumentException('You should a least set one argument to true');
323+
}
324+
325+
try {
326+
$result = $this->repository->run('branch', $arguments);
327+
} catch (\Exception $e) {
328+
return array();
329+
}
330+
331+
if (!$result) {
332+
return array();
333+
}
334+
335+
$branchesName = explode("\n", trim(str_replace('*', '', $result)));
336+
$branchesName = array_filter($branchesName, function($v) { return false === strpos($v, '->');});
337+
$branchesName = array_map('trim', $branchesName);
338+
339+
$references = $this->repository->getReferences();
340+
341+
$branches = array();
342+
foreach ($branchesName as $branchName) {
343+
if (false === $local) {
344+
$branches[] = $references->getRemoteBranch($branchName);
345+
} elseif (0 === strrpos($branchName, 'remotes/')) {
346+
$branches[] = $references->getRemoteBranch(str_replace('remotes/', '', $branchName));
347+
} else {
348+
$branches[] = $references->getBranch($branchName);
349+
}
350+
}
351+
352+
return $branches;
353+
}
354+
305355
/**
306356
* Returns the author name.
307357
*

tests/Gitonomy/Git/Tests/CommitTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@ public function testGetBodyMessage($repository)
207207
$this->assertEquals('', $commit->getBodyMessage());
208208
}
209209

210+
/**
211+
* @expectedException InvalidArgumentException
212+
* @dataProvider provideFoobar
213+
*/
214+
public function testGetIncludingBranchesException($repository)
215+
{
216+
$commit = $repository->getCommit(self::INITIAL_COMMIT);
217+
218+
$commit->getIncludingBranches(false, false);
219+
}
220+
221+
/**
222+
* @dataProvider provideFoobar
223+
*/
224+
public function testGetIncludingBranches($repository)
225+
{
226+
$commit = $repository->getCommit(self::INITIAL_COMMIT);
227+
228+
$branches = $commit->getIncludingBranches(true, false);
229+
$this->assertCount(count($repository->getReferences()->getLocalBranches()), $branches);
230+
231+
$branches = $commit->getIncludingBranches(true, true);
232+
$this->assertCount(count($repository->getReferences()->getBranches()), $branches);
233+
234+
$branches = $commit->getIncludingBranches(false, true);
235+
$this->assertCount(count($repository->getReferences()->getRemoteBranches()), $branches);
236+
}
237+
210238
/**
211239
* @dataProvider provideFoobar
212240
*/

tests/Gitonomy/Git/Tests/HooksTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
namespace Gitonomy\Git\Tests;
1414

15-
use Gitonomy\Git\Admin;
16-
1715
class HooksTest extends AbstractTest
1816
{
1917
public function hookPath($repository, $hook)

tests/Gitonomy/Git/Tests/ReferenceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class ReferenceTest extends AbstractTest
1919
{
2020
private $references;
2121

22-
2322
/**
2423
* @dataProvider provideFoobar
2524
*/

0 commit comments

Comments
 (0)