Skip to content

Commit 2677d07

Browse files
authored
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
1 parent bca367c commit 2677d07

File tree

11 files changed

+208
-183
lines changed

11 files changed

+208
-183
lines changed

integrations/api_repo_get_contents_list_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/context"
1515
"code.gitea.io/gitea/modules/git"
16+
repo_module "code.gitea.io/gitea/modules/repository"
1617
"code.gitea.io/gitea/modules/setting"
1718
api "code.gitea.io/gitea/modules/structs"
1819

@@ -71,15 +72,18 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
7172

7273
// Make a new branch in repo1
7374
newBranch := "test_branch"
74-
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
75+
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
76+
assert.NoError(t, err)
7577
// Get the commit ID of the default branch
76-
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
78+
gitRepo, err := git.OpenRepository(repo1.RepoPath())
79+
assert.NoError(t, err)
7780
defer gitRepo.Close()
7881

7982
commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
8083
// Make a new tag in repo1
8184
newTag := "test_tag"
82-
gitRepo.CreateTag(newTag, commitID)
85+
err = gitRepo.CreateTag(newTag, commitID)
86+
assert.NoError(t, err)
8387
/*** END SETUP ***/
8488

8589
// ref is default ref

integrations/api_repo_get_contents_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/git"
15+
repo_module "code.gitea.io/gitea/modules/repository"
1516
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
1718

@@ -72,15 +73,19 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
7273

7374
// Make a new branch in repo1
7475
newBranch := "test_branch"
75-
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
76+
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
77+
assert.NoError(t, err)
7678
// Get the commit ID of the default branch
77-
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
79+
gitRepo, err := git.OpenRepository(repo1.RepoPath())
80+
assert.NoError(t, err)
7881
defer gitRepo.Close()
7982

80-
commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
83+
commitID, err := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
84+
assert.NoError(t, err)
8185
// Make a new tag in repo1
8286
newTag := "test_tag"
83-
gitRepo.CreateTag(newTag, commitID)
87+
err = gitRepo.CreateTag(newTag, commitID)
88+
assert.NoError(t, err)
8489
/*** END SETUP ***/
8590

8691
// ref is default ref

models/repo_branch.go

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -5,158 +5,7 @@
55

66
package models
77

8-
import (
9-
"fmt"
10-
11-
"code.gitea.io/gitea/modules/git"
12-
"code.gitea.io/gitea/modules/log"
13-
)
14-
158
// CanCreateBranch returns true if repository meets the requirements for creating new branches.
169
func (repo *Repository) CanCreateBranch() bool {
1710
return !repo.IsMirror
1811
}
19-
20-
// GetBranch returns a branch by its name
21-
func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
22-
gitRepo, err := git.OpenRepository(repo.RepoPath())
23-
if err != nil {
24-
return nil, err
25-
}
26-
defer gitRepo.Close()
27-
28-
return gitRepo.GetBranch(branch)
29-
}
30-
31-
// GetBranches returns all the branches of a repository
32-
func (repo *Repository) GetBranches() ([]*git.Branch, error) {
33-
return git.GetBranchesByPath(repo.RepoPath())
34-
}
35-
36-
// CheckBranchName validates branch name with existing repository branches
37-
func (repo *Repository) CheckBranchName(name string) error {
38-
gitRepo, err := git.OpenRepository(repo.RepoPath())
39-
if err != nil {
40-
return err
41-
}
42-
defer gitRepo.Close()
43-
44-
branches, err := repo.GetBranches()
45-
if err != nil {
46-
return err
47-
}
48-
49-
for _, branch := range branches {
50-
if branch.Name == name {
51-
return ErrBranchAlreadyExists{branch.Name}
52-
} else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
53-
(len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
54-
return ErrBranchNameConflict{branch.Name}
55-
}
56-
}
57-
58-
if _, err := gitRepo.GetTag(name); err == nil {
59-
return ErrTagAlreadyExists{name}
60-
}
61-
62-
return nil
63-
}
64-
65-
// CreateNewBranch creates a new repository branch
66-
func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
67-
// Check if branch name can be used
68-
if err := repo.CheckBranchName(branchName); err != nil {
69-
return err
70-
}
71-
72-
if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
73-
return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
74-
}
75-
76-
basePath, err := CreateTemporaryPath("branch-maker")
77-
if err != nil {
78-
return err
79-
}
80-
defer func() {
81-
if err := RemoveTemporaryPath(basePath); err != nil {
82-
log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err)
83-
}
84-
}()
85-
86-
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
87-
Bare: true,
88-
Shared: true,
89-
}); err != nil {
90-
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
91-
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
92-
}
93-
94-
gitRepo, err := git.OpenRepository(basePath)
95-
if err != nil {
96-
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
97-
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
98-
}
99-
defer gitRepo.Close()
100-
101-
if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
102-
log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
103-
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
104-
}
105-
106-
if err = git.Push(basePath, git.PushOptions{
107-
Remote: "origin",
108-
Branch: branchName,
109-
Env: PushingEnvironment(doer, repo),
110-
}); err != nil {
111-
return fmt.Errorf("Push: %v", err)
112-
}
113-
114-
return nil
115-
}
116-
117-
// CreateNewBranchFromCommit creates a new repository branch
118-
func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
119-
// Check if branch name can be used
120-
if err := repo.CheckBranchName(branchName); err != nil {
121-
return err
122-
}
123-
basePath, err := CreateTemporaryPath("branch-maker")
124-
if err != nil {
125-
return err
126-
}
127-
defer func() {
128-
if err := RemoveTemporaryPath(basePath); err != nil {
129-
log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err)
130-
}
131-
}()
132-
133-
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
134-
Bare: true,
135-
Shared: true,
136-
}); err != nil {
137-
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
138-
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
139-
}
140-
141-
gitRepo, err := git.OpenRepository(basePath)
142-
if err != nil {
143-
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
144-
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
145-
}
146-
defer gitRepo.Close()
147-
148-
if err = gitRepo.CreateBranch(branchName, commit); err != nil {
149-
log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
150-
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
151-
}
152-
153-
if err = git.Push(basePath, git.PushOptions{
154-
Remote: "origin",
155-
Branch: branchName,
156-
Env: PushingEnvironment(doer, repo),
157-
}); err != nil {
158-
return fmt.Errorf("Push: %v", err)
159-
}
160-
161-
return nil
162-
}

