Skip to content

Commit aa7e795

Browse files
committed
fix & rename & more logging
1 parent 179efd8 commit aa7e795

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

integrations/branches_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
5757

5858
htmlDoc := NewHTMLParser(t, resp.Body)
5959
link, exists := htmlDoc.doc.Find(button).Attr("data-url")
60-
assert.True(t, exists, "The template has changed")
60+
if !assert.True(t, exists, "The template has changed") {
61+
t.Skip()
62+
}
6163

6264
req = NewRequestWithValues(t, "POST", link, map[string]string{
6365
"_csrf": getCsrf(t, htmlDoc.doc),
@@ -69,7 +71,7 @@ func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
6971
req = NewRequest(t, "GET", "/user2/repo1/branches")
7072
resp = session.MakeRequest(t, req, http.StatusOK)
7173

72-
return NewHTMLParser(t, resp.Body), url.Query()["name"][0]
74+
return NewHTMLParser(t, resp.Body), url.Query().Get("name")
7375
}
7476

7577
func getCsrf(t *testing.T, doc *goquery.Document) string {

modules/git/repo_branch_gogit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {
4242
if i < skip {
4343
i++
4444
return nil
45-
} else if limit != 0 && i < skip+limit {
45+
} else if limit != 0 && count > skip+limit {
4646
return nil
4747
}
48+
4849
branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
4950
return nil
5051
})

modules/repository/branch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313

1414
// GetBranch returns a branch by its name
1515
func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
16+
if len(branch) == 0 {
17+
return nil, fmt.Errorf("GetBranch: empty string for branch")
18+
}
1619
gitRepo, err := git.OpenRepository(repo.RepoPath())
1720
if err != nil {
1821
return nil, err

routers/repo/branch.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ func Branches(ctx *context.Context) {
5858
page = 1
5959
}
6060

61-
pageSize := ctx.QueryInt("limit")
62-
if pageSize <= 0 || pageSize > git.BranchesRangeSize {
63-
pageSize = git.BranchesRangeSize
61+
limit := ctx.QueryInt("limit")
62+
if limit <= 0 || limit > git.BranchesRangeSize {
63+
limit = git.BranchesRangeSize
6464
}
6565

66-
branches, branchesCount := loadBranches(ctx, page, pageSize)
66+
skip := (page - 1) * limit
67+
log.Info("Branches: skip: %d limit: %d", skip, limit)
68+
branches, branchesCount := loadBranches(ctx, skip, limit)
6769
if ctx.Written() {
6870
return
6971
}
@@ -80,6 +82,7 @@ func DeleteBranchPost(ctx *context.Context) {
8082
defer redirect(ctx)
8183
branchName := ctx.Query("name")
8284
if branchName == ctx.Repo.Repository.DefaultBranch {
85+
log.Warn("DeleteBranch: Can't delete default branch '%s'", branchName)
8386
ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
8487
return
8588
}
@@ -92,16 +95,19 @@ func DeleteBranchPost(ctx *context.Context) {
9295
}
9396

9497
if isProtected {
98+
log.Warn("DeleteBranch: Can't delete protected branch '%s'", branchName)
9599
ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
96100
return
97101
}
98102

99-
if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == ctx.Repo.Repository.DefaultBranch {
103+
if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
104+
log.Warn("DeleteBranch: Can't delete non existing branch '%s'", branchName)
100105
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
101106
return
102107
}
103108

104109
if err := deleteBranch(ctx, branchName); err != nil {
110+
log.Error("DeleteBranch: %v", err)
105111
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
106112
return
107113
}
@@ -129,10 +135,11 @@ func RestoreBranchPost(ctx *context.Context) {
129135
Env: models.PushingEnvironment(ctx.User, ctx.Repo.Repository),
130136
}); err != nil {
131137
if strings.Contains(err.Error(), "already exists") {
138+
log.Warn("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
132139
ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
133140
return
134141
}
135-
log.Error("CreateBranch: %v", err)
142+
log.Error("RestoreBranch: CreateBranch: %v", err)
136143
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
137144
return
138145
}
@@ -148,7 +155,7 @@ func RestoreBranchPost(ctx *context.Context) {
148155
RepoUserName: ctx.Repo.Owner.Name,
149156
RepoName: ctx.Repo.Repository.Name,
150157
}); err != nil {
151-
log.Error("Update: %v", err)
158+
log.Error("RestoreBranch: Update: %v", err)
152159
}
153160

154161
ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
@@ -197,17 +204,17 @@ func deleteBranch(ctx *context.Context, branchName string) error {
197204

198205
// loadBranches loads branches from the repository limited by page & pageSize.
199206
// NOTE: May write to context on error.
200-
func loadBranches(ctx *context.Context, page, pageSize int) ([]*Branch, int) {
207+
func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
201208
defaultBranch, err := repo_module.GetBranch(ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch)
202209
if err != nil {
210+
log.Error("loadBranches: get default branch: %v", err)
203211
ctx.ServerError("GetDefaultBranch", err)
204212
return nil, 0
205213
}
206214

207-
skip, _ := models.ListOptions{Page: page, PageSize: pageSize}.GetStartEnd()
208-
209-
rawBranches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, pageSize)
215+
rawBranches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, limit)
210216
if err != nil {
217+
log.Error("GetBranches: %v", err)
211218
ctx.ServerError("GetBranches", err)
212219
return nil, 0
213220
}
@@ -226,7 +233,7 @@ func loadBranches(ctx *context.Context, page, pageSize int) ([]*Branch, int) {
226233

227234
var branches []*Branch
228235
for i := range rawBranches {
229-
if strings.EqualFold(rawBranches[i].Name, ctx.Repo.Repository.DefaultBranch) {
236+
if rawBranches[i].Name == defaultBranch.Name {
230237
// Skip default branch
231238
continue
232239
}
@@ -240,6 +247,7 @@ func loadBranches(ctx *context.Context, page, pageSize int) ([]*Branch, int) {
240247
}
241248

242249
// Always add the default branch
250+
log.Info("loadOneBranch: load default: '%s'", defaultBranch.Name)
243251
branches = append(branches, loadOneBranch(ctx, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo))
244252

245253
if ctx.Repo.CanWrite(models.UnitTypeCode) {
@@ -257,6 +265,7 @@ func loadBranches(ctx *context.Context, page, pageSize int) ([]*Branch, int) {
257265
func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranches []*models.ProtectedBranch,
258266
repoIDToRepo map[int64]*models.Repository,
259267
repoIDToGitRepo map[int64]*git.Repository) *Branch {
268+
log.Info("loadOneBranch: '%s'", rawBranch.Name)
260269

261270
commit, err := rawBranch.GetCommit()
262271
if err != nil {

0 commit comments

Comments
 (0)