aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorMeaglith Ma <genedna@gmail.com>2014-03-31 17:23:37 +0800
committerMeaglith Ma <genedna@gmail.com>2014-03-31 17:23:37 +0800
commit1a247340dbea3404431f60a24bb8f8d06d94b1e9 (patch)
tree80d3ecab43506e03405c742a84dcc61f474e2212 /modules
parent9047cadcd33f95eebafa2f794b895c8406eb80c5 (diff)
parentdd9fb807a46db120ef800d7465f50a73a86df288 (diff)
Merge pull request #1 from gogits/master
Sync to lastest
Diffstat (limited to 'modules')
-rw-r--r--modules/auth/admin.go2
-rw-r--r--modules/auth/auth.go53
-rw-r--r--modules/auth/issue.go2
-rw-r--r--modules/auth/repo.go2
-rw-r--r--modules/auth/setting.go2
-rw-r--r--modules/auth/user.go9
-rw-r--r--modules/base/conf.go26
-rw-r--r--modules/base/template.go8
-rw-r--r--modules/base/tool.go17
-rw-r--r--modules/log/log.go5
-rw-r--r--modules/middleware/auth.go7
-rw-r--r--modules/middleware/context.go18
-rw-r--r--modules/middleware/logger.go2
-rw-r--r--modules/middleware/render.go2
-rw-r--r--modules/middleware/repo.go117
15 files changed, 219 insertions, 53 deletions
diff --git a/modules/auth/admin.go b/modules/auth/admin.go
index eccab007..fe889c23 100644
--- a/modules/auth/admin.go
+++ b/modules/auth/admin.go
@@ -8,7 +8,7 @@ import (
"net/http"
"reflect"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
diff --git a/modules/auth/auth.go b/modules/auth/auth.go
index 2e0555f6..4561dd83 100644
--- a/modules/auth/auth.go
+++ b/modules/auth/auth.go
@@ -9,7 +9,7 @@ import (
"reflect"
"strings"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
@@ -161,3 +161,54 @@ func AssignForm(form interface{}, data base.TmplData) {
data[fieldName] = val.Field(i).Interface()
}
}
+
+type InstallForm struct {
+ Database string `form:"database" binding:"Required"`
+ Host string `form:"host"`
+ User string `form:"user"`
+ Passwd string `form:"passwd"`
+ DatabaseName string `form:"database_name"`
+ SslMode string `form:"ssl_mode"`
+ DatabasePath string `form:"database_path"`
+ RepoRootPath string `form:"repo_path"`
+ RunUser string `form:"run_user"`
+ Domain string `form:"domain"`
+ AppUrl string `form:"app_url"`
+ AdminName string `form:"admin_name" binding:"Required"`
+ AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"`
+ AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"`
+ SmtpHost string `form:"smtp_host"`
+ SmtpEmail string `form:"mailer_user"`
+ SmtpPasswd string `form:"mailer_pwd"`
+ RegisterConfirm string `form:"register_confirm"`
+ MailNotify string `form:"mail_notify"`
+}
+
+func (f *InstallForm) Name(field string) string {
+ names := map[string]string{
+ "Database": "Database name",
+ "AdminName": "Admin user name",
+ "AdminPasswd": "Admin password",
+ "AdminEmail": "Admin e-maill address",
+ }
+ return names[field]
+}
+
+func (f *InstallForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
+ if req.Method == "GET" || errors.Count() == 0 {
+ return
+ }
+
+ data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
+ data["HasError"] = true
+ AssignForm(f, data)
+
+ if len(errors.Overall) > 0 {
+ for _, err := range errors.Overall {
+ log.Error("InstallForm.Validate: %v", err)
+ }
+ return
+ }
+
+ validate(errors, data, f)
+}
diff --git a/modules/auth/issue.go b/modules/auth/issue.go
index 8bf49684..36c87627 100644
--- a/modules/auth/issue.go
+++ b/modules/auth/issue.go
@@ -8,7 +8,7 @@ import (
"net/http"
"reflect"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
diff --git a/modules/auth/repo.go b/modules/auth/repo.go
index 2cc93744..eddd6475 100644
--- a/modules/auth/repo.go
+++ b/modules/auth/repo.go
@@ -8,7 +8,7 @@ import (
"net/http"
"reflect"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
diff --git a/modules/auth/setting.go b/modules/auth/setting.go
index 0bc6afd4..cada7eea 100644
--- a/modules/auth/setting.go
+++ b/modules/auth/setting.go
@@ -9,7 +9,7 @@ import (
"reflect"
"strings"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
diff --git a/modules/auth/user.go b/modules/auth/user.go
index cb8db1b2..2d3c29fd 100644
--- a/modules/auth/user.go
+++ b/modules/auth/user.go
@@ -8,11 +8,10 @@ import (
"net/http"
"reflect"
- "github.com/codegangsta/martini"
-
- "github.com/gogits/session"
+ "github.com/go-martini/martini"
"github.com/gogits/binding"
+ "github.com/gogits/session"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
@@ -21,6 +20,10 @@ import (
// SignedInId returns the id of signed in user.
func SignedInId(session session.SessionStore) int64 {
+ if !models.HasEngine {
+ return 0
+ }
+
userId := session.Get("userId")
if userId == nil {
return 0
diff --git a/modules/base/conf.go b/modules/base/conf.go
index b3a987e6..3ebc4ede 100644
--- a/modules/base/conf.go
+++ b/modules/base/conf.go
@@ -212,9 +212,9 @@ func newMailService() {
if Cfg.MustBool("mailer", "ENABLED") {
MailService = &Mailer{
Name: Cfg.MustValue("mailer", "NAME", AppName),
- Host: Cfg.MustValue("mailer", "HOST", "127.0.0.1:25"),
- User: Cfg.MustValue("mailer", "USER", "example@example.com"),
- Passwd: Cfg.MustValue("mailer", "PASSWD", "******"),
+ Host: Cfg.MustValue("mailer", "HOST"),
+ User: Cfg.MustValue("mailer", "USER"),
+ Passwd: Cfg.MustValue("mailer", "PASSWD"),
}
log.Info("Mail Service Enabled")
}
@@ -253,7 +253,7 @@ func NewConfigContext() {
cfgPath := filepath.Join(workDir, "conf/app.ini")
Cfg, err = goconfig.LoadConfigFile(cfgPath)
if err != nil {
- fmt.Printf("Cannot load config file '%s'\n", cfgPath)
+ fmt.Printf("Cannot load config file(%s): %v\n", cfgPath, err)
os.Exit(2)
}
Cfg.BlockMode = false
@@ -261,7 +261,7 @@ func NewConfigContext() {
cfgPath = filepath.Join(workDir, "custom/conf/app.ini")
if com.IsFile(cfgPath) {
if err = Cfg.AppendFiles(cfgPath); err != nil {
- fmt.Printf("Cannot load config file '%s'\n", cfgPath)
+ fmt.Printf("Cannot load config file(%s): %v\n", cfgPath, err)
os.Exit(2)
}
}
@@ -272,18 +272,19 @@ func NewConfigContext() {
Domain = Cfg.MustValue("server", "DOMAIN")
SecretKey = Cfg.MustValue("security", "SECRET_KEY")
+ InstallLock = Cfg.MustBool("security", "INSTALL_LOCK", false)
+
RunUser = Cfg.MustValue("", "RUN_USER")
curUser := os.Getenv("USERNAME")
if len(curUser) == 0 {
curUser = os.Getenv("USER")
}
- if RunUser != curUser {
+ // Does not check run user when the install lock is off.
+ if InstallLock && RunUser != curUser {
fmt.Printf("Expect user(%s) but current user is: %s\n", RunUser, curUser)
os.Exit(2)
}
- InstallLock = Cfg.MustBool("security", "INSTALL_LOCK", false)
-
LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS")
CookieUserName = Cfg.MustValue("security", "COOKIE_USERNAME")
CookieRememberName = Cfg.MustValue("security", "COOKIE_REMEMBER_NAME")
@@ -291,9 +292,14 @@ func NewConfigContext() {
PictureService = Cfg.MustValue("picture", "SERVICE")
// Determine and create root git reposiroty path.
- RepoRootPath = Cfg.MustValue("repository", "ROOT")
+ homeDir, err := com.HomeDir()
+ if err != nil {
+ fmt.Printf("Fail to get home directory): %v\n", err)
+ os.Exit(2)
+ }
+ RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "git/gogs-repositories"))
if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil {
- fmt.Printf("models.init(fail to create RepoRootPath(%s)): %v\n", RepoRootPath, err)
+ fmt.Printf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err)
os.Exit(2)
}
}
diff --git a/modules/base/template.go b/modules/base/template.go
index dca76faf..dfcae931 100644
--- a/modules/base/template.go
+++ b/modules/base/template.go
@@ -33,6 +33,13 @@ func List(l *list.List) chan interface{} {
return c
}
+func ShortSha(sha1 string) string {
+ if len(sha1) == 40 {
+ return sha1[:10]
+ }
+ return sha1
+}
+
var mailDomains = map[string]string{
"gmail.com": "gmail.com",
}
@@ -72,4 +79,5 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{
},
"DiffTypeToStr": DiffTypeToStr,
"DiffLineTypeToStr": DiffLineTypeToStr,
+ "ShortSha": ShortSha,
}
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 9ddb90f7..6876da76 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -478,6 +478,7 @@ func (a argInt) Get(i int, args ...int) (r int) {
type Actioner interface {
GetOpType() int
GetActUserName() string
+ GetActEmail() string
GetRepoName() string
GetBranch() string
GetContent() string
@@ -506,15 +507,23 @@ const (
<div><img src="%s?s=16" alt="user-avatar"/> %s</div>`
)
+type PushCommit struct {
+ Sha1 string
+ Message string
+ AuthorEmail string
+ AuthorName string
+}
+
type PushCommits struct {
Len int
- Commits [][]string
+ Commits []*PushCommit
}
// ActionDesc accepts int that represents action operation type
// and returns the description.
-func ActionDesc(act Actioner, avatarLink string) string {
+func ActionDesc(act Actioner) string {
actUserName := act.GetActUserName()
+ email := act.GetActEmail()
repoName := act.GetRepoName()
repoLink := actUserName + "/" + repoName
branch := act.GetBranch()
@@ -529,7 +538,7 @@ func ActionDesc(act Actioner, avatarLink string) string {
}
buf := bytes.NewBuffer([]byte("\n"))
for _, commit := range push.Commits {
- buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, avatarLink, repoLink, commit[0], commit[0][:7], commit[1]) + "\n")
+ buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n")
}
if push.Len > 3 {
buf.WriteString(fmt.Sprintf(`<div><a href="/%s/%s/commits/%s">%d other commits >></a></div>`, actUserName, repoName, branch, push.Len))
@@ -539,7 +548,7 @@ func ActionDesc(act Actioner, avatarLink string) string {
case 6: // Create issue.
infos := strings.SplitN(content, "|", 2)
return fmt.Sprintf(TPL_CREATE_Issue, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0],
- avatarLink, infos[1])
+ AvatarLink(email), infos[1])
default:
return "invalid type"
}
diff --git a/modules/log/log.go b/modules/log/log.go
index e1bab8ae..f0067548 100644
--- a/modules/log/log.go
+++ b/modules/log/log.go
@@ -15,13 +15,14 @@ var (
)
func init() {
- logger = logs.NewLogger(10000)
- logger.SetLogger("console", `{"level": 0}`)
+ NewLogger(10000, "console", `{"level": 0}`)
}
func NewLogger(bufLen int64, mode, config string) {
Mode, Config = mode, config
logger = logs.NewLogger(bufLen)
+ logger.EnableFuncCallDepth(true)
+ logger.SetLogFuncCallDepth(4)
logger.SetLogger(mode, config)
}
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go
index 64f75d75..bde3be72 100644
--- a/modules/middleware/auth.go
+++ b/modules/middleware/auth.go
@@ -7,7 +7,7 @@ package middleware
import (
"net/url"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/gogs/modules/base"
)
@@ -21,6 +21,11 @@ type ToggleOptions struct {
func Toggle(options *ToggleOptions) martini.Handler {
return func(ctx *Context) {
+ if !base.InstallLock {
+ ctx.Redirect("/install")
+ return
+ }
+
if options.SignOutRequire && ctx.IsSigned && ctx.Req.RequestURI != "/" {
ctx.Redirect("/")
return
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index d81ab999..d2b268cd 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -15,9 +15,10 @@ import (
"strings"
"time"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/cache"
+ "github.com/gogits/git"
"github.com/gogits/session"
"github.com/gogits/gogs/models"
@@ -41,11 +42,18 @@ type Context struct {
csrfToken string
Repo struct {
- IsValid bool
IsOwner bool
IsWatching bool
+ IsBranch bool
+ IsTag bool
+ IsCommit bool
Repository *models.Repository
Owner *models.User
+ Commit *git.Commit
+ GitRepo *git.Repository
+ BranchName string
+ CommitId string
+ RepoLink string
CloneLink struct {
SSH string
HTTPS string
@@ -98,6 +106,10 @@ func (ctx *Context) Handle(status int, title string, err error) {
ctx.HTML(status, fmt.Sprintf("status/%d", status))
}
+func (ctx *Context) Debug(msg string, args ...interface{}) {
+ log.Debug(msg, args...)
+}
+
func (ctx *Context) GetCookie(name string) string {
cookie, err := ctx.Req.Cookie(name)
if err != nil {
@@ -257,7 +269,7 @@ func InitContext() martini.Handler {
if user != nil {
ctx.Data["SignedUser"] = user
ctx.Data["SignedUserId"] = user.Id
- ctx.Data["SignedUserName"] = user.LowerName
+ ctx.Data["SignedUserName"] = user.Name
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
}
diff --git a/modules/middleware/logger.go b/modules/middleware/logger.go
index dcf85246..fc8e1a81 100644
--- a/modules/middleware/logger.go
+++ b/modules/middleware/logger.go
@@ -11,7 +11,7 @@ import (
"runtime"
"time"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
)
var isWindows bool
diff --git a/modules/middleware/render.go b/modules/middleware/render.go
index 869ef9ab..98d485af 100644
--- a/modules/middleware/render.go
+++ b/modules/middleware/render.go
@@ -17,7 +17,7 @@ import (
"path/filepath"
"time"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
"github.com/gogits/gogs/modules/base"
)
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index 54b735af..f446d6a8 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -9,24 +9,40 @@ import (
"fmt"
"strings"
- "github.com/codegangsta/martini"
+ "github.com/go-martini/martini"
+
+ "github.com/gogits/git"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
)
-func RepoAssignment(redirect bool) martini.Handler {
+func RepoAssignment(redirect bool, args ...bool) martini.Handler {
return func(ctx *Context, params martini.Params) {
- // assign false first
- ctx.Data["IsRepositoryValid"] = false
+ // valid brachname
+ var validBranch bool
+ // display bare quick start if it is a bare repo
+ var displayBare bool
+
+ if len(args) >= 1 {
+ validBranch = args[0]
+ }
+
+ if len(args) >= 2 {
+ displayBare = args[1]
+ }
var (
user *models.User
err error
)
+ userName := params["username"]
+ repoName := params["reponame"]
+ branchName := params["branchname"]
+
// get repository owner
- ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(params["username"])
+ ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName)
if !ctx.Repo.IsOwner {
user, err = models.GetUserByName(params["username"])
@@ -51,10 +67,8 @@ func RepoAssignment(redirect bool) martini.Handler {
return
}
- ctx.Repo.Owner = user
-
// get repository
- repo, err := models.GetRepositoryByName(user.Id, params["reponame"])
+ repo, err := models.GetRepositoryByName(user.Id, repoName)
if err != nil {
if err == models.ErrRepoNotExist {
ctx.Handle(404, "RepoAssignment", err)
@@ -62,30 +76,87 @@ func RepoAssignment(redirect bool) martini.Handler {
ctx.Redirect("/")
return
}
- ctx.Handle(200, "RepoAssignment", err)
+ ctx.Handle(404, "RepoAssignment", err)
return
}
-
- ctx.Repo.IsValid = true
- if ctx.User != nil {
- ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
- }
ctx.Repo.Repository = repo
- ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
- ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
- if len(params["branchname"]) == 0 {
- params["branchname"] = "master"
+ ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
+
+ gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
+ if err != nil {
+ ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
+ return
}
- ctx.Data["Branchname"] = params["branchname"]
+ ctx.Repo.GitRepo = gitRepo
- ctx.Data["IsRepositoryValid"] = true
+ ctx.Repo.Owner = user
+ ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name
+
+ ctx.Data["Title"] = user.Name + "/" + repo.Name
ctx.Data["Repository"] = repo
ctx.Data["Owner"] = user
- ctx.Data["Title"] = user.Name + "/" + repo.Name
- ctx.Data["CloneLink"] = ctx.Repo.CloneLink
- ctx.Data["RepositoryLink"] = ctx.Data["Title"]
+ ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
+ ctx.Data["BranchName"] = ""
+
+ ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName)
+ ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
+ ctx.Data["CloneLink"] = ctx.Repo.CloneLink
+
+ // when repo is bare, not valid branch
+ if !ctx.Repo.Repository.IsBare && validBranch {
+ detect:
+ if len(branchName) > 0 {
+ // TODO check tag
+ if models.IsBranchExist(user.Name, repoName, branchName) {
+ ctx.Repo.IsBranch = true
+ ctx.Repo.BranchName = branchName
+
+ ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName)
+ if err != nil {
+ ctx.Handle(404, "RepoAssignment invalid branch", nil)
+ return
+ }
+
+ ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String()
+
+ } else if len(branchName) == 40 {
+ ctx.Repo.IsCommit = true
+ ctx.Repo.CommitId = branchName
+ ctx.Repo.BranchName = branchName
+
+ ctx.Repo.Commit, err = gitRepo.GetCommit(branchName)
+ if err != nil {
+ ctx.Handle(404, "RepoAssignment invalid commit", nil)
+ return
+ }
+ } else {
+ ctx.Handle(404, "RepoAssignment invalid repo", nil)
+ return
+ }
+
+ } else {
+ branchName = "master"
+ goto detect
+ }
+
+ ctx.Data["IsBranch"] = ctx.Repo.IsBranch
+ ctx.Data["IsCommit"] = ctx.Repo.IsCommit
+ }
+
+ // repo is bare and display enable
+ if displayBare && ctx.Repo.Repository.IsBare {
+ ctx.HTML(200, "repo/single_bare")
+ return
+ }
+
+ if ctx.IsSigned {
+ ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id)
+ }
+
+ ctx.Data["BranchName"] = ctx.Repo.BranchName
+ ctx.Data["CommitId"] = ctx.Repo.CommitId
ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
}
}