Turn RepoRef and RepoAssignment back into func(*Context) (#15372)

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
tokarchuk/v1.17
zeripath 4 years ago committed by GitHub
parent d0eeba9ff9
commit 136a20926c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 603
      modules/context/repo.go
  2. 20
      routers/routes/web.go

@ -8,7 +8,6 @@ package context
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"path" "path"
"strings" "strings"
@ -394,238 +393,231 @@ func RepoIDAssignment() func(ctx *Context) {
} }
// RepoAssignment returns a middleware to handle repository assignment // RepoAssignment returns a middleware to handle repository assignment
func RepoAssignment() func(http.Handler) http.Handler { func RepoAssignment(ctx *Context) {
return func(next http.Handler) http.Handler { var (
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { owner *models.User
var ( err error
owner *models.User )
err error
ctx = GetContext(req) userName := ctx.Params(":username")
) repoName := ctx.Params(":reponame")
repoName = strings.TrimSuffix(repoName, ".git")
userName := ctx.Params(":username")
repoName := ctx.Params(":reponame") // Check if the user is the same as the repository owner
repoName = strings.TrimSuffix(repoName, ".git") if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
owner = ctx.User
// Check if the user is the same as the repository owner } else {
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) { owner, err = models.GetUserByName(userName)
owner = ctx.User if err != nil {
} else { if models.IsErrUserNotExist(err) {
owner, err = models.GetUserByName(userName) if ctx.Query("go-get") == "1" {
if err != nil { EarlyResponseForGoGetMeta(ctx)
if models.IsErrUserNotExist(err) {
if ctx.Query("go-get") == "1" {
EarlyResponseForGoGetMeta(ctx)
return
}
ctx.NotFound("GetUserByName", nil)
} else {
ctx.ServerError("GetUserByName", err)
}
return return
} }
ctx.NotFound("GetUserByName", nil)
} else {
ctx.ServerError("GetUserByName", err)
} }
ctx.Repo.Owner = owner return
ctx.Data["Username"] = ctx.Repo.Owner.Name }
}
ctx.Repo.Owner = owner
ctx.Data["Username"] = ctx.Repo.Owner.Name
// Get repository. // Get repository.
repo, err := models.GetRepositoryByName(owner.ID, repoName) repo, err := models.GetRepositoryByName(owner.ID, repoName)
if err != nil { if err != nil {
if models.IsErrRepoNotExist(err) { if models.IsErrRepoNotExist(err) {
redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName) redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
if err == nil { if err == nil {
RedirectToRepo(ctx, redirectRepoID) RedirectToRepo(ctx, redirectRepoID)
} else if models.IsErrRepoRedirectNotExist(err) { } else if models.IsErrRepoRedirectNotExist(err) {
if ctx.Query("go-get") == "1" { if ctx.Query("go-get") == "1" {
EarlyResponseForGoGetMeta(ctx) EarlyResponseForGoGetMeta(ctx)
return return
}
ctx.NotFound("GetRepositoryByName", nil)
} else {
ctx.ServerError("LookupRepoRedirect", err)
}
} else {
ctx.ServerError("GetRepositoryByName", err)
} }
return ctx.NotFound("GetRepositoryByName", nil)
} else {
ctx.ServerError("LookupRepoRedirect", err)
} }
repo.Owner = owner } else {
ctx.ServerError("GetRepositoryByName", err)
}
return
}
repo.Owner = owner
repoAssignment(ctx, repo) repoAssignment(ctx, repo)
if ctx.Written() { if ctx.Written() {
return return
} }
ctx.Repo.RepoLink = repo.Link() ctx.Repo.RepoLink = repo.Link()
ctx.Data["RepoLink"] = ctx.Repo.RepoLink ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker) unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker)
if err == nil { if err == nil {
ctx.Data["RepoExternalIssuesLink"] = unit.ExternalTrackerConfig().ExternalTrackerURL ctx.Data["RepoExternalIssuesLink"] = unit.ExternalTrackerConfig().ExternalTrackerURL
} }
ctx.Data["NumTags"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ ctx.Data["NumTags"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{
IncludeTags: true, IncludeTags: true,
}) })
if err != nil { if err != nil {
ctx.ServerError("GetReleaseCountByRepoID", err) ctx.ServerError("GetReleaseCountByRepoID", err)
return return
} }
ctx.Data["NumReleases"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{}) ctx.Data["NumReleases"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetReleaseCountByRepoID", err) ctx.ServerError("GetReleaseCountByRepoID", err)
return return
} }
ctx.Data["Title"] = owner.Name + "/" + repo.Name ctx.Data["Title"] = owner.Name + "/" + repo.Name
ctx.Data["Repository"] = repo ctx.Data["Repository"] = repo
ctx.Data["Owner"] = ctx.Repo.Repository.Owner ctx.Data["Owner"] = ctx.Repo.Repository.Owner
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner() ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner()
ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin() ctx.Data["IsRepositoryAdmin"] = ctx.Repo.IsAdmin()
ctx.Data["RepoOwnerIsOrganization"] = repo.Owner.IsOrganization() ctx.Data["RepoOwnerIsOrganization"] = repo.Owner.IsOrganization()
ctx.Data["CanWriteCode"] = ctx.Repo.CanWrite(models.UnitTypeCode) ctx.Data["CanWriteCode"] = ctx.Repo.CanWrite(models.UnitTypeCode)
ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(models.UnitTypeIssues) ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(models.UnitTypeIssues)
ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(models.UnitTypePullRequests) ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(models.UnitTypePullRequests)
if ctx.Data["CanSignedUserFork"], err = ctx.Repo.Repository.CanUserFork(ctx.User); err != nil {
ctx.ServerError("CanUserFork", err)
return
}
ctx.Data["DisableSSH"] = setting.SSH.Disabled if ctx.Data["CanSignedUserFork"], err = ctx.Repo.Repository.CanUserFork(ctx.User); err != nil {
ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous ctx.ServerError("CanUserFork", err)
ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit return
ctx.Data["RepoSearchEnabled"] = setting.Indexer.RepoIndexerEnabled }
ctx.Data["CloneLink"] = repo.CloneLink()
ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
if ctx.IsSigned { ctx.Data["DisableSSH"] = setting.SSH.Disabled
ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.ID, repo.ID) ctx.Data["ExposeAnonSSH"] = setting.SSH.ExposeAnonymous
ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.ID, repo.ID) ctx.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
} ctx.Data["RepoSearchEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.Data["CloneLink"] = repo.CloneLink()
ctx.Data["WikiCloneLink"] = repo.WikiCloneLink()
if repo.IsFork { if ctx.IsSigned {
RetrieveBaseRepo(ctx, repo) ctx.Data["IsWatchingRepo"] = models.IsWatching(ctx.User.ID, repo.ID)
if ctx.Written() { ctx.Data["IsStaringRepo"] = models.IsStaring(ctx.User.ID, repo.ID)
return }
}
}
if repo.IsGenerated() { if repo.IsFork {
RetrieveTemplateRepo(ctx, repo) RetrieveBaseRepo(ctx, repo)
if ctx.Written() { if ctx.Written() {
return return
} }
} }
// Disable everything when the repo is being created if repo.IsGenerated() {
if ctx.Repo.Repository.IsBeingCreated() { RetrieveTemplateRepo(ctx, repo)
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch if ctx.Written() {
return return
} }
}
gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) // Disable everything when the repo is being created
if err != nil { if ctx.Repo.Repository.IsBeingCreated() {
ctx.ServerError("RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
return return
} }
ctx.Repo.GitRepo = gitRepo
// We opened it, we should close it gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
defer func() { if err != nil {
// If it's been set to nil then assume someone else has closed it. ctx.ServerError("RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
if ctx.Repo.GitRepo != nil { return
ctx.Repo.GitRepo.Close() }
} ctx.Repo.GitRepo = gitRepo
}()
// Stop at this point when the repo is empty. // We opened it, we should close it
if ctx.Repo.Repository.IsEmpty { defer func() {
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch // If it's been set to nil then assume someone else has closed it.
next.ServeHTTP(w, req) if ctx.Repo.GitRepo != nil {
return ctx.Repo.GitRepo.Close()
} }
}()
tags, err := ctx.Repo.GitRepo.GetTags() // Stop at this point when the repo is empty.
if err != nil { if ctx.Repo.Repository.IsEmpty {
ctx.ServerError("GetTags", err) ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
return return
} }
ctx.Data["Tags"] = tags
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) tags, err := ctx.Repo.GitRepo.GetTags()
if err != nil { if err != nil {
ctx.ServerError("GetBranches", err) ctx.ServerError("GetTags", err)
return return
} }
ctx.Data["Branches"] = brs ctx.Data["Tags"] = tags
ctx.Data["BranchesCount"] = len(brs)
ctx.Data["TagName"] = ctx.Repo.TagName
// If not branch selected, try default one.
// If default branch doesn't exists, fall back to some other branch.
if len(ctx.Repo.BranchName) == 0 {
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
} else if len(brs) > 0 {
ctx.Repo.BranchName = brs[0]
}
}
ctx.Data["BranchName"] = ctx.Repo.BranchName
ctx.Data["CommitID"] = ctx.Repo.CommitID
// People who have push access or have forked repository can propose a new pull request.
canPush := ctx.Repo.CanWrite(models.UnitTypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID))
canCompare := false
// Pull request is allowed if this is a fork repository
// and base repository accepts pull requests.
if repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls() {
canCompare = true
ctx.Data["BaseRepo"] = repo.BaseRepo
ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
ctx.Repo.PullRequest.Allowed = canPush
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
} else if repo.AllowsPulls() {
// Or, this is repository accepts pull requests between branches.
canCompare = true
ctx.Data["BaseRepo"] = repo
ctx.Repo.PullRequest.BaseRepo = repo
ctx.Repo.PullRequest.Allowed = canPush
ctx.Repo.PullRequest.SameRepo = true
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
}
ctx.Data["CanCompareOrPull"] = canCompare
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
if ctx.Repo.Repository.Status == models.RepositoryPendingTransfer { brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0)
repoTransfer, err := models.GetPendingRepositoryTransfer(ctx.Repo.Repository) if err != nil {
if err != nil { ctx.ServerError("GetBranches", err)
ctx.ServerError("GetPendingRepositoryTransfer", err) return
return }
} ctx.Data["Branches"] = brs
ctx.Data["BranchesCount"] = len(brs)
if err := repoTransfer.LoadAttributes(); err != nil { ctx.Data["TagName"] = ctx.Repo.TagName
ctx.ServerError("LoadRecipient", err)
return
}
ctx.Data["RepoTransfer"] = repoTransfer // If not branch selected, try default one.
if ctx.User != nil { // If default branch doesn't exists, fall back to some other branch.
ctx.Data["CanUserAcceptTransfer"] = repoTransfer.CanUserAcceptTransfer(ctx.User) if len(ctx.Repo.BranchName) == 0 {
} if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
} ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
} else if len(brs) > 0 {
ctx.Repo.BranchName = brs[0]
}
}
ctx.Data["BranchName"] = ctx.Repo.BranchName
ctx.Data["CommitID"] = ctx.Repo.CommitID
// People who have push access or have forked repository can propose a new pull request.
canPush := ctx.Repo.CanWrite(models.UnitTypeCode) || (ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID))
canCompare := false
// Pull request is allowed if this is a fork repository
// and base repository accepts pull requests.
if repo.BaseRepo != nil && repo.BaseRepo.AllowsPulls() {
canCompare = true
ctx.Data["BaseRepo"] = repo.BaseRepo
ctx.Repo.PullRequest.BaseRepo = repo.BaseRepo
ctx.Repo.PullRequest.Allowed = canPush
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.Owner.Name + ":" + ctx.Repo.BranchName
} else if repo.AllowsPulls() {
// Or, this is repository accepts pull requests between branches.
canCompare = true
ctx.Data["BaseRepo"] = repo
ctx.Repo.PullRequest.BaseRepo = repo
ctx.Repo.PullRequest.Allowed = canPush
ctx.Repo.PullRequest.SameRepo = true
ctx.Repo.PullRequest.HeadInfo = ctx.Repo.BranchName
}
ctx.Data["CanCompareOrPull"] = canCompare
ctx.Data["PullRequestCtx"] = ctx.Repo.PullRequest
if ctx.Repo.Repository.Status == models.RepositoryPendingTransfer {
repoTransfer, err := models.GetPendingRepositoryTransfer(ctx.Repo.Repository)
if err != nil {
ctx.ServerError("GetPendingRepositoryTransfer", err)
return
}
if ctx.Query("go-get") == "1" { if err := repoTransfer.LoadAttributes(); err != nil {
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name) ctx.ServerError("LoadRecipient", err)
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", "branch", ctx.Repo.BranchName) return
ctx.Data["GoDocDirectory"] = prefix + "{/dir}" }
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
} ctx.Data["RepoTransfer"] = repoTransfer
next.ServeHTTP(w, req) if ctx.User != nil {
}) ctx.Data["CanUserAcceptTransfer"] = repoTransfer.CanUserAcceptTransfer(ctx.User)
}
}
if ctx.Query("go-get") == "1" {
ctx.Data["GoGetImport"] = ComposeGoGetImport(owner.Name, repo.Name)
prefix := setting.AppURL + path.Join(owner.Name, repo.Name, "src", "branch", ctx.Repo.BranchName)
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
} }
} }
@ -651,7 +643,7 @@ const (
// RepoRef handles repository reference names when the ref name is not // RepoRef handles repository reference names when the ref name is not
// explicitly given // explicitly given
func RepoRef() func(http.Handler) http.Handler { func RepoRef() func(*Context) {
// since no ref name is explicitly specified, ok to just use branch // since no ref name is explicitly specified, ok to just use branch
return RepoRefByType(RepoRefBranch) return RepoRefByType(RepoRefBranch)
} }
@ -730,130 +722,125 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
// RepoRefByType handles repository reference name for a specific type // RepoRefByType handles repository reference name for a specific type
// of repository reference // of repository reference
func RepoRefByType(refType RepoRefType) func(http.Handler) http.Handler { func RepoRefByType(refType RepoRefType) func(*Context) {
return func(next http.Handler) http.Handler { return func(ctx *Context) {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { // Empty repository does not have reference information.
ctx := GetContext(req) if ctx.Repo.Repository.IsEmpty {
// Empty repository does not have reference information. return
if ctx.Repo.Repository.IsEmpty { }
var (
refName string
err error
)
if ctx.Repo.GitRepo == nil {
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
if err != nil {
ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
return return
} }
// We opened it, we should close it
defer func() {
// If it's been set to nil then assume someone else has closed it.
if ctx.Repo.GitRepo != nil {
ctx.Repo.GitRepo.Close()
}
}()
}
var ( // Get default branch.
refName string if len(ctx.Params("*")) == 0 {
err error refName = ctx.Repo.Repository.DefaultBranch
) ctx.Repo.BranchName = refName
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
if ctx.Repo.GitRepo == nil { brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0)
repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepository(repoPath)
if err != nil { if err != nil {
ctx.ServerError("RepoRef Invalid repo "+repoPath, err) ctx.ServerError("GetBranches", err)
return
} else if len(brs) == 0 {
err = fmt.Errorf("No branches in non-empty repository %s",
ctx.Repo.GitRepo.Path)
ctx.ServerError("GetBranches", err)
return return
} }
// We opened it, we should close it refName = brs[0]
defer func() { }
// If it's been set to nil then assume someone else has closed it. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
if ctx.Repo.GitRepo != nil { if err != nil {
ctx.Repo.GitRepo.Close() ctx.ServerError("GetBranchCommit", err)
} return
}()
} }
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
ctx.Repo.IsViewBranch = true
} else {
refName = getRefName(ctx, refType)
ctx.Repo.BranchName = refName
if refType.RefTypeIncludesBranches() && ctx.Repo.GitRepo.IsBranchExist(refName) {
ctx.Repo.IsViewBranch = true
// Get default branch.
if len(ctx.Params("*")) == 0 {
refName = ctx.Repo.Repository.DefaultBranch
ctx.Repo.BranchName = refName
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0)
if err != nil {
ctx.ServerError("GetBranches", err)
return
} else if len(brs) == 0 {
err = fmt.Errorf("No branches in non-empty repository %s",
ctx.Repo.GitRepo.Path)
ctx.ServerError("GetBranches", err)
return
}
refName = brs[0]
}
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
if err != nil { if err != nil {
ctx.ServerError("GetBranchCommit", err) ctx.ServerError("GetBranchCommit", err)
return return
} }
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
ctx.Repo.IsViewBranch = true
} else { } else if refType.RefTypeIncludesTags() && ctx.Repo.GitRepo.IsTagExist(refName) {
refName = getRefName(ctx, refType) ctx.Repo.IsViewTag = true
ctx.Repo.BranchName = refName ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
if refType.RefTypeIncludesBranches() && ctx.Repo.GitRepo.IsBranchExist(refName) { if err != nil {
ctx.Repo.IsViewBranch = true ctx.ServerError("GetTagCommit", err)
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
if err != nil {
ctx.ServerError("GetBranchCommit", err)
return
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if refType.RefTypeIncludesTags() && ctx.Repo.GitRepo.IsTagExist(refName) {
ctx.Repo.IsViewTag = true
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName)
if err != nil {
ctx.ServerError("GetTagCommit", err)
return
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) >= 7 && len(refName) <= 40 {
ctx.Repo.IsViewCommit = true
ctx.Repo.CommitID = refName
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
if err != nil {
ctx.NotFound("GetCommit", err)
return
}
// If short commit ID add canonical link header
if len(refName) < 40 {
ctx.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), refName, ctx.Repo.Commit.ID.String(), 1))))
}
} else {
ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
return return
} }
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) >= 7 && len(refName) <= 40 {
ctx.Repo.IsViewCommit = true
ctx.Repo.CommitID = refName
if refType == RepoRefLegacy { ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
// redirect from old URL scheme to new URL scheme if err != nil {
ctx.Redirect(path.Join( ctx.NotFound("GetCommit", err)
setting.AppSubURL,
strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*")),
ctx.Repo.BranchNameSubURL(),
ctx.Repo.TreePath))
return return
} }
// If short commit ID add canonical link header
if len(refName) < 40 {
ctx.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), refName, ctx.Repo.Commit.ID.String(), 1))))
}
} else {
ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
return
} }
ctx.Data["BranchName"] = ctx.Repo.BranchName if refType == RepoRefLegacy {
ctx.Data["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL() // redirect from old URL scheme to new URL scheme
ctx.Data["CommitID"] = ctx.Repo.CommitID ctx.Redirect(path.Join(
ctx.Data["TreePath"] = ctx.Repo.TreePath setting.AppSubURL,
ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*")),
ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag ctx.Repo.BranchNameSubURL(),
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit ctx.Repo.TreePath))
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
return return
} }
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount }
next.ServeHTTP(w, req) ctx.Data["BranchName"] = ctx.Repo.BranchName
}) ctx.Data["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
ctx.Data["CommitID"] = ctx.Repo.CommitID
ctx.Data["TreePath"] = ctx.Repo.TreePath
ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch
ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
if err != nil {
ctx.ServerError("GetCommitsCount", err)
return
}
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
} }
} }

