summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-12-04 17:20:23 -0500
committerUnknwon <u@gogs.io>2015-12-04 17:20:23 -0500
commit942fd6be536ced006e028bb191711773b752fe22 (patch)
tree6766a25890d8c937ae6902ab926f5f6882f71606 /modules
parent56dd430a10bf5281caf648344e4660fbdc5d4dee (diff)
downloadgitea-942fd6be536ced006e028bb191711773b752fe22.tar.gz
gitea-942fd6be536ced006e028bb191711773b752fe22.zip
fix panic for #2045
Diffstat (limited to 'modules')
-rw-r--r--modules/middleware/context.go1
-rw-r--r--modules/middleware/repo.go226
2 files changed, 113 insertions, 114 deletions
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index 4f7c438382..7528e2a7a4 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -208,6 +208,7 @@ func Contexter() macaron.Handler {
csrf: x,
Flash: f,
Session: sess,
+ Repo: &RepoContext{},
}
// Compute current URL for real-time change language.
ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/")
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index 78bab4c564..11b74e5591 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -17,118 +17,6 @@ import (
"github.com/gogits/gogs/modules/setting"
)
-// RepoRef handles repository reference name including those contain `/`.
-func RepoRef() macaron.Handler {
- return func(ctx *Context) {
- // Empty repository does not have reference information.
- if ctx.Repo.Repository.IsBare {
- return
- }
-
- var (
- refName string
- err error
- )
-
- // For API calls.
- if ctx.Repo.GitRepo == nil {
- repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
- gitRepo, err := git.OpenRepository(repoPath)
- if err != nil {
- ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
- return
- }
- ctx.Repo.GitRepo = gitRepo
- }
-
- // Get default branch.
- if len(ctx.Params("*")) == 0 {
- refName = ctx.Repo.Repository.DefaultBranch
- if !ctx.Repo.GitRepo.IsBranchExist(refName) {
- brs, err := ctx.Repo.GitRepo.GetBranches()
- if err != nil {
- ctx.Handle(500, "GetBranches", err)
- return
- }
- refName = brs[0]
- }
- ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
- if err != nil {
- ctx.Handle(500, "GetCommitOfBranch", err)
- return
- }
- ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
- ctx.Repo.IsBranch = true
-
- } else {
- hasMatched := false
- parts := strings.Split(ctx.Params("*"), "/")
- for i, part := range parts {
- refName = strings.TrimPrefix(refName+"/"+part, "/")
-
- if ctx.Repo.GitRepo.IsBranchExist(refName) ||
- ctx.Repo.GitRepo.IsTagExist(refName) {
- if i < len(parts)-1 {
- ctx.Repo.TreeName = strings.Join(parts[i+1:], "/")
- }
- hasMatched = true
- break
- }
- }
- if !hasMatched && len(parts[0]) == 40 {
- refName = parts[0]
- ctx.Repo.TreeName = strings.Join(parts[1:], "/")
- }
-
- if ctx.Repo.GitRepo.IsBranchExist(refName) {
- ctx.Repo.IsBranch = true
-
- ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
- if err != nil {
- ctx.Handle(500, "GetCommitOfBranch", err)
- return
- }
- ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
-
- } else if ctx.Repo.GitRepo.IsTagExist(refName) {
- ctx.Repo.IsTag = true
- ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
- if err != nil {
- ctx.Handle(500, "GetCommitOfTag", err)
- return
- }
- ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
- } else if len(refName) == 40 {
- ctx.Repo.IsCommit = true
- ctx.Repo.CommitID = refName
-
- ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
- if err != nil {
- ctx.Handle(404, "GetCommit", nil)
- return
- }
- } else {
- ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
- return
- }
- }
-
- ctx.Repo.BranchName = refName
- ctx.Data["BranchName"] = ctx.Repo.BranchName
- ctx.Data["CommitID"] = ctx.Repo.CommitID
- ctx.Data["IsBranch"] = ctx.Repo.IsBranch
- ctx.Data["IsTag"] = ctx.Repo.IsTag
- ctx.Data["IsCommit"] = ctx.Repo.IsCommit
-
- ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
- if err != nil {
- ctx.Handle(500, "CommitsCount", err)
- return
- }
- ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
- }
-}
-
func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
// Non-fork repository will not return error in this method.
if err := repo.GetBaseRepo(); err != nil {
@@ -166,8 +54,6 @@ func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
func RepoAssignment(args ...bool) macaron.Handler {
return func(ctx *Context) {
- ctx.Repo = &RepoContext{}
-
var (
displayBare bool // To display bare page if it is a bare repo.
)
@@ -334,6 +220,118 @@ func RepoAssignment(args ...bool) macaron.Handler {
}
}
+// RepoRef handles repository reference name including those contain `/`.
+func RepoRef() macaron.Handler {
+ return func(ctx *Context) {
+ // Empty repository does not have reference information.
+ if ctx.Repo.Repository.IsBare {
+ return
+ }
+
+ var (
+ refName string
+ err error
+ )
+
+ // For API calls.
+ if ctx.Repo.GitRepo == nil {
+ repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
+ gitRepo, err := git.OpenRepository(repoPath)
+ if err != nil {
+ ctx.Handle(500, "RepoRef Invalid repo "+repoPath, err)
+ return
+ }
+ ctx.Repo.GitRepo = gitRepo
+ }
+
+ // Get default branch.
+ if len(ctx.Params("*")) == 0 {
+ refName = ctx.Repo.Repository.DefaultBranch
+ if !ctx.Repo.GitRepo.IsBranchExist(refName) {
+ brs, err := ctx.Repo.GitRepo.GetBranches()
+ if err != nil {
+ ctx.Handle(500, "GetBranches", err)
+ return
+ }
+ refName = brs[0]
+ }
+ ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
+ if err != nil {
+ ctx.Handle(500, "GetCommitOfBranch", err)
+ return
+ }
+ ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
+ ctx.Repo.IsBranch = true
+
+ } else {
+ hasMatched := false
+ parts := strings.Split(ctx.Params("*"), "/")
+ for i, part := range parts {
+ refName = strings.TrimPrefix(refName+"/"+part, "/")
+
+ if ctx.Repo.GitRepo.IsBranchExist(refName) ||
+ ctx.Repo.GitRepo.IsTagExist(refName) {
+ if i < len(parts)-1 {
+ ctx.Repo.TreeName = strings.Join(parts[i+1:], "/")
+ }
+ hasMatched = true
+ break
+ }
+ }
+ if !hasMatched && len(parts[0]) == 40 {
+ refName = parts[0]
+ ctx.Repo.TreeName = strings.Join(parts[1:], "/")
+ }
+
+ if ctx.Repo.GitRepo.IsBranchExist(refName) {
+ ctx.Repo.IsBranch = true
+
+ ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfBranch(refName)
+ if err != nil {
+ ctx.Handle(500, "GetCommitOfBranch", err)
+ return
+ }
+ ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
+
+ } else if ctx.Repo.GitRepo.IsTagExist(refName) {
+ ctx.Repo.IsTag = true
+ ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommitOfTag(refName)
+ if err != nil {
+ ctx.Handle(500, "GetCommitOfTag", err)
+ return
+ }
+ ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
+ } else if len(refName) == 40 {
+ ctx.Repo.IsCommit = true
+ ctx.Repo.CommitID = refName
+
+ ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
+ if err != nil {
+ ctx.Handle(404, "GetCommit", nil)
+ return
+ }
+ } else {
+ ctx.Handle(404, "RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
+ return
+ }
+ }
+
+ ctx.Repo.BranchName = refName
+ ctx.Data["BranchName"] = ctx.Repo.BranchName
+ ctx.Data["CommitID"] = ctx.Repo.CommitID
+ ctx.Data["IsBranch"] = ctx.Repo.IsBranch
+ ctx.Data["IsTag"] = ctx.Repo.IsTag
+ ctx.Data["IsCommit"] = ctx.Repo.IsCommit
+
+ ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
+ if err != nil {
+ ctx.Handle(500, "CommitsCount", err)
+ return
+ }
+ ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
+ }
+}
+
func RequireRepoAdmin() macaron.Handler {
return func(ctx *Context) {
if !ctx.Repo.IsAdmin() {