diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-14 11:38:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-14 11:38:04 +0800 |
commit | 2677d071f911fb91f382acfedaedc251bd807f70 (patch) | |
tree | 61d48125ff6e509e5ffe385b23fdc14fb454b9e6 /modules | |
parent | bca367cecc6004957a952bb6222b9bbdc868e784 (diff) | |
download | gitea-2677d071f911fb91f382acfedaedc251bd807f70.tar.gz gitea-2677d071f911fb91f382acfedaedc251bd807f70.zip |
Move newbranch to standalone package (#9627)
* Move newbranch to standalone package
* move branch functions to modules to avoid dependencies cycles
* fix tests
* fix lint
* fix lint
Diffstat (limited to 'modules')
-rw-r--r-- | modules/convert/pull.go | 5 | ||||
-rw-r--r-- | modules/repofiles/delete.go | 5 | ||||
-rw-r--r-- | modules/repofiles/update.go | 16 | ||||
-rw-r--r-- | modules/repository/branch.go | 163 |
4 files changed, 177 insertions, 12 deletions
diff --git a/modules/convert/pull.go b/modules/convert/pull.go index f8534f839a..18b4bc8de5 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + repo_module "code.gitea.io/gitea/modules/repository" api "code.gitea.io/gitea/modules/structs" ) @@ -69,7 +70,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest { Created: pr.Issue.CreatedUnix.AsTimePtr(), Updated: pr.Issue.UpdatedUnix.AsTimePtr(), } - baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch) + baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch) if err != nil { if git.IsErrBranchNotExist(err) { apiPullRequest.Base = nil @@ -98,7 +99,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest { apiPullRequest.Base = apiBaseBranchInfo } - headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch) + headBranch, err = repo_module.GetBranch(pr.HeadRepo, pr.HeadBranch) if err != nil { if git.IsErrBranchNotExist(err) { apiPullRequest.Head = nil diff --git a/modules/repofiles/delete.go b/modules/repofiles/delete.go index 43937c49e1..f774b375a5 100644 --- a/modules/repofiles/delete.go +++ b/modules/repofiles/delete.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" + repo_module "code.gitea.io/gitea/modules/repository" api "code.gitea.io/gitea/modules/structs" ) @@ -37,7 +38,7 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo } // oldBranch must exist for this operation - if _, err := repo.GetBranch(opts.OldBranch); err != nil { + if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil { return nil, err } @@ -45,7 +46,7 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - newBranch, err := repo.GetBranch(opts.NewBranch) + newBranch, err := repo_module.GetBranch(repo, opts.NewBranch) if git.IsErrNotExist(err) { return nil, err } diff --git a/modules/repofiles/update.go b/modules/repofiles/update.go index 812649af36..e22a2062a0 100644 --- a/modules/repofiles/update.go +++ b/modules/repofiles/update.go @@ -18,7 +18,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/repository" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" pull_service "code.gitea.io/gitea/services/pull" @@ -134,7 +134,7 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up } // oldBranch must exist for this operation - if _, err := repo.GetBranch(opts.OldBranch); err != nil { + if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil { return nil, err } @@ -142,7 +142,7 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - existingBranch, err := repo.GetBranch(opts.NewBranch) + existingBranch, err := repo_module.GetBranch(repo, opts.NewBranch) if existingBranch != nil { return nil, models.ErrBranchAlreadyExists{ BranchName: opts.NewBranch, @@ -550,7 +550,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o if isNewRef && isDelRef { return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA) } - var commits = &repository.PushCommits{} + var commits = &repo_module.PushCommits{} if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { // If is tag reference tagName := opts.RefFullName[len(git.TagPrefix):] @@ -585,7 +585,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o } } - commits = repository.ListToPushCommits(l) + commits = repo_module.ListToPushCommits(l) } actions = append(actions, &CommitRepoActionOptions{ PusherName: opts.PusherName, @@ -610,7 +610,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA) } - var commits = &repository.PushCommits{} + var commits = &repo_module.PushCommits{} if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { // If is tag reference tagName := opts.RefFullName[len(git.TagPrefix):] @@ -621,7 +621,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito } else { // Clear cache for tag commit count cache.Remove(repo.GetCommitsCountCacheKey(tagName, true)) - if err := repository.PushUpdateAddTag(repo, gitRepo, tagName); err != nil { + if err := repo_module.PushUpdateAddTag(repo, gitRepo, tagName); err != nil { return nil, fmt.Errorf("PushUpdateAddTag: %v", err) } } @@ -650,7 +650,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito } } - commits = repository.ListToPushCommits(l) + commits = repo_module.ListToPushCommits(l) } return &CommitRepoActionOptions{ diff --git a/modules/repository/branch.go b/modules/repository/branch.go new file mode 100644 index 0000000000..9867aadad6 --- /dev/null +++ b/modules/repository/branch.go @@ -0,0 +1,163 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repository + +import ( + "fmt" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" +) + +// GetBranch returns a branch by its name +func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) { + gitRepo, err := git.OpenRepository(repo.RepoPath()) + if err != nil { + return nil, err + } + defer gitRepo.Close() + + return gitRepo.GetBranch(branch) +} + +// GetBranches returns all the branches of a repository +func GetBranches(repo *models.Repository) ([]*git.Branch, error) { + return git.GetBranchesByPath(repo.RepoPath()) +} + +// 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) + 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 +} + +// 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 fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName) + } + + basePath, err := models.CreateTemporaryPath("branch-maker") + if err != nil { + return err + } + defer func() { + if err := models.RemoveTemporaryPath(basePath); err != nil { + log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err) + } + }() + + if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{ + Bare: true, + Shared: true, + }); err != nil { + log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) + return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) + } + + gitRepo, err := git.OpenRepository(basePath) + if err != nil { + log.Error("Unable to open temporary repository: %s (%v)", basePath, err) + return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) + } + defer gitRepo.Close() + + if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil { + log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err) + return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err) + } + + if err = git.Push(basePath, git.PushOptions{ + Remote: "origin", + Branch: branchName, + Env: models.PushingEnvironment(doer, repo), + }); err != nil { + return fmt.Errorf("Push: %v", err) + } + + 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 + } + basePath, err := models.CreateTemporaryPath("branch-maker") + if err != nil { + return err + } + defer func() { + if err := models.RemoveTemporaryPath(basePath); err != nil { + log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err) + } + }() + + if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{ + Bare: true, + Shared: true, + }); err != nil { + log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err) + return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err) + } + + gitRepo, err := git.OpenRepository(basePath) + if err != nil { + log.Error("Unable to open temporary repository: %s (%v)", basePath, err) + return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err) + } + defer gitRepo.Close() + + if err = gitRepo.CreateBranch(branchName, commit); err != nil { + log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err) + return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err) + } + + if err = git.Push(basePath, git.PushOptions{ + Remote: "origin", + Branch: branchName, + Env: models.PushingEnvironment(doer, repo), + }); err != nil { + return fmt.Errorf("Push: %v", err) + } + + return nil +} |