@@ -12,6 +12,7 @@ import (
12
12
"code.gitea.io/gitea/modules/util"
13
13
14
14
"xorm.io/builder"
15
+ "xorm.io/xorm"
15
16
)
16
17
17
18
// RepositoryListDefaultPageSize is the default number of repositories
@@ -363,6 +364,35 @@ func SearchRepository(opts *SearchRepoOptions) (RepositoryList, int64, error) {
363
364
364
365
// SearchRepositoryByCondition search repositories by condition
365
366
func SearchRepositoryByCondition (opts * SearchRepoOptions , cond builder.Cond , loadAttributes bool ) (RepositoryList , int64 , error ) {
367
+ sess , count , err := searchRepositoryByCondition (opts , cond )
368
+ if err != nil {
369
+ return nil , 0 , err
370
+ }
371
+ defer sess .Close ()
372
+
373
+ defaultSize := 50
374
+ if opts .PageSize > 0 {
375
+ defaultSize = opts .PageSize
376
+ }
377
+ repos := make (RepositoryList , 0 , defaultSize )
378
+ if err := sess .Find (& repos ); err != nil {
379
+ return nil , 0 , fmt .Errorf ("Repo: %v" , err )
380
+ }
381
+
382
+ if opts .PageSize <= 0 {
383
+ count = int64 (len (repos ))
384
+ }
385
+
386
+ if loadAttributes {
387
+ if err := repos .loadAttributes (sess ); err != nil {
388
+ return nil , 0 , fmt .Errorf ("LoadAttributes: %v" , err )
389
+ }
390
+ }
391
+
392
+ return repos , count , nil
393
+ }
394
+
395
+ func searchRepositoryByCondition (opts * SearchRepoOptions , cond builder.Cond ) (* xorm.Session , int64 , error ) {
366
396
if opts .Page <= 0 {
367
397
opts .Page = 1
368
398
}
@@ -376,31 +406,24 @@ func SearchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond, loa
376
406
}
377
407
378
408
sess := x .NewSession ()
379
- defer sess .Close ()
380
409
381
- count , err := sess .
382
- Where (cond ).
383
- Count (new (Repository ))
384
- if err != nil {
385
- return nil , 0 , fmt .Errorf ("Count: %v" , err )
410
+ var count int64
411
+ if opts .PageSize > 0 {
412
+ var err error
413
+ count , err = sess .
414
+ Where (cond ).
415
+ Count (new (Repository ))
416
+ if err != nil {
417
+ _ = sess .Close ()
418
+ return nil , 0 , fmt .Errorf ("Count: %v" , err )
419
+ }
386
420
}
387
421
388
- repos := make (RepositoryList , 0 , opts .PageSize )
389
422
sess .Where (cond ).OrderBy (opts .OrderBy .String ())
390
423
if opts .PageSize > 0 {
391
424
sess .Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize )
392
425
}
393
- if err = sess .Find (& repos ); err != nil {
394
- return nil , 0 , fmt .Errorf ("Repo: %v" , err )
395
- }
396
-
397
- if loadAttributes {
398
- if err = repos .loadAttributes (sess ); err != nil {
399
- return nil , 0 , fmt .Errorf ("LoadAttributes: %v" , err )
400
- }
401
- }
402
-
403
- return repos , count , nil
426
+ return sess , count , nil
404
427
}
405
428
406
429
// accessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
@@ -456,6 +479,33 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
456
479
return SearchRepository (opts )
457
480
}
458
481
482
+ // SearchRepositoryIDs takes keyword and part of repository name to search,
483
+ // it returns results in given range and number of total results.
484
+ func SearchRepositoryIDs (opts * SearchRepoOptions ) ([]int64 , int64 , error ) {
485
+ opts .IncludeDescription = false
486
+
487
+ cond := SearchRepositoryCondition (opts )
488
+
489
+ sess , count , err := searchRepositoryByCondition (opts , cond )
490
+ if err != nil {
491
+ return nil , 0 , err
492
+ }
493
+ defer sess .Close ()
494
+
495
+ defaultSize := 50
496
+ if opts .PageSize > 0 {
497
+ defaultSize = opts .PageSize
498
+ }
499
+
500
+ ids := make ([]int64 , 0 , defaultSize )
501
+ err = sess .Select ("id" ).Table ("repository" ).Find (& ids )
502
+ if opts .PageSize <= 0 {
503
+ count = int64 (len (ids ))
504
+ }
505
+
506
+ return ids , count , err
507
+ }
508
+
459
509
// AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
460
510
func AccessibleRepoIDsQuery (user * User ) * builder.Builder {
461
511
// NB: Please note this code needs to still work if user is nil
0 commit comments