[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/data/ -> DiffusionGitBranch.php (source)

   1  <?php
   2  
   3  final class DiffusionGitBranch {
   4  
   5    const DEFAULT_GIT_REMOTE = 'origin';
   6  
   7    /**
   8     * Parse the output of 'git branch -r --verbose --no-abbrev' or similar into
   9     * a map. For instance:
  10     *
  11     *   array(
  12     *     'origin/master' => '99a9c082f9a1b68c7264e26b9e552484a5ae5f25',
  13     *   );
  14     *
  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:
  17     *
  18     *   array(
  19     *     'master' => '99a9c082f9a1b68c7264e26b9e552484a5ae5f25',
  20     *   );
  21     *
  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.
  25     */
  26    public static function parseRemoteBranchOutput(
  27      $stdout,
  28      $only_this_remote = null) {
  29      $map = array();
  30  
  31      $lines = array_filter(explode("\n", $stdout));
  32      foreach ($lines as $line) {
  33        $matches = null;
  34        if (preg_match('/^  (\S+)\s+-> (\S+)$/', $line, $matches)) {
  35            // This is a line like:
  36            //
  37            //   origin/HEAD          -> origin/master
  38            //
  39            // ...which we don't currently do anything interesting with, although
  40            // in theory we could use it to automatically choose the default
  41            // branch.
  42            continue;
  43        }
  44        if (!preg_match('/^ *(\S+)\s+([a-z0-9]{40})/', $line, $matches)) {
  45          throw new Exception("Failed to parse {$line}!");
  46        }
  47  
  48        $remote_branch = $matches[1];
  49        $branch_head = $matches[2];
  50  
  51        if (strpos($remote_branch, 'HEAD') !== false) {
  52          // let's assume that no one will call their remote or branch HEAD
  53          continue;
  54        }
  55  
  56        if ($only_this_remote) {
  57          $matches = null;
  58          if (!preg_match('#^([^/]+)/(.*)$#', $remote_branch, $matches)) {
  59            throw new Exception(
  60              "Failed to parse remote branch '{$remote_branch}'!");
  61          }
  62          $remote_name = $matches[1];
  63          $branch_name = $matches[2];
  64          if ($remote_name != $only_this_remote) {
  65            continue;
  66          }
  67          $map[$branch_name] = $branch_head;
  68        } else {
  69          $map[$remote_branch] = $branch_head;
  70        }
  71      }
  72  
  73      return $map;
  74    }
  75  
  76    /**
  77     * As above, but with no `-r`. Used for bare repositories.
  78     */
  79    public static function parseLocalBranchOutput($stdout) {
  80      $map = array();
  81  
  82      $lines = array_filter(explode("\n", $stdout));
  83      $regex = '/^[* ]*(\(no branch\)|\S+)\s+([a-z0-9]{40})/';
  84      foreach ($lines as $line) {
  85        $matches = null;
  86        if (!preg_match($regex, $line, $matches)) {
  87          throw new Exception("Failed to parse {$line}!");
  88        }
  89  
  90        $branch = $matches[1];
  91        $branch_head = $matches[2];
  92        if ($branch == '(no branch)') {
  93          continue;
  94        }
  95  
  96        $map[$branch] = $branch_head;
  97      }
  98  
  99      return $map;
 100    }
 101  
 102  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1