gitea源码

repository.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. activities_model "code.gitea.io/gitea/models/activities"
  12. "code.gitea.io/gitea/models/db"
  13. "code.gitea.io/gitea/models/git"
  14. issues_model "code.gitea.io/gitea/models/issues"
  15. "code.gitea.io/gitea/models/organization"
  16. access_model "code.gitea.io/gitea/models/perm/access"
  17. repo_model "code.gitea.io/gitea/models/repo"
  18. "code.gitea.io/gitea/models/unit"
  19. user_model "code.gitea.io/gitea/models/user"
  20. "code.gitea.io/gitea/modules/gitrepo"
  21. "code.gitea.io/gitea/modules/graceful"
  22. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  23. "code.gitea.io/gitea/modules/log"
  24. "code.gitea.io/gitea/modules/queue"
  25. repo_module "code.gitea.io/gitea/modules/repository"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/structs"
  28. "code.gitea.io/gitea/modules/util"
  29. notify_service "code.gitea.io/gitea/services/notify"
  30. pull_service "code.gitea.io/gitea/services/pull"
  31. )
  32. // WebSearchRepository represents a repository returned by web search
  33. type WebSearchRepository struct {
  34. Repository *structs.Repository `json:"repository"`
  35. LatestCommitStatus *git.CommitStatus `json:"latest_commit_status"`
  36. LocaleLatestCommitStatus string `json:"locale_latest_commit_status"`
  37. }
  38. // WebSearchResults results of a successful web search
  39. type WebSearchResults struct {
  40. OK bool `json:"ok"`
  41. Data []*WebSearchRepository `json:"data"`
  42. }
  43. // CreateRepository creates a repository for the user/organization.
  44. func CreateRepository(ctx context.Context, doer, owner *user_model.User, opts CreateRepoOptions) (*repo_model.Repository, error) {
  45. repo, err := CreateRepositoryDirectly(ctx, doer, owner, opts, true)
  46. if err != nil {
  47. // No need to rollback here we should do this in CreateRepository...
  48. return nil, err
  49. }
  50. notify_service.CreateRepository(ctx, doer, owner, repo)
  51. return repo, nil
  52. }
  53. // DeleteRepository deletes a repository for a user or organization.
  54. func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, notify bool) error {
  55. if err := pull_service.CloseRepoBranchesPulls(ctx, doer, repo); err != nil {
  56. log.Error("CloseRepoBranchesPulls failed: %v", err)
  57. }
  58. if notify {
  59. // If the repo itself has webhooks, we need to trigger them before deleting it...
  60. notify_service.DeleteRepository(ctx, doer, repo)
  61. }
  62. return DeleteRepositoryDirectly(ctx, repo.ID)
  63. }
  64. // PushCreateRepo creates a repository when a new repository is pushed to an appropriate namespace
  65. func PushCreateRepo(ctx context.Context, authUser, owner *user_model.User, repoName string) (*repo_model.Repository, error) {
  66. if !authUser.IsAdmin {
  67. if owner.IsOrganization() {
  68. if ok, err := organization.CanCreateOrgRepo(ctx, owner.ID, authUser.ID); err != nil {
  69. return nil, err
  70. } else if !ok {
  71. return nil, errors.New("cannot push-create repository for org")
  72. }
  73. } else if authUser.ID != owner.ID {
  74. return nil, errors.New("cannot push-create repository for another user")
  75. }
  76. }
  77. repo, err := CreateRepository(ctx, authUser, owner, CreateRepoOptions{
  78. Name: repoName,
  79. IsPrivate: setting.Repository.DefaultPushCreatePrivate || setting.Repository.ForcePrivate,
  80. })
  81. if err != nil {
  82. return nil, err
  83. }
  84. return repo, nil
  85. }
  86. // Init start repository service
  87. func Init(ctx context.Context) error {
  88. licenseUpdaterQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "repo_license_updater", repoLicenseUpdater)
  89. if licenseUpdaterQueue == nil {
  90. return errors.New("unable to create repo_license_updater queue")
  91. }
  92. go graceful.GetManager().RunWithCancel(licenseUpdaterQueue)
  93. if err := repo_module.LoadRepoConfig(); err != nil {
  94. return err
  95. }
  96. if err := initPushQueue(); err != nil {
  97. return err
  98. }
  99. return initBranchSyncQueue(graceful.GetManager().ShutdownContext())
  100. }
  101. // UpdateRepository updates a repository
  102. func UpdateRepository(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) {
  103. return db.WithTx(ctx, func(ctx context.Context) error {
  104. if err = updateRepository(ctx, repo, visibilityChanged); err != nil {
  105. return fmt.Errorf("updateRepository: %w", err)
  106. }
  107. return nil
  108. })
  109. }
  110. func MakeRepoPublic(ctx context.Context, repo *repo_model.Repository) (err error) {
  111. return db.WithTx(ctx, func(ctx context.Context) error {
  112. repo.IsPrivate = false
  113. if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_private"); err != nil {
  114. return err
  115. }
  116. if err = repo.LoadOwner(ctx); err != nil {
  117. return fmt.Errorf("LoadOwner: %w", err)
  118. }
  119. if repo.Owner.IsOrganization() {
  120. // Organization repository need to recalculate access table when visibility is changed.
  121. if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
  122. return fmt.Errorf("recalculateTeamAccesses: %w", err)
  123. }
  124. }
  125. // Create/Remove git-daemon-export-ok for git-daemon...
  126. if err := CheckDaemonExportOK(ctx, repo); err != nil {
  127. return err
  128. }
  129. forkRepos, err := repo_model.GetRepositoriesByForkID(ctx, repo.ID)
  130. if err != nil {
  131. return fmt.Errorf("getRepositoriesByForkID: %w", err)
  132. }
  133. if repo.Owner.Visibility != structs.VisibleTypePrivate {
  134. for i := range forkRepos {
  135. if err = MakeRepoPublic(ctx, forkRepos[i]); err != nil {
  136. return fmt.Errorf("MakeRepoPublic[%d]: %w", forkRepos[i].ID, err)
  137. }
  138. }
  139. }
  140. // If visibility is changed, we need to update the issue indexer.
  141. // Since the data in the issue indexer have field to indicate if the repo is public or not.
  142. issue_indexer.UpdateRepoIndexer(ctx, repo.ID)
  143. return nil
  144. })
  145. }
  146. func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err error) {
  147. return db.WithTx(ctx, func(ctx context.Context) error {
  148. repo.IsPrivate = true
  149. if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_private"); err != nil {
  150. return err
  151. }
  152. if err = repo.LoadOwner(ctx); err != nil {
  153. return fmt.Errorf("LoadOwner: %w", err)
  154. }
  155. if repo.Owner.IsOrganization() {
  156. // Organization repository need to recalculate access table when visibility is changed.
  157. if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
  158. return fmt.Errorf("recalculateTeamAccesses: %w", err)
  159. }
  160. }
  161. // If repo has become private, we need to set its actions to private.
  162. _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).Cols("is_private").Update(&activities_model.Action{
  163. IsPrivate: true,
  164. })
  165. if err != nil {
  166. return err
  167. }
  168. if err = repo_model.ClearRepoStars(ctx, repo.ID); err != nil {
  169. return err
  170. }
  171. // Create/Remove git-daemon-export-ok for git-daemon...
  172. if err := CheckDaemonExportOK(ctx, repo); err != nil {
  173. return err
  174. }
  175. forkRepos, err := repo_model.GetRepositoriesByForkID(ctx, repo.ID)
  176. if err != nil {
  177. return fmt.Errorf("getRepositoriesByForkID: %w", err)
  178. }
  179. for i := range forkRepos {
  180. if err = MakeRepoPrivate(ctx, forkRepos[i]); err != nil {
  181. return fmt.Errorf("MakeRepoPrivate[%d]: %w", forkRepos[i].ID, err)
  182. }
  183. }
  184. // If visibility is changed, we need to update the issue indexer.
  185. // Since the data in the issue indexer have field to indicate if the repo is public or not.
  186. issue_indexer.UpdateRepoIndexer(ctx, repo.ID)
  187. return nil
  188. })
  189. }
  190. // LinkedRepository returns the linked repo if any
  191. func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) {
  192. if a.IssueID != 0 {
  193. iss, err := issues_model.GetIssueByID(ctx, a.IssueID)
  194. if err != nil {
  195. return nil, unit.TypeIssues, err
  196. }
  197. repo, err := repo_model.GetRepositoryByID(ctx, iss.RepoID)
  198. unitType := unit.TypeIssues
  199. if iss.IsPull {
  200. unitType = unit.TypePullRequests
  201. }
  202. return repo, unitType, err
  203. } else if a.ReleaseID != 0 {
  204. rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
  205. if err != nil {
  206. return nil, unit.TypeReleases, err
  207. }
  208. repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID)
  209. return repo, unit.TypeReleases, err
  210. }
  211. return nil, -1, nil
  212. }
  213. // CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
  214. func CheckDaemonExportOK(ctx context.Context, repo *repo_model.Repository) error {
  215. if err := repo.LoadOwner(ctx); err != nil {
  216. return err
  217. }
  218. // Create/Remove git-daemon-export-ok for git-daemon...
  219. daemonExportFile := filepath.Join(repo.RepoPath(), `git-daemon-export-ok`)
  220. isExist, err := util.IsExist(daemonExportFile)
  221. if err != nil {
  222. log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
  223. return err
  224. }
  225. isPublic := !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic
  226. if !isPublic && isExist {
  227. if err = util.Remove(daemonExportFile); err != nil {
  228. log.Error("Failed to remove %s: %v", daemonExportFile, err)
  229. }
  230. } else if isPublic && !isExist {
  231. if f, err := os.Create(daemonExportFile); err != nil {
  232. log.Error("Failed to create %s: %v", daemonExportFile, err)
  233. } else {
  234. f.Close()
  235. }
  236. }
  237. return nil
  238. }
  239. // updateRepository updates a repository with db context
  240. func updateRepository(ctx context.Context, repo *repo_model.Repository, visibilityChanged bool) (err error) {
  241. repo.LowerName = strings.ToLower(repo.Name)
  242. e := db.GetEngine(ctx)
  243. if _, err = e.ID(repo.ID).NoAutoTime().AllCols().Update(repo); err != nil {
  244. return fmt.Errorf("update: %w", err)
  245. }
  246. if err = repo_module.UpdateRepoSize(ctx, repo); err != nil {
  247. log.Error("Failed to update size for repository: %v", err)
  248. }
  249. if visibilityChanged {
  250. if err = repo.LoadOwner(ctx); err != nil {
  251. return fmt.Errorf("LoadOwner: %w", err)
  252. }
  253. if repo.Owner.IsOrganization() {
  254. // Organization repository need to recalculate access table when visibility is changed.
  255. if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
  256. return fmt.Errorf("recalculateTeamAccesses: %w", err)
  257. }
  258. }
  259. // If repo has become private, we need to set its actions to private.
  260. if repo.IsPrivate {
  261. _, err = e.Where("repo_id = ?", repo.ID).Cols("is_private").Update(&activities_model.Action{
  262. IsPrivate: true,
  263. })
  264. if err != nil {
  265. return err
  266. }
  267. if err = repo_model.ClearRepoStars(ctx, repo.ID); err != nil {
  268. return err
  269. }
  270. }
  271. // Create/Remove git-daemon-export-ok for git-daemon...
  272. if err := CheckDaemonExportOK(ctx, repo); err != nil {
  273. return err
  274. }
  275. forkRepos, err := repo_model.GetRepositoriesByForkID(ctx, repo.ID)
  276. if err != nil {
  277. return fmt.Errorf("getRepositoriesByForkID: %w", err)
  278. }
  279. for i := range forkRepos {
  280. forkRepos[i].IsPrivate = repo.IsPrivate || repo.Owner.Visibility == structs.VisibleTypePrivate
  281. if err = updateRepository(ctx, forkRepos[i], true); err != nil {
  282. return fmt.Errorf("updateRepository[%d]: %w", forkRepos[i].ID, err)
  283. }
  284. }
  285. // If visibility is changed, we need to update the issue indexer.
  286. // Since the data in the issue indexer have field to indicate if the repo is public or not.
  287. issue_indexer.UpdateRepoIndexer(ctx, repo.ID)
  288. }
  289. return nil
  290. }
  291. func HasWiki(ctx context.Context, repo *repo_model.Repository) bool {
  292. hasWiki, err := gitrepo.IsRepositoryExist(ctx, repo.WikiStorageRepo())
  293. if err != nil {
  294. log.Error("gitrepo.IsRepositoryExist: %v", err)
  295. }
  296. return hasWiki && err == nil
  297. }