2017-02-05 22:35:03 +08:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
2019-06-13 03:41:28 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
2021-09-19 19:49:59 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2019-06-13 03:41:28 +08:00
|
|
|
)
|
2017-02-05 22:35:03 +08:00
|
|
|
|
|
|
|
// RepoRedirect represents that a repo name should be redirected to another
|
|
|
|
type RepoRedirect struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OwnerID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
RedirectRepoID int64 // repoID to redirect to
|
|
|
|
}
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(RepoRedirect))
|
|
|
|
}
|
|
|
|
|
2017-02-05 22:35:03 +08:00
|
|
|
// LookupRepoRedirect look up if a repository has a redirect name
|
|
|
|
func LookupRepoRedirect(ownerID int64, repoName string) (int64, error) {
|
|
|
|
repoName = strings.ToLower(repoName)
|
|
|
|
redirect := &RepoRedirect{OwnerID: ownerID, LowerName: repoName}
|
2021-09-23 23:45:36 +08:00
|
|
|
if has, err := db.GetEngine(db.DefaultContext).Get(redirect); err != nil {
|
2017-02-05 22:35:03 +08:00
|
|
|
return 0, err
|
|
|
|
} else if !has {
|
|
|
|
return 0, ErrRepoRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
|
|
|
|
}
|
|
|
|
return redirect.RedirectRepoID, nil
|
|
|
|
}
|
|
|
|
|
2021-01-24 23:23:05 +08:00
|
|
|
// newRepoRedirect create a new repo redirect
|
2021-09-19 19:49:59 +08:00
|
|
|
func newRepoRedirect(e db.Engine, ownerID, repoID int64, oldRepoName, newRepoName string) error {
|
2017-02-05 22:35:03 +08:00
|
|
|
oldRepoName = strings.ToLower(oldRepoName)
|
|
|
|
newRepoName = strings.ToLower(newRepoName)
|
|
|
|
|
2021-01-24 23:23:05 +08:00
|
|
|
if err := deleteRepoRedirect(e, ownerID, newRepoName); err != nil {
|
2017-02-05 22:35:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-24 23:23:05 +08:00
|
|
|
if _, err := e.Insert(&RepoRedirect{
|
2017-02-05 22:35:03 +08:00
|
|
|
OwnerID: ownerID,
|
|
|
|
LowerName: oldRepoName,
|
|
|
|
RedirectRepoID: repoID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-06 12:00:50 +08:00
|
|
|
return nil
|
2017-02-05 22:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// deleteRepoRedirect delete any redirect from the specified repo name to
|
|
|
|
// anything else
|
2021-09-19 19:49:59 +08:00
|
|
|
func deleteRepoRedirect(e db.Engine, ownerID int64, repoName string) error {
|
2017-02-05 22:35:03 +08:00
|
|
|
repoName = strings.ToLower(repoName)
|
|
|
|
_, err := e.Delete(&RepoRedirect{OwnerID: ownerID, LowerName: repoName})
|
|
|
|
return err
|
|
|
|
}
|