@@ -52,7 +52,22 @@ func Branches(ctx *context.Context) {
52
52
ctx .Data ["PageIsViewCode" ] = true
53
53
ctx .Data ["PageIsBranches" ] = true
54
54
55
- ctx .Data ["Branches" ] = loadBranches (ctx )
55
+ page := ctx .QueryInt ("page" )
56
+ if page <= 1 {
57
+ page = 1
58
+ }
59
+
60
+ pageSize := ctx .QueryInt ("limit" )
61
+ if pageSize <= 0 {
62
+ pageSize = git .BranchesRangeSize
63
+ }
64
+
65
+ branches , branchesCount := loadBranches (ctx , page , pageSize )
66
+ ctx .Data ["Branches" ] = branches
67
+ pager := context .NewPagination (int (branchesCount ), git .BranchesRangeSize , page , 5 )
68
+ pager .SetDefaultParams (ctx )
69
+ ctx .Data ["Page" ] = pager
70
+
56
71
ctx .HTML (200 , tplBranch )
57
72
}
58
73
@@ -176,17 +191,18 @@ func deleteBranch(ctx *context.Context, branchName string) error {
176
191
return nil
177
192
}
178
193
179
- func loadBranches (ctx * context.Context ) []* Branch {
194
+ func loadBranches (ctx * context.Context , page , pageSize int ) ([]* Branch , int ) {
195
+ defaultBranch , err := repo_module .GetBranch (ctx .Repo .Repository , ctx .Repo .Repository .DefaultBranch )
180
196
rawBranches , err := repo_module .GetBranches (ctx .Repo .Repository )
181
197
if err != nil {
182
198
ctx .ServerError ("GetBranches" , err )
183
- return nil
199
+ return nil , 0
184
200
}
185
201
186
202
protectedBranches , err := ctx .Repo .Repository .GetProtectedBranches ()
187
203
if err != nil {
188
204
ctx .ServerError ("GetProtectedBranches" , err )
189
- return nil
205
+ return nil , 0
190
206
}
191
207
192
208
repoIDToRepo := map [int64 ]* models.Repository {}
@@ -195,100 +211,129 @@ func loadBranches(ctx *context.Context) []*Branch {
195
211
repoIDToGitRepo := map [int64 ]* git.Repository {}
196
212
repoIDToGitRepo [ctx .Repo .Repository .ID ] = ctx .Repo .GitRepo
197
213
198
- branches := make ([]* Branch , len (rawBranches ))
199
- for i := range rawBranches {
200
- commit , err := rawBranches [i ].GetCommit ()
214
+ var totalNumOfBranches = len (rawBranches )
215
+ var startIndex = (page - 1 ) * pageSize
216
+ if startIndex > totalNumOfBranches {
217
+ startIndex = totalNumOfBranches - 1
218
+ }
219
+ var endIndex = startIndex + pageSize
220
+ if endIndex > totalNumOfBranches {
221
+ endIndex = totalNumOfBranches - 1
222
+ }
223
+
224
+ var branches []* Branch
225
+ for i := startIndex ; i < endIndex ; i ++ {
226
+ var branch = loadOneBranch (ctx , rawBranches [i ], protectedBranches , repoIDToRepo , repoIDToGitRepo )
227
+ if branch == nil {
228
+ return nil , 0
229
+ }
230
+
231
+ if branch .Name == ctx .Repo .Repository .DefaultBranch {
232
+ // Skip defult branch
233
+ continue
234
+ }
235
+
236
+ branches = append (branches , branch )
237
+ }
238
+
239
+ // Always add the default branch
240
+ branches = append (branches , loadOneBranch (ctx , defaultBranch , protectedBranches , repoIDToRepo , repoIDToGitRepo ))
241
+
242
+ if ctx .Repo .CanWrite (models .UnitTypeCode ) {
243
+ deletedBranches , err := getDeletedBranches (ctx )
201
244
if err != nil {
202
- ctx .ServerError ("GetCommit " , err )
203
- return nil
245
+ ctx .ServerError ("getDeletedBranches " , err )
246
+ return nil , 0
204
247
}
248
+ branches = append (branches , deletedBranches ... )
249
+ }
205
250
206
- var isProtected bool
207
- branchName := rawBranches [i ].Name
208
- for _ , b := range protectedBranches {
209
- if b .BranchName == branchName {
210
- isProtected = true
211
- break
212
- }
251
+ return branches , len (rawBranches ) - 1
252
+ }
253
+
254
+ func loadOneBranch (ctx * context.Context , rawBranch * git.Branch , protectedBranches []* models.ProtectedBranch ,
255
+ repoIDToRepo map [int64 ]* models.Repository ,
256
+ repoIDToGitRepo map [int64 ]* git.Repository ) * Branch {
257
+
258
+ commit , err := rawBranch .GetCommit ()
259
+ if err != nil {
260
+ ctx .ServerError ("GetCommit" , err )
261
+ return nil
262
+ }
263
+
264
+ branchName := rawBranch .Name
265
+ var isProtected bool
266
+ for _ , b := range protectedBranches {
267
+ if b .BranchName == branchName {
268
+ isProtected = true
269
+ break
213
270
}
271
+ }
272
+
273
+ divergence , divergenceError := repofiles .CountDivergingCommits (ctx .Repo .Repository , git .BranchPrefix + branchName )
274
+ if divergenceError != nil {
275
+ ctx .ServerError ("CountDivergingCommits" , divergenceError )
276
+ return nil
277
+ }
214
278
215
- divergence , divergenceError := repofiles .CountDivergingCommits (ctx .Repo .Repository , git .BranchPrefix + branchName )
216
- if divergenceError != nil {
217
- ctx .ServerError ("CountDivergingCommits" , divergenceError )
279
+ pr , err := models .GetLatestPullRequestByHeadInfo (ctx .Repo .Repository .ID , branchName )
280
+ if err != nil {
281
+ ctx .ServerError ("GetLatestPullRequestByHeadInfo" , err )
282
+ return nil
283
+ }
284
+ headCommit := commit .ID .String ()
285
+
286
+ mergeMovedOn := false
287
+ if pr != nil {
288
+ pr .HeadRepo = ctx .Repo .Repository
289
+ if err := pr .LoadIssue (); err != nil {
290
+ ctx .ServerError ("pr.LoadIssue" , err )
218
291
return nil
219
292
}
220
-
221
- pr , err := models . GetLatestPullRequestByHeadInfo ( ctx . Repo . Repository . ID , branchName )
222
- if err != nil {
223
- ctx .ServerError ("GetLatestPullRequestByHeadInfo " , err )
293
+ if repo , ok := repoIDToRepo [ pr . BaseRepoID ]; ok {
294
+ pr . BaseRepo = repo
295
+ } else if err := pr . LoadBaseRepo (); err != nil {
296
+ ctx .ServerError ("pr.LoadBaseRepo " , err )
224
297
return nil
298
+ } else {
299
+ repoIDToRepo [pr .BaseRepoID ] = pr .BaseRepo
225
300
}
226
- headCommit := commit . ID . String ()
301
+ pr . Issue . Repo = pr . BaseRepo
227
302
228
- mergeMovedOn := false
229
- if pr != nil {
230
- pr .HeadRepo = ctx .Repo .Repository
231
- if err := pr .LoadIssue (); err != nil {
232
- ctx .ServerError ("pr.LoadIssue" , err )
233
- return nil
303
+ if pr .HasMerged {
304
+ baseGitRepo , ok := repoIDToGitRepo [pr .BaseRepoID ]
305
+ if ! ok {
306
+ baseGitRepo , err = git .OpenRepository (pr .BaseRepo .RepoPath ())
307
+ if err != nil {
308
+ ctx .ServerError ("OpenRepository" , err )
309
+ return nil
310
+ }
311
+ defer baseGitRepo .Close ()
312
+ repoIDToGitRepo [pr .BaseRepoID ] = baseGitRepo
234
313
}
235
- if repo , ok := repoIDToRepo [pr .BaseRepoID ]; ok {
236
- pr .BaseRepo = repo
237
- } else if err := pr .LoadBaseRepo (); err != nil {
238
- ctx .ServerError ("pr.LoadBaseRepo" , err )
314
+ pullCommit , err := baseGitRepo .GetRefCommitID (pr .GetGitRefName ())
315
+ if err != nil && ! git .IsErrNotExist (err ) {
316
+ ctx .ServerError ("GetBranchCommitID" , err )
239
317
return nil
240
- } else {
241
- repoIDToRepo [pr .BaseRepoID ] = pr .BaseRepo
242
318
}
243
- pr .Issue .Repo = pr .BaseRepo
244
-
245
- if pr .HasMerged {
246
- baseGitRepo , ok := repoIDToGitRepo [pr .BaseRepoID ]
247
- if ! ok {
248
- baseGitRepo , err = git .OpenRepository (pr .BaseRepo .RepoPath ())
249
- if err != nil {
250
- ctx .ServerError ("OpenRepository" , err )
251
- return nil
252
- }
253
- defer baseGitRepo .Close ()
254
- repoIDToGitRepo [pr .BaseRepoID ] = baseGitRepo
255
- }
256
- pullCommit , err := baseGitRepo .GetRefCommitID (pr .GetGitRefName ())
257
- if err != nil && ! git .IsErrNotExist (err ) {
258
- ctx .ServerError ("GetBranchCommitID" , err )
259
- return nil
260
- }
261
- if err == nil && headCommit != pullCommit {
262
- // the head has moved on from the merge - we shouldn't delete
263
- mergeMovedOn = true
264
- }
319
+ if err == nil && headCommit != pullCommit {
320
+ // the head has moved on from the merge - we shouldn't delete
321
+ mergeMovedOn = true
265
322
}
266
323
}
267
-
268
- isIncluded := divergence .Ahead == 0 && ctx .Repo .Repository .DefaultBranch != branchName
269
-
270
- branches [i ] = & Branch {
271
- Name : branchName ,
272
- Commit : commit ,
273
- IsProtected : isProtected ,
274
- IsIncluded : isIncluded ,
275
- CommitsAhead : divergence .Ahead ,
276
- CommitsBehind : divergence .Behind ,
277
- LatestPullRequest : pr ,
278
- MergeMovedOn : mergeMovedOn ,
279
- }
280
324
}
281
325
282
- if ctx .Repo .CanWrite (models .UnitTypeCode ) {
283
- deletedBranches , err := getDeletedBranches (ctx )
284
- if err != nil {
285
- ctx .ServerError ("getDeletedBranches" , err )
286
- return nil
287
- }
288
- branches = append (branches , deletedBranches ... )
326
+ isIncluded := divergence .Ahead == 0 && ctx .Repo .Repository .DefaultBranch != branchName
327
+ return & Branch {
328
+ Name : branchName ,
329
+ Commit : commit ,
330
+ IsProtected : isProtected ,
331
+ IsIncluded : isIncluded ,
332
+ CommitsAhead : divergence .Ahead ,
333
+ CommitsBehind : divergence .Behind ,
334
+ LatestPullRequest : pr ,
335
+ MergeMovedOn : mergeMovedOn ,
289
336
}
290
-
291
- return branches
292
337
}
293
338
294
339
func getDeletedBranches (ctx * context.Context ) ([]* Branch , error ) {
0 commit comments