app\components\Git::getBranchList PHP Method

getBranchList() public method

获取分支列表
public getBranchList ( ) : array
return array
    public function getBranchList()
    {
        $destination = Project::getDeployFromDir();
        // 应该先更新,不然在remote git删除当前选中的分支后,获取分支列表会失败
        $this->updateRepo('master', $destination);
        $cmd[] = sprintf('cd %s ', $destination);
        $cmd[] = '/usr/bin/env git fetch -p';
        $cmd[] = '/usr/bin/env git pull -a';
        $cmd[] = '/usr/bin/env git branch -a';
        $command = join(' && ', $cmd);
        $result = $this->runLocalCommand($command);
        if (!$result) {
            throw new \Exception(\yii::t('walle', 'get branches failed') . $this->getExeLog());
        }
        $history = [];
        $list = explode(PHP_EOL, $this->getExeLog());
        foreach ($list as &$item) {
            $item = trim($item);
            $remotePrefix = 'remotes/origin/';
            $remoteHeadPrefix = 'remotes/origin/HEAD';
            // 只取远端的分支,排除当前分支
            if (strcmp(substr($item, 0, strlen($remotePrefix)), $remotePrefix) === 0 && strcmp(substr($item, 0, strlen($remoteHeadPrefix)), $remoteHeadPrefix) !== 0) {
                $item = substr($item, strlen($remotePrefix));
                $history[] = ['id' => $item, 'message' => $item];
            }
        }
        return $history;
    }

Usage Example

Example #1
0
 /**
  * 获取branch分支列表
  *
  * @param $projectId
  */
 public function actionGetBranch($projectId)
 {
     $git = new Git();
     $conf = Project::getConf($projectId);
     $git->setConfig($conf);
     $list = $git->getBranchList();
     $this->renderJson($list);
 }