Skip to content

Commit 2303bc9

Browse files
committed
Move newbranch to standalone package
1 parent f2e6c45 commit 2303bc9

File tree

7 files changed

+167
-158
lines changed

7 files changed

+167
-158
lines changed

integrations/api_repo_get_contents_list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/setting"
1717
api "code.gitea.io/gitea/modules/structs"
18+
repo_service "code.gitea.io/gitea/services/repository"
1819

1920
"github.com/stretchr/testify/assert"
2021
)
@@ -71,7 +72,7 @@ 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+
repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7576
// Get the commit ID of the default branch
7677
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
7778
defer gitRepo.Close()

integrations/api_repo_get_contents_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/gitea/modules/structs"
17+
repo_service "code.gitea.io/gitea/services/repository"
1718

1819
"github.com/stretchr/testify/assert"
1920
)
@@ -72,7 +73,7 @@ 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+
repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7677
// Get the commit ID of the default branch
7778
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
7879
defer gitRepo.Close()

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-
}

routers/api/v1/repo/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func ListBranches(ctx *context.APIContext) {
9595
// "200":
9696
// "$ref": "#/responses/BranchList"
9797

98-
branches, err := ctx.Repo.Repository.GetBranches()
98+
branches, err := repo_service.GetBranches(ctx.Repo.Repository)
9999
if err != nil {
100100
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
101101
return

routers/repo/branch.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/repofiles"
1818
"code.gitea.io/gitea/modules/util"
1919
"gopkg.in/src-d/go-git.v4/plumbing"
20+
repo_service "code.gitea.io/gitea/services/repository"
2021
)
2122

2223
const (
@@ -175,7 +176,7 @@ func deleteBranch(ctx *context.Context, branchName string) error {
175176
}
176177

177178
func loadBranches(ctx *context.Context) []*Branch {
178-
rawBranches, err := ctx.Repo.Repository.GetBranches()
179+
rawBranches, err := repo_service.GetBranches(ctx.Repo.Repository)
179180
if err != nil {
180181
ctx.ServerError("GetBranches", err)
181182
return nil
@@ -324,9 +325,9 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
324325

325326
var err error
326327
if ctx.Repo.IsViewBranch {
327-
err = ctx.Repo.Repository.CreateNewBranch(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
328+
err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
328329
} else {
329-
err = ctx.Repo.Repository.CreateNewBranchFromCommit(ctx.User, ctx.Repo.BranchName, form.NewBranchName)
330+
err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
330331
}
331332
if err != nil {
332333
if models.IsErrTagAlreadyExists(err) {

services/mirror/mirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
253253
}
254254
}
255255

256-
branches, err := m.Repo.GetBranches()
256+
branches, err := repo_service.GetBranches(m.Repo)
257257
if err != nil {
258258
log.Error("GetBranches: %v", err)
259259
return nil, false

services/repository/branch.go

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

0 commit comments

Comments
 (0)