From 27a9e18e4809a31f70456056f34c6a0440a3e84c Mon Sep 17 00:00:00 2001 From: slene Date: Sat, 15 Mar 2014 21:58:32 +0800 Subject: [PATCH 1/2] clean repo.go --- routers/repo/repo.go | 65 ++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index f920a70c1..710e5a21d 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -5,29 +5,23 @@ package repo import ( - "net/http" - - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/middleware" ) -func Create(form auth.CreateRepoForm, ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { - data["Title"] = "Create repository" +func Create(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = "Create repository" - if req.Method == "GET" { - data["LanguageIgns"] = models.LanguageIgns - data["Licenses"] = models.Licenses - r.HTML(200, "repo/create", data) + if ctx.Req.Method == "GET" { + ctx.Data["LanguageIgns"] = models.LanguageIgns + ctx.Data["Licenses"] = models.Licenses + ctx.Render.HTML(200, "repo/create", ctx.Data) return } - if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { - r.HTML(200, "repo/create", data) + if hasErr, ok := ctx.Data["HasError"]; ok && hasErr.(bool) { + ctx.Render.HTML(200, "repo/create", ctx.Data) return } @@ -36,10 +30,10 @@ func Create(form auth.CreateRepoForm, ctx *middleware.Context, req *http.Request user, err := models.GetUserById(form.UserId) if err != nil { if err.Error() == models.ErrUserNotExist.Error() { - data["HasError"] = true - data["ErrorMsg"] = "User does not exist" - auth.AssignForm(form, data) - r.HTML(200, "repo/create", data) + ctx.Data["HasError"] = true + ctx.Data["ErrorMsg"] = "User does not exist" + auth.AssignForm(form, ctx.Data) + ctx.Render.HTML(200, "repo/create", ctx.Data) return } } @@ -48,27 +42,27 @@ func Create(form auth.CreateRepoForm, ctx *middleware.Context, req *http.Request if _, err = models.CreateRepository(user, form.RepoName, form.Description, form.Language, form.License, form.Visibility == "private", form.InitReadme == "on"); err == nil { - r.Redirect("/"+user.Name+"/"+form.RepoName, 302) + ctx.Render.Redirect("/"+user.Name+"/"+form.RepoName, 302) return } } if err.Error() == models.ErrRepoAlreadyExist.Error() { - data["HasError"] = true - data["ErrorMsg"] = "Repository name has already been used" - auth.AssignForm(form, data) - r.HTML(200, "repo/create", data) + ctx.Data["HasError"] = true + ctx.Data["ErrorMsg"] = "Repository name has already been used" + auth.AssignForm(form, ctx.Data) + ctx.Render.HTML(200, "repo/create", ctx.Data) return } ctx.Handle(200, "repo.Create", err) } -func Delete(form auth.DeleteRepoForm, ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { - data["Title"] = "Delete repository" +func Delete(ctx *middleware.Context, form auth.DeleteRepoForm) { + ctx.Data["Title"] = "Delete repository" - if req.Method == "GET" { - r.HTML(200, "repo/delete", data) + if ctx.Req.Method == "GET" { + ctx.Render.HTML(200, "repo/delete", ctx.Data) return } @@ -77,23 +71,22 @@ func Delete(form auth.DeleteRepoForm, ctx *middleware.Context, req *http.Request return } - r.Redirect("/", 302) + ctx.Render.Redirect("/", 302) } -func List(ctx *middleware.Context, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { - u := auth.SignedInUser(session) - if u != nil { - r.Redirect("/") +func List(ctx *middleware.Context) { + if ctx.User != nil { + ctx.Render.Redirect("/") return } - data["Title"] = "Repositories" - repos, err := models.GetRepositories(u) + ctx.Data["Title"] = "Repositories" + repos, err := models.GetRepositories(ctx.User) if err != nil { ctx.Handle(200, "repo.List", err) return } - data["Repos"] = repos - r.HTML(200, "repo/list", data) + ctx.Data["Repos"] = repos + ctx.Render.HTML(200, "repo/list", ctx.Data) } From 218cafed1d2ff1ef04174d433a73cd210925902c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 15 Mar 2014 22:27:24 +0800 Subject: [PATCH 2/2] show folder first on file list page --- models/repo2.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/models/repo2.go b/models/repo2.go index 6aa6eda60..beeb8021e 100644 --- a/models/repo2.go +++ b/models/repo2.go @@ -8,7 +8,7 @@ import ( "path" "time" - git "github.com/speedata/gogit" + git "github.com/gogits/git" ) type RepoFile struct { @@ -46,20 +46,33 @@ func GetReposFiles(userName, reposName, branchName, rpath string) ([]*RepoFile, return nil, err } + var repodirs []*RepoFile var repofiles []*RepoFile lastCommit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int { if dirname == rpath { - repofiles = append(repofiles, &RepoFile{ - entry.Id, - entry.Filemode, - entry.Name, - path.Join(dirname, entry.Name), - lastCommit.Message(), - lastCommit.Committer.When, - }) + switch entry.Filemode { + case git.FileModeBlob, git.FileModeBlobExec: + repofiles = append(repofiles, &RepoFile{ + entry.Id, + entry.Filemode, + entry.Name, + path.Join(dirname, entry.Name), + lastCommit.Message(), + lastCommit.Committer.When, + }) + case git.FileModeTree: + repodirs = append(repodirs, &RepoFile{ + entry.Id, + entry.Filemode, + entry.Name, + path.Join(dirname, entry.Name), + lastCommit.Message(), + lastCommit.Committer.When, + }) + } } return 0 }) - return repofiles, nil + return append(repodirs, repofiles...), nil }