From efa6f360535e33e0095a3674c8eba6db93642fed Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Tue, 31 Oct 2023 14:19:51 +0800 Subject: [PATCH] + Add create branch method for scm. --- lib/scm/gitea.class.php | 18 ++++++++++++++++++ lib/scm/gitlab.class.php | 19 +++++++++++++++++++ lib/scm/gitrepo.class.php | 18 ++++++++++++++++++ lib/scm/gogs.class.php | 18 ++++++++++++++++++ lib/scm/scm.class.php | 15 +++++++++++++++ 5 files changed, 88 insertions(+) diff --git a/lib/scm/gitea.class.php b/lib/scm/gitea.class.php index fbf21a2e28..f95ee982d8 100644 --- a/lib/scm/gitea.class.php +++ b/lib/scm/gitea.class.php @@ -178,6 +178,24 @@ class Gitea return $branches; } + /** + * Create a branch. + * + * @param string $branchName + * @param string $ref + * @access public + * @return bool + */ + public function createBranch($branchName = '', $ref = 'master') + { + chdir($this->root); + $res = execCmd(escapeCmd("{$this->client} checkout -b {$branchName} origin/{$ref}"), 'array'); + if(empty($res[0])) return array('result' => 'fail', 'message' => 'Branch is exists.'); + + execCmd(escapeCmd("{$this->client} push origin {$branchName}"), 'array'); + return array('result' => 'success', 'message' => ''); + } + /** * Get last log. * diff --git a/lib/scm/gitlab.class.php b/lib/scm/gitlab.class.php index b5d87b4c20..0a68ef1374 100644 --- a/lib/scm/gitlab.class.php +++ b/lib/scm/gitlab.class.php @@ -199,6 +199,25 @@ class gitlab return $branches; } + /** + * Create a branch. + * + * @param string $branchName + * @param string $ref + * @access public + * @return bool + */ + public function createBranch($branchName = '', $ref = 'master') + { + $param = new stdclass(); + $param->ref = $ref; + $param->branch = $branchName; + + $api = $this->root . 'branches?private_token=' . $this->token; + $result = json_decode(commonModel::http($api, $param)); + return array('result' => empty($result->name) ? 'fail' : 'success', 'message' => $result->message); + } + /** * Get last log. * diff --git a/lib/scm/gitrepo.class.php b/lib/scm/gitrepo.class.php index 704e2f4554..c91bd20311 100644 --- a/lib/scm/gitrepo.class.php +++ b/lib/scm/gitrepo.class.php @@ -166,6 +166,24 @@ class GitRepo return $branches; } + /** + * Create a branch. + * + * @param string $branchName + * @param string $ref + * @access public + * @return bool + */ + public function createBranch($branchName = '', $ref = 'master') + { + chdir($this->root); + $res = execCmd(escapeCmd("{$this->client} checkout -b {$branchName} origin/{$ref}"), 'array'); + if(empty($res[0])) return array('result' => 'fail', 'message' => 'Branch is exists.'); + + execCmd(escapeCmd("{$this->client} push origin {$branchName}"), 'array'); + return array('result' => 'success', 'message' => ''); + } + /** * Get last log. * diff --git a/lib/scm/gogs.class.php b/lib/scm/gogs.class.php index 9cd5cce82d..cd377b3274 100644 --- a/lib/scm/gogs.class.php +++ b/lib/scm/gogs.class.php @@ -178,6 +178,24 @@ class Gogs return $branches; } + /** + * Create a branch. + * + * @param string $branchName + * @param string $ref + * @access public + * @return bool + */ + public function createBranch($branchName = '', $ref = 'master') + { + chdir($this->root); + $res = execCmd(escapeCmd("{$this->client} checkout -b {$branchName} origin/{$ref}"), 'array'); + if(empty($res[0])) return array('result' => 'fail', 'message' => 'Branch is exists.'); + + execCmd(escapeCmd("{$this->client} push origin {$branchName}"), 'array'); + return array('result' => 'success', 'message' => ''); + } + /** * Get last log. * diff --git a/lib/scm/scm.class.php b/lib/scm/scm.class.php index eb76c9b1a5..d3e4744e02 100644 --- a/lib/scm/scm.class.php +++ b/lib/scm/scm.class.php @@ -58,6 +58,21 @@ class scm return $this->engine->branch(); } + /** + * Create a branch. + * + * @param string $branchName + * @param string $ref + * @access public + * @return bool + */ + public function createBranch($branchName = '', $ref = 'master') + { + if(get_class($this->engine) == 'subversion') return false; + + return $this->engine->createBranch($branchName, $ref); + } + /** * Get log. *