modules/convert/pull.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/git"
1010
"code.gitea.io/gitea/modules/log"
11+
repo_module "code.gitea.io/gitea/modules/repository"
1112
api "code.gitea.io/gitea/modules/structs"
1213
)
1314

@@ -69,7 +70,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
6970
Created: pr.Issue.CreatedUnix.AsTimePtr(),
7071
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
7172
}
72-
baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch)
73+
baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
7374
if err != nil {
7475
if git.IsErrBranchNotExist(err) {
7576
apiPullRequest.Base = nil
@@ -98,7 +99,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
9899
apiPullRequest.Base = apiBaseBranchInfo
99100
}
100101

101-
headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch)
102+
headBranch, err = repo_module.GetBranch(pr.HeadRepo, pr.HeadBranch)
102103
if err != nil {
103104
if git.IsErrBranchNotExist(err) {
104105
apiPullRequest.Head = nil

modules/repofiles/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/git"
13+
repo_module "code.gitea.io/gitea/modules/repository"
1314
api "code.gitea.io/gitea/modules/structs"
1415
)
1516

@@ -37,15 +38,15 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo
3738
}
3839

3940
// oldBranch must exist for this operation
40-
if _, err := repo.GetBranch(opts.OldBranch); err != nil {
41+
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
4142
return nil, err
4243
}
4344

4445
// A NewBranch can be specified for the file to be created/updated in a new branch.
4546
// Check to make sure the branch does not already exist, otherwise we can't proceed.
4647
// If we aren't branching to a new branch, make sure user can commit to the given branch
4748
if opts.NewBranch != opts.OldBranch {
48-
newBranch, err := repo.GetBranch(opts.NewBranch)
49+
newBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
4950
if git.IsErrNotExist(err) {
5051
return nil, err
5152
}

modules/repofiles/update.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/lfs"
2020
"code.gitea.io/gitea/modules/log"
21-
"code.gitea.io/gitea/modules/repository"
21+
repo_module "code.gitea.io/gitea/modules/repository"
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/structs"
2424
pull_service "code.gitea.io/gitea/services/pull"
@@ -134,15 +134,15 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
134134
}
135135

136136
// oldBranch must exist for this operation
137-
if _, err := repo.GetBranch(opts.OldBranch); err != nil {
137+
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
138138
return nil, err
139139
}
140140

141141
// A NewBranch can be specified for the file to be created/updated in a new branch.
142142
// Check to make sure the branch does not already exist, otherwise we can't proceed.
143143
// If we aren't branching to a new branch, make sure user can commit to the given branch
144144
if opts.NewBranch != opts.OldBranch {
145-
existingBranch, err := repo.GetBranch(opts.NewBranch)
145+
existingBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
146146
if existingBranch != nil {
147147
return nil, models.ErrBranchAlreadyExists{
148148
BranchName: opts.NewBranch,
@@ -550,7 +550,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
550550
if isNewRef && isDelRef {
551551
return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
552552
}
553-
var commits = &repository.PushCommits{}
553+
var commits = &repo_module.PushCommits{}
554554
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
555555
// If is tag reference
556556
tagName := opts.RefFullName[len(git.TagPrefix):]
@@ -585,7 +585,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
585585
}
586586
}
587587

588-
commits = repository.ListToPushCommits(l)
588+
commits = repo_module.ListToPushCommits(l)
589589
}
590590
actions = append(actions, &CommitRepoActionOptions{
591591
PusherName: opts.PusherName,
@@ -610,7 +610,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
610610
return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
611611
}
612612

613-
var commits = &repository.PushCommits{}
613+
var commits = &repo_module.PushCommits{}
614614
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
615615
// If is tag reference
616616
tagName := opts.RefFullName[len(git.TagPrefix):]
@@ -621,7 +621,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
621621
} else {
622622
// Clear cache for tag commit count
623623
cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
624-
if err := repository.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
624+
if err := repo_module.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
625625
return nil, fmt.Errorf("PushUpdateAddTag: %v", err)
626626
}
627627
}
@@ -650,7 +650,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
650650
}
651651
}
652652

653-
commits = repository.ListToPushCommits(l)
653+
commits = repo_module.ListToPushCommits(l)
654654
}
655655

656656
return &CommitRepoActionOptions{

0 commit comments

Comments
 (0)