From 4a46613916cdfa6a168746aba6abcd698cd17875 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 15 Aug 2016 15:27:19 -0700 Subject: [PATCH] markdown: fix treating pure number as SHA1 - Detect non-exist commit and return 404 not 500 --- .gopmfile | 2 +- cmd/web.go | 2 +- glide.lock | 2 +- modules/markdown/markdown.go | 5 +++++ routers/repo/commit.go | 6 +++++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.gopmfile b/.gopmfile index 7c2420931e..e5e077459f 100644 --- a/.gopmfile +++ b/.gopmfile @@ -18,7 +18,7 @@ github.com/go-xorm/core = commit:5bf745d github.com/go-xorm/xorm = commit:c6c7056 github.com/gogits/chardet = commit:2404f77 github.com/gogits/cron = commit:7f3990a -github.com/gogits/git-module = commit:31d8d73 +github.com/gogits/git-module = commit:2a820b5 github.com/gogits/go-gogs-client = commit:e363d3f github.com/issue9/identicon = commit:d36b545 github.com/jaytaylor/html2text = commit:52d9b78 diff --git a/cmd/web.go b/cmd/web.go index 3b43a71cc2..cdf42657c8 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -88,7 +88,7 @@ func checkVersion() { {"github.com/go-macaron/toolbox", toolbox.Version, "0.1.0"}, {"gopkg.in/ini.v1", ini.Version, "1.8.4"}, {"gopkg.in/macaron.v1", macaron.Version, "1.1.7"}, - {"github.com/gogits/git-module", git.Version, "0.3.5"}, + {"github.com/gogits/git-module", git.Version, "0.3.6"}, {"github.com/gogits/go-gogs-client", gogs.Version, "0.12.0"}, } for _, c := range checkers { diff --git a/glide.lock b/glide.lock index 9d720077f5..a06d9722de 100644 --- a/glide.lock +++ b/glide.lock @@ -41,7 +41,7 @@ imports: - name: github.com/gogits/cron version: 7f3990acf1833faa5ebd0e86f0a4c72a4b5eba3c - name: github.com/gogits/git-module - version: 31d8d73910d6f9ccb94ba6c17bbd18b1d01492ee + version: 2a820b5471795de4c8b993e15b0ed08155090c6a - name: github.com/gogits/go-gogs-client version: e363d3ff8f70d0fe813324eedf228684af41c29c - name: github.com/issue9/identicon diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index a6cdf222e5..7bfb0ec60a 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -91,6 +91,8 @@ var ( IssueAlphanumericPattern = regexp.MustCompile(`( |^|\()[A-Z]{1,10}-[1-9][0-9]*\b`) // Sha1CurrentPattern matches string that represents a commit SHA, e.g. d8a994ef243349f321568f9e36d5c3f444b99cae + // FIXME: this pattern matches pure numbers as well, right now we do a hack to check in RenderSha1CurrentPattern + // by converting string to a number. Sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{7,40}\b`) ) @@ -262,6 +264,9 @@ func RenderIssueIndexPattern(rawBytes []byte, urlPrefix string, metas map[string // RenderSha1CurrentPattern renders SHA1 strings to corresponding links that assumes in the same repository. func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte { return []byte(Sha1CurrentPattern.ReplaceAllStringFunc(string(rawBytes[:]), func(m string) string { + if com.StrTo(m).MustInt() > 0 { + return m + } return fmt.Sprintf(`%s`, urlPrefix, m, base.ShortSha(string(m))) })) } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index c102ceae41..0dc0ceba55 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -152,7 +152,11 @@ func Diff(ctx *context.Context) { commit, err := ctx.Repo.GitRepo.GetCommit(commitID) if err != nil { - ctx.Handle(500, "Repo.GitRepo.GetCommit", err) + if git.IsErrNotExist(err) { + ctx.Handle(404, "Repo.GitRepo.GetCommit", err) + } else { + ctx.Handle(500, "Repo.GitRepo.GetCommit", err) + } return }