From 2303bc908a7dfea5dfb73f3b95aee19de418421b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 7 Jan 2020 13:06:44 +0800 Subject: [PATCH 1/5] Move newbranch to standalone package --- .../api_repo_get_contents_list_test.go | 3 +- integrations/api_repo_get_contents_test.go | 3 +- models/repo_branch.go | 151 ----------------- routers/api/v1/repo/branch.go | 2 +- routers/repo/branch.go | 7 +- services/mirror/mirror.go | 2 +- services/repository/branch.go | 157 ++++++++++++++++++ 7 files changed, 167 insertions(+), 158 deletions(-) create mode 100644 services/repository/branch.go diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index 4605ccf4d933b..3c9fa783e5ba4 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" ) @@ -71,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch) + repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) // Get the commit ID of the default branch gitRepo, _ := git.OpenRepository(repo1.RepoPath()) defer gitRepo.Close() diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index 77a827ec6125b..495a7132a769c 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" ) @@ -72,7 +73,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch) + repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) // Get the commit ID of the default branch gitRepo, _ := git.OpenRepository(repo1.RepoPath()) defer gitRepo.Close() diff --git a/models/repo_branch.go b/models/repo_branch.go index c51323183688d..08e8fccb59e15 100644 --- a/models/repo_branch.go +++ b/models/repo_branch.go @@ -5,158 +5,7 @@ package models -import ( - "fmt" - - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" -) - // CanCreateBranch returns true if repository meets the requirements for creating new branches. func (repo *Repository) CanCreateBranch() bool { return !repo.IsMirror } - -// GetBranch returns a branch by its name -func (repo *Repository) GetBranch(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 (repo *Repository) GetBranches() ([]*git.Branch, error) { - return git.GetBranchesByPath(repo.RepoPath()) -} - -// CheckBranchName validates branch name with existing repository branches -func (repo *Repository) CheckBranchName(name string) error { - gitRepo, err := git.OpenRepository(repo.RepoPath()) - if err != nil { - return err - } - defer gitRepo.Close() - - branches, err := repo.GetBranches() - if err != nil { - return err - } - - for _, branch := range branches { - if branch.Name == name { - return ErrBranchAlreadyExists{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 ErrBranchNameConflict{branch.Name} - } - } - - if _, err := gitRepo.GetTag(name); err == nil { - return ErrTagAlreadyExists{name} - } - - return nil -} - -// CreateNewBranch creates a new repository branch -func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) { - // Check if branch name can be used - if err := repo.CheckBranchName(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 := CreateTemporaryPath("branch-maker") - if err != nil { - return err - } - defer func() { - if err := 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: PushingEnvironment(doer, repo), - }); err != nil { - return fmt.Errorf("Push: %v", err) - } - - return nil -} - -// CreateNewBranchFromCommit creates a new repository branch -func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) { - // Check if branch name can be used - if err := repo.CheckBranchName(branchName); err != nil { - return err - } - basePath, err := CreateTemporaryPath("branch-maker") - if err != nil { - return err - } - defer func() { - if err := 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: PushingEnvironment(doer, repo), - }); err != nil { - return fmt.Errorf("Push: %v", err) - } - - return nil -} diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index afc4763b522c4..8be45bda3e36e 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -95,7 +95,7 @@ func ListBranches(ctx *context.APIContext) { // "200": // "$ref": "#/responses/BranchList" - branches, err := ctx.Repo.Repository.GetBranches() + branches, err := repo_service.GetBranches(ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranches", err) return diff --git a/routers/repo/branch.go b/routers/repo/branch.go index df6e0bf21f6c7..6b1ae1e26a071 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/repofiles" "code.gitea.io/gitea/modules/util" "gopkg.in/src-d/go-git.v4/plumbing" + repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -175,7 +176,7 @@ func deleteBranch(ctx *context.Context, branchName string) error { } func loadBranches(ctx *context.Context) []*Branch { - rawBranches, err := ctx.Repo.Repository.GetBranches() + rawBranches, err := repo_service.GetBranches(ctx.Repo.Repository) if err != nil { ctx.ServerError("GetBranches", err) return nil @@ -324,9 +325,9 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) { var err error if ctx.Repo.IsViewBranch { - err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName) + err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } else { - err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName) + err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } if err != nil { if models.IsErrTagAlreadyExists(err) { diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index d4f97c2600647..6c7d4ec84d792 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -253,7 +253,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) { } } - branches, err := m.Repo.GetBranches() + branches, err := repo_service.GetBranches(m.Repo) if err != nil { log.Error("GetBranches: %v", err) return nil, false diff --git a/services/repository/branch.go b/services/repository/branch.go new file mode 100644 index 0000000000000..96ca50fb0c7f4 --- /dev/null +++ b/services/repository/branch.go @@ -0,0 +1,157 @@ +// 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{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{branch.Name} + } + } + + if _, err := gitRepo.GetTag(name); err == nil { + return models.ErrTagAlreadyExists{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 +} From a30ddde6a1d8e3710bc04c47d6a0ca4fe3dccd1b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 10 Jan 2020 16:13:13 +0800 Subject: [PATCH 2/5] move branch functions to modules to avoid dependencies cycles --- modules/convert/pull.go | 5 +++-- modules/repofiles/delete.go | 5 +++-- modules/repofiles/update.go | 16 ++++++++-------- {services => modules}/repository/branch.go | 0 routers/api/v1/repo/branch.go | 5 +++-- routers/repo/branch.go | 8 ++++---- routers/repo/editor.go | 5 +++-- services/mirror/mirror.go | 11 +++++------ 8 files changed, 29 insertions(+), 26 deletions(-) rename {services => modules}/repository/branch.go (100%) diff --git a/modules/convert/pull.go b/modules/convert/pull.go index f8534f839aff3..18b4bc8de5e91 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 43937c49e19c8..f5dd7ca87d6bd 100644 --- a/modules/repofiles/delete.go +++ b/modules/repofiles/delete.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + repo_module "code.gitea.io/gitea/modules/repository" ) // DeleteRepoFileOptions holds the repository delete file options @@ -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 3a0ba668c1adb..c4d1b3b54bbb6 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/services/repository/branch.go b/modules/repository/branch.go similarity index 100% rename from services/repository/branch.go rename to modules/repository/branch.go diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 8be45bda3e36e..5dbb4b29dfe9c 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + repo_module "code.gitea.io/gitea/modules/repository" ) // GetBranch get a branch of a repository @@ -48,7 +49,7 @@ func GetBranch(ctx *context.APIContext) { ctx.NotFound() return } - branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName) + branch, err := repo_module.GetBranch(ctx.Repo.Repository, ctx.Repo.BranchName) if err != nil { if git.IsErrBranchNotExist(err) { ctx.NotFound(err) @@ -95,7 +96,7 @@ func ListBranches(ctx *context.APIContext) { // "200": // "$ref": "#/responses/BranchList" - branches, err := repo_service.GetBranches(ctx.Repo.Repository) + branches, err := repo_module.GetBranches(ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranches", err) return diff --git a/routers/repo/branch.go b/routers/repo/branch.go index 6b1ae1e26a071..88bbe2be56989 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -15,9 +15,9 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repofiles" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/util" "gopkg.in/src-d/go-git.v4/plumbing" - repo_service "code.gitea.io/gitea/services/repository" ) const ( @@ -176,7 +176,7 @@ func deleteBranch(ctx *context.Context, branchName string) error { } func loadBranches(ctx *context.Context) []*Branch { - rawBranches, err := repo_service.GetBranches(ctx.Repo.Repository) + rawBranches, err := repo_module.GetBranches(ctx.Repo.Repository) if err != nil { ctx.ServerError("GetBranches", err) return nil @@ -325,9 +325,9 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) { var err error if ctx.Repo.IsViewBranch { - err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) + err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } else { - err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) + err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName) } if err != nil { if models.IsErrTagAlreadyExists(err) { diff --git a/routers/repo/editor.go b/routers/repo/editor.go index fec572852567d..0f13e742ff747 100644 --- a/routers/repo/editor.go +++ b/routers/repo/editor.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/util" + repo_module "code.gitea.io/gitea/modules/repository" ) const ( @@ -534,7 +535,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) { } if oldBranchName != branchName { - if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil { + if _, err := repo_module.GetBranch(ctx.Repo.Repository, branchName); err == nil { ctx.Data["Err_NewBranchName"] = true ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form) return @@ -679,7 +680,7 @@ func GetUniquePatchBranchName(ctx *context.Context) string { prefix := ctx.User.LowerName + "-patch-" for i := 1; i <= 1000; i++ { branchName := fmt.Sprintf("%s%d", prefix, i) - if _, err := ctx.Repo.Repository.GetBranch(branchName); err != nil { + if _, err := repo_module.GetBranch(ctx.Repo.Repository, branchName); err != nil { if git.IsErrBranchNotExist(err) { return branchName } diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index 6c7d4ec84d792..2cfca666de187 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -11,14 +11,13 @@ import ( "strings" "time" - "code.gitea.io/gitea/modules/graceful" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" - "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/sync" "code.gitea.io/gitea/modules/timeutil" @@ -211,7 +210,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) { log.Error("OpenRepository: %v", err) return nil, false } - if err = repository.SyncReleasesWithTags(m.Repo, gitRepo); err != nil { + if err = repo_module.SyncReleasesWithTags(m.Repo, gitRepo); err != nil { gitRepo.Close() log.Error("Failed to synchronize tags to releases for repository: %v", err) } @@ -253,7 +252,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) { } } - branches, err := repo_service.GetBranches(m.Repo) + branches, err := repo_module.GetBranches(m.Repo) if err != nil { log.Error("GetBranches: %v", err) return nil, false @@ -403,7 +402,7 @@ func syncMirror(repoID string) { continue } - theCommits := repository.ListToPushCommits(commits) + theCommits := repo_module.ListToPushCommits(commits) if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum { theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum] } From 647c2820b90ef8b8c6c2e1ae7c99ef1b8132aa6a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 10 Jan 2020 16:16:43 +0800 Subject: [PATCH 3/5] fix tests --- integrations/api_repo_get_contents_list_test.go | 4 ++-- integrations/api_repo_get_contents_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index 3c9fa783e5ba4..b87cdad88858d 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -15,7 +15,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - repo_service "code.gitea.io/gitea/services/repository" + repo_module "code.gitea.io/gitea/modules/repository" "github.com/stretchr/testify/assert" ) @@ -72,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) // Get the commit ID of the default branch gitRepo, _ := git.OpenRepository(repo1.RepoPath()) defer gitRepo.Close() diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index 495a7132a769c..83f2618ca17ea 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -12,9 +12,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" ) @@ -73,7 +73,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) // Get the commit ID of the default branch gitRepo, _ := git.OpenRepository(repo1.RepoPath()) defer gitRepo.Close() From 9d0cee82f4d276bf5aa03b48c40f6f0404628a1e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 10 Jan 2020 23:48:12 +0800 Subject: [PATCH 4/5] fix lint --- integrations/api_repo_get_contents_list_test.go | 11 +++++++---- integrations/api_repo_get_contents_test.go | 12 ++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index b87cdad88858d..abc4f42cf4d3e 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -13,9 +13,9 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" - repo_module "code.gitea.io/gitea/modules/repository" "github.com/stretchr/testify/assert" ) @@ -72,15 +72,18 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + assert.NoError(t, err) // Get the commit ID of the default branch - gitRepo, _ := git.OpenRepository(repo1.RepoPath()) + gitRepo, err := git.OpenRepository(repo1.RepoPath()) + assert.NoError(t, err) defer gitRepo.Close() commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch) // Make a new tag in repo1 newTag := "test_tag" - gitRepo.CreateTag(newTag, commitID) + err = gitRepo.CreateTag(newTag, commitID) + assert.NoError(t, err) /*** END SETUP ***/ // ref is default ref diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index 83f2618ca17ea..184e76831bbd5 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -73,15 +73,19 @@ func testAPIGetContents(t *testing.T, u *url.URL) { // Make a new branch in repo1 newBranch := "test_branch" - repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch) + assert.NoError(t, err) // Get the commit ID of the default branch - gitRepo, _ := git.OpenRepository(repo1.RepoPath()) + gitRepo, err := git.OpenRepository(repo1.RepoPath()) + assert.NoError(t, err) defer gitRepo.Close() - commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch) + commitID, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch) + assert.NoError(t, err) // Make a new tag in repo1 newTag := "test_tag" - gitRepo.CreateTag(newTag, commitID) + err = gitRepo.CreateTag(newTag, commitID) + assert.NoError(t, err) /*** END SETUP ***/ // ref is default ref From 7c519cb0708913e1bb841eb359790a17849ec1a5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 11 Jan 2020 20:10:56 +0800 Subject: [PATCH 5/5] fix lint --- modules/repofiles/delete.go | 2 +- modules/repository/branch.go | 12 +++++++++--- routers/api/v1/repo/branch.go | 2 +- routers/repo/editor.go | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/repofiles/delete.go b/modules/repofiles/delete.go index f5dd7ca87d6bd..f774b375a52e1 100644 --- a/modules/repofiles/delete.go +++ b/modules/repofiles/delete.go @@ -10,8 +10,8 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" - api "code.gitea.io/gitea/modules/structs" repo_module "code.gitea.io/gitea/modules/repository" + api "code.gitea.io/gitea/modules/structs" ) // DeleteRepoFileOptions holds the repository delete file options diff --git a/modules/repository/branch.go b/modules/repository/branch.go index 96ca50fb0c7f4..9867aadad6384 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -43,15 +43,21 @@ func checkBranchName(repo *models.Repository, name string) error { for _, branch := range branches { if branch.Name == name { - return models.ErrBranchAlreadyExists{branch.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{branch.Name} + return models.ErrBranchNameConflict{ + BranchName: branch.Name, + } } } if _, err := gitRepo.GetTag(name); err == nil { - return models.ErrTagAlreadyExists{name} + return models.ErrTagAlreadyExists{ + TagName: name, + } } return nil diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 5dbb4b29dfe9c..8f8ca158771eb 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -11,8 +11,8 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" - api "code.gitea.io/gitea/modules/structs" repo_module "code.gitea.io/gitea/modules/repository" + api "code.gitea.io/gitea/modules/structs" ) // GetBranch get a branch of a repository diff --git a/routers/repo/editor.go b/routers/repo/editor.go index 0f13e742ff747..82c74ba75f9d7 100644 --- a/routers/repo/editor.go +++ b/routers/repo/editor.go @@ -19,10 +19,10 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repofiles" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/util" - repo_module "code.gitea.io/gitea/modules/repository" ) const (