@ -691,7 +691,7 @@ func RegisterRoutes(m *web.Route) {
}, reqSignIn) }, reqSignIn)
// ***** Release Attachment Download without Signin // ***** Release Attachment Download without Signin
m.Get("/{username}/{reponame}/releases/download/{vTag}/{fileName}", ignSignIn, context.RepoAssignment(), repo.MustBeNotEmpty, repo.RedirectDownload) m.Get("/{username}/{reponame}/releases/download/{vTag}/{fileName}", ignSignIn, context.RepoAssignment, repo.MustBeNotEmpty, repo.RedirectDownload)
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
m.Group("/settings", func() { m.Group("/settings", func() {
@ -771,9 +771,9 @@ func RegisterRoutes(m *web.Route) {
ctx.Data["PageIsSettings"] = true ctx.Data["PageIsSettings"] = true
ctx.Data["LFSStartServer"] = setting.LFS.StartServer ctx.Data["LFSStartServer"] = setting.LFS.StartServer
}) })
}, reqSignIn, context.RepoAssignment(), context.UnitTypes(), reqRepoAdmin, context.RepoRef()) }, reqSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoAdmin, context.RepoRef())
m.Post("/{username}/{reponame}/action/{action}", reqSignIn, context.RepoAssignment(), context.UnitTypes(), repo.Action) m.Post("/{username}/{reponame}/action/{action}", reqSignIn, context.RepoAssignment, context.UnitTypes(), repo.Action)
// Grouping for those endpoints not requiring authentication // Grouping for those endpoints not requiring authentication
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
@ -783,7 +783,7 @@ func RegisterRoutes(m *web.Route) {
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff). Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost) Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
}, context.RepoAssignment(), context.UnitTypes()) }, context.RepoAssignment, context.UnitTypes())
// Grouping for those endpoints that do require authentication // Grouping for those endpoints that do require authentication
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
@ -890,7 +890,7 @@ func RegisterRoutes(m *web.Route) {
m.Post("/restore", repo.RestoreBranchPost) m.Post("/restore", repo.RestoreBranchPost)
}, context.RepoMustNotBeArchived(), reqRepoCodeWriter, repo.MustBeNotEmpty) }, context.RepoMustNotBeArchived(), reqRepoCodeWriter, repo.MustBeNotEmpty)
}, reqSignIn, context.RepoAssignment(), context.UnitTypes()) }, reqSignIn, context.RepoAssignment, context.UnitTypes())
// Releases // Releases
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
@ -928,11 +928,11 @@ func RegisterRoutes(m *web.Route) {
} }
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
}) })
}, ignSignIn, context.RepoAssignment(), context.UnitTypes(), reqRepoReleaseReader) }, ignSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoReleaseReader)
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
m.Post("/topics", repo.TopicsPost) m.Post("/topics", repo.TopicsPost)
}, context.RepoAssignment(), context.RepoMustNotBeArchived(), reqRepoAdmin) }, context.RepoAssignment, context.RepoMustNotBeArchived(), reqRepoAdmin)
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
m.Group("", func() { m.Group("", func() {
@ -1080,17 +1080,17 @@ func RegisterRoutes(m *web.Route) {
}, context.RepoRef(), reqRepoCodeReader) }, context.RepoRef(), reqRepoCodeReader)
m.Get("/commit/{sha:([a-f0-9]{7,40})}.{ext:patch|diff}", m.Get("/commit/{sha:([a-f0-9]{7,40})}.{ext:patch|diff}",
repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff) repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff)
}, ignSignIn, context.RepoAssignment(), context.UnitTypes()) }, ignSignIn, context.RepoAssignment, context.UnitTypes())
m.Group("/{username}/{reponame}", func() { m.Group("/{username}/{reponame}", func() {
m.Get("/stars", repo.Stars) m.Get("/stars", repo.Stars)
m.Get("/watchers", repo.Watchers) m.Get("/watchers", repo.Watchers)
m.Get("/search", reqRepoCodeReader, repo.Search) m.Get("/search", reqRepoCodeReader, repo.Search)
}, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) }, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
m.Group("/{username}", func() { m.Group("/{username}", func() {
m.Group("/{reponame}", func() { m.Group("/{reponame}", func() {
m.Get("", repo.SetEditorconfigIfExists, repo.Home) m.Get("", repo.SetEditorconfigIfExists, repo.Home)
}, goGet, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) }, goGet, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
m.Group("/{reponame}", func() { m.Group("/{reponame}", func() {
m.Group("/info/lfs", func() { m.Group("/info/lfs", func() {

Loading…
Cancel
Save