3 final class DiffusionGitBranch
extends Phobject
{
5 const DEFAULT_GIT_REMOTE
= 'origin';
8 * Parse the output of 'git branch -r --verbose --no-abbrev' or similar into
12 * 'origin/master' => '99a9c082f9a1b68c7264e26b9e552484a5ae5f25',
15 * If you specify $only_this_remote, branches will be filtered to only those
16 * on the given remote, **and the remote name will be stripped**. For example:
19 * 'master' => '99a9c082f9a1b68c7264e26b9e552484a5ae5f25',
22 * @param string stdout of git branch command.
23 * @param string Filter branches to those on a specific remote.
24 * @return map Map of 'branch' or 'remote/branch' to hash at HEAD.
26 public static function parseRemoteBranchOutput(
28 $only_this_remote = null) {
31 $lines = array_filter(explode("\n", $stdout));
32 foreach ($lines as $line) {
34 if (preg_match('/^ (\S+)\s+-> (\S+)$/', $line, $matches)) {
35 // This is a line like:
37 // origin/HEAD -> origin/master
39 // ...which we don't currently do anything interesting with, although
40 // in theory we could use it to automatically choose the default
44 if (!preg_match('/^ *(\S+)\s+([a-z0-9]{40})/', $line, $matches)) {
47 'Failed to parse %s!',
51 $remote_branch = $matches[1];
52 $branch_head = $matches[2];
54 if (strpos($remote_branch, 'HEAD') !== false) {
55 // let's assume that no one will call their remote or branch HEAD
59 if ($only_this_remote) {
61 if (!preg_match('#^([^/]+)/(.*)$#', $remote_branch, $matches)) {
64 "Failed to parse remote branch '%s'!",
67 $remote_name = $matches[1];
68 $branch_name = $matches[2];
69 if ($remote_name != $only_this_remote) {
72 $map[$branch_name] = $branch_head;
74 $map[$remote_branch] = $branch_head;
82 * As above, but with no `-r`. Used for bare repositories.
84 public static function parseLocalBranchOutput($stdout) {
87 $lines = array_filter(explode("\n", $stdout));
88 $regex = '/^[* ]*(\(no branch\)|\S+)\s+([a-z0-9]{40})/';
89 foreach ($lines as $line) {
91 if (!preg_match($regex, $line, $matches)) {
94 'Failed to parse %s!',
98 $branch = $matches[1];
99 $branch_head = $matches[2];
100 if ($branch == '(no branch)') {
104 $map[$branch] = $branch_head;