Skip to content

Commit f9b3653

Browse files
authored
Method to check if a branch is merged (added code from PR #151) (#197)
* added code from PR #151 * added user credentials which are needed for the next commands * stylistic changes to satisfy style-ci * moved user credentials to the top * stylistic changes to satisfy style-ci
1 parent 00b57b7 commit f9b3653

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Gitonomy/Git/Reference/Branch.php

+45
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
namespace Gitonomy\Git\Reference;
1414

15+
use Gitonomy\Git\Exception\ProcessException;
1516
use Gitonomy\Git\Exception\RuntimeException;
1617
use Gitonomy\Git\Reference;
18+
use Gitonomy\Git\Util\StringHelper;
1719

1820
/**
1921
* Representation of a branch reference.
@@ -53,6 +55,49 @@ public function isLocal()
5355
return $this->local;
5456
}
5557

58+
/**
59+
* Check if this branch is merged to a destination branch
60+
* Optionally, check only with remote branches.
61+
*
62+
* @param string $destinationBranchName
63+
* @param bool $compareOnlyWithRemote
64+
*
65+
* @return null|bool
66+
*/
67+
public function isMergedTo($destinationBranchName = 'master', $compareOnlyWithRemote = false)
68+
{
69+
$arguments = ['-a'];
70+
71+
if ($compareOnlyWithRemote) {
72+
$arguments = ['-r'];
73+
}
74+
75+
$arguments[] = '--merged';
76+
$arguments[] = $destinationBranchName;
77+
78+
try {
79+
$result = $this->repository->run('branch', $arguments);
80+
} catch (ProcessException $e) {
81+
throw new RuntimeException(
82+
sprintf('Cannot determine if merged to the branch "%s"', $destinationBranchName),
83+
$e->getCode(),
84+
$e
85+
);
86+
}
87+
88+
if (!$result) {
89+
return false;
90+
}
91+
92+
$output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result)));
93+
$filtered_output = array_filter($output, static function ($v) {
94+
return false === StringHelper::strpos($v, '->');
95+
});
96+
$trimmed_output = array_map('trim', $filtered_output);
97+
98+
return in_array($this->getName(), $trimmed_output, true);
99+
}
100+
56101
private function detectBranchType()
57102
{
58103
if (null === $this->local) {

tests/Gitonomy/Git/Tests/ReferenceTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,32 @@ public function testCreateAndDeleteBranch($repository)
209209
$branch->delete();
210210
$this->assertFalse($references->hasBranch('foobar'), 'Branch foobar removed');
211211
}
212+
213+
/**
214+
* @dataProvider provideFoobar
215+
*/
216+
public function testIsBranchMergedToMaster()
217+
{
218+
$repository = self::createFoobarRepository(false);
219+
220+
$repository->run('config', ['--local', 'user.name', '"Unit Test"']);
221+
$repository->run('config', ['--local', 'user.email', '"[email protected]"']);
222+
223+
$master = $repository->getReferences()->getBranch('master');
224+
$references = $repository->getReferences();
225+
226+
$branch = $references->createBranch('foobar-new', $master->getCommit()->getHash());
227+
228+
$this->assertTrue($branch->isMergedTo('master'));
229+
230+
$wc = $repository->getWorkingCopy();
231+
$wc->checkout('foobar-new');
232+
233+
$file = $repository->getWorkingDir().'/foobar-test.txt';
234+
file_put_contents($file, 'test');
235+
$repository->run('add', [$file]);
236+
$repository->run('commit', ['-m', 'foobar-test.txt updated']);
237+
238+
$this->assertFalse($branch->isMergedTo('master'));
239+
}
212240
}

0 commit comments

Comments
 (0)