code.gitea.io/gitea@v1.21.7/models/repo/fork.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package repo 5 6 import ( 7 "context" 8 9 "code.gitea.io/gitea/models/db" 10 user_model "code.gitea.io/gitea/models/user" 11 12 "xorm.io/builder" 13 ) 14 15 // GetRepositoriesByForkID returns all repositories with given fork ID. 16 func GetRepositoriesByForkID(ctx context.Context, forkID int64) ([]*Repository, error) { 17 repos := make([]*Repository, 0, 10) 18 return repos, db.GetEngine(ctx). 19 Where("fork_id=?", forkID). 20 Find(&repos) 21 } 22 23 // GetForkedRepo checks if given user has already forked a repository with given ID. 24 func GetForkedRepo(ctx context.Context, ownerID, repoID int64) *Repository { 25 repo := new(Repository) 26 has, _ := db.GetEngine(ctx). 27 Where("owner_id=? AND fork_id=?", ownerID, repoID). 28 Get(repo) 29 if has { 30 return repo 31 } 32 return nil 33 } 34 35 // HasForkedRepo checks if given user has already forked a repository with given ID. 36 func HasForkedRepo(ctx context.Context, ownerID, repoID int64) bool { 37 has, _ := db.GetEngine(ctx). 38 Table("repository"). 39 Where("owner_id=? AND fork_id=?", ownerID, repoID). 40 Exist() 41 return has 42 } 43 44 // GetUserFork return user forked repository from this repository, if not forked return nil 45 func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) { 46 var forkedRepo Repository 47 has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo) 48 if err != nil { 49 return nil, err 50 } 51 if !has { 52 return nil, nil 53 } 54 return &forkedRepo, nil 55 } 56 57 // GetForks returns all the forks of the repository 58 func GetForks(ctx context.Context, repo *Repository, listOptions db.ListOptions) ([]*Repository, error) { 59 if listOptions.Page == 0 { 60 forks := make([]*Repository, 0, repo.NumForks) 61 return forks, db.GetEngine(ctx).Find(&forks, &Repository{ForkID: repo.ID}) 62 } 63 64 sess := db.GetPaginatedSession(&listOptions) 65 forks := make([]*Repository, 0, listOptions.PageSize) 66 return forks, sess.Find(&forks, &Repository{ForkID: repo.ID}) 67 } 68 69 // IncrementRepoForkNum increment repository fork number 70 func IncrementRepoForkNum(ctx context.Context, repoID int64) error { 71 _, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", repoID) 72 return err 73 } 74 75 // DecrementRepoForkNum decrement repository fork number 76 func DecrementRepoForkNum(ctx context.Context, repoID int64) error { 77 _, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repoID) 78 return err 79 } 80 81 // FindUserOrgForks returns the forked repositories for one user from a repository 82 func FindUserOrgForks(ctx context.Context, repoID, userID int64) ([]*Repository, error) { 83 cond := builder.And( 84 builder.Eq{"fork_id": repoID}, 85 builder.In("owner_id", 86 builder.Select("org_id"). 87 From("org_user"). 88 Where(builder.Eq{"uid": userID}), 89 ), 90 ) 91 92 var repos []*Repository 93 return repos, db.GetEngine(ctx).Table("repository").Where(cond).Find(&repos) 94 } 95 96 // GetForksByUserAndOrgs return forked repos of the user and owned orgs 97 func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *Repository) ([]*Repository, error) { 98 var repoList []*Repository 99 if user == nil { 100 return repoList, nil 101 } 102 forkedRepo, err := GetUserFork(ctx, repo.ID, user.ID) 103 if err != nil { 104 return repoList, err 105 } 106 if forkedRepo != nil { 107 repoList = append(repoList, forkedRepo) 108 } 109 orgForks, err := FindUserOrgForks(ctx, repo.ID, user.ID) 110 if err != nil { 111 return nil, err 112 } 113 repoList = append(repoList, orgForks...) 114 return repoList, nil 115 }