Skip to content

Allow checking if a branch is already merged to master (or another branch) #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Gitonomy/Git/Reference/Branch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

namespace Gitonomy\Git\Reference;

use Gitonomy\Git\Exception\ProcessException;
use Gitonomy\Git\Exception\RuntimeException;
use Gitonomy\Git\Reference;
use Gitonomy\Git\Util\StringHelper;

/**
* Representation of a branch reference.
Expand Down Expand Up @@ -53,6 +55,46 @@ public function isLocal()
return $this->local;
}

/**
*
* Check if this branch is merged to a destination branch
* Optionally, check only with remote branches
*
* @param string $destinationBranchName
* @param bool $compareOnlyWithRemote
*
* @return null|bool
*/
public function isMergedTo($destinationBranchName = 'master', $compareOnlyWithRemote = false)
{
$arguments = ['-a'];

if ($compareOnlyWithRemote) {
$arguments = ['-r'];
}

$arguments[] = '--merged';
$arguments[] = $destinationBranchName;

try {
$result = $this->repository->run('branch', $arguments);
} catch (ProcessException $e) {
return null;
}

if (!$result) {
return null;
}

$output = explode("\n", trim(str_replace(['*', 'remotes/'], '', $result)));
$output = array_filter($output, function ($v) {
return false === StringHelper::strpos($v, '->');
});
$output = array_map('trim', $output);

return (in_array($this->getName(), $output));
}

private function detectBranchType()
{
if (null === $this->local) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Gitonomy/Git/Tests/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,28 @@ public function testCreateAndDeleteBranch($repository)
$branch->delete();
$this->assertFalse($references->hasBranch('foobar'), 'Branch foobar removed');
}

/**
* @dataProvider provideFoobar
*/
public function testIsBranchMergedToMaster()
{
$repository = self::createFoobarRepository(false);

$master = $repository->getReferences()->getBranch('master');
$references = $repository->getReferences();
$branch = $references->createBranch('foobar-new', $master->getCommit()->getHash());

$this->assertTrue($branch->isMergedTo('master'));

$wc = $repository->getWorkingCopy();
$wc->checkout('foobar-new');

$file = $repository->getWorkingDir().'/foobar-test.txt';
file_put_contents($file, 'test');
$repository->run('add', [$file]);
$repository->run('commit', ['-m', 'foobar-test.txt updated']);

$this->assertFalse($branch->isMergedTo('master'));
}
}