You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

internal_repo.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. // Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  4. package private
  5. import (
  6. "context"
  7. "fmt"
  8. "net/http"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. gitea_context "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. )
  14. // __________
  15. // \______ \ ____ ______ ____
  16. // | _// __ \\____ \ / _ \
  17. // | | \ ___/| |_> > <_> )
  18. // |____|_ /\___ > __/ \____/
  19. // \/ \/|__|
  20. // _____ .__ __
  21. // / _ \ ______ _____|__| ____ ____ _____ ____ _____/ |_
  22. // / /_\ \ / ___// ___/ |/ ___\ / \ / \_/ __ \ / \ __\
  23. // / | \\___ \ \___ \| / /_/ > | \ Y Y \ ___/| | \ |
  24. // \____|__ /____ >____ >__\___ /|___| /__|_| /\___ >___| /__|
  25. // \/ \/ \/ /_____/ \/ \/ \/ \/
  26. // This file contains common functions relating to setting the Repository for the
  27. // internal routes
  28. // RepoAssignment assigns the repository and gitrepository to the private context
  29. func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
  30. ownerName := ctx.Params(":owner")
  31. repoName := ctx.Params(":repo")
  32. repo := loadRepository(ctx, ownerName, repoName)
  33. if ctx.Written() {
  34. // Error handled in loadRepository
  35. return nil
  36. }
  37. gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
  38. if err != nil {
  39. log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
  40. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  41. "Err": fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err),
  42. })
  43. return nil
  44. }
  45. ctx.Repo = &gitea_context.Repository{
  46. Repository: repo,
  47. GitRepo: gitRepo,
  48. }
  49. // We opened it, we should close it
  50. cancel := func() {
  51. // If it's been set to nil then assume someone else has closed it.
  52. if ctx.Repo.GitRepo != nil {
  53. ctx.Repo.GitRepo.Close()
  54. }
  55. }
  56. return cancel
  57. }
  58. func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *repo_model.Repository {
  59. repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, ownerName, repoName)
  60. if err != nil {
  61. log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
  62. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  63. "Err": fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
  64. })
  65. return nil
  66. }
  67. if repo.OwnerName == "" {
  68. repo.OwnerName = ownerName
  69. }
  70. return repo
  71. }