diff --git a/cmd/serve.go b/cmd/serve.go index 5eac51f5e..f7b0d2045 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -5,7 +5,6 @@ package cmd import ( - //"container/list" "fmt" "os" "os/exec" @@ -16,24 +15,10 @@ import ( "github.com/codegangsta/cli" qlog "github.com/qiniu/log" - //"github.com/gogits/git" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" ) -var ( - COMMANDS_READONLY = map[string]int{ - "git-upload-pack": models.AU_WRITABLE, - "git upload-pack": models.AU_WRITABLE, - "git-upload-archive": models.AU_WRITABLE, - } - - COMMANDS_WRITE = map[string]int{ - "git-receive-pack": models.AU_READABLE, - "git receive-pack": models.AU_READABLE, - } -) - var CmdServ = cli.Command{ Name: "serv", Usage: "This command should only be called by SSH shell", @@ -42,8 +27,7 @@ var CmdServ = cli.Command{ Flags: []cli.Flag{}, } -func newLogger(execDir string) { - logPath := execDir + "/log/serv.log" +func newLogger(logPath string) { os.MkdirAll(path.Dir(logPath), os.ModePerm) f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) @@ -56,6 +40,20 @@ func newLogger(execDir string) { qlog.Info("Start logging serv...") } +func setup(logPath string) { + execDir, _ := base.ExecDir() + newLogger(path.Join(execDir, logPath)) + + base.NewConfigContext() + models.LoadModelsConfig() + + if models.UseSQLite3 { + os.Chdir(execDir) + } + + models.SetEngine() +} + func parseCmd(cmd string) (string, string) { ss := strings.SplitN(cmd, " ", 2) if len(ss) != 2 { @@ -71,39 +69,46 @@ func parseCmd(cmd string) (string, string) { return verb, strings.Replace(args, "'/", "'", 1) } +var ( + COMMANDS_READONLY = map[string]int{ + "git-upload-pack": models.AU_WRITABLE, + "git upload-pack": models.AU_WRITABLE, + "git-upload-archive": models.AU_WRITABLE, + } + + COMMANDS_WRITE = map[string]int{ + "git-receive-pack": models.AU_READABLE, + "git receive-pack": models.AU_READABLE, + } +) + func In(b string, sl map[string]int) bool { _, e := sl[b] return e } func runServ(k *cli.Context) { - execDir, _ := base.ExecDir() - newLogger(execDir) - - base.NewConfigContext() - models.LoadModelsConfig() - - if models.UseSQLite3 { - os.Chdir(execDir) - } - - models.SetEngine() + setup("log/serv.log") keys := strings.Split(os.Args[2], "-") if len(keys) != 2 { - println("auth file format error") - qlog.Fatal("auth file format error") + println("Gogs: auth file format error") + qlog.Fatal("Invalid auth file format: %s", os.Args[2]) } keyId, err := strconv.ParseInt(keys[1], 10, 64) if err != nil { - println("auth file format error") - qlog.Fatal("auth file format error", err) + println("Gogs: auth file format error") + qlog.Fatalf("Invalid auth file format: %v", err) } user, err := models.GetUserByKeyId(keyId) if err != nil { - println("You have no right to access") - qlog.Fatalf("SSH visit error: %v", err) + if err == models.ErrUserNotKeyOwner { + println("Gogs: you are not the owner of SSH key") + qlog.Fatalf("Invalid owner of SSH key: %d", keyId) + } + println("Gogs: internal error:", err) + qlog.Fatalf("Fail to get user by key ID(%d): %v", keyId, err) } cmd := os.Getenv("SSH_ORIGINAL_COMMAND") @@ -116,8 +121,8 @@ func runServ(k *cli.Context) { repoPath := strings.Trim(args, "'") rr := strings.SplitN(repoPath, "/", 2) if len(rr) != 2 { - println("Unavailable repository", args) - qlog.Fatalf("Unavailable repository %v", args) + println("Gogs: unavailable repository", args) + qlog.Fatalf("Unavailable repository: %v", args) } repoUserName := rr[0] repoName := strings.TrimSuffix(rr[1], ".git") @@ -127,17 +132,21 @@ func runServ(k *cli.Context) { repoUser, err := models.GetUserByName(repoUserName) if err != nil { - println("You have no right to access") - qlog.Fatalf("Get user failed: %v", err) + if err == models.ErrUserNotExist { + println("Gogs: given repository owner are not registered") + qlog.Fatalf("Unregistered owner: %s", repoUserName) + } + println("Gogs: internal error:", err) + qlog.Fatalf("Fail to get repository owner(%s): %v", repoUserName, err) } - // access check + // Access check. switch { case isWrite: - has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE) + has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) if err != nil { - println("Internal error:", err) - qlog.Fatal(err) + println("Gogs: internal error:", err) + qlog.Fatal("Fail to check write access:", err) } else if !has { println("You have no right to write this repository") qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath) @@ -145,8 +154,12 @@ func runServ(k *cli.Context) { case isRead: repo, err := models.GetRepositoryByName(repoUser.Id, repoName) if err != nil { - println("Get repository error:", err) - qlog.Fatal("Get repository error: " + err.Error()) + if err == models.ErrRepoNotExist { + println("Gogs: given repository does not exist") + qlog.Fatalf("Repository does not exist: %s/%s", repoUser.Name, repoName) + } + println("Gogs: internal error:", err) + qlog.Fatalf("Fail to get repository: %v", err) } if !repo.IsPrivate { @@ -155,23 +168,15 @@ func runServ(k *cli.Context) { has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE) if err != nil { - println("Internal error") - qlog.Fatal(err) - } - if !has { - has, err = models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE) - if err != nil { - println("Internal error") - qlog.Fatal(err) - } - } - if !has { + println("Gogs: internal error:", err) + qlog.Fatal("Fail to check read access:", err) + } else if !has { println("You have no right to access this repository") - qlog.Fatal("You have no right to access this repository") + qlog.Fatalf("User %s has no right to read repository %s", user.Name, repoPath) } default: println("Unknown command") - qlog.Fatal("Unknown command") + return } models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName) @@ -183,8 +188,8 @@ func runServ(k *cli.Context) { gitcmd.Stderr = os.Stderr if err = gitcmd.Run(); err != nil { - println("execute command error:", err.Error()) - qlog.Fatal("execute command error: " + err.Error()) + println("Gogs: internal error:", err) + qlog.Fatalf("Fail to execute git command: %v", err) } //refName := os.Getenv("refName") diff --git a/cmd/update.go b/cmd/update.go index 1ab08ca19..cae492cfb 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -6,14 +6,12 @@ package cmd import ( "os" - "path" "strconv" "github.com/codegangsta/cli" qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" ) var CmdUpdate = cli.Command{ @@ -24,51 +22,25 @@ var CmdUpdate = cli.Command{ Flags: []cli.Flag{}, } -func newUpdateLogger(execDir string) { - logPath := execDir + "/log/update.log" - os.MkdirAll(path.Dir(logPath), os.ModePerm) - - f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) - if err != nil { - qlog.Fatal(err) - } - - qlog.SetOutput(f) - qlog.Info("Start logging update...") -} - func updateEnv(refName, oldCommitId, newCommitId string) { os.Setenv("refName", refName) os.Setenv("oldCommitId", oldCommitId) os.Setenv("newCommitId", newCommitId) - qlog.Error("set envs:", refName, oldCommitId, newCommitId) + qlog.Info("set envs:", refName, oldCommitId, newCommitId) } -// for command: ./gogs update func runUpdate(c *cli.Context) { cmd := os.Getenv("SSH_ORIGINAL_COMMAND") if cmd == "" { return } - execDir, _ := base.ExecDir() - newUpdateLogger(execDir) - - base.NewConfigContext() - models.LoadModelsConfig() - - if models.UseSQLite3 { - os.Chdir(execDir) - } - - models.SetEngine() + setup("log/update.log") args := c.Args() if len(args) != 3 { qlog.Fatal("received less 3 parameters") - } - - if args[0] == "" { + } else if args[0] == "" { qlog.Fatal("refName is empty, shouldn't use") } diff --git a/cmd/web.go b/cmd/web.go index 3335c5396..60bef9d03 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -78,16 +78,18 @@ func runWeb(*cli.Context) { m.Get("/stars", reqSignIn, user.Stars) m.Get("/help", routers.Help) - m.Group("/api/v1", func(r martini.Router) { - // Miscellaneous. - r.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown) - r.Post("/markdown/raw", v1.MarkdownRaw) + m.Group("/api", func(r martini.Router) { + m.Group("/v1", func(r martini.Router) { + // Miscellaneous. + r.Post("/markdown", bindIgnErr(apiv1.MarkdownForm{}), v1.Markdown) + r.Post("/markdown/raw", v1.MarkdownRaw) - // Users. - r.Get("/users/search", v1.SearchUser) + // Users. + r.Get("/users/search", v1.SearchUser) - r.Any("**", func(ctx *middleware.Context) { - ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL}) + r.Any("**", func(ctx *middleware.Context) { + ctx.JSON(404, &base.ApiJsonErr{"Not Found", v1.DOC_URL}) + }) }) }) @@ -170,29 +172,36 @@ func runWeb(*cli.Context) { m.Group("/:username/:reponame", func(r martini.Router) { r.Get("/settings", repo.Setting) r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost) - r.Get("/settings/collaboration", repo.Collaboration) - r.Post("/settings/collaboration", repo.CollaborationPost) - r.Get("/settings/hooks", repo.WebHooks) - r.Get("/settings/hooks/add", repo.WebHooksAdd) - r.Post("/settings/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost) - r.Get("/settings/hooks/:id", repo.WebHooksEdit) - r.Post("/settings/hooks/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + + m.Group("/settings", func(r martini.Router) { + r.Get("/collaboration", repo.Collaboration) + r.Post("/collaboration", repo.CollaborationPost) + r.Get("/hooks", repo.WebHooks) + r.Get("/hooks/add", repo.WebHooksAdd) + r.Post("/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost) + r.Get("/hooks/:id", repo.WebHooksEdit) + r.Post("/hooks/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + }) }, reqSignIn, middleware.RepoAssignment(true), reqOwner) m.Group("/:username/:reponame", func(r martini.Router) { r.Get("/action/:action", repo.Action) - r.Get("/issues/new", repo.CreateIssue) - r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost) - r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) - r.Post("/issues/:index/assignee", repo.UpdateAssignee) - r.Post("/issues/:index/milestone", repo.UpdateIssueMilestone) - r.Post("/issues/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel) - r.Get("/issues/milestones", repo.Milestones) - r.Get("/issues/milestones/new", repo.NewMilestone) - r.Post("/issues/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost) - r.Get("/issues/milestones/:index/edit", repo.UpdateMilestone) - r.Post("/issues/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost) - r.Get("/issues/milestones/:index/:action", repo.UpdateMilestone) + + m.Group("/issues", func(r martini.Router) { + r.Get("/new", repo.CreateIssue) + r.Post("/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost) + r.Post("/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) + r.Post("/:index/assignee", repo.UpdateAssignee) + r.Post("/:index/milestone", repo.UpdateIssueMilestone) + r.Post("/labels/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel) + r.Get("/milestones", repo.Milestones) + r.Get("/milestones/new", repo.NewMilestone) + r.Post("/milestones/new", bindIgnErr(auth.CreateMilestoneForm{}), repo.NewMilestonePost) + r.Get("/milestones/:index/edit", repo.UpdateMilestone) + r.Post("/milestones/:index/edit", bindIgnErr(auth.CreateMilestoneForm{}), repo.UpdateMilestonePost) + r.Get("/milestones/:index/:action", repo.UpdateMilestone) + }) + r.Post("/comment/:action", repo.Comment) r.Get("/releases/new", repo.ReleasesNew) }, reqSignIn, middleware.RepoAssignment(true)) diff --git a/gogs.go b/gogs.go index ca00fb508..16fd5ec76 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/base" ) -const APP_VER = "0.3.5.0518 Alpha" +const APP_VER = "0.3.5.0521 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/user.go b/models/user.go index dd0498501..6f1400277 100644 --- a/models/user.go +++ b/models/user.go @@ -30,6 +30,7 @@ var ( ErrUserOwnRepos = errors.New("User still have ownership of repositories") ErrUserAlreadyExist = errors.New("User already exist") ErrUserNotExist = errors.New("User does not exist") + ErrUserNotKeyOwner = errors.New("User does not the owner of public key") ErrEmailAlreadyUsed = errors.New("E-mail already used") ErrUserNameIllegal = errors.New("User name contains illegal characters") ErrLoginSourceNotExist = errors.New("Login source does not exist") @@ -349,8 +350,7 @@ func GetUserByKeyId(keyId int64) (*User, error) { if err != nil { return nil, err } else if !has { - err = errors.New("not exist key owner") - return nil, err + return nil, ErrUserNotKeyOwner } return user, nil } diff --git a/modules/base/template.go b/modules/base/template.go index b9449e30a..5aa8ac5cc 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "html/template" + "runtime" "strings" "time" ) @@ -47,6 +48,9 @@ var mailDomains = map[string]string{ } var TemplateFuncs template.FuncMap = map[string]interface{}{ + "GoVer": func() string { + return runtime.Version() + }, "AppName": func() string { return AppName }, diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl index b3e3368b4..aed5f488d 100644 --- a/templates/base/footer.tmpl +++ b/templates/base/footer.tmpl @@ -15,9 +15,17 @@
-

Official Website

+

+ Official Website +

+ +
+
+ Go Version: {{GoVer}} +
+