diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-27 08:45:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-27 08:45:03 +0800 |
commit | 32faec00e63511907780def0b539fc5eacb7a168 (patch) | |
tree | df123d0f2724511b66fe8c6be734887fd2191657 /models | |
parent | d4e38cf129e30327039d08fa460017ace7959a84 (diff) | |
download | gitea-32faec00e63511907780def0b539fc5eacb7a168.tar.gz gitea-32faec00e63511907780def0b539fc5eacb7a168.zip |
move all repo list functions as a standalone file (#1061)
Diffstat (limited to 'models')
-rw-r--r-- | models/repo.go | 259 | ||||
-rw-r--r-- | models/repo_list.go | 271 |
2 files changed, 271 insertions, 259 deletions
diff --git a/models/repo.go b/models/repo.go index 021986f0aa..2018f4c134 100644 --- a/models/repo.go +++ b/models/repo.go @@ -30,7 +30,6 @@ import ( "github.com/Unknwon/cae/zip" "github.com/Unknwon/com" - "github.com/go-xorm/builder" "github.com/go-xorm/xorm" version "github.com/mcuadros/go-version" ini "gopkg.in/ini.v1" @@ -1230,30 +1229,6 @@ func CountUserRepositories(userID int64, private bool) int64 { return countRepositories(userID, private) } -// Repositories returns all repositories -func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) { - if len(opts.OrderBy) == 0 { - opts.OrderBy = "id ASC" - } - - repos := make(RepositoryList, 0, opts.PageSize) - - if err = x. - Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - OrderBy(opts.OrderBy). - Find(&repos); err != nil { - return nil, 0, fmt.Errorf("Repo: %v", err) - } - - if err = repos.loadAttributes(x); err != nil { - return nil, 0, fmt.Errorf("LoadAttributes: %v", err) - } - - count = countRepositories(-1, opts.Private) - - return repos, count, nil -} - // RepoPath returns repository path by given user and repository name. func RepoPath(userName, repoName string) string { return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git") @@ -1758,57 +1733,6 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) { Find(&repos) } -// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. -func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) { - var cond = builder.NewCond() - - if len(opts.OrderBy) == 0 { - opts.OrderBy = "updated_unix DESC" - } - - if !opts.Private { - cond = builder.Eq{ - "is_private": false, - } - } - - if opts.Searcher != nil && !opts.Searcher.IsAdmin { - var ownerIds []int64 - - ownerIds = append(ownerIds, opts.Searcher.ID) - err := opts.Searcher.GetOrganizations(true) - - if err != nil { - return nil, 0, fmt.Errorf("Organization: %v", err) - } - - for _, org := range opts.Searcher.Orgs { - ownerIds = append(ownerIds, org.ID) - } - - cond = cond.Or(builder.In("owner_id", ownerIds)) - } - - count, err := x.Where(cond).Count(new(Repository)) - if err != nil { - return nil, 0, fmt.Errorf("Count: %v", err) - } - - if err = x.Where(cond). - Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - Limit(opts.PageSize). - OrderBy(opts.OrderBy). - Find(&repos); err != nil { - return nil, 0, fmt.Errorf("Repo: %v", err) - } - - if err = repos.loadAttributes(x); err != nil { - return nil, 0, fmt.Errorf("LoadAttributes: %v", err) - } - - return repos, count, nil -} - func getRepositoryCount(e Engine, u *User) (int64, error) { return x.Count(&Repository{OwnerID: u.ID}) } @@ -1836,111 +1760,6 @@ func GetPrivateRepositoryCount(u *User) (int64, error) { return getPrivateRepositoryCount(x, u) } -// SearchRepoOptions holds the search options -type SearchRepoOptions struct { - Keyword string - OwnerID int64 - Searcher *User //ID of the person who's seeking - OrderBy string - Private bool // Include private repositories in results - Starred bool - Page int - IsProfile bool - PageSize int // Can be smaller than or equal to setting.ExplorePagingNum -} - -// SearchRepositoryByName takes keyword and part of repository name to search, -// it returns results in given range and number of total results. -func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) { - var ( - sess *xorm.Session - cond = builder.NewCond() - ) - - if len(opts.Keyword) == 0 { - return repos, 0, nil - } - opts.Keyword = strings.ToLower(opts.Keyword) - - if opts.Page <= 0 { - opts.Page = 1 - } - - repos = make([]*Repository, 0, opts.PageSize) - - if opts.Starred && opts.OwnerID > 0 { - cond = builder.Eq{ - "star.uid": opts.OwnerID, - } - } - cond = cond.And(builder.Like{"lower_name", opts.Keyword}) - - // Append conditions - if !opts.Starred && opts.OwnerID > 0 { - cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) - } - if !opts.Private { - cond = cond.And(builder.Eq{"is_private": false}) - } - - if opts.Searcher != nil { - var ownerIds []int64 - - ownerIds = append(ownerIds, opts.Searcher.ID) - err = opts.Searcher.GetOrganizations(true) - - if err != nil { - return nil, 0, fmt.Errorf("Organization: %v", err) - } - - for _, org := range opts.Searcher.Orgs { - ownerIds = append(ownerIds, org.ID) - } - - cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds))) - } - - if len(opts.OrderBy) == 0 { - opts.OrderBy = "name ASC" - } - - if opts.Starred && opts.OwnerID > 0 { - sess = x. - Join("INNER", "star", "star.repo_id = repository.id"). - Where(cond) - count, err = x. - Join("INNER", "star", "star.repo_id = repository.id"). - Where(cond). - Count(new(Repository)) - if err != nil { - return nil, 0, fmt.Errorf("Count: %v", err) - } - } else { - sess = x.Where(cond) - count, err = x. - Where(cond). - Count(new(Repository)) - if err != nil { - return nil, 0, fmt.Errorf("Count: %v", err) - } - } - - if err = sess. - Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - OrderBy(opts.OrderBy). - Find(&repos); err != nil { - return nil, 0, fmt.Errorf("Repo: %v", err) - } - - if !opts.IsProfile { - if err = repos.loadAttributes(x); err != nil { - return nil, 0, fmt.Errorf("LoadAttributes: %v", err) - } - } - - return -} - // DeleteRepositoryArchives deletes all repositories' archives. func DeleteRepositoryArchives() error { return x. @@ -2258,84 +2077,6 @@ func CheckRepoStats() { // ***** END: Repository.NumForks ***** } -// RepositoryList contains a list of repositories -type RepositoryList []*Repository - -func (repos RepositoryList) loadAttributes(e Engine) error { - if len(repos) == 0 { - return nil - } - - // Load owners. - set := make(map[int64]*User) - for i := range repos { - set[repos[i].OwnerID] = nil - } - userIDs := make([]int64, 0, len(set)) - for userID := range set { - userIDs = append(userIDs, userID) - } - users := make([]*User, 0, len(userIDs)) - if err := e. - Where("id > 0"). - In("id", userIDs). - Find(&users); err != nil { - return fmt.Errorf("find users: %v", err) - } - for i := range users { - set[users[i].ID] = users[i] - } - for i := range repos { - repos[i].Owner = set[repos[i].OwnerID] - } - return nil -} - -// LoadAttributes loads the attributes for the given RepositoryList -func (repos RepositoryList) LoadAttributes() error { - return repos.loadAttributes(x) -} - -// MirrorRepositoryList contains the mirror repositories -type MirrorRepositoryList []*Repository - -func (repos MirrorRepositoryList) loadAttributes(e Engine) error { - if len(repos) == 0 { - return nil - } - - // Load mirrors. - repoIDs := make([]int64, 0, len(repos)) - for i := range repos { - if !repos[i].IsMirror { - continue - } - - repoIDs = append(repoIDs, repos[i].ID) - } - mirrors := make([]*Mirror, 0, len(repoIDs)) - if err := e. - Where("id > 0"). - In("repo_id", repoIDs). - Find(&mirrors); err != nil { - return fmt.Errorf("find mirrors: %v", err) - } - - set := make(map[int64]*Mirror) - for i := range mirrors { - set[mirrors[i].RepoID] = mirrors[i] - } - for i := range repos { - repos[i].Mirror = set[repos[i].ID] - } - return nil -} - -// LoadAttributes loads the attributes for the given MirrorRepositoryList -func (repos MirrorRepositoryList) LoadAttributes() error { - return repos.loadAttributes(x) -} - // ___________ __ // \_ _____/__________| | __ // | __)/ _ \_ __ \ |/ / diff --git a/models/repo_list.go b/models/repo_list.go new file mode 100644 index 0000000000..a1a309f9c4 --- /dev/null +++ b/models/repo_list.go @@ -0,0 +1,271 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "fmt" + "strings" + + "github.com/go-xorm/builder" + "github.com/go-xorm/xorm" +) + +// RepositoryList contains a list of repositories +type RepositoryList []*Repository + +func (repos RepositoryList) loadAttributes(e Engine) error { + if len(repos) == 0 { + return nil + } + + // Load owners. + set := make(map[int64]*User) + for i := range repos { + set[repos[i].OwnerID] = nil + } + userIDs := make([]int64, 0, len(set)) + for userID := range set { + userIDs = append(userIDs, userID) + } + users := make([]*User, 0, len(userIDs)) + if err := e. + Where("id > 0"). + In("id", userIDs). + Find(&users); err != nil { + return fmt.Errorf("find users: %v", err) + } + for i := range users { + set[users[i].ID] = users[i] + } + for i := range repos { + repos[i].Owner = set[repos[i].OwnerID] + } + return nil +} + +// LoadAttributes loads the attributes for the given RepositoryList +func (repos RepositoryList) LoadAttributes() error { + return repos.loadAttributes(x) +} + +// MirrorRepositoryList contains the mirror repositories +type MirrorRepositoryList []*Repository + +func (repos MirrorRepositoryList) loadAttributes(e Engine) error { + if len(repos) == 0 { + return nil + } + + // Load mirrors. + repoIDs := make([]int64, 0, len(repos)) + for i := range repos { + if !repos[i].IsMirror { + continue + } + + repoIDs = append(repoIDs, repos[i].ID) + } + mirrors := make([]*Mirror, 0, len(repoIDs)) + if err := e. + Where("id > 0"). + In("repo_id", repoIDs). + Find(&mirrors); err != nil { + return fmt.Errorf("find mirrors: %v", err) + } + + set := make(map[int64]*Mirror) + for i := range mirrors { + set[mirrors[i].RepoID] = mirrors[i] + } + for i := range repos { + repos[i].Mirror = set[repos[i].ID] + } + return nil +} + +// LoadAttributes loads the attributes for the given MirrorRepositoryList +func (repos MirrorRepositoryList) LoadAttributes() error { + return repos.loadAttributes(x) +} + +// SearchRepoOptions holds the search options +type SearchRepoOptions struct { + Keyword string + OwnerID int64 + Searcher *User //ID of the person who's seeking + OrderBy string + Private bool // Include private repositories in results + Starred bool + Page int + IsProfile bool + PageSize int // Can be smaller than or equal to setting.ExplorePagingNum +} + +// SearchRepositoryByName takes keyword and part of repository name to search, +// it returns results in given range and number of total results. +func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) { + var ( + sess *xorm.Session + cond = builder.NewCond() + ) + + if len(opts.Keyword) == 0 { + return repos, 0, nil + } + opts.Keyword = strings.ToLower(opts.Keyword) + + if opts.Page <= 0 { + opts.Page = 1 + } + + repos = make([]*Repository, 0, opts.PageSize) + + if opts.Starred && opts.OwnerID > 0 { + cond = builder.Eq{ + "star.uid": opts.OwnerID, + } + } + cond = cond.And(builder.Like{"lower_name", opts.Keyword}) + + // Append conditions + if !opts.Starred && opts.OwnerID > 0 { + cond = cond.And(builder.Eq{"owner_id": opts.OwnerID}) + } + if !opts.Private { + cond = cond.And(builder.Eq{"is_private": false}) + } + + if opts.Searcher != nil { + var ownerIds []int64 + + ownerIds = append(ownerIds, opts.Searcher.ID) + err = opts.Searcher.GetOrganizations(true) + + if err != nil { + return nil, 0, fmt.Errorf("Organization: %v", err) + } + + for _, org := range opts.Searcher.Orgs { + ownerIds = append(ownerIds, org.ID) + } + + cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds))) + } + + if len(opts.OrderBy) == 0 { + opts.OrderBy = "name ASC" + } + + if opts.Starred && opts.OwnerID > 0 { + sess = x. + Join("INNER", "star", "star.repo_id = repository.id"). + Where(cond) + count, err = x. + Join("INNER", "star", "star.repo_id = repository.id"). + Where(cond). + Count(new(Repository)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + } else { + sess = x.Where(cond) + count, err = x. + Where(cond). + Count(new(Repository)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + } + + if err = sess. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + OrderBy(opts.OrderBy). + Find(&repos); err != nil { + return nil, 0, fmt.Errorf("Repo: %v", err) + } + + if !opts.IsProfile { + if err = repos.loadAttributes(x); err != nil { + return nil, 0, fmt.Errorf("LoadAttributes: %v", err) + } + } + + return +} + +// Repositories returns all repositories +func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) { + if len(opts.OrderBy) == 0 { + opts.OrderBy = "id ASC" + } + + repos := make(RepositoryList, 0, opts.PageSize) + + if err = x. + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + OrderBy(opts.OrderBy). + Find(&repos); err != nil { + return nil, 0, fmt.Errorf("Repo: %v", err) + } + + if err = repos.loadAttributes(x); err != nil { + return nil, 0, fmt.Errorf("LoadAttributes: %v", err) + } + + count = countRepositories(-1, opts.Private) + + return repos, count, nil +} + +// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. +func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) { + var cond = builder.NewCond() + + if len(opts.OrderBy) == 0 { + opts.OrderBy = "updated_unix DESC" + } + + if !opts.Private { + cond = builder.Eq{ + "is_private": false, + } + } + + if opts.Searcher != nil && !opts.Searcher.IsAdmin { + var ownerIds []int64 + + ownerIds = append(ownerIds, opts.Searcher.ID) + err := opts.Searcher.GetOrganizations(true) + + if err != nil { + return nil, 0, fmt.Errorf("Organization: %v", err) + } + + for _, org := range opts.Searcher.Orgs { + ownerIds = append(ownerIds, org.ID) + } + + cond = cond.Or(builder.In("owner_id", ownerIds)) + } + + count, err := x.Where(cond).Count(new(Repository)) + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + + if err = x.Where(cond). + Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). + Limit(opts.PageSize). + OrderBy(opts.OrderBy). + Find(&repos); err != nil { + return nil, 0, fmt.Errorf("Repo: %v", err) + } + + if err = repos.loadAttributes(x); err != nil { + return nil, 0, fmt.Errorf("LoadAttributes: %v", err) + } + + return repos, count, nil +} |