本站源代码
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.

73 lines
2.1KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. // RepoRedirect represents that a repo name should be redirected to another
  10. type RepoRedirect struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. OwnerID int64 `xorm:"UNIQUE(s)"`
  13. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  14. RedirectRepoID int64 // repoID to redirect to
  15. }
  16. // LookupRepoRedirect look up if a repository has a redirect name
  17. func LookupRepoRedirect(ownerID int64, repoName string) (int64, error) {
  18. repoName = strings.ToLower(repoName)
  19. redirect := &RepoRedirect{OwnerID: ownerID, LowerName: repoName}
  20. if has, err := x.Get(redirect); err != nil {
  21. return 0, err
  22. } else if !has {
  23. return 0, ErrRepoRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
  24. }
  25. return redirect.RedirectRepoID, nil
  26. }
  27. // NewRepoRedirect create a new repo redirect
  28. func NewRepoRedirect(ownerID, repoID int64, oldRepoName, newRepoName string) error {
  29. oldRepoName = strings.ToLower(oldRepoName)
  30. newRepoName = strings.ToLower(newRepoName)
  31. sess := x.NewSession()
  32. defer sess.Close()
  33. if err := sess.Begin(); err != nil {
  34. return err
  35. }
  36. if err := deleteRepoRedirect(sess, ownerID, newRepoName); err != nil {
  37. errRollback := sess.Rollback()
  38. if errRollback != nil {
  39. log.Error("NewRepoRedirect sess.Rollback: %v", errRollback)
  40. }
  41. return err
  42. }
  43. if _, err := sess.Insert(&RepoRedirect{
  44. OwnerID: ownerID,
  45. LowerName: oldRepoName,
  46. RedirectRepoID: repoID,
  47. }); err != nil {
  48. errRollback := sess.Rollback()
  49. if errRollback != nil {
  50. log.Error("NewRepoRedirect sess.Rollback: %v", errRollback)
  51. }
  52. return err
  53. }
  54. return sess.Commit()
  55. }
  56. // deleteRepoRedirect delete any redirect from the specified repo name to
  57. // anything else
  58. func deleteRepoRedirect(e Engine, ownerID int64, repoName string) error {
  59. repoName = strings.ToLower(repoName)
  60. _, err := e.Delete(&RepoRedirect{OwnerID: ownerID, LowerName: repoName})
  61. return err
  62. }
上海开阖软件有限公司 沪ICP备12045867号-1