diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/template.go | 3 | ||||
-rw-r--r-- | modules/log/log.go | 1 | ||||
-rw-r--r-- | modules/middleware/context.go | 42 | ||||
-rw-r--r-- | modules/middleware/repo.go | 15 |
4 files changed, 55 insertions, 6 deletions
diff --git a/modules/base/template.go b/modules/base/template.go index 5a42107c..62414979 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -67,6 +67,9 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "TimeSince": TimeSince, "FileSize": FileSize, "Subtract": Subtract, + "Add": func(a, b int) int { + return a + b + }, "ActionIcon": ActionIcon, "ActionDesc": ActionDesc, "DateFormat": DateFormat, diff --git a/modules/log/log.go b/modules/log/log.go index f21897b9..636ea787 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -21,6 +21,7 @@ func init() { func NewLogger(bufLen int64, mode, config string) { Mode, Config = mode, config logger = logs.NewLogger(bufLen) + logger.SetLogFuncCallDepth(3) logger.SetLogger(mode, config) } diff --git a/modules/middleware/context.go b/modules/middleware/context.go index 8129b13b..e7f962c3 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -11,6 +11,7 @@ import ( "fmt" "html/template" "net/http" + "net/url" "strconv" "strings" "time" @@ -34,6 +35,7 @@ type Context struct { p martini.Params Req *http.Request Res http.ResponseWriter + Flash *Flash Session session.SessionStore Cache cache.Cache User *models.User @@ -78,6 +80,7 @@ func (ctx *Context) HasError() bool { if !ok { return false } + ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) return hasErr.(bool) } @@ -88,11 +91,11 @@ func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) { // RenderWithErr used for page has form validation but need to prompt error to users. func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { - ctx.Data["HasError"] = true - ctx.Data["ErrorMsg"] = msg if form != nil { auth.AssignForm(form, ctx.Data) } + ctx.Flash.ErrorMsg = msg + ctx.Data["Flash"] = ctx.Flash ctx.HTML(200, tpl) } @@ -100,7 +103,7 @@ func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { func (ctx *Context) Handle(status int, title string, err error) { log.Error("%s: %v", title, err) if martini.Dev == martini.Prod { - ctx.HTML(500, "status/500") + ctx.HTML(200, "status/500") return } @@ -239,6 +242,21 @@ func (ctx *Context) CsrfTokenValid() bool { return true } +type Flash struct { + url.Values + ErrorMsg, SuccessMsg string +} + +func (f *Flash) Error(msg string) { + f.Set("error", msg) + f.ErrorMsg = msg +} + +func (f *Flash) Success(msg string) { + f.Set("success", msg) + f.SuccessMsg = msg +} + // InitContext initializes a classic context for a request. func InitContext() martini.Handler { return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) { @@ -256,9 +274,27 @@ func InitContext() martini.Handler { // start session ctx.Session = base.SessionManager.SessionStart(res, r) + + // Get flash. + values, err := url.ParseQuery(ctx.GetCookie("gogs_flash")) + if err != nil { + log.Error("InitContext.ParseQuery(flash): %v", err) + } else if len(values) > 0 { + ctx.Flash = &Flash{Values: values} + ctx.Flash.ErrorMsg = ctx.Flash.Get("error") + ctx.Flash.SuccessMsg = ctx.Flash.Get("success") + ctx.Data["Flash"] = ctx.Flash + ctx.SetCookie("gogs_flash", "", -1) + } + ctx.Flash = &Flash{Values: url.Values{}} + rw := res.(martini.ResponseWriter) rw.Before(func(martini.ResponseWriter) { ctx.Session.SessionRelease(res) + + if flash := ctx.Flash.Encode(); len(flash) > 0 { + ctx.SetCookie("gogs_flash", ctx.Flash.Encode(), 0) + } }) // Get user from session if logined. diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 2139742c..258fc9d2 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -15,6 +15,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" ) func RepoAssignment(redirect bool, args ...bool) martini.Handler { @@ -76,7 +77,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Redirect("/") return } - ctx.Handle(404, "RepoAssignment", err) + ctx.Handle(500, "RepoAssignment", err) return } repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues @@ -86,7 +87,7 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) if err != nil { - ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) + ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) return } ctx.Repo.GitRepo = gitRepo @@ -138,7 +139,10 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { } } else { - branchName = "master" + branchName = ctx.Repo.Repository.DefaultBranch + if len(branchName) == 0 { + branchName = "master" + } goto detect } @@ -157,6 +161,11 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { } ctx.Data["BranchName"] = ctx.Repo.BranchName + brs, err := models.GetBranches(user.Name, repoName) + if err != nil { + log.Error("RepoAssignment(GetBranches): %v", err) + } + ctx.Data["Branches"] = brs ctx.Data["CommitId"] = ctx.Repo.CommitId ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching } |