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.

62 lines
1.8 KiB

  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 "strings"
  6. // RepoRedirect represents that a repo name should be redirected to another
  7. type RepoRedirect struct {
  8. ID int64 `xorm:"pk autoincr"`
  9. OwnerID int64 `xorm:"UNIQUE(s)"`
  10. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  11. RedirectRepoID int64 // repoID to redirect to
  12. }
  13. // LookupRepoRedirect look up if a repository has a redirect name
  14. func LookupRepoRedirect(ownerID int64, repoName string) (int64, error) {
  15. repoName = strings.ToLower(repoName)
  16. redirect := &RepoRedirect{OwnerID: ownerID, LowerName: repoName}
  17. if has, err := x.Get(redirect); err != nil {
  18. return 0, err
  19. } else if !has {
  20. return 0, ErrRepoRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
  21. }
  22. return redirect.RedirectRepoID, nil
  23. }
  24. // NewRepoRedirect create a new repo redirect
  25. func NewRepoRedirect(ownerID, repoID int64, oldRepoName, newRepoName string) error {
  26. oldRepoName = strings.ToLower(oldRepoName)
  27. newRepoName = strings.ToLower(newRepoName)
  28. sess := x.NewSession()
  29. defer sess.Close()
  30. if err := sess.Begin(); err != nil {
  31. return err
  32. }
  33. if err := deleteRepoRedirect(sess, ownerID, newRepoName); err != nil {
  34. sess.Rollback()
  35. return err
  36. }
  37. if _, err := sess.Insert(&RepoRedirect{
  38. OwnerID: ownerID,
  39. LowerName: oldRepoName,
  40. RedirectRepoID: repoID,
  41. }); err != nil {
  42. sess.Rollback()
  43. return err
  44. }
  45. return sess.Commit()
  46. }
  47. // deleteRepoRedirect delete any redirect from the specified repo name to
  48. // anything else
  49. func deleteRepoRedirect(e Engine, ownerID int64, repoName string) error {
  50. repoName = strings.ToLower(repoName)
  51. _, err := e.Delete(&RepoRedirect{OwnerID: ownerID, LowerName: repoName})
  52. return err
  53. }