From 79cfbe832062d1584a6e6283bd90be7ffb8da8b5 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Tue, 3 Jan 2017 18:26:00 +0800 Subject: [PATCH 1/8] Preliminary implementation --- routers/repo/view.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index b92380d897a5a..f25c245cea95e 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -12,6 +12,9 @@ import ( "path" "strings" + "encoding/base64" + "strconv" + "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/base" @@ -22,9 +25,7 @@ import ( "code.gitea.io/gitea/modules/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" - "encoding/base64" "github.com/Unknwon/paginater" - "strconv" ) const ( @@ -40,12 +41,34 @@ func renderDirectory(ctx *context.Context, treeLink string) { return } + query := ctx.Req.URL.Query() + + var start, count int = 0, 100 + + if _start, ok := query["start"]; ok && len(_start) == 1 { + start, err = strconv.Atoi(_start[0]) + if err != nil { + ctx.Handle(500, "QueryString", err) + return + } + } + + if _count, ok := query["count"]; ok && len(_count) == 1 { + count, err = strconv.Atoi(_count[0]) + if err != nil { + ctx.Handle(500, "QueryString", err) + return + } + } + entries, err := tree.ListEntries() if err != nil { ctx.Handle(500, "ListEntries", err) return } + entries.Sort() + entries = entries[start : start+count] ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath) if err != nil { From 0088b5965d31be6ffe2d4506a59065230ef71553 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Tue, 3 Jan 2017 18:54:28 +0800 Subject: [PATCH 2/8] Do not redundantly sort the tree entries They are pre-sorted by git-mktree. --- routers/repo/view.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index f25c245cea95e..c1a946e65c775 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -67,7 +67,6 @@ func renderDirectory(ctx *context.Context, treeLink string) { return } - entries.Sort() entries = entries[start : start+count] ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath) From 791d3e43bf2b8957e2e08e6d920f531d7cb86bf8 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Tue, 3 Jan 2017 19:46:35 +0800 Subject: [PATCH 3/8] Use ctx.QueryInt("start") and ctx.QueryInt("count") --- routers/repo/view.go | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index c1a946e65c775..515c84d7fbb0c 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -41,32 +41,14 @@ func renderDirectory(ctx *context.Context, treeLink string) { return } - query := ctx.Req.URL.Query() - - var start, count int = 0, 100 - - if _start, ok := query["start"]; ok && len(_start) == 1 { - start, err = strconv.Atoi(_start[0]) - if err != nil { - ctx.Handle(500, "QueryString", err) - return - } - } - - if _count, ok := query["count"]; ok && len(_count) == 1 { - count, err = strconv.Atoi(_count[0]) - if err != nil { - ctx.Handle(500, "QueryString", err) - return - } - } - entries, err := tree.ListEntries() if err != nil { ctx.Handle(500, "ListEntries", err) return } + start := ctx.QueryInt("start") + count := ctx.QueryInt("count") entries = entries[start : start+count] ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath) From 1fe0d48ca2547249f4cc96835f5b97d4e7671abd Mon Sep 17 00:00:00 2001 From: Mura Li Date: Fri, 6 Jan 2017 09:38:46 +0800 Subject: [PATCH 4/8] Check invalid query values --- routers/repo/view.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/routers/repo/view.go b/routers/repo/view.go index 515c84d7fbb0c..c37b77a29a33d 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -49,6 +49,12 @@ func renderDirectory(ctx *context.Context, treeLink string) { start := ctx.QueryInt("start") count := ctx.QueryInt("count") + + // Check invalid values + if start < 0 || count < 0 || start+count > len(entries) { + start = 0 + count = len(entries) + } entries = entries[start : start+count] ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath) From 702565ebddd71a95fcc8289c2805d2d322c598dc Mon Sep 17 00:00:00 2001 From: Mura Li Date: Fri, 6 Jan 2017 10:05:38 +0800 Subject: [PATCH 5/8] Should check if the values are not provided (start=0 count=0) --- routers/repo/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index c37b77a29a33d..9c9ed885f00f9 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -51,7 +51,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { count := ctx.QueryInt("count") // Check invalid values - if start < 0 || count < 0 || start+count > len(entries) { + if (start == 0 && count == 0) || start+count < start || start+count > len(entries) { start = 0 count = len(entries) } From 0c6d9ce39252c8ccde542a430ccf0f53be917c38 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Sat, 7 Jan 2017 10:25:12 +0800 Subject: [PATCH 6/8] Revert the change of removing entries sorting The sorting done by git-mktree is alphabetical but it doesn't take directories into consideration. --- routers/repo/view.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/repo/view.go b/routers/repo/view.go index 9c9ed885f00f9..4e752240c4780 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -47,6 +47,8 @@ func renderDirectory(ctx *context.Context, treeLink string) { return } + entries.Sort() + start := ctx.QueryInt("start") count := ctx.QueryInt("count") From f155fc1eef075824e3b9efe37c1316f9d005f8d9 Mon Sep 17 00:00:00 2001 From: Mura Li Date: Sat, 7 Jan 2017 10:58:34 +0800 Subject: [PATCH 7/8] Remove redundant empty lines --- routers/repo/view.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index 4e752240c4780..b7982197b445d 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -6,14 +6,13 @@ package repo import ( "bytes" + "encoding/base64" "fmt" gotemplate "html/template" "io/ioutil" "path" - "strings" - - "encoding/base64" "strconv" + "strings" "code.gitea.io/git" "code.gitea.io/gitea/models" From 7594d2436f516a1b1f2a3b6ad3ace741fa38966d Mon Sep 17 00:00:00 2001 From: Mura Li Date: Fri, 13 Jan 2017 21:53:03 +0800 Subject: [PATCH 8/8] Reduce the condition formula of input validation --- routers/repo/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index b7982197b445d..9ee4741ce9ff5 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -52,7 +52,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { count := ctx.QueryInt("count") // Check invalid values - if (start == 0 && count == 0) || start+count < start || start+count > len(entries) { + if (start == 0 && count == 0) || count < 0 || start+count > len(entries) { start = 0 count = len(entries) }