diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2017-10-29 19:04:25 -0700 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-10-30 10:04:25 +0800 |
commit | 513375c429435ba60a667b219bdfb00e5b760b38 (patch) | |
tree | 9f516c8d0ebbdc66808c9017df7db2ff9aa34b57 /routers | |
parent | 6e98812ecf4efb6f53d72414ca8f67b14fac6595 (diff) | |
download | gitea-513375c429435ba60a667b219bdfb00e5b760b38.tar.gz gitea-513375c429435ba60a667b219bdfb00e5b760b38.zip |
Make URL scheme unambiguous (#2408)
* Make URL scheme unambiguous
Redirect old routes to new routes
* Fix redirects to new URL scheme, and update template
* Fix branches/_new endpoints, and update integration test
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/api.go | 2 | ||||
-rw-r--r-- | routers/repo/branch.go | 10 | ||||
-rw-r--r-- | routers/repo/commit.go | 2 | ||||
-rw-r--r-- | routers/repo/editor.go | 18 | ||||
-rw-r--r-- | routers/repo/repo.go | 15 | ||||
-rw-r--r-- | routers/repo/view.go | 4 | ||||
-rw-r--r-- | routers/routes/routes.go | 67 |
7 files changed, 75 insertions, 43 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 1039135440..79ae5799e3 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -391,7 +391,7 @@ func RegisterRoutes(m *macaron.Macaron) { Post(reqToken(), bind(api.CreateForkOption{}), repo.CreateFork) m.Group("/branches", func() { m.Get("", repo.ListBranches) - m.Get("/*", context.RepoRef(), repo.GetBranch) + m.Get("/*", context.RepoRefByType(context.RepoRefBranch), repo.GetBranch) }) m.Group("/keys", func() { m.Combo("").Get(repo.ListDeployKeys). diff --git a/routers/repo/branch.go b/routers/repo/branch.go index f6eca39353..c56e8e86b2 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -202,7 +202,7 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) { if ctx.HasError() { ctx.Flash.Error(ctx.GetErrMsg()) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) return } @@ -216,19 +216,19 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) { if models.IsErrTagAlreadyExists(err) { e := err.(models.ErrTagAlreadyExists) ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName)) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) return } if models.IsErrBranchAlreadyExists(err) { e := err.(models.ErrBranchAlreadyExists) ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", e.BranchName)) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) return } if models.IsErrBranchNameConflict(err) { e := err.(models.ErrBranchNameConflict) ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName)) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()) return } @@ -237,5 +237,5 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) { } ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName)) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + form.NewBranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + form.NewBranchName) } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 637a50543a..afd6b113c4 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -120,7 +120,7 @@ func SearchCommits(ctx *context.Context) { keyword := strings.Trim(ctx.Query("q"), " ") if len(keyword) == 0 { - ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName) + ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL()) return } all := ctx.QueryBool("all") diff --git a/routers/repo/editor.go b/routers/repo/editor.go index 42ede9a28f..a6cc922364 100644 --- a/routers/repo/editor.go +++ b/routers/repo/editor.go @@ -113,7 +113,7 @@ func editFile(ctx *context.Context, isNewFile bool) { ctx.Data["TreeNames"] = treeNames ctx.Data["TreePaths"] = treePaths - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() ctx.Data["commit_summary"] = "" ctx.Data["commit_message"] = "" if canCommit { @@ -164,7 +164,7 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo ctx.Data["TreePath"] = form.TreePath ctx.Data["TreeNames"] = treeNames ctx.Data["TreePaths"] = treePaths - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + branchName ctx.Data["FileContent"] = form.Content ctx.Data["commit_summary"] = form.CommitSummary ctx.Data["commit_message"] = form.CommitMessage @@ -304,7 +304,7 @@ func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bo return } - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(form.TreePath)) + ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName + "/" + strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(form.TreePath)) } // EditFilePost response for editing file @@ -348,7 +348,7 @@ func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) { // DeleteFile render delete file page func DeleteFile(ctx *context.Context) { ctx.Data["PageIsDelete"] = true - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() ctx.Data["TreePath"] = ctx.Repo.TreePath canCommit := renderCommitRights(ctx) @@ -367,7 +367,7 @@ func DeleteFile(ctx *context.Context) { // DeleteFilePost response for deleting file func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) { ctx.Data["PageIsDelete"] = true - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() ctx.Data["TreePath"] = ctx.Repo.TreePath canCommit := renderCommitRights(ctx) @@ -422,7 +422,7 @@ func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) { } ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", ctx.Repo.TreePath)) - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName) + ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName) } func renderUploadSettings(ctx *context.Context) { @@ -446,7 +446,7 @@ func UploadFile(ctx *context.Context) { ctx.Data["TreeNames"] = treeNames ctx.Data["TreePaths"] = treePaths - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() ctx.Data["commit_summary"] = "" ctx.Data["commit_message"] = "" if canCommit { @@ -482,7 +482,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) { ctx.Data["TreePath"] = form.TreePath ctx.Data["TreeNames"] = treeNames ctx.Data["TreePaths"] = treePaths - ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName + ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/branch/" + branchName ctx.Data["commit_summary"] = form.CommitSummary ctx.Data["commit_message"] = form.CommitMessage ctx.Data["commit_choice"] = form.CommitChoice @@ -551,7 +551,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) { return } - ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + form.TreePath) + ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName + "/" + form.TreePath) } // UploadFileToServer upload file to server file dir not git diff --git a/routers/repo/repo.go b/routers/repo/repo.go index ebbce1e19c..dbe78f6d1e 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -34,6 +34,21 @@ func MustBeNotBare(ctx *context.Context) { } } +// MustBeEditable check that repo can be edited +func MustBeEditable(ctx *context.Context) { + if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit { + ctx.Handle(404, "", nil) + return + } +} + +// MustBeAbleToUpload check that repo can be uploaded to +func MustBeAbleToUpload(ctx *context.Context) { + if !setting.Repository.Upload.Enabled { + ctx.Handle(404, "", nil) + } +} + func checkContextUser(ctx *context.Context, uid int64) *models.User { orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.ID, "updated_unix") if err != nil { diff --git a/routers/repo/view.go b/routers/repo/view.go index 38260d2bec..d43b4d7f78 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -297,9 +297,9 @@ func renderCode(ctx *context.Context) { ctx.Data["Title"] = title ctx.Data["RequireHighlightJS"] = true - branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName + branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL() treeLink := branchLink - rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchName + rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL() if len(ctx.Repo.TreePath) > 0 { treeLink += "/" + ctx.Repo.TreePath diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 94dfe0ab36..f1c9f18489 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -522,34 +522,30 @@ func RegisterRoutes(m *macaron.Macaron) { Post(bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost) m.Group("", func() { - m.Combo("/_edit/*").Get(repo.EditFile). - Post(bindIgnErr(auth.EditRepoFileForm{}), repo.EditFilePost) - m.Combo("/_new/*").Get(repo.NewFile). - Post(bindIgnErr(auth.EditRepoFileForm{}), repo.NewFilePost) - m.Post("/_preview/*", bindIgnErr(auth.EditPreviewDiffForm{}), repo.DiffPreviewPost) - m.Combo("/_delete/*").Get(repo.DeleteFile). - Post(bindIgnErr(auth.DeleteRepoFileForm{}), repo.DeleteFilePost) - m.Group("", func() { - m.Combo("/_upload/*").Get(repo.UploadFile). + m.Combo("/_edit/*").Get(repo.EditFile). + Post(bindIgnErr(auth.EditRepoFileForm{}), repo.EditFilePost) + m.Combo("/_new/*").Get(repo.NewFile). + Post(bindIgnErr(auth.EditRepoFileForm{}), repo.NewFilePost) + m.Post("/_preview/*", bindIgnErr(auth.EditPreviewDiffForm{}), repo.DiffPreviewPost) + m.Combo("/_delete/*").Get(repo.DeleteFile). + Post(bindIgnErr(auth.DeleteRepoFileForm{}), repo.DeleteFilePost) + m.Combo("/_upload/*", repo.MustBeAbleToUpload). + Get(repo.UploadFile). Post(bindIgnErr(auth.UploadRepoFileForm{}), repo.UploadFilePost) + }, context.RepoRefByType(context.RepoRefBranch), repo.MustBeEditable) + m.Group("", func() { m.Post("/upload-file", repo.UploadFileToServer) m.Post("/upload-remove", bindIgnErr(auth.RemoveUploadFileForm{}), repo.RemoveUploadFileFromServer) - }, func(ctx *context.Context) { - if !setting.Repository.Upload.Enabled { - ctx.Handle(404, "", nil) - return - } - }) - }, repo.MustBeNotBare, reqRepoWriter, context.RepoRef(), func(ctx *context.Context) { - if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit { - ctx.Handle(404, "", nil) - return - } - }) + }, context.RepoRef(), repo.MustBeEditable, repo.MustBeAbleToUpload) + }, repo.MustBeNotBare, reqRepoWriter) m.Group("/branches", func() { - m.Post("/_new/*", context.RepoRef(), bindIgnErr(auth.NewBranchForm{}), repo.CreateBranch) + m.Group("/_new/", func() { + m.Post("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.CreateBranch) + m.Post("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.CreateBranch) + m.Post("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.CreateBranch) + }, bindIgnErr(auth.NewBranchForm{})) m.Post("/delete", repo.DeleteBranchPost) m.Post("/restore", repo.RestoreBranchPost) }, reqRepoWriter, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode)) @@ -629,15 +625,36 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/cleanup", context.RepoRef(), repo.CleanUpPullRequest) }, repo.MustAllowPulls) + m.Group("/raw", func() { + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload) + m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload) + // "/*" route is deprecated, and kept for backward compatibility + m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload) + }, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode)) + + m.Group("/commits", func() { + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.RefCommits) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.RefCommits) + m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RefCommits) + // "/*" route is deprecated, and kept for backward compatibility + m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.RefCommits) + }, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode)) + m.Group("", func() { - m.Get("/raw/*", repo.SingleDownload) - m.Get("/commits/*", repo.RefCommits) m.Get("/graph", repo.Graph) m.Get("/commit/:sha([a-f0-9]{7,40})$", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.Diff) }, repo.MustBeNotBare, context.RepoRef(), context.CheckUnit(models.UnitTypeCode)) + m.Group("/src", func() { + m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.Home) + m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.Home) + m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.Home) + // "/*" route is deprecated, and kept for backward compatibility + m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.Home) + }, repo.SetEditorconfigIfExists) + m.Group("", func() { - m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home) m.Get("/forks", repo.Forks) }, context.RepoRef(), context.CheckUnit(models.UnitTypeCode)) m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", |