Skip to content

Allow checking if a branch is merged #159

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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ before_install: composer require "symfony/process:${SYMFONY_VERSION}" --no-updat

install: travis_retry composer install --no-interaction --no-progress --no-suggest

before_script:
- git config --global user.email "you@example.com"
- git config --global user.name "Your Name"

script: vendor/bin/phpunit
41 changes: 41 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,45 @@ 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 bool
*/
public function isMergedTo($destinationBranchName, $compareOnlyWithRemote = false)
{
$arguments = ['-a'];

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

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

try {
$result = $this->repository->run('branch', $arguments);
} catch (ProcessException $e) {
throw new RuntimeException(
sprintf('Cannot determine if merged to the branch "%s"', $destinationBranchName),
$e->getCode(),
$e
);
}

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

return in_array($this->getName(), array_map('trim', $output), true);
}

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'));
}
}