From d4565483e67dfd17f723114d5849b2ce6895c077 Mon Sep 17 00:00:00 2001 From: skyblue Date: Wed, 9 Apr 2014 00:26:12 +0800 Subject: [PATCH 01/14] add id for oauth2 --- .fswatch.json | 4 ++-- models/oauth2.go | 25 +++++++++++++++++++------ routers/user/social.go | 30 ++++++++++++------------------ 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/.fswatch.json b/.fswatch.json index 90a6e4eae..4ef36ce47 100644 --- a/.fswatch.json +++ b/.fswatch.json @@ -2,12 +2,12 @@ "paths": ["."], "depth": 2, "exclude": [], - "include": ["\\.go$"], + "include": ["\\.go$", "\\.ini$"], "command": [ "bash", "-c", "go build && ./gogs web" ], "env": { "POWERED_BY": "github.com/shxsun/fswatch" }, - "enable-restart": true + "enable-restart": false } diff --git a/models/oauth2.go b/models/oauth2.go index a17d4e30f..10771d6a7 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -1,6 +1,9 @@ package models -import "fmt" +import ( + "errors" + "fmt" +) // OT: Oauth2 Type const ( @@ -9,12 +12,18 @@ const ( OT_TWITTER ) +var ( + ErrOauth2RecordNotExists = errors.New("not exists oauth2 record") + ErrOauth2NotAssociatedWithUser = errors.New("not associated with user") +) + type Oauth2 struct { - Uid int64 `xorm:"pk"` // userId + Id int64 + Uid int64 `xorm:"pk"` // userId + User *User `xorm:"-"` Type int `xorm:"pk unique(oauth)"` // twitter,github,google... Identity string `xorm:"pk unique(oauth)"` // id.. Token string `xorm:"VARCHAR(200) not null"` - //RefreshTime time.Time `xorm:"created"` } func AddOauth2(oa *Oauth2) (err error) { @@ -24,8 +33,8 @@ func AddOauth2(oa *Oauth2) (err error) { return nil } -func GetOauth2User(identity string) (u *User, err error) { - oa := &Oauth2{} +func GetOauth2(identity string) (oa *Oauth2, err error) { + oa = &Oauth2{} oa.Identity = identity exists, err := orm.Get(oa) if err != nil { @@ -35,5 +44,9 @@ func GetOauth2User(identity string) (u *User, err error) { err = fmt.Errorf("not exists oauth2: %s", identity) return } - return GetUserById(oa.Uid) + if oa.Uid == 0 { + return oa, ErrOauth2NotAssociatedWithUser + } + oa.User, err = GetUserById(oa.Uid) + return } diff --git a/routers/user/social.go b/routers/user/social.go index 08cfcd83f..b47a4c1ce 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -11,7 +11,6 @@ import ( "code.google.com/p/goauth2/oauth" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/oauth2" @@ -85,7 +84,6 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { return } var err error - var u *models.User if err = gh.Update(); err != nil { // FIXME: handle error page log.Error("connect with github error: %s", err) @@ -93,20 +91,14 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { } var soc SocialConnector = gh log.Info("login: %s", soc.Name()) - // FIXME: login here, user email to check auth, if not registe, then generate a uniq username - if u, err = models.GetOauth2User(soc.Identity()); err != nil { - u = &models.User{ - Name: soc.Name(), - Email: soc.Email(), - Passwd: "123456", - IsActive: !base.Service.RegisterEmailConfirm, - } - if u, err = models.RegisterUser(u); err != nil { - log.Error("register user: %v", err) - return - } - oa := &models.Oauth2{} - oa.Uid = u.Id + oa, err := models.GetOauth2(soc.Identity()) + switch err { + case nil: + ctx.Session.Set("userId", oa.User.Id) + ctx.Session.Set("userName", oa.User.Name) + case models.ErrOauth2RecordNotExists: + oa = &models.Oauth2{} + oa.Uid = 0 oa.Type = soc.Type() oa.Token = soc.Token() oa.Identity = soc.Identity() @@ -115,8 +107,10 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { log.Error("add oauth2 %v", err) return } + case models.ErrOauth2NotAssociatedWithUser: + // pass } - ctx.Session.Set("userId", u.Id) - ctx.Session.Set("userName", u.Name) + ctx.Session.Set("socialId", oa.Id) + log.Info("socialId: %v", oa.Id) ctx.Redirect("/") } From 24d0ca4aa02bd4245cd4c91f71b918c5da1d2e7d Mon Sep 17 00:00:00 2001 From: skyblue Date: Wed, 9 Apr 2014 00:31:09 +0800 Subject: [PATCH 02/14] clean tail --- models/oauth2.go | 8 ++------ routers/user/social.go | 3 +++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/models/oauth2.go b/models/oauth2.go index 10771d6a7..4da980067 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -1,9 +1,6 @@ package models -import ( - "errors" - "fmt" -) +import "errors" // OT: Oauth2 Type const ( @@ -41,8 +38,7 @@ func GetOauth2(identity string) (oa *Oauth2, err error) { return } if !exists { - err = fmt.Errorf("not exists oauth2: %s", identity) - return + return nil, ErrOauth2RecordNotExists } if oa.Uid == 0 { return oa, ErrOauth2NotAssociatedWithUser diff --git a/routers/user/social.go b/routers/user/social.go index b47a4c1ce..7b4d23298 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -109,6 +109,9 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { } case models.ErrOauth2NotAssociatedWithUser: // pass + default: + log.Error(err) // FIXME: handle error page + return } ctx.Session.Set("socialId", oa.Id) log.Info("socialId: %v", oa.Id) From 115a349131242201953a3f5693141679049355c6 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 8 Apr 2014 12:41:33 -0400 Subject: [PATCH 03/14] Fix #67 --- README.md | 2 +- README_ZH.md | 2 +- gogs.go | 2 +- models/repo.go | 12 ++++++++---- modules/base/conf.go | 1 + modules/base/template.go | 3 +++ routers/install.go | 1 + serve.go | 5 +---- templates/base/head.tmpl | 16 +++++++++++++--- 9 files changed, 30 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fe15328b1..a4e8901c5 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ More importantly, Gogs only needs one binary to setup your own project hosting o ## Features - Activity timeline -- SSH/HTTPS(Clone only) protocol support. +- SSH/HTTP(S) protocol support. - Register/delete/rename account. - Create/delete/watch/rename/transfer public repository. - Repository viewer. diff --git a/README_ZH.md b/README_ZH.md index 015ee0af9..2f8015410 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -23,7 +23,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 ## 功能特性 - 活动时间线 -- SSH/HTTPS(仅限 Clone) 协议支持 +- SSH/HTTP(S) 协议支持 - 注册/删除/重命名用户 - 创建/删除/关注/重命名/转移公开仓库 - 仓库浏览器 diff --git a/gogs.go b/gogs.go index df268980f..4616141e3 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.2.2.0407 Alpha" +const APP_VER = "0.2.2.0408 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/repo.go b/models/repo.go index bb5c36372..4f58f407f 100644 --- a/models/repo.go +++ b/models/repo.go @@ -261,6 +261,13 @@ func createHookUpdate(hookPath, content string) error { return err } +// SetRepoEnvs sets environment variables for command update. +func SetRepoEnvs(userId int64, userName, repoName string) { + os.Setenv("userId", base.ToStr(userId)) + os.Setenv("userName", userName) + os.Setenv("repoName", repoName) +} + // InitRepository initializes README and .gitignore if needed. func initRepository(f string, user *User, repo *Repository, initReadme bool, repoLang, license string) error { repoPath := RepoPath(user.Name, repo.Name) @@ -333,10 +340,7 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return nil } - // for update use - os.Setenv("userName", user.Name) - os.Setenv("userId", base.ToStr(user.Id)) - os.Setenv("repoName", repo.Name) + SetRepoEnvs(user.Id, user.Name, repo.Name) // Apply changes and commit. return initRepoCommit(tmpDir, user.NewGitSig()) diff --git a/modules/base/conf.go b/modules/base/conf.go index 69df49dc4..871595e47 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -43,6 +43,7 @@ var ( AppName string AppLogo string AppUrl string + IsProdMode bool Domain string SecretKey string RunUser string diff --git a/modules/base/template.go b/modules/base/template.go index 6cd8ade61..5a42107c4 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -56,6 +56,9 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "AppDomain": func() string { return Domain }, + "IsProdMode": func() bool { + return IsProdMode + }, "LoadTimes": func(startTime time.Time) string { return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms" }, diff --git a/routers/install.go b/routers/install.go index 1c4e6181d..b9e8bb290 100644 --- a/routers/install.go +++ b/routers/install.go @@ -27,6 +27,7 @@ func checkRunMode() { switch base.Cfg.MustValue("", "RUN_MODE") { case "prod": martini.Env = martini.Prod + base.IsProdMode = true case "test": martini.Env = martini.Test } diff --git a/serve.go b/serve.go index 7e00db473..3843da617 100644 --- a/serve.go +++ b/serve.go @@ -177,10 +177,7 @@ func runServ(k *cli.Context) { qlog.Fatal("Unknown command") } - // for update use - os.Setenv("userName", user.Name) - os.Setenv("userId", strconv.Itoa(int(user.Id))) - os.Setenv("repoName", repoName) + models.SetRepoEnvs(user.Id, user.Name, repoName) gitcmd := exec.Command(verb, repoPath) gitcmd.Dir = base.RepoRootPath diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 7f56ed708..2f88e918f 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -11,14 +11,24 @@ + {{if IsProdMode}} + + + + + + {{else}} - - - + {{end}} + + + + + {{if .Title}}{{.Title}} - {{end}}{{AppName}} From a991ebf5d0fe06c97be4b90b562e058ea2642de9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 8 Apr 2014 15:27:35 -0400 Subject: [PATCH 04/14] Fix #54 --- routers/install.go | 6 ++++++ routers/user/social.go | 2 +- templates/install.tmpl | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/routers/install.go b/routers/install.go index b9e8bb290..5d6c65ef9 100644 --- a/routers/install.go +++ b/routers/install.go @@ -7,6 +7,7 @@ package routers import ( "errors" "os" + "os/exec" "strings" "github.com/Unknwon/goconfig" @@ -103,6 +104,11 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { return } + if _, err := exec.LookPath("git"); err != nil { + ctx.RenderWithErr("Fail to test 'git' command: "+err.Error(), "install", &form) + return + } + // Pass basic check, now test configuration. // Test database setting. dbTypes := map[string]string{"mysql": "mysql", "pgsql": "postgres", "sqlite": "sqlite3"} diff --git a/routers/user/social.go b/routers/user/social.go index 7b4d23298..a35da5493 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -110,7 +110,7 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { case models.ErrOauth2NotAssociatedWithUser: // pass default: - log.Error(err) // FIXME: handle error page + log.Error(err.Error()) // FIXME: handle error page return } ctx.Session.Set("socialId", oa.Id) diff --git a/templates/install.tmpl b/templates/install.tmpl index 1fbc74bc7..c70cfa3e6 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -156,11 +156,11 @@
- +
- +
From 721834a2673fed7fc21988b8e91a88a26bcef051 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Wed, 9 Apr 2014 20:10:56 +0800 Subject: [PATCH 05/14] change new-repo button to dropdown menu in dashboard page --- public/css/gogs.css | 28 +++++++++++++++++++++++++++- templates/user/dashboard.tmpl | 15 ++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/public/css/gogs.css b/public/css/gogs.css index da2a7fd1a..bf39ea39c 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -444,6 +444,32 @@ html, body { margin-right: 1em; } +#user-dashboard-repo-new .btn-sm.dropdown-toggle { + padding: 3px 8px; +} + +#user-dashboard-repo-new .dropdown-menu { + padding: 0; + margin: 0; +} + +#user-dashboard-repo-new ul { + margin: 0; + width: 200px; +} + +#user-dashboard-repo-new li a { + line-height: 36px; + display: block; + padding: 0 18px; + color: #444; +} + +#user-dashboard-repo-new li a:hover { + background: #0093c4; + color: #FFF; +} + /* gogits repo single page */ #body-nav.repo-nav { @@ -1372,6 +1398,6 @@ html, body { margin: 16px 0; } -#release-preview{ +#release-preview { margin: 6px 0; } \ No newline at end of file diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index bc0853fb0..39e277e3c 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -29,7 +29,20 @@
Your Repositories - New Repo +
+ + + +
    {{range .MyRepos}} From c72e1b5c2ac8d2cbd123f3f53db7b4a125045725 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Wed, 9 Apr 2014 21:28:00 +0800 Subject: [PATCH 06/14] repo-import page ui --- public/css/gogs.css | 12 ++++ routers/repo/repo.go | 32 ++++++++++ templates/repo/import.tmpl | 112 ++++++++++++++++++++++++++++++++++ templates/user/dashboard.tmpl | 2 +- web.go | 1 + 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 templates/repo/import.tmpl diff --git a/public/css/gogs.css b/public/css/gogs.css index bf39ea39c..dcfb2758a 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -309,6 +309,18 @@ html, body { height: 8em; } +#repo-import-auth{ + width: 100%; + margin-top: 48px; + box-sizing: border-box; +} + +#repo-import-auth .form-group{ + box-sizing: border-box; + margin-left: 0; + margin-right: 0; +} + /* gogits user setting */ #user-setting-nav > h4, #user-setting-container > h4, #user-setting-container > div > h4, diff --git a/routers/repo/repo.go b/routers/repo/repo.go index d223600c5..0ab1c9e42 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -55,6 +55,38 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Handle(200, "repo.Create", err) } +func Import(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = "Import repository" + ctx.Data["PageIsNewRepo"] = true // For navbar arrow. + ctx.Data["LanguageIgns"] = models.LanguageIgns + ctx.Data["Licenses"] = models.Licenses + + if ctx.Req.Method == "GET" { + ctx.HTML(200, "repo/import") + return + } + + if ctx.HasError() { + ctx.HTML(200, "repo/import") + return + } + + _, err := models.CreateRepository(ctx.User, form.RepoName, form.Description, + form.Language, form.License, form.Visibility == "private", form.InitReadme == "on") + if err == nil { + log.Trace("%s Repository created: %s/%s", ctx.Req.RequestURI, ctx.User.LowerName, form.RepoName) + ctx.Redirect("/" + ctx.User.Name + "/" + form.RepoName) + return + } else if err == models.ErrRepoAlreadyExist { + ctx.RenderWithErr("Repository name has already been used", "repo/import", &form) + return + } else if err == models.ErrRepoNameIllegal { + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/import", &form) + return + } + ctx.Handle(200, "repo.Import", err) +} + func Single(ctx *middleware.Context, params martini.Params) { branchName := ctx.Repo.BranchName commitId := ctx.Repo.CommitId diff --git a/templates/repo/import.tmpl b/templates/repo/import.tmpl new file mode 100644 index 000000000..75d928d13 --- /dev/null +++ b/templates/repo/import.tmpl @@ -0,0 +1,112 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +
    +
    + {{.CsrfTokenHtml}} +

    Import Repository

    +
    {{.ErrorMsg}}
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    +
    +
    +
    + +
    +

    {{.SignedUserName}}

    + +
    +
    + +
    + +
    + + Great repository names are short and memorable. +
    +
    + +
    + +
    +

    Public

    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + + + +
    +
    + + Cancel +
    +
    +
    +
    +{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index 39e277e3c..f0a0a0cca 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -37,7 +37,7 @@ -
    - -
    - -
    -
    - -
    - -
    - -
    -
    - - -
    - + Cancel
    diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index f0a0a0cca..cd55b651d 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -37,7 +37,7 @@
+
+ +
+
+
+ +
+
+
+
diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index fc0a3bd96..3f6391535 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -9,6 +9,20 @@

Quick Guide

+
+ {{.CsrfTokenHtml}} +

Clone from existing repository

+
+ + + + + + + +
+
+

Clone this repository

diff --git a/templates/repo/toolbar.tmpl b/templates/repo/toolbar.tmpl index d8ab26214..9c137e517 100644 --- a/templates/repo/toolbar.tmpl +++ b/templates/repo/toolbar.tmpl @@ -11,7 +11,7 @@
  • {{if .Repository.NumOpenIssues}}{{.Repository.NumOpenIssues}} {{end}}Issues
  • {{if .IsRepoToolbarIssues}}
  • {{if .IsRepoToolbarIssuesList}} - {{else}}{{end}}
  • + {{end}} {{end}}
  • {{if .Repository.NumReleases}}{{.Repository.NumReleases}} {{end}}Releases
  • {{if .IsRepoToolbarReleases}} diff --git a/templates/user/forgot_passwd.tmpl b/templates/user/forgot_passwd.tmpl index ff25406fd..a099ff274 100644 --- a/templates/user/forgot_passwd.tmpl +++ b/templates/user/forgot_passwd.tmpl @@ -24,6 +24,8 @@
    {{else if .IsResetDisable}}

    Sorry, mail service is not enabled.

    + {{else if .ResendLimited}} +

    Sorry, you are sending e-mail too frequently, please wait 3 minutes.

    {{end}}
    From a354f33ac2458d788cfeeeea6b92b9a3c1fc9b92 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Thu, 10 Apr 2014 22:00:32 +0800 Subject: [PATCH 13/14] new-repo dropdown in top navbar --- public/css/gogs.css | 23 +++++++++++++++++------ templates/base/navbar.tmpl | 11 ++++++++++- templates/user/dashboard.tmpl | 6 +----- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/public/css/gogs.css b/public/css/gogs.css index dcfb2758a..2850d15e2 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -309,13 +309,13 @@ html, body { height: 8em; } -#repo-import-auth{ +#repo-import-auth { width: 100%; margin-top: 48px; box-sizing: border-box; } -#repo-import-auth .form-group{ +#repo-import-auth .form-group { box-sizing: border-box; margin-left: 0; margin-right: 0; @@ -460,28 +460,39 @@ html, body { padding: 3px 8px; } -#user-dashboard-repo-new .dropdown-menu { +#user-dashboard-repo-new .dropdown-menu, #nav-repo-new .dropdown-menu { padding: 0; margin: 0; } -#user-dashboard-repo-new ul { +#user-dashboard-repo-new ul, #nav-repo-new ul { margin: 0; width: 200px; } -#user-dashboard-repo-new li a { +#user-dashboard-repo-new li a, #nav-repo-new li a { line-height: 36px; display: block; padding: 0 18px; color: #444; } -#user-dashboard-repo-new li a:hover { +#user-dashboard-repo-new li a:hover, #nav-repo-new li a:hover { background: #0093c4; color: #FFF; } +#nav-repo-new button { + border: none; + background: transparent; + padding: 0; + width: 15px; +} + +#nav-repo-new li .fa { + margin: 0 .5em; +} + /* gogits repo single page */ #body-nav.repo-nav { diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 7d1f64e49..c0855d81e 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -8,9 +8,18 @@ user-avatar - {{if .IsAdmin}}{{end}} + {{else}}Sign In Sign Up{{end}} diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index cd55b651d..e2d7a5093 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -30,17 +30,13 @@
    Your Repositories
    - - +
    From 6b30d9b0f27a8cb0961d52980a8e655d9a0bbda2 Mon Sep 17 00:00:00 2001 From: FuXiaoHei Date: Thu, 10 Apr 2014 22:06:52 +0800 Subject: [PATCH 14/14] fix mirror typo --- templates/repo/mirror.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/mirror.tmpl b/templates/repo/mirror.tmpl index c69f47a30..2ac21dd61 100644 --- a/templates/repo/mirror.tmpl +++ b/templates/repo/mirror.tmpl @@ -3,7 +3,7 @@
    {{.CsrfTokenHtml}} -

    Import Repository

    +

    Create Repository Mirror

    {{.ErrorMsg}}