From e500250a1bd35850c98d738fee04b58f0188b478 Mon Sep 17 00:00:00 2001 From: Jan Pecha Date: Sun, 18 Aug 2013 15:34:40 +0200 Subject: [PATCH] GitRepository: updated getCurrentBranchName() Signed-off-by: Jan Pecha --- src/GitRepository.php | 25 ++++++++++++------------- tests/GitPhp/basic.phpt | 2 ++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index 432d51b..2de9e36 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -179,24 +179,23 @@ public function removeBranch($name) */ public function getCurrentBranchName() { - $output = array(); - $exitCode = NULL; - - $this->begin(); - exec('git branch', $output, $exitCode); - $this->end(); - - if($exitCode === 0 && is_array($output)) + try { - foreach($output as $line) - { - if($line[0] === '*') + $branch = $this->extractFromCommand('git branch', function($value) { + if(isset($value[0]) && $value[0] === '*') { - return trim(substr($line, 1)); + return trim(substr($value, 1)); } + + return FALSE; + }); + + if(is_array($branch)) + { + return $branch[0]; } } - + catch(GitException $e) {} throw new GitException('Getting current branch name failed.'); } diff --git a/tests/GitPhp/basic.phpt b/tests/GitPhp/basic.phpt index 47026d2..bcd2929 100644 --- a/tests/GitPhp/basic.phpt +++ b/tests/GitPhp/basic.phpt @@ -60,6 +60,7 @@ Assert::same(array( 'develop', 'master', ), $repo->getBranches()); +Assert::same('develop', $repo->getCurrentBranchName()); // ...change file $file = TEMP_DIR . '/first.txt'; @@ -74,6 +75,7 @@ $repo->commit('Changed first file.'); Assert::false($repo->isChanges()); $repo->checkout('master'); +Assert::same('master', $repo->getCurrentBranchName()); Assert::null($repo->getTags()); $repo->createTag('v0.9.0'); Assert::same(array(