diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-02-05 09:35:03 -0500 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-05 22:35:03 +0800 |
commit | 027591a3a556477a26d6c849e1ed9b9a53c15110 (patch) | |
tree | a2530f9419e864fbb68e6b38dd7b28ce58f592b6 /modules/context | |
parent | e86d9351754f85b1508092b793dbd8a5cd1456f2 (diff) | |
download | gitea-027591a3a556477a26d6c849e1ed9b9a53c15110.tar.gz gitea-027591a3a556477a26d6c849e1ed9b9a53c15110.zip |
Redirects for renamed repos (#807)
* Redirects for renamed repos
* Remove unused phrase from locales
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/repo.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index 8de5b9821b..1ae98545a7 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -133,6 +133,26 @@ func earlyResponseForGoGetMeta(ctx *Context) { }))) } +// RedirectToRepo redirect to a differently-named repository +func RedirectToRepo(ctx *Context, redirectRepoID int64) { + ownerName := ctx.Params(":username") + previousRepoName := ctx.Params(":reponame") + + repo, err := models.GetRepositoryByID(redirectRepoID) + if err != nil { + ctx.Handle(500, "GetRepositoryByID", err) + return + } + + redirectPath := strings.Replace( + ctx.Req.URL.Path, + fmt.Sprintf("%s/%s", ownerName, previousRepoName), + fmt.Sprintf("%s/%s", ownerName, repo.Name), + 1, + ) + ctx.Redirect(redirectPath) +} + // RepoAssignment returns a macaron to handle repository assignment func RepoAssignment(args ...bool) macaron.Handler { return func(ctx *Context) { @@ -176,11 +196,18 @@ func RepoAssignment(args ...bool) macaron.Handler { repo, err := models.GetRepositoryByName(owner.ID, repoName) if err != nil { if models.IsErrRepoNotExist(err) { - if ctx.Query("go-get") == "1" { - earlyResponseForGoGetMeta(ctx) - return + redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName) + if err == nil { + RedirectToRepo(ctx, redirectRepoID) + } else if models.IsErrRepoRedirectNotExist(err) { + if ctx.Query("go-get") == "1" { + earlyResponseForGoGetMeta(ctx) + return + } + ctx.Handle(404, "GetRepositoryByName", err) + } else { + ctx.Handle(500, "LookupRepoRedirect", err) } - ctx.Handle(404, "GetRepositoryByName", err) } else { ctx.Handle(500, "GetRepositoryByName", err) } |