summaryrefslogtreecommitdiffstats
path: root/services/repository/branch.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-17 23:17:31 +0800
committerGitHub <noreply@github.com>2021-11-17 23:17:31 +0800
commit5233051e64e90238bb7b6ddf9ecd1513e57bf8e9 (patch)
tree6e21565fd47954877bc14fd66f57d798c86e3300 /services/repository/branch.go
parent750a8465f547e9f08a87612c75898d56b8ec1f88 (diff)
downloadgitea-5233051e64e90238bb7b6ddf9ecd1513e57bf8e9.tar.gz
gitea-5233051e64e90238bb7b6ddf9ecd1513e57bf8e9.zip
Move some functions into services/repository (#17677)
Diffstat (limited to 'services/repository/branch.go')
-rw-r--r--services/repository/branch.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/services/repository/branch.go b/services/repository/branch.go
index 5e246cbec6..83980c5bd9 100644
--- a/services/repository/branch.go
+++ b/services/repository/branch.go
@@ -6,6 +6,7 @@ package repository
import (
"errors"
+ "fmt"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
@@ -15,6 +16,95 @@ import (
pull_service "code.gitea.io/gitea/services/pull"
)
+// CreateNewBranch creates a new repository branch
+func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
+ // Check if branch name can be used
+ if err := checkBranchName(repo, branchName); err != nil {
+ return err
+ }
+
+ if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
+ return models.ErrBranchDoesNotExist{
+ BranchName: oldBranchName,
+ }
+ }
+
+ if err := git.Push(repo.RepoPath(), git.PushOptions{
+ Remote: repo.RepoPath(),
+ Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
+ Env: models.PushingEnvironment(doer, repo),
+ }); err != nil {
+ if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
+ return err
+ }
+ return fmt.Errorf("Push: %v", err)
+ }
+
+ return nil
+}
+
+// GetBranches returns branches from the repository, skipping skip initial branches and
+// returning at most limit branches, or all branches if limit is 0.
+func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
+ return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
+}
+
+// checkBranchName validates branch name with existing repository branches
+func checkBranchName(repo *models.Repository, name string) error {
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
+ return err
+ }
+ defer gitRepo.Close()
+
+ branches, _, err := GetBranches(repo, 0, 0)
+ if err != nil {
+ return err
+ }
+
+ for _, branch := range branches {
+ if branch.Name == name {
+ return models.ErrBranchAlreadyExists{
+ BranchName: branch.Name,
+ }
+ } else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
+ (len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
+ return models.ErrBranchNameConflict{
+ BranchName: branch.Name,
+ }
+ }
+ }
+
+ if _, err := gitRepo.GetTag(name); err == nil {
+ return models.ErrTagAlreadyExists{
+ TagName: name,
+ }
+ }
+
+ return nil
+}
+
+// CreateNewBranchFromCommit creates a new repository branch
+func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
+ // Check if branch name can be used
+ if err := checkBranchName(repo, branchName); err != nil {
+ return err
+ }
+
+ if err := git.Push(repo.RepoPath(), git.PushOptions{
+ Remote: repo.RepoPath(),
+ Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
+ Env: models.PushingEnvironment(doer, repo),
+ }); err != nil {
+ if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
+ return err
+ }
+ return fmt.Errorf("Push: %v", err)
+ }
+
+ return nil
+}
+
// RenameBranch rename a branch
func RenameBranch(repo *models.Repository, doer *models.User, gitRepo *git.Repository, from, to string) (string, error) {
if from == to {