Skip to content

Commit b2c9a1c

Browse files
committed
Added list_branches and active_branch methods
1 parent 7ceb75e commit b2c9a1c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Git.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,41 @@ public function delete_branch($branch, $force = false) {
340340
return $this->run("branch ".(($force) ? '-D' : '-d')." $branch");
341341
}
342342

343+
/**
344+
* Runs a `git branch` call
345+
*
346+
* @access public
347+
* @param bool keep_asterisk Keep asterisk mark on active branch
348+
* @return array branch names
349+
*/
350+
public function list_branches($keep_asterisk = false) {
351+
$branchArray = explode("\n", $this->run("branch"));
352+
foreach($branchArray as $i => &$branch) {
353+
$branch = trim($branch);
354+
if(!$keep_asterisk)
355+
$branch = str_replace("* ", "", $branch);
356+
if($branch == "")
357+
unset($branchArray[$i]);
358+
}
359+
return $branchArray;
360+
}
361+
362+
/**
363+
* Retrurns name of active branch
364+
*
365+
* @access public
366+
* @param bool keep_asterisk Keep asterisk mark on branch name
367+
*/
368+
public function active_branch($keep_asterisk = false) {
369+
$branchArray = $this->list_branches(true);
370+
$active_branch = preg_grep("/^\*/", $branchArray);
371+
reset($active_branch);
372+
if($keep_asterisk)
373+
return current($active_branch);
374+
else
375+
return str_replace("* ", "", current($active_branch));
376+
}
377+
343378
/**
344379
* Runs a `git checkout` call
345380
*

0 commit comments

Comments
 (0)