diff options
57 files changed, 2070 insertions, 549 deletions
diff --git a/.fswatch.json b/.fswatch.json new file mode 100644 index 00000000..7b12022c --- /dev/null +++ b/.fswatch.json @@ -0,0 +1,12 @@ +{ + "paths": ["."], + "depth": 2, + "exclude": [], + "include": ["\\.go$", "\\.ini$"], + "command": [ + "bash", "-c", "go build && ./gogs web" + ], + "env": { + "POWERED_BY": "github.com/shxsun/fswatch" + } +} @@ -33,3 +33,4 @@ _testmain.go *.exe~ gogs __pycache__ +*.pem @@ -1,28 +1,26 @@ [target] -path=github.com/gogits/gogs +path = github.com/gogits/gogs [deps] -github.com/codegangsta/cli= -github.com/go-martini/martini= -github.com/Unknwon/com= -github.com/Unknwon/cae= -github.com/Unknwon/goconfig= -github.com/dchest/scrypt= -github.com/nfnt/resize= -github.com/lunny/xorm= -github.com/go-sql-driver/mysql= -github.com/lib/pq= -github.com/gogits/logs= -github.com/gogits/binding= -github.com/gogits/git= -github.com/gogits/gfm= -github.com/gogits/cache= -github.com/gogits/session= -github.com/gogits/webdav= -github.com/martini-contrib/oauth2= -github.com/martini-contrib/sessions= -code.google.com/p/goauth2= +github.com/codegangsta/cli = +github.com/go-martini/martini = +github.com/Unknwon/com = +github.com/Unknwon/cae = +github.com/Unknwon/goconfig = +github.com/nfnt/resize = +github.com/lunny/xorm = +github.com/go-sql-driver/mysql = +github.com/lib/pq = +github.com/qiniu/log = +code.google.com/p/goauth2 = +github.com/gogits/logs = +github.com/gogits/binding = +github.com/gogits/git = +github.com/gogits/gfm = +github.com/gogits/cache = +github.com/gogits/session = +github.com/gogits/webdav = [res] -include=templates|public|conf +include = templates|public|conf @@ -5,9 +5,9 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language  -##### Current version: 0.2.0 Alpha +##### Current version: 0.2.3 Alpha -#### Due to testing purpose, data of [try.gogits.org](http://try.gogits.org) has been reset in March 29, 2014 and will reset multiple times after. Please do NOT put your important data on the site. +#### Due to testing purpose, data of [try.gogits.org](http://try.gogits.org) has been reset in April 6, 2014 and will reset multiple times after. Please do NOT put your important data on the site. #### Other language version @@ -29,9 +29,9 @@ 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 public repository. +- Create/delete/watch/rename/transfer public repository. - Repository viewer. - Issue tracker. - Gravatar and cache support. diff --git a/README_ZH.md b/README_ZH.md index e66f607a..35a0b763 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。  -##### 当前版本:0.2.0 Alpha +##### 当前版本:0.2.3 Alpha ## 开发目的 @@ -23,9 +23,9 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 ## 功能特性 - 活动时间线 -- SSH/HTTPS(仅限 Clone) 协议支持 +- SSH/HTTP(S) 协议支持 - 注册/删除/重命名用户 -- 创建/删除/关注/重命名公开仓库 +- 创建/删除/关注/重命名/转移公开仓库 - 仓库浏览器 - Bug 追踪系统 - Gravatar 以及缓存支持 @@ -13,6 +13,8 @@ "others": [ "modules", "$GOPATH/src/github.com/gogits/binding", + "$GOPATH/src/github.com/gogits/webdav", + "$GOPATH/src/github.com/gogits/logs", "$GOPATH/src/github.com/gogits/git", "$GOPATH/src/github.com/gogits/gfm" ] diff --git a/conf/app.ini b/conf/app.ini index abc27c39..575e18a4 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -12,10 +12,13 @@ LANG_IGNS = Google Go|C|C++|Python|Ruby|C Sharp LICENSES = Apache v2 License|GPL v2|MIT License|Affero GPL|Artistic License 2.0|BSD (3-Clause) License [server] +PROTOCOL = http DOMAIN = localhost -ROOT_URL = http://%(DOMAIN)s:%(HTTP_PORT)s/ +ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/ HTTP_ADDR = HTTP_PORT = 3000 +CERT_FILE = cert.pem +KEY_FILE = key.pem [database] ; Either "mysql", "postgres" or "sqlite3"(binary release only), it's your choice @@ -69,6 +72,15 @@ FROM = USER = PASSWD = +[oauth] +ENABLED = false + +[oauth.github] +ENABLED = +CLIENT_ID = +CLIENT_SECRET = +SCOPES = https://api.github.com/user + [cache] ; Either "memory", "redis", or "memcache", default is "memory" ADAPTER = memory @@ -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.0.0403 Alpha" +const APP_VER = "0.2.3.0409 Alpha" func init() { base.AppVer = APP_VER diff --git a/models/access.go b/models/access.go index 83261575..2c090015 100644 --- a/models/access.go +++ b/models/access.go @@ -7,6 +7,8 @@ package models import ( "strings" "time" + + "github.com/lunny/xorm" ) // Access types. @@ -40,6 +42,15 @@ func UpdateAccess(access *Access) error { return err } +// UpdateAccess updates access information with session for rolling back. +func UpdateAccessWithSession(sess *xorm.Session, access *Access) error { + if _, err := sess.Id(access.Id).Update(access); err != nil { + sess.Rollback() + return err + } + return nil +} + // HasAccess returns true if someone can read or write to given repository. func HasAccess(userName, repoName string, mode int) (bool, error) { return orm.Get(&Access{ diff --git a/models/git.go b/models/git.go index 46345d0f..77b7ef2d 100644 --- a/models/git.go +++ b/models/git.go @@ -142,7 +142,8 @@ func GetReposFiles(userName, repoName, commitId, rpath string) ([]*RepoFile, err } func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, repoName)) + repopath := RepoPath(userName, repoName) + repo, err := git.OpenRepository(repopath) if err != nil { return nil, err } @@ -162,77 +163,23 @@ func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFi return 0 } - var cm = commit - var i int - for { - i = i + 1 - //fmt.Println(".....", i, cm.Id(), cm.ParentCount()) - if cm.ParentCount() == 0 { - break - } else if cm.ParentCount() == 1 { - pt, _ := repo.SubTree(cm.Parent(0).Tree, dirname) - if pt == nil { - break - } - pEntry := pt.EntryByName(entry.Name) - if pEntry == nil || !pEntry.Id.Equal(entry.Id) { - break - } else { - cm = cm.Parent(0) - } - } else { - var emptyCnt = 0 - var sameIdcnt = 0 - var lastSameCm *git.Commit - //fmt.Println(".....", cm.ParentCount()) - for i := 0; i < cm.ParentCount(); i++ { - //fmt.Println("parent", i, cm.Parent(i).Id()) - p := cm.Parent(i) - pt, _ := repo.SubTree(p.Tree, dirname) - var pEntry *git.TreeEntry - if pt != nil { - pEntry = pt.EntryByName(entry.Name) - } - - //fmt.Println("pEntry", pEntry) - - if pEntry == nil { - emptyCnt = emptyCnt + 1 - if emptyCnt+sameIdcnt == cm.ParentCount() { - if lastSameCm == nil { - goto loop - } else { - cm = lastSameCm - break - } - } - } else { - //fmt.Println(i, "pEntry", pEntry.Id, "entry", entry.Id) - if !pEntry.Id.Equal(entry.Id) { - goto loop - } else { - lastSameCm = cm.Parent(i) - sameIdcnt = sameIdcnt + 1 - if emptyCnt+sameIdcnt == cm.ParentCount() { - // TODO: now follow the first parent commit? - cm = lastSameCm - //fmt.Println("sameId...") - break - } - } - } - } - } + cmd := exec.Command("git", "log", "-1", "--pretty=format:%H", commitId, "--", path.Join(dirname, entry.Name)) + cmd.Dir = repopath + out, err := cmd.Output() + if err != nil { + return 0 + } + filecm, err := repo.GetCommit(string(out)) + if err != nil { + return 0 } - - loop: rp := &RepoFile{ entry, path.Join(dirname, entry.Name), size, repo, - cm, + filecm, } if entry.IsFile() { diff --git a/models/models.go b/models/models.go index 0ad86337..ee96207d 100644 --- a/models/models.go +++ b/models/models.go @@ -18,7 +18,9 @@ import ( ) var ( - orm *xorm.Engine + orm *xorm.Engine + tables []interface{} + HasEngine bool DbCfg struct { @@ -28,6 +30,11 @@ var ( UseSQLite3 bool ) +func init() { + tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), + new(Action), new(Access), new(Issue), new(Comment), new(Oauth2)) +} + func LoadModelsConfig() { DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE") if DbCfg.Type == "sqlite3" { @@ -58,9 +65,7 @@ func NewTestEngine(x *xorm.Engine) (err error) { if err != nil { return fmt.Errorf("models.init(fail to conntect database): %v", err) } - - return x.Sync(new(User), new(PublicKey), new(Repository), new(Watch), - new(Action), new(Access), new(Issue), new(Comment)) + return x.Sync(tables...) } func SetEngine() (err error) { @@ -102,9 +107,9 @@ func SetEngine() (err error) { func NewEngine() (err error) { if err = SetEngine(); err != nil { return err - } else if err = orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch), - new(Action), new(Access), new(Issue), new(Comment)); err != nil { - return fmt.Errorf("sync database struct error: %v", err) + } + if err = orm.Sync(tables...); err != nil { + return fmt.Errorf("sync database struct error: %v\n", err) } return nil } diff --git a/models/oauth2.go b/models/oauth2.go index 70dcd510..45728b0d 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -1,6 +1,10 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package models -import "time" +import "errors" // OT: Oauth2 Type const ( @@ -9,10 +13,37 @@ 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 - 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"` + Id int64 + Uid int64 // 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"` +} + +func AddOauth2(oa *Oauth2) (err error) { + if _, err = orm.Insert(oa); err != nil { + return err + } + return nil +} + +func GetOauth2(identity string) (oa *Oauth2, err error) { + oa = &Oauth2{Identity: identity} + isExist, err := orm.Get(oa) + if err != nil { + return + } else if !isExist { + return nil, ErrOauth2RecordNotExists + } else if oa.Uid == 0 { + return oa, ErrOauth2NotAssociatedWithUser + } + oa.User, err = GetUserById(oa.Uid) + return oa, err } diff --git a/models/publickey.go b/models/publickey.go index 42d2523b..ed47ff20 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -77,8 +77,8 @@ func init() { // PublicKey represents a SSH key of user. type PublicKey struct { Id int64 - OwnerId int64 `xorm:" index not null"` - Name string `xorm:" not null"` //UNIQUE(s) + OwnerId int64 `xorm:"unique(s) index not null"` + Name string `xorm:"unique(s) not null"` Fingerprint string Content string `xorm:"TEXT not null"` Created time.Time `xorm:"created"` diff --git a/models/repo.go b/models/repo.go index e8ebce92..573e0f4e 100644 --- a/models/repo.go +++ b/models/repo.go @@ -79,6 +79,7 @@ type Repository struct { NumOpenIssues int `xorm:"-"` IsPrivate bool IsBare bool + IsGoget bool Created time.Time `xorm:"created"` Updated time.Time `xorm:"updated"` } @@ -138,11 +139,8 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv IsPrivate: private, IsBare: repoLang == "" && license == "" && !initReadme, } - repoPath := RepoPath(user.Name, repoName) - if err = initRepository(repoPath, user, repo, initReadme, repoLang, license); err != nil { - return nil, err - } + sess := orm.NewSession() defer sess.Close() sess.Begin() @@ -207,6 +205,10 @@ func CreateRepository(user *User, repoName, desc, repoLang, license string, priv log.Error("repo.CreateRepository(WatchRepo): %v", err) } + if err = initRepository(repoPath, user, repo, initReadme, repoLang, license); err != nil { + return nil, err + } + return repo, nil } @@ -260,6 +262,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) @@ -332,6 +341,8 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return nil } + SetRepoEnvs(user.Id, user.Name, repo.Name) + // Apply changes and commit. return initRepoCommit(tmpDir, user.NewGitSig()) } @@ -381,45 +392,62 @@ func TransferOwnership(user *User, newOwner string, repo *Repository) (err error if err = orm.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repo.LowerName}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].RepoName = newUser.LowerName + "/" + repo.LowerName if accesses[i].UserName == user.LowerName { accesses[i].UserName = newUser.LowerName } - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } // Update repository. repo.OwnerId = newUser.Id - if _, err := orm.Id(repo.Id).Update(repo); err != nil { + if _, err := sess.Id(repo.Id).Update(repo); err != nil { + sess.Rollback() return err } // Update user repository number. rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = orm.Exec(rawSql, newUser.Id); err != nil { + if _, err = sess.Exec(rawSql, newUser.Id); err != nil { + sess.Rollback() return err } rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" - if _, err = orm.Exec(rawSql, user.Id); err != nil { + if _, err = sess.Exec(rawSql, user.Id); err != nil { + sess.Rollback() return err } // Add watch of new owner to repository. if !IsWatching(newUser.Id, repo.Id) { if err = WatchRepo(newUser.Id, repo.Id, true); err != nil { + sess.Rollback() return err } } if err = TransferRepoAction(user, newUser, repo); err != nil { + sess.Rollback() return err } // Change repository directory name. - return os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)) + if err = os.Rename(RepoPath(user.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } // ChangeRepositoryName changes all corresponding setting from old repository name to new one. @@ -429,15 +457,27 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error) if err = orm.Find(&accesses, &Access{RepoName: strings.ToLower(userName + "/" + oldRepoName)}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].RepoName = userName + "/" + newRepoName - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } // Change repository directory name. - return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)) + if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } func UpdateRepository(repo *Repository) error { diff --git a/models/user.go b/models/user.go index 2641a15f..b2fddd0a 100644 --- a/models/user.go +++ b/models/user.go @@ -5,6 +5,7 @@ package models import ( + "crypto/sha256" "encoding/hex" "errors" "fmt" @@ -13,8 +14,6 @@ import ( "strings" "time" - "github.com/dchest/scrypt" - "github.com/gogits/git" "github.com/gogits/gogs/modules/base" @@ -62,6 +61,7 @@ type User struct { IsActive bool IsAdmin bool Rands string `xorm:"VARCHAR(10)"` + Salt string `xorm:"VARCHAR(10)"` Created time.Time `xorm:"created"` Updated time.Time `xorm:"updated"` } @@ -89,10 +89,9 @@ func (user *User) NewGitSig() *git.Signature { } // EncodePasswd encodes password to safe format. -func (user *User) EncodePasswd() error { - newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte(base.SecretKey), 16384, 8, 1, 64) +func (user *User) EncodePasswd() { + newPasswd := base.PBKDF2([]byte(user.Passwd), []byte(user.Salt), 10000, 50, sha256.New) user.Passwd = fmt.Sprintf("%x", newPasswd) - return err } // Member represents user is member of organization. @@ -148,9 +147,9 @@ func RegisterUser(user *User) (*User, error) { user.Avatar = base.EncodeMd5(user.Email) user.AvatarEmail = user.Email user.Rands = GetUserSalt() - if err = user.EncodePasswd(); err != nil { - return nil, err - } else if _, err = orm.Insert(user); err != nil { + user.Salt = GetUserSalt() + user.EncodePasswd() + if _, err = orm.Insert(user); err != nil { return nil, err } else if err = os.MkdirAll(UserPath(user.Name), os.ModePerm); err != nil { if _, err := orm.Id(user.Id).Delete(&User{}); err != nil { @@ -218,11 +217,18 @@ func ChangeUserName(user *User, newUserName string) (err error) { if err = orm.Find(&accesses, &Access{UserName: user.LowerName}); err != nil { return err } + + sess := orm.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + for i := range accesses { accesses[i].UserName = newUserName if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") { accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1) - if err = UpdateAccess(&accesses[i]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { return err } } @@ -241,14 +247,19 @@ func ChangeUserName(user *User, newUserName string) (err error) { for j := range accesses { accesses[j].RepoName = newUserName + "/" + repos[i].LowerName - if err = UpdateAccess(&accesses[j]); err != nil { + if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil { return err } } } // Change user directory name. - return os.Rename(UserPath(user.LowerName), UserPath(newUserName)) + if err = os.Rename(UserPath(user.LowerName), UserPath(newUserName)); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() } // UpdateUser updates user's information. @@ -278,11 +289,21 @@ func DeleteUser(user *User) error { // TODO: check issues, other repos' commits + // Delete all followers. + if _, err = orm.Delete(&Follow{FollowId: user.Id}); err != nil { + return err + } + // Delete all feeds. if _, err = orm.Delete(&Action{UserId: user.Id}); err != nil { return err } + // Delete all watches. + if _, err = orm.Delete(&Watch{UserId: user.Id}); err != nil { + return err + } + // Delete all accesses. if _, err = orm.Delete(&Access{UserName: user.LowerName}); err != nil { return err @@ -305,7 +326,6 @@ func DeleteUser(user *User) error { } _, err = orm.Delete(user) - // TODO: delete and update follower information. return err } @@ -355,20 +375,50 @@ func GetUserByName(name string) (*User, error) { return user, nil } -// LoginUserPlain validates user by raw user name and password. -func LoginUserPlain(name, passwd string) (*User, error) { - user := User{LowerName: strings.ToLower(name), Passwd: passwd} - if err := user.EncodePasswd(); err != nil { +// GetUserEmailsByNames returns a slice of e-mails corresponds to names. +func GetUserEmailsByNames(names []string) []string { + mails := make([]string, 0, len(names)) + for _, name := range names { + u, err := GetUserByName(name) + if err != nil { + continue + } + mails = append(mails, u.Email) + } + return mails +} + +// GetUserByEmail returns the user object by given e-mail if exists. +func GetUserByEmail(email string) (*User, error) { + if len(email) == 0 { + return nil, ErrUserNotExist + } + user := &User{Email: strings.ToLower(email)} + has, err := orm.Get(user) + if err != nil { return nil, err + } else if !has { + return nil, ErrUserNotExist } + return user, nil +} +// LoginUserPlain validates user by raw user name and password. +func LoginUserPlain(name, passwd string) (*User, error) { + user := User{LowerName: strings.ToLower(name)} has, err := orm.Get(&user) if err != nil { return nil, err } else if !has { - err = ErrUserNotExist + return nil, ErrUserNotExist + } + + newUser := &User{Passwd: passwd, Salt: user.Salt} + newUser.EncodePasswd() + if user.Passwd != newUser.Passwd { + return nil, ErrUserNotExist } - return &user, err + return &user, nil } // Follow is connection request for receiving user notifycation. diff --git a/modules/base/conf.go b/modules/base/conf.go index 3ebc4ede..871595e4 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -14,6 +14,7 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/goconfig" + qlog "github.com/qiniu/log" "github.com/gogits/cache" "github.com/gogits/session" @@ -21,18 +22,28 @@ import ( "github.com/gogits/gogs/modules/log" ) -// Mailer represents a mail service. +// Mailer represents mail service. type Mailer struct { Name string Host string User, Passwd string } +// Oauther represents oauth service. +type Oauther struct { + GitHub struct { + Enabled bool + ClientId, ClientSecret string + Scopes string + } +} + var ( AppVer string AppName string AppLogo string AppUrl string + IsProdMode bool Domain string SecretKey string RunUser string @@ -44,8 +55,9 @@ var ( CookieUserName string CookieRememberName string - Cfg *goconfig.ConfigFile - MailService *Mailer + Cfg *goconfig.ConfigFile + MailService *Mailer + OauthService *Oauther LogMode string LogConfig string @@ -105,16 +117,14 @@ func newLogService() { LogMode = Cfg.MustValue("log", "MODE", "console") modeSec := "log." + LogMode if _, err := Cfg.GetSection(modeSec); err != nil { - fmt.Printf("Unknown log mode: %s\n", LogMode) - os.Exit(2) + qlog.Fatalf("Unknown log mode: %s\n", LogMode) } // Log level. levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace") level, ok := logLevels[levelName] if !ok { - fmt.Printf("Unknown log level: %s\n", levelName) - os.Exit(2) + qlog.Fatalf("Unknown log level: %s\n", levelName) } // Generate log configuration. @@ -151,6 +161,7 @@ func newLogService() { Cfg.MustValue(modeSec, "CONN")) } + log.Info("%s %s", AppName, AppVer) log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), LogMode, LogConfig) log.Info("Log Mode: %s(%s)", strings.Title(LogMode), levelName) } @@ -164,16 +175,14 @@ func newCacheService() { case "redis", "memcache": CacheConfig = fmt.Sprintf(`{"conn":"%s"}`, Cfg.MustValue("cache", "HOST")) default: - fmt.Printf("Unknown cache adapter: %s\n", CacheAdapter) - os.Exit(2) + qlog.Fatalf("Unknown cache adapter: %s\n", CacheAdapter) } var err error Cache, err = cache.NewCache(CacheAdapter, CacheConfig) if err != nil { - fmt.Printf("Init cache system failed, adapter: %s, config: %s, %v\n", + qlog.Fatalf("Init cache system failed, adapter: %s, config: %s, %v\n", CacheAdapter, CacheConfig, err) - os.Exit(2) } log.Info("Cache Service Enabled") @@ -199,9 +208,8 @@ func newSessionService() { var err error SessionManager, err = session.NewManager(SessionProvider, *SessionConfig) if err != nil { - fmt.Printf("Init session system failed, provider: %s, %v\n", + qlog.Fatalf("Init session system failed, provider: %s, %v\n", SessionProvider, err) - os.Exit(2) } log.Info("Session Service Enabled") @@ -209,15 +217,17 @@ func newSessionService() { func newMailService() { // Check mailer setting. - if Cfg.MustBool("mailer", "ENABLED") { - MailService = &Mailer{ - Name: Cfg.MustValue("mailer", "NAME", AppName), - Host: Cfg.MustValue("mailer", "HOST"), - User: Cfg.MustValue("mailer", "USER"), - Passwd: Cfg.MustValue("mailer", "PASSWD"), - } - log.Info("Mail Service Enabled") + if !Cfg.MustBool("mailer", "ENABLED") { + return + } + + MailService = &Mailer{ + Name: Cfg.MustValue("mailer", "NAME", AppName), + Host: Cfg.MustValue("mailer", "HOST"), + User: Cfg.MustValue("mailer", "USER"), + Passwd: Cfg.MustValue("mailer", "PASSWD"), } + log.Info("Mail Service Enabled") } func newRegisterMailService() { @@ -242,27 +252,44 @@ func newNotifyMailService() { log.Info("Notify Mail Service Enabled") } +func newOauthService() { + if !Cfg.MustBool("oauth", "ENABLED") { + return + } + + OauthService = &Oauther{} + oauths := make([]string, 0, 10) + + // GitHub. + if Cfg.MustBool("oauth.github", "ENABLED") { + OauthService.GitHub.Enabled = true + OauthService.GitHub.ClientId = Cfg.MustValue("oauth.github", "CLIENT_ID") + OauthService.GitHub.ClientSecret = Cfg.MustValue("oauth.github", "CLIENT_SECRET") + OauthService.GitHub.Scopes = Cfg.MustValue("oauth.github", "SCOPES") + oauths = append(oauths, "GitHub") + } + + log.Info("Oauth Service Enabled %s", oauths) +} + func NewConfigContext() { //var err error workDir, err := ExecDir() if err != nil { - fmt.Printf("Fail to get work directory: %s\n", err) - os.Exit(2) + qlog.Fatalf("Fail to get work directory: %s\n", err) } cfgPath := filepath.Join(workDir, "conf/app.ini") Cfg, err = goconfig.LoadConfigFile(cfgPath) if err != nil { - fmt.Printf("Cannot load config file(%s): %v\n", cfgPath, err) - os.Exit(2) + qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err) } Cfg.BlockMode = false 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): %v\n", cfgPath, err) - os.Exit(2) + qlog.Fatalf("Cannot load config file(%s): %v\n", cfgPath, err) } } @@ -281,8 +308,7 @@ func NewConfigContext() { } // 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) + qlog.Fatalf("Expect user(%s) but current user is: %s\n", RunUser, curUser) } LogInRememberDays = Cfg.MustInt("security", "LOGIN_REMEMBER_DAYS") @@ -294,13 +320,11 @@ func NewConfigContext() { // Determine and create root git reposiroty path. homeDir, err := com.HomeDir() if err != nil { - fmt.Printf("Fail to get home directory): %v\n", err) - os.Exit(2) + qlog.Fatalf("Fail to get home directory): %v\n", err) } - RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "git/gogs-repositories")) + RepoRootPath = Cfg.MustValue("repository", "ROOT", filepath.Join(homeDir, "gogs-repositories")) if err = os.MkdirAll(RepoRootPath, os.ModePerm); err != nil { - fmt.Printf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err) - os.Exit(2) + qlog.Fatalf("Fail to create RepoRootPath(%s): %v\n", RepoRootPath, err) } } @@ -312,4 +336,5 @@ func NewServices() { newMailService() newRegisterMailService() newNotifyMailService() + newOauthService() } diff --git a/modules/base/markdown.go b/modules/base/markdown.go index 962e1ae1..cc180775 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -6,9 +6,11 @@ package base import ( "bytes" + "fmt" "net/http" "path" "path/filepath" + "regexp" "strings" "github.com/gogits/gfm" @@ -87,13 +89,58 @@ func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, options.Renderer.Link(out, link, title, content) } +var ( + MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`) + commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) + issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) + issueIndexPattern = regexp.MustCompile(`#[0-9]+`) +) + +func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte { + ms := MentionPattern.FindAll(rawBytes, -1) + for _, m := range ms { + rawBytes = bytes.Replace(rawBytes, m, + []byte(fmt.Sprintf(`<a href="/user/%s">%s</a>`, m[1:], m)), -1) + } + ms = commitPattern.FindAll(rawBytes, -1) + for _, m := range ms { + m = bytes.TrimSpace(m) + i := strings.Index(string(m), "commit/") + j := strings.Index(string(m), "#") + if j == -1 { + j = len(m) + } + rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( + ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1) + } + ms = issueFullPattern.FindAll(rawBytes, -1) + for _, m := range ms { + m = bytes.TrimSpace(m) + i := strings.Index(string(m), "issues/") + j := strings.Index(string(m), "#") + if j == -1 { + j = len(m) + } + rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( + ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1) + } + ms = issueIndexPattern.FindAll(rawBytes, -1) + for _, m := range ms { + rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( + `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1) + } + return rawBytes +} + func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { + body := RenderSpecialLink(rawBytes, urlPrefix) + // fmt.Println(string(body)) htmlFlags := 0 // htmlFlags |= gfm.HTML_USE_XHTML // htmlFlags |= gfm.HTML_USE_SMARTYPANTS // htmlFlags |= gfm.HTML_SMARTYPANTS_FRACTIONS // htmlFlags |= gfm.HTML_SMARTYPANTS_LATEX_DASHES - htmlFlags |= gfm.HTML_SKIP_HTML + // htmlFlags |= gfm.HTML_SKIP_HTML htmlFlags |= gfm.HTML_SKIP_STYLE htmlFlags |= gfm.HTML_SKIP_SCRIPT htmlFlags |= gfm.HTML_GITHUB_BLOCKCODE @@ -115,7 +162,7 @@ func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { extensions |= gfm.EXTENSION_SPACE_HEADERS extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK - body := gfm.Markdown(rawBytes, renderer, extensions) - + body = gfm.Markdown(body, renderer, extensions) + // fmt.Println(string(body)) return body } diff --git a/modules/base/template.go b/modules/base/template.go index dfcae931..5a42107c 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -5,7 +5,9 @@ package base import ( + "bytes" "container/list" + "encoding/json" "fmt" "html/template" "strings" @@ -54,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" }, @@ -67,6 +72,10 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DateFormat": DateFormat, "List": List, "Mail2Domain": func(mail string) string { + if !strings.Contains(mail, "@") { + return "try.gogits.org" + } + suffix := strings.SplitN(mail, "@", 2)[1] domain, ok := mailDomains[suffix] if !ok { @@ -81,3 +90,107 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DiffLineTypeToStr": DiffLineTypeToStr, "ShortSha": ShortSha, } + +type Actioner interface { + GetOpType() int + GetActUserName() string + GetActEmail() string + GetRepoName() string + GetBranch() string + GetContent() string +} + +// ActionIcon accepts a int that represents action operation type +// and returns a icon class name. +func ActionIcon(opType int) string { + switch opType { + case 1: // Create repository. + return "plus-circle" + case 5: // Commit repository. + return "arrow-circle-o-right" + case 6: // Create issue. + return "exclamation-circle" + case 8: // Transfer repository. + return "share" + default: + return "invalid type" + } +} + +const ( + TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>` + TPL_COMMIT_REPO = `<a href="/user/%s">%s</a> pushed to <a href="/%s/src/%s">%s</a> at <a href="/%s">%s</a>%s` + TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s">%s</a> %s</div>` + TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a> +<div><img src="%s?s=16" alt="user-avatar"/> %s</div>` + TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>` +) + +type PushCommit struct { + Sha1 string + Message string + AuthorEmail string + AuthorName string +} + +type PushCommits struct { + Len int + Commits []*PushCommit +} + +// ActionDesc accepts int that represents action operation type +// and returns the description. +func ActionDesc(act Actioner) string { + actUserName := act.GetActUserName() + email := act.GetActEmail() + repoName := act.GetRepoName() + repoLink := actUserName + "/" + repoName + branch := act.GetBranch() + content := act.GetContent() + switch act.GetOpType() { + case 1: // Create repository. + return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName) + case 5: // Commit repository. + var push *PushCommits + if err := json.Unmarshal([]byte(content), &push); err != nil { + return err.Error() + } + buf := bytes.NewBuffer([]byte("\n")) + for _, commit := range push.Commits { + 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)) + } + return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink, + buf.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(email), infos[1]) + case 8: // Transfer repository. + newRepoLink := content + "/" + repoName + return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) + default: + return "invalid type" + } +} + +func DiffTypeToStr(diffType int) string { + diffTypes := map[int]string{ + 1: "add", 2: "modify", 3: "del", + } + return diffTypes[diffType] +} + +func DiffLineTypeToStr(diffType int) string { + switch diffType { + case 2: + return "add" + case 3: + return "del" + case 4: + return "tag" + } + return "same" +} diff --git a/modules/base/tool.go b/modules/base/tool.go index 3946c4b5..0f06b3e0 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -5,13 +5,13 @@ package base import ( - "bytes" + "crypto/hmac" "crypto/md5" "crypto/rand" "crypto/sha1" "encoding/hex" - "encoding/json" "fmt" + "hash" "math" "strconv" "strings" @@ -40,6 +40,44 @@ func GetRandomString(n int, alphabets ...byte) string { return string(bytes) } +// http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto +func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte { + prf := hmac.New(h, password) + hashLen := prf.Size() + numBlocks := (keyLen + hashLen - 1) / hashLen + + var buf [4]byte + dk := make([]byte, 0, numBlocks*hashLen) + U := make([]byte, hashLen) + for block := 1; block <= numBlocks; block++ { + // N.B.: || means concatenation, ^ means XOR + // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter + // U_1 = PRF(password, salt || uint(i)) + prf.Reset() + prf.Write(salt) + buf[0] = byte(block >> 24) + buf[1] = byte(block >> 16) + buf[2] = byte(block >> 8) + buf[3] = byte(block) + prf.Write(buf[:4]) + dk = prf.Sum(dk) + T := dk[len(dk)-hashLen:] + copy(U, T) + + // U_n = PRF(password, U_(n-1)) + for n := 2; n <= iter; n++ { + prf.Reset() + prf.Write(U) + U = U[:0] + U = prf.Sum(U) + for x := range U { + T[x] ^= U[x] + } + } + } + return dk[:keyLen] +} + // verify time limit code func VerifyTimeLimitCode(data string, minutes int, code string) bool { if len(code) <= 18 { @@ -474,107 +512,3 @@ func (a argInt) Get(i int, args ...int) (r int) { } return } - -type Actioner interface { - GetOpType() int - GetActUserName() string - GetActEmail() string - GetRepoName() string - GetBranch() string - GetContent() string -} - -// ActionIcon accepts a int that represents action operation type -// and returns a icon class name. -func ActionIcon(opType int) string { - switch opType { - case 1: // Create repository. - return "plus-circle" - case 5: // Commit repository. - return "arrow-circle-o-right" - case 6: // Create issue. - return "exclamation-circle" - case 8: // Transfer repository. - return "share" - default: - return "invalid type" - } -} - -const ( - TPL_CREATE_REPO = `<a href="/user/%s">%s</a> created repository <a href="/%s">%s</a>` - TPL_COMMIT_REPO = `<a href="/user/%s">%s</a> pushed to <a href="/%s/src/%s">%s</a> at <a href="/%s">%s</a>%s` - TPL_COMMIT_REPO_LI = `<div><img src="%s?s=16" alt="user-avatar"/> <a href="/%s/commit/%s">%s</a> %s</div>` - TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a> -<div><img src="%s?s=16" alt="user-avatar"/> %s</div>` - TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>` -) - -type PushCommit struct { - Sha1 string - Message string - AuthorEmail string - AuthorName string -} - -type PushCommits struct { - Len int - Commits []*PushCommit -} - -// ActionDesc accepts int that represents action operation type -// and returns the description. -func ActionDesc(act Actioner) string { - actUserName := act.GetActUserName() - email := act.GetActEmail() - repoName := act.GetRepoName() - repoLink := actUserName + "/" + repoName - branch := act.GetBranch() - content := act.GetContent() - switch act.GetOpType() { - case 1: // Create repository. - return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName) - case 5: // Commit repository. - var push *PushCommits - if err := json.Unmarshal([]byte(content), &push); err != nil { - return err.Error() - } - buf := bytes.NewBuffer([]byte("\n")) - for _, commit := range push.Commits { - 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)) - } - return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink, - buf.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(email), infos[1]) - case 8: // Transfer repository. - newRepoLink := content + "/" + repoName - return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) - default: - return "invalid type" - } -} - -func DiffTypeToStr(diffType int) string { - diffTypes := map[int]string{ - 1: "add", 2: "modify", 3: "del", - } - return diffTypes[diffType] -} - -func DiffLineTypeToStr(diffType int) string { - switch diffType { - case 2: - return "add" - case 3: - return "del" - case 4: - return "tag" - } - return "same" -} diff --git a/modules/log/log.go b/modules/log/log.go index 65150237..f21897b9 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -21,8 +21,6 @@ func init() { 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/mailer/mail.go b/modules/mailer/mail.go index b99fc8fd..d2bf1310 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -86,16 +86,36 @@ func SendActiveMail(r *middleware.Render, user *models.User) { } msg := NewMailMessage([]string{user.Email}, subject, body) - msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id) + msg.Info = fmt.Sprintf("UID: %d, send active mail", user.Id) SendAsync(&msg) } -// SendNotifyMail sends mail notification of all watchers. -func SendNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) error { +// Send reset password email. +func SendResetPasswdMail(r *middleware.Render, user *models.User) { + code := CreateUserActiveCode(user, nil) + + subject := "Reset your password" + + data := GetMailTmplData(user) + data["Code"] = code + body, err := r.HTMLString("mail/auth/reset_passwd", data) + if err != nil { + log.Error("mail.SendResetPasswdMail(fail to render): %v", err) + return + } + + msg := NewMailMessage([]string{user.Email}, subject, body) + msg.Info = fmt.Sprintf("UID: %d, send reset password email", user.Id) + + SendAsync(&msg) +} + +// SendIssueNotifyMail sends mail notification of all watchers of repository. +func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) { watches, err := models.GetWatches(repo.Id) if err != nil { - return errors.New("mail.NotifyWatchers(get watches): " + err.Error()) + return nil, errors.New("mail.NotifyWatchers(get watches): " + err.Error()) } tos := make([]string, 0, len(watches)) @@ -106,20 +126,37 @@ func SendNotifyMail(user, owner *models.User, repo *models.Repository, issue *mo } u, err := models.GetUserById(uid) if err != nil { - return errors.New("mail.NotifyWatchers(get user): " + err.Error()) + return nil, errors.New("mail.NotifyWatchers(get user): " + err.Error()) } tos = append(tos, u.Email) } if len(tos) == 0 { - return nil + return tos, nil } subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name) content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.", - issue.Content, base.AppUrl, owner.Name, repo.Name, issue.Index) + base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name), + base.AppUrl, owner.Name, repo.Name, issue.Index) + msg := NewMailMessageFrom(tos, user.Name, subject, content) + msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject) + SendAsync(&msg) + return tos, nil +} + +// SendIssueMentionMail sends mail notification for who are mentioned in issue. +func SendIssueMentionMail(user, owner *models.User, repo *models.Repository, issue *models.Issue, tos []string) error { + if len(tos) == 0 { + return nil + } + + issueLink := fmt.Sprintf("%s%s/%s/issues/%d", base.AppUrl, owner.Name, repo.Name, issue.Index) + body := fmt.Sprintf(`%s mentioned you.`) + subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name) + content := fmt.Sprintf("%s<br>-<br> <a href=\"%s\">View it on Gogs</a>.", body, issueLink) msg := NewMailMessageFrom(tos, user.Name, subject, content) - msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject) + msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject) SendAsync(&msg) return nil } diff --git a/modules/middleware/render.go b/modules/middleware/render.go index 98d485af..66289988 100644 --- a/modules/middleware/render.go +++ b/modules/middleware/render.go @@ -146,7 +146,7 @@ func compile(options RenderOptions) *template.Template { tmpl := t.New(filepath.ToSlash(name)) for _, funcs := range options.Funcs { - tmpl.Funcs(funcs) + tmpl = tmpl.Funcs(funcs) } template.Must(tmpl.Funcs(helperFuncs).Parse(string(buf))) diff --git a/modules/oauth2/oauth2.go b/modules/oauth2/oauth2.go index 088d65dd..05ae4606 100644 --- a/modules/oauth2/oauth2.go +++ b/modules/oauth2/oauth2.go @@ -1,16 +1,7 @@ // Copyright 2014 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. // Package oauth2 contains Martini handlers to provide // user login via an OAuth 2.0 backend. @@ -26,13 +17,16 @@ import ( "code.google.com/p/goauth2/oauth" "github.com/go-martini/martini" - "github.com/martini-contrib/sessions" + + "github.com/gogits/session" + + "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/middleware" ) const ( - codeRedirect = 302 - keyToken = "oauth2_token" - keyNextPage = "next" + keyToken = "oauth2_token" + keyNextPage = "next" ) var ( @@ -142,23 +136,23 @@ func NewOAuth2Provider(opts *Options) martini.Handler { Transport: http.DefaultTransport, } - return func(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) { - if r.Method == "GET" { - switch r.URL.Path { + return func(c martini.Context, ctx *middleware.Context) { + if ctx.Req.Method == "GET" { + switch ctx.Req.URL.Path { case PathLogin: - login(transport, s, w, r) + login(transport, ctx) case PathLogout: - logout(transport, s, w, r) + logout(transport, ctx) case PathCallback: - handleOAuth2Callback(transport, s, w, r) + handleOAuth2Callback(transport, ctx) } } - tk := unmarshallToken(s) + tk := unmarshallToken(ctx.Session) if tk != nil { // check if the access token is expired if tk.IsExpired() && tk.Refresh() == "" { - s.Delete(keyToken) + ctx.Session.Delete(keyToken) tk = nil } } @@ -172,49 +166,56 @@ func NewOAuth2Provider(opts *Options) martini.Handler { // Sample usage: // m.Get("/login-required", oauth2.LoginRequired, func() ... {}) var LoginRequired martini.Handler = func() martini.Handler { - return func(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) { - token := unmarshallToken(s) + return func(c martini.Context, ctx *middleware.Context) { + token := unmarshallToken(ctx.Session) if token == nil || token.IsExpired() { - next := url.QueryEscape(r.URL.RequestURI()) - http.Redirect(w, r, PathLogin+"?next="+next, codeRedirect) + next := url.QueryEscape(ctx.Req.URL.RequestURI()) + ctx.Redirect(PathLogin + "?next=" + next) + return } } }() -func login(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) { - next := extractPath(r.URL.Query().Get(keyNextPage)) - if s.Get(keyToken) == nil { +func login(t *oauth.Transport, ctx *middleware.Context) { + next := extractPath(ctx.Query(keyNextPage)) + if ctx.Session.Get(keyToken) == nil { // User is not logged in. - http.Redirect(w, r, t.Config.AuthCodeURL(next), codeRedirect) + ctx.Redirect(t.Config.AuthCodeURL(next)) return } // No need to login, redirect to the next page. - http.Redirect(w, r, next, codeRedirect) + ctx.Redirect(next) } -func logout(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) { - next := extractPath(r.URL.Query().Get(keyNextPage)) - s.Delete(keyToken) - http.Redirect(w, r, next, codeRedirect) +func logout(t *oauth.Transport, ctx *middleware.Context) { + next := extractPath(ctx.Query(keyNextPage)) + ctx.Session.Delete(keyToken) + ctx.Redirect(next) } -func handleOAuth2Callback(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) { - next := extractPath(r.URL.Query().Get("state")) - code := r.URL.Query().Get("code") +func handleOAuth2Callback(t *oauth.Transport, ctx *middleware.Context) { + if errMsg := ctx.Query("error_description"); len(errMsg) > 0 { + log.Error("oauth2.handleOAuth2Callback: %s", errMsg) + return + } + + next := extractPath(ctx.Query("state")) + code := ctx.Query("code") tk, err := t.Exchange(code) if err != nil { // Pass the error message, or allow dev to provide its own // error handler. - http.Redirect(w, r, PathError, codeRedirect) + log.Error("oauth2.handleOAuth2Callback(token.Exchange): %v", err) + // ctx.Redirect(PathError) return } // Store the credentials in the session. val, _ := json.Marshal(tk) - s.Set(keyToken, val) - http.Redirect(w, r, next, codeRedirect) + ctx.Session.Set(keyToken, val) + ctx.Redirect(next) } -func unmarshallToken(s sessions.Session) (t *token) { +func unmarshallToken(s session.SessionStore) (t *token) { if s.Get(keyToken) == nil { return } diff --git a/public/css/gogs.css b/public/css/gogs.css index a6d6b4cc..2850d15e 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, @@ -444,6 +456,43 @@ html, body { margin-right: 1em; } +#user-dashboard-repo-new .btn-sm.dropdown-toggle { + padding: 3px 8px; +} + +#user-dashboard-repo-new .dropdown-menu, #nav-repo-new .dropdown-menu { + padding: 0; + margin: 0; +} + +#user-dashboard-repo-new ul, #nav-repo-new ul { + margin: 0; + width: 200px; +} + +#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, #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 { @@ -1304,4 +1353,74 @@ html, body { #release .release-item .info .avatar { vertical-align: middle; +} + +#release-new-form { + margin-top: 24px; +} + +#release-new-form .target-at { + margin: 0 1em; +} + +#release-new-form .target-text { + color: #888; +} + +#release-new-target-branch-list { + padding-top: 0; + padding-bottom: 0; + min-width: 200px; +} + +#release-new-target-branch-list ul { + margin-bottom: 0; +} + +#release-new-target-branch-list li { + padding: 8px 20px; +} + +#release-new-target-branch-list li a { + margin-left: 0; + background-color: transparent; + padding: 0; +} + +#release-new-target-branch-list li a:hover { + background-image: none; +} + +#release-new-target-branch-list li:hover { + background-color: #0093c4; +} + +#release-new-target-branch-list li:hover a { + color: #FFF; +} + +#release-new-title { + width: 50%; +} + +#release-new-content-div { + margin-top: 16px; + padding-left: 0; +} + +#release-new-content-div .md-help { + margin-top: 6px; +} + +#release-textarea .form-group { + display: block; +} + +#release-new-content { + width: 100%; + margin: 16px 0; +} + +#release-preview { + margin: 6px 0; }
\ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index 0ba0675f..059663e1 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -354,6 +354,7 @@ function initRegister() { } function initUserSetting() { + // ssh confirmation $('#ssh-keys .delete').confirmation({ singleton: true, onConfirm: function (e, $this) { @@ -366,6 +367,18 @@ function initUserSetting() { }); } }); + + // profile form + (function () { + $('#user-setting-username').on("keyup", function () { + var $this = $(this); + if ($this.val() != $this.attr('title')) { + $this.next('.help-block').toggleShow(); + } else { + $this.next('.help-block').toggleHide(); + } + }); + }()) } function initRepository() { @@ -383,7 +396,7 @@ function initRepository() { $clone.find('span.clone-url').text($this.data('link')); } }).eq(0).trigger("click"); - $("#repo-clone").on("shown.bs.dropdown",function () { + $("#repo-clone").on("shown.bs.dropdown", function () { Gogits.bindCopy("[data-init=copy]"); }); Gogits.bindCopy("[data-init=copy]:visible"); @@ -438,6 +451,18 @@ function initRepository() { $item.find(".bar .add").css("width", addPercent + "%"); }); }()); + + // repo setting form + (function () { + $('#repo-setting-name').on("keyup", function () { + var $this = $(this); + if ($this.val() != $this.attr('title')) { + $this.next('.help-block').toggleShow(); + } else { + $this.next('.help-block').toggleHide(); + } + }); + }()) } function initInstall() { @@ -520,6 +545,23 @@ function initIssue() { } +function initRelease() { +// release new ajax preview + (function () { + $('[data-ajax-name=release-preview]').on("click", function () { + var $this = $(this); + $this.toggleAjax(function (json) { + if (json.ok) { + $($this.data("preview")).html(json.content); + } + }) + }); + $('.release-write a[data-toggle]').on("click", function () { + $('.release-preview-content').html("loading..."); + }); + }()) +} + (function ($) { $(function () { initCore(); @@ -539,5 +581,8 @@ function initIssue() { if ($('#issue').length) { initIssue(); } + if ($('#release').length) { + initRelease(); + } }); })(jQuery); diff --git a/routers/api/v1/miscellaneous.go b/routers/api/v1/miscellaneous.go index 0ff1eb04..babdfce9 100644 --- a/routers/api/v1/miscellaneous.go +++ b/routers/api/v1/miscellaneous.go @@ -13,6 +13,6 @@ func Markdown(ctx *middleware.Context) { content := ctx.Query("content") ctx.Render.JSON(200, map[string]interface{}{ "ok": true, - "content": string(base.RenderMarkdown([]byte(content), "")), + "content": string(base.RenderMarkdown([]byte(content), ctx.Query("repoLink"))), }) } diff --git a/routers/install.go b/routers/install.go index 032af480..5d6c65ef 100644 --- a/routers/install.go +++ b/routers/install.go @@ -6,13 +6,14 @@ package routers import ( "errors" - "fmt" "os" + "os/exec" "strings" "github.com/Unknwon/goconfig" "github.com/go-martini/martini" "github.com/lunny/xorm" + qlog "github.com/qiniu/log" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -27,6 +28,7 @@ func checkRunMode() { switch base.Cfg.MustValue("", "RUN_MODE") { case "prod": martini.Env = martini.Prod + base.IsProdMode = true case "test": martini.Env = martini.Test } @@ -43,8 +45,7 @@ func GlobalInit() { if base.InstallLock { if err := models.NewEngine(); err != nil { - fmt.Println(err) - os.Exit(2) + qlog.Fatal(err) } models.HasEngine = true @@ -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"} @@ -183,6 +189,7 @@ func Install(ctx *middleware.Context, form auth.InstallForm) { if _, err := models.RegisterUser(&models.User{Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, IsAdmin: true, IsActive: true}); err != nil { if err != models.ErrUserAlreadyExist { + base.InstallLock = false ctx.RenderWithErr("Admin account setting is invalid: "+err.Error(), "install", &form) return } diff --git a/routers/repo/git.go b/routers/repo/git.go new file mode 100644 index 00000000..30c1042e --- /dev/null +++ b/routers/repo/git.go @@ -0,0 +1,55 @@ +package repo + +import ( + "fmt" + "strings" +) + +const advertise_refs = "--advertise-refs" + +func command(cmd string, opts ...string) string { + return fmt.Sprintf("git %s %s", cmd, strings.Join(opts, " ")) +} + +/*func upload_pack(repository_path string, opts ...string) string { + cmd = "upload-pack" + opts = append(opts, "--stateless-rpc", repository_path) + return command(cmd, opts...) +} + +func receive_pack(repository_path string, opts ...string) string { + cmd = "receive-pack" + opts = append(opts, "--stateless-rpc", repository_path) + return command(cmd, opts...) +}*/ + +/*func update_server_info(repository_path, opts = {}, &block) + cmd = "update-server-info" + args = [] + opts.each {|k,v| args << command_options[k] if command_options.has_key?(k) } + opts[:args] = args + Dir.chdir(repository_path) do # "git update-server-info" does not take a parameter to specify the repository, so set the working directory to the repository + self.command(cmd, opts, &block) + end + end + + def get_config_setting(repository_path, key) + path = get_config_location(repository_path) + raise "Config file could not be found for repository in #{repository_path}." unless path + self.command("config", {:args => ["-f #{path}", key]}).chomp + end + + def get_config_location(repository_path) + non_bare = File.join(repository_path,'.git') # This is where the config file will be if the repository is non-bare + if File.exists?(non_bare) then # The repository is non-bare + non_bare_config = File.join(non_bare, 'config') + return non_bare_config if File.exists?(non_bare_config) + else # We are dealing with a bare repository + bare_config = File.join(repository_path, "config") + return bare_config if File.exists?(bare_config) + end + return nil + end + + end +*/ diff --git a/routers/repo/http.go b/routers/repo/http.go new file mode 100644 index 00000000..5aa3139f --- /dev/null +++ b/routers/repo/http.go @@ -0,0 +1,471 @@ +package repo + +import ( + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "os/exec" + "path" + "regexp" + "strconv" + "strings" + "time" + + "github.com/go-martini/martini" + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/middleware" +) + +func Http(ctx *middleware.Context, params martini.Params) { + username := params["username"] + reponame := params["reponame"] + if strings.HasSuffix(reponame, ".git") { + reponame = reponame[:len(reponame)-4] + } + + var isPull bool + service := ctx.Query("service") + if service == "git-receive-pack" || + strings.HasSuffix(ctx.Req.URL.Path, "git-receive-pack") { + isPull = false + } else if service == "git-upload-pack" || + strings.HasSuffix(ctx.Req.URL.Path, "git-upload-pack") { + isPull = true + } else { + isPull = (ctx.Req.Method == "GET") + } + + repoUser, err := models.GetUserByName(username) + if err != nil { + ctx.Handle(500, "repo.GetUserByName", nil) + return + } + + repo, err := models.GetRepositoryByName(repoUser.Id, reponame) + if err != nil { + ctx.Handle(500, "repo.GetRepositoryByName", nil) + return + } + + // only public pull don't need auth + var askAuth = !(!repo.IsPrivate && isPull) + + // check access + if askAuth { + baHead := ctx.Req.Header.Get("Authorization") + if baHead == "" { + // ask auth + authRequired(ctx) + return + } + + auths := strings.Fields(baHead) + // currently check basic auth + // TODO: support digit auth + if len(auths) != 2 || auths[0] != "Basic" { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + authUsername, passwd, err := basicDecode(auths[1]) + if err != nil { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + + authUser, err := models.GetUserByName(authUsername) + if err != nil { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + + newUser := &models.User{Passwd: passwd, Salt: authUser.Salt} + + newUser.EncodePasswd() + if authUser.Passwd != newUser.Passwd { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + + var tp = models.AU_WRITABLE + if isPull { + tp = models.AU_READABLE + } + + has, err := models.HasAccess(authUsername, username+"/"+reponame, tp) + if err != nil { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } else if !has { + if tp == models.AU_READABLE { + has, err = models.HasAccess(authUsername, username+"/"+reponame, models.AU_WRITABLE) + if err != nil || !has { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + } else { + ctx.Handle(401, "no basic auth and digit auth", nil) + return + } + } + } + + config := Config{base.RepoRootPath, "git", true, true, func(rpc string, input []byte) { + //fmt.Println("rpc:", rpc) + //fmt.Println("input:", string(input)) + }} + + handler := HttpBackend(&config) + handler(ctx.ResponseWriter, ctx.Req) + + /* Webdav + dir := models.RepoPath(username, reponame) + + prefix := path.Join("/", username, params["reponame"]) + server := webdav.NewServer( + dir, prefix, true) + + server.ServeHTTP(ctx.ResponseWriter, ctx.Req) + */ +} + +type route struct { + cr *regexp.Regexp + method string + handler func(handler) +} + +type Config struct { + ReposRoot string + GitBinPath string + UploadPack bool + ReceivePack bool + OnSucceed func(rpc string, input []byte) +} + +type handler struct { + *Config + w http.ResponseWriter + r *http.Request + Dir string + File string +} + +var routes = []route{ + {regexp.MustCompile("(.*?)/git-upload-pack$"), "POST", serviceUploadPack}, + {regexp.MustCompile("(.*?)/git-receive-pack$"), "POST", serviceReceivePack}, + {regexp.MustCompile("(.*?)/info/refs$"), "GET", getInfoRefs}, + {regexp.MustCompile("(.*?)/HEAD$"), "GET", getTextFile}, + {regexp.MustCompile("(.*?)/objects/info/alternates$"), "GET", getTextFile}, + {regexp.MustCompile("(.*?)/objects/info/http-alternates$"), "GET", getTextFile}, + {regexp.MustCompile("(.*?)/objects/info/packs$"), "GET", getInfoPacks}, + {regexp.MustCompile("(.*?)/objects/info/[^/]*$"), "GET", getTextFile}, + {regexp.MustCompile("(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$"), "GET", getLooseObject}, + {regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$"), "GET", getPackFile}, + {regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$"), "GET", getIdxFile}, +} + +// Request handling function +func HttpBackend(config *Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + //log.Printf("%s %s %s %s", r.RemoteAddr, r.Method, r.URL.Path, r.Proto) + for _, route := range routes { + if m := route.cr.FindStringSubmatch(r.URL.Path); m != nil { + if route.method != r.Method { + renderMethodNotAllowed(w, r) + return + } + + file := strings.Replace(r.URL.Path, m[1]+"/", "", 1) + dir, err := getGitDir(config, m[1]) + + if err != nil { + log.Print(err) + renderNotFound(w) + return + } + + hr := handler{config, w, r, dir, file} + route.handler(hr) + return + } + } + renderNotFound(w) + return + } +} + +// Actual command handling functions + +func serviceUploadPack(hr handler) { + serviceRpc("upload-pack", hr) +} + +func serviceReceivePack(hr handler) { + serviceRpc("receive-pack", hr) +} + +func serviceRpc(rpc string, hr handler) { + w, r, dir := hr.w, hr.r, hr.Dir + access := hasAccess(r, hr.Config, dir, rpc, true) + + if access == false { + renderNoAccess(w) + return + } + + input, _ := ioutil.ReadAll(r.Body) + + w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", rpc)) + w.WriteHeader(http.StatusOK) + + args := []string{rpc, "--stateless-rpc", dir} + cmd := exec.Command(hr.Config.GitBinPath, args...) + cmd.Dir = dir + in, err := cmd.StdinPipe() + if err != nil { + log.Print(err) + return + } + + stdout, err := cmd.StdoutPipe() + if err != nil { + log.Print(err) + return + } + + err = cmd.Start() + if err != nil { + log.Print(err) + return + } + + in.Write(input) + io.Copy(w, stdout) + cmd.Wait() + + if hr.Config.OnSucceed != nil { + hr.Config.OnSucceed(rpc, input) + } +} + +func getInfoRefs(hr handler) { + w, r, dir := hr.w, hr.r, hr.Dir + serviceName := getServiceType(r) + access := hasAccess(r, hr.Config, dir, serviceName, false) + + if access { + args := []string{serviceName, "--stateless-rpc", "--advertise-refs", "."} + refs := gitCommand(hr.Config.GitBinPath, dir, args...) + + hdrNocache(w) + w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", serviceName)) + w.WriteHeader(http.StatusOK) + w.Write(packetWrite("# service=git-" + serviceName + "\n")) + w.Write(packetFlush()) + w.Write(refs) + } else { + updateServerInfo(hr.Config.GitBinPath, dir) + hdrNocache(w) + sendFile("text/plain; charset=utf-8", hr) + } +} + +func getInfoPacks(hr handler) { + hdrCacheForever(hr.w) + sendFile("text/plain; charset=utf-8", hr) +} + +func getLooseObject(hr handler) { + hdrCacheForever(hr.w) + sendFile("application/x-git-loose-object", hr) +} + +func getPackFile(hr handler) { + hdrCacheForever(hr.w) + sendFile("application/x-git-packed-objects", hr) +} + +func getIdxFile(hr handler) { + hdrCacheForever(hr.w) + sendFile("application/x-git-packed-objects-toc", hr) +} + +func getTextFile(hr handler) { + hdrNocache(hr.w) + sendFile("text/plain", hr) +} + +// Logic helping functions + +func sendFile(contentType string, hr handler) { + w, r := hr.w, hr.r + reqFile := path.Join(hr.Dir, hr.File) + + //fmt.Println("sendFile:", reqFile) + + f, err := os.Stat(reqFile) + if os.IsNotExist(err) { + renderNotFound(w) + return + } + + w.Header().Set("Content-Type", contentType) + w.Header().Set("Content-Length", fmt.Sprintf("%d", f.Size())) + w.Header().Set("Last-Modified", f.ModTime().Format(http.TimeFormat)) + http.ServeFile(w, r, reqFile) +} + +func getGitDir(config *Config, filePath string) (string, error) { + root := config.ReposRoot + + if root == "" { + cwd, err := os.Getwd() + + if err != nil { + log.Print(err) + return "", err + } + + root = cwd + } + + f := path.Join(root, filePath) + if _, err := os.Stat(f); os.IsNotExist(err) { + return "", err + } + + return f, nil +} + +func getServiceType(r *http.Request) string { + serviceType := r.FormValue("service") + + if s := strings.HasPrefix(serviceType, "git-"); !s { + return "" + } + + return strings.Replace(serviceType, "git-", "", 1) +} + +func hasAccess(r *http.Request, config *Config, dir string, rpc string, checkContentType bool) bool { + if checkContentType { + if r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", rpc) { + return false + } + } + + if !(rpc == "upload-pack" || rpc == "receive-pack") { + return false + } + if rpc == "receive-pack" { + return config.ReceivePack + } + if rpc == "upload-pack" { + return config.UploadPack + } + + return getConfigSetting(config.GitBinPath, rpc, dir) +} + +func getConfigSetting(gitBinPath, serviceName string, dir string) bool { + serviceName = strings.Replace(serviceName, "-", "", -1) + setting := getGitConfig(gitBinPath, "http."+serviceName, dir) + + if serviceName == "uploadpack" { + return setting != "false" + } + + return setting == "true" +} + +func getGitConfig(gitBinPath, configName string, dir string) string { + args := []string{"config", configName} + out := string(gitCommand(gitBinPath, dir, args...)) + return out[0 : len(out)-1] +} + +func updateServerInfo(gitBinPath, dir string) []byte { + args := []string{"update-server-info"} + return gitCommand(gitBinPath, dir, args...) +} + +func gitCommand(gitBinPath, dir string, args ...string) []byte { + command := exec.Command(gitBinPath, args...) + command.Dir = dir + out, err := command.Output() + + if err != nil { + log.Print(err) + } + + return out +} + +// HTTP error response handling functions + +func renderMethodNotAllowed(w http.ResponseWriter, r *http.Request) { + if r.Proto == "HTTP/1.1" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte("Method Not Allowed")) + } else { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Bad Request")) + } +} + +func renderNotFound(w http.ResponseWriter) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("Not Found")) +} + +func renderNoAccess(w http.ResponseWriter) { + w.WriteHeader(http.StatusForbidden) + w.Write([]byte("Forbidden")) +} + +// Packet-line handling function + +func packetFlush() []byte { + return []byte("0000") +} + +func packetWrite(str string) []byte { + s := strconv.FormatInt(int64(len(str)+4), 16) + + if len(s)%4 != 0 { + s = strings.Repeat("0", 4-len(s)%4) + s + } + + return []byte(s + str) +} + +// Header writing functions + +func hdrNocache(w http.ResponseWriter) { + w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate") +} + +func hdrCacheForever(w http.ResponseWriter) { + now := time.Now().Unix() + expires := now + 31536000 + w.Header().Set("Date", fmt.Sprintf("%d", now)) + w.Header().Set("Expires", fmt.Sprintf("%d", expires)) + w.Header().Set("Cache-Control", "public, max-age=31536000") +} + +// Main +/* +func main() { + http.HandleFunc("/", requestHandler()) + + err := http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } +}*/ diff --git a/routers/repo/issue.go b/routers/repo/issue.go index be925426..9688fd4d 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -9,6 +9,7 @@ import ( "net/url" "strings" + "github.com/Unknwon/com" "github.com/go-martini/martini" "github.com/gogits/gogs/models" @@ -99,7 +100,7 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat issue, err := models.CreateIssue(ctx.User.Id, ctx.Repo.Repository.Id, form.MilestoneId, form.AssigneeId, ctx.Repo.Repository.NumIssues, form.IssueName, form.Labels, form.Content, false) if err != nil { - ctx.Handle(200, "issue.CreateIssue", err) + ctx.Handle(200, "issue.CreateIssue(CreateIssue)", err) return } @@ -107,14 +108,31 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email, OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil { - ctx.Handle(200, "issue.CreateIssue", err) + ctx.Handle(200, "issue.CreateIssue(NotifyWatchers)", err) return } - // Mail watchers. + // Mail watchers and mentions. if base.Service.NotifyMail { - if err = mailer.SendNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue); err != nil { - ctx.Handle(200, "issue.CreateIssue", err) + tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) + if err != nil { + ctx.Handle(200, "issue.CreateIssue(SendIssueNotifyMail)", err) + return + } + + tos = append(tos, ctx.User.LowerName) + ms := base.MentionPattern.FindAllString(issue.Content, -1) + newTos := make([]string, 0, len(ms)) + for _, m := range ms { + if com.IsSliceContainsStr(tos, m[1:]) { + continue + } + + newTos = append(newTos, m[1:]) + } + if err = mailer.SendIssueMentionMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, + issue, models.GetUserEmailsByNames(newTos)); err != nil { + ctx.Handle(200, "issue.CreateIssue(SendIssueMentionMail)", err) return } } @@ -147,7 +165,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { return } issue.Poster = u - issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), "")) + issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)) // Get comments. comments, err := models.GetIssueComments(issue.Id) @@ -164,7 +182,7 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) { return } comments[i].Poster = u - comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), "")) + comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink)) } ctx.Data["Title"] = issue.Name @@ -193,7 +211,7 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat return } - if ctx.User.Id != issue.PosterId { + if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner { ctx.Handle(404, "issue.UpdateIssue", nil) return } @@ -211,7 +229,7 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat ctx.JSON(200, map[string]interface{}{ "ok": true, "title": issue.Name, - "content": string(base.RenderMarkdown([]byte(issue.Content), "")), + "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)), }) } diff --git a/routers/repo/release.go b/routers/repo/release.go index 8e8b93c9..279fc169 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -12,6 +12,7 @@ import ( func Releases(ctx *middleware.Context) { ctx.Data["Title"] = "Releases" ctx.Data["IsRepoToolbarReleases"] = true + ctx.Data["IsRepoReleaseNew"] = false tags, err := models.GetTags(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) if err != nil { ctx.Handle(404, "repo.Releases(GetTags)", err) @@ -20,3 +21,10 @@ func Releases(ctx *middleware.Context) { ctx.Data["Releases"] = tags ctx.HTML(200, "release/list") } + +func ReleasesNew(ctx *middleware.Context) { + ctx.Data["Title"] = "New Release" + ctx.Data["IsRepoToolbarReleases"] = true + ctx.Data["IsRepoReleaseNew"] = true + ctx.HTML(200, "release/new") +} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 6a5a7ae4..d4d52ba0 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -5,6 +5,8 @@ package repo import ( + "encoding/base64" + "errors" "fmt" "path" "path/filepath" @@ -12,8 +14,6 @@ import ( "github.com/go-martini/martini" - "github.com/gogits/webdav" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" @@ -53,6 +53,36 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Handle(200, "repo.Create", err) } +func Mirror(ctx *middleware.Context, form auth.CreateRepoForm) { + ctx.Data["Title"] = "Mirror repository" + ctx.Data["PageIsNewRepo"] = true // For navbar arrow. + + if ctx.Req.Method == "GET" { + ctx.HTML(200, "repo/mirror") + return + } + + if ctx.HasError() { + ctx.HTML(200, "repo/mirror") + return + } + + _, err := models.CreateRepository(ctx.User, form.RepoName, form.Description, + "", form.License, form.Visibility == "private", false) + 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/mirror", &form) + return + } else if err == models.ErrRepoNameIllegal { + ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/mirror", &form) + return + } + ctx.Handle(200, "repo.Mirror", err) +} + func Single(ctx *middleware.Context, params martini.Params) { branchName := ctx.Repo.BranchName commitId := ctx.Repo.CommitId @@ -237,21 +267,31 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) { ctx.Res.Write(data) } -func Http(ctx *middleware.Context, params martini.Params) { - // TODO: access check +func basicEncode(username, password string) string { + auth := username + ":" + password + return base64.StdEncoding.EncodeToString([]byte(auth)) +} - username := params["username"] - reponame := params["reponame"] - if strings.HasSuffix(reponame, ".git") { - reponame = reponame[:len(reponame)-4] +func basicDecode(encoded string) (user string, name string, err error) { + var s []byte + s, err = base64.StdEncoding.DecodeString(encoded) + if err != nil { + return } - dir := models.RepoPath(username, reponame) - prefix := path.Join("/", username, params["reponame"]) - server := webdav.NewServer( - dir, prefix, true) + a := strings.Split(string(s), ":") + if len(a) == 2 { + user, name = a[0], a[1] + } else { + err = errors.New("decode failed") + } + return +} - server.ServeHTTP(ctx.ResponseWriter, ctx.Req) +func authRequired(ctx *middleware.Context) { + ctx.ResponseWriter.Header().Set("WWW-Authenticate", "Basic realm=\".\"") + ctx.Data["ErrorMsg"] = "no basic auth and digit auth" + ctx.HTML(401, fmt.Sprintf("status/401")) } func Setting(ctx *middleware.Context, params martini.Params) { @@ -302,6 +342,7 @@ func SettingPost(ctx *middleware.Context) { ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") + ctx.Repo.Repository.IsGoget = ctx.Query("goget") == "on" if err := models.UpdateRepository(ctx.Repo.Repository); err != nil { ctx.Handle(404, "repo.SettingPost(update)", err) return diff --git a/routers/user/setting.go b/routers/user/setting.go index 4b6d88a3..ea779e85 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -73,11 +73,7 @@ func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) { user := ctx.User newUser := &models.User{Passwd: form.NewPasswd} - if err := newUser.EncodePasswd(); err != nil { - ctx.Handle(200, "setting.SettingPassword", err) - return - } - + newUser.EncodePasswd() if user.Passwd != newUser.Passwd { ctx.Data["HasError"] = true ctx.Data["ErrorMsg"] = "Old password is not correct" @@ -85,6 +81,7 @@ func SettingPassword(ctx *middleware.Context, form auth.UpdatePasswdForm) { ctx.Data["HasError"] = true ctx.Data["ErrorMsg"] = "New password and re-type password are not same" } else { + newUser.Salt = models.GetUserSalt() user.Passwd = newUser.Passwd if err := models.UpdateUser(user); err != nil { ctx.Handle(200, "setting.SettingPassword", err) diff --git a/routers/user/social.go b/routers/user/social.go index b59f4963..b87c313f 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -1,49 +1,159 @@ // Copyright 2014 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. + package user import ( "encoding/json" + "net/http" + "net/url" + "strconv" + "strings" "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" ) -// github && google && ... -func SocialSignIn(tokens oauth2.Tokens) { - transport := &oauth.Transport{} - transport.Token = &oauth.Token{ - AccessToken: tokens.Access(), - RefreshToken: tokens.Refresh(), - Expiry: tokens.ExpiryTime(), - Extra: tokens.ExtraData(), - } - - // Github API refer: https://developer.github.com/v3/users/ - // FIXME: need to judge url - type GithubUser struct { +type SocialConnector interface { + Identity() string + Type() int + Name() string + Email() string + Token() string +} + +type SocialGithub struct { + data struct { Id int `json:"id"` Name string `json:"login"` Email string `json:"email"` } + WebToken *oauth.Token +} + +func (s *SocialGithub) Identity() string { + return strconv.Itoa(s.data.Id) +} + +func (s *SocialGithub) Type() int { + return models.OT_GITHUB +} - // Make the request. +func (s *SocialGithub) Name() string { + return s.data.Name +} + +func (s *SocialGithub) Email() string { + return s.data.Email +} + +func (s *SocialGithub) Token() string { + data, _ := json.Marshal(s.WebToken) + return string(data) +} + +// Github API refer: https://developer.github.com/v3/users/ +func (s *SocialGithub) Update() error { scope := "https://api.github.com/user" + transport := &oauth.Transport{ + Token: s.WebToken, + } + log.Debug("update github info") r, err := transport.Client().Get(scope) if err != nil { - log.Error("connect with github error: %s", err) - // FIXME: handle error page - return + return err } defer r.Body.Close() + return json.NewDecoder(r.Body).Decode(&s.data) +} - user := &GithubUser{} - err = json.NewDecoder(r.Body).Decode(user) +func extractPath(next string) string { + n, err := url.Parse(next) if err != nil { - log.Error("Get: %s", err) + return "/" + } + return n.Path +} + +// github && google && ... +func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { + var socid int64 + var ok bool + next := extractPath(ctx.Query("next")) + log.Debug("social signed check %s", next) + if socid, ok = ctx.Session.Get("socialId").(int64); ok && socid != 0 { + // already login + ctx.Redirect(next) + log.Info("login soc id: %v", socid) + return + } + config := &oauth.Config{ + //ClientId: base.OauthService.Github.ClientId, + //ClientSecret: base.OauthService.Github.ClientSecret, // FIXME: I don't know why compile error here + ClientId: "09383403ff2dc16daaa1", + ClientSecret: "0e4aa0c3630df396cdcea01a9d45cacf79925fea", + RedirectURL: strings.TrimSuffix(base.AppUrl, "/") + ctx.Req.URL.RequestURI(), + Scope: base.OauthService.GitHub.Scopes, + AuthURL: "https://github.com/login/oauth/authorize", + TokenURL: "https://github.com/login/oauth/access_token", + } + transport := &oauth.Transport{ + Config: config, + Transport: http.DefaultTransport, + } + code := ctx.Query("code") + if code == "" { + // redirect to social login page + ctx.Redirect(config.AuthCodeURL(next)) + return + } + + // handle call back + tk, err := transport.Exchange(code) + if err != nil { + log.Error("oauth2 handle callback error: %v", err) + return // FIXME, need error page 501 + } + next = extractPath(ctx.Query("state")) + log.Debug("success token: %v", tk) + + gh := &SocialGithub{WebToken: tk} + if err = gh.Update(); err != nil { + // FIXME: handle error page 501 + log.Error("connect with github error: %s", err) + return + } + var soc SocialConnector = gh + log.Info("login: %s", soc.Name()) + 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() + log.Debug("oa: %v", oa) + if err = models.AddOauth2(oa); err != nil { + log.Error("add oauth2 %v", err) // 501 + return + } + case models.ErrOauth2NotAssociatedWithUser: + // ignore it. judge in /usr/login page + default: + log.Error(err.Error()) // FIXME: handle error page + return } - log.Info("login: %s", user.Name) - // FIXME: login here, user email to check auth, if not registe, then generate a uniq username + ctx.Session.Set("socialId", oa.Id) + log.Debug("socialId: %v", oa.Id) + ctx.Redirect(next) } diff --git a/routers/user/user.go b/routers/user/user.go index 08930e22..084d0bbd 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -78,6 +78,11 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) { ctx.Data["Title"] = "Log In" if ctx.Req.Method == "GET" { + if base.OauthService != nil { + ctx.Data["OauthEnabled"] = true + ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled + } + // Check auto-login. userName := ctx.GetCookie(base.CookieUserName) if len(userName) == 0 { @@ -391,6 +396,10 @@ func Activate(ctx *middleware.Context) { } else { ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 mailer.SendActiveMail(ctx.Render, ctx.User) + + if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { + log.Error("Set cache(MailResendLimit) fail: %v", err) + } } } else { ctx.Data["ServiceNotEnabled"] = true @@ -403,9 +412,12 @@ func Activate(ctx *middleware.Context) { if user := models.VerifyUserActiveCode(code); user != nil { user.IsActive = true user.Rands = models.GetUserSalt() - models.UpdateUser(user) + if err := models.UpdateUser(user); err != nil { + ctx.Handle(404, "user.Activate", err) + return + } - log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.LowerName) + log.Trace("%s User activated: %s", ctx.Req.RequestURI, user.Name) ctx.Session.Set("userId", user.Id) ctx.Session.Set("userName", user.Name) @@ -416,3 +428,87 @@ func Activate(ctx *middleware.Context) { ctx.Data["IsActivateFailed"] = true ctx.HTML(200, "user/active") } + +func ForgotPasswd(ctx *middleware.Context) { + ctx.Data["Title"] = "Forgot Password" + + if base.MailService == nil { + ctx.Data["IsResetDisable"] = true + ctx.HTML(200, "user/forgot_passwd") + return + } + + ctx.Data["IsResetRequest"] = true + if ctx.Req.Method == "GET" { + ctx.HTML(200, "user/forgot_passwd") + return + } + + email := ctx.Query("email") + u, err := models.GetUserByEmail(email) + if err != nil { + if err == models.ErrUserNotExist { + ctx.RenderWithErr("This e-mail address does not associate to any account.", "user/forgot_passwd", nil) + } else { + ctx.Handle(404, "user.ResetPasswd(check existence)", err) + } + return + } + + if ctx.Cache.IsExist("MailResendLimit_" + u.LowerName) { + ctx.Data["ResendLimited"] = true + ctx.HTML(200, "user/forgot_passwd") + return + } + + mailer.SendResetPasswdMail(ctx.Render, u) + if err = ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { + log.Error("Set cache(MailResendLimit) fail: %v", err) + } + + ctx.Data["Email"] = email + ctx.Data["Hours"] = base.Service.ActiveCodeLives / 60 + ctx.Data["IsResetSent"] = true + ctx.HTML(200, "user/forgot_passwd") +} + +func ResetPasswd(ctx *middleware.Context) { + code := ctx.Query("code") + if len(code) == 0 { + ctx.Error(404) + return + } + ctx.Data["Code"] = code + + if ctx.Req.Method == "GET" { + ctx.Data["IsResetForm"] = true + ctx.HTML(200, "user/reset_passwd") + return + } + + if u := models.VerifyUserActiveCode(code); u != nil { + // Validate password length. + passwd := ctx.Query("passwd") + if len(passwd) < 6 || len(passwd) > 30 { + ctx.Data["IsResetForm"] = true + ctx.RenderWithErr("Password length should be in 6 and 30.", "user/reset_passwd", nil) + return + } + + u.Passwd = passwd + u.Rands = models.GetUserSalt() + u.Salt = models.GetUserSalt() + u.EncodePasswd() + if err := models.UpdateUser(u); err != nil { + ctx.Handle(404, "user.ResetPasswd(UpdateUser)", err) + return + } + + log.Trace("%s User password reset: %s", ctx.Req.RequestURI, u.Name) + ctx.Redirect("/user/login") + return + } + + ctx.Data["IsResetFailed"] = true + ctx.HTML(200, "user/reset_passwd") +} @@ -14,7 +14,7 @@ import ( "strings" "github.com/codegangsta/cli" - "github.com/gogits/gogs/modules/log" + qlog "github.com/qiniu/log" //"github.com/gogits/git" "github.com/gogits/gogs/models" @@ -44,11 +44,16 @@ gogs serv provide access auth for repositories`, } func newLogger(execDir string) { - level := "0" logPath := execDir + "/log/serv.log" os.MkdirAll(path.Dir(logPath), os.ModePerm) - log.NewLogger(0, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, logPath)) - log.Trace("start logging...") + + 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 serv...") } func parseCmd(cmd string) (string, string) { @@ -87,21 +92,18 @@ func runServ(k *cli.Context) { keys := strings.Split(os.Args[2], "-") if len(keys) != 2 { println("auth file format error") - log.Error("auth file format error") - return + qlog.Fatal("auth file format error") } keyId, err := strconv.ParseInt(keys[1], 10, 64) if err != nil { println("auth file format error") - log.Error("auth file format error", err) - return + qlog.Fatal("auth file format error", err) } user, err := models.GetUserByKeyId(keyId) if err != nil { println("You have no right to access") - log.Error("SSH visit error: %v", err) - return + qlog.Fatalf("SSH visit error: %v", err) } cmd := os.Getenv("SSH_ORIGINAL_COMMAND") @@ -115,8 +117,7 @@ func runServ(k *cli.Context) { rr := strings.SplitN(repoPath, "/", 2) if len(rr) != 2 { println("Unavilable repository", args) - log.Error("Unavilable repository %v", args) - return + qlog.Fatalf("Unavilable repository %v", args) } repoUserName := rr[0] repoName := rr[1] @@ -129,9 +130,8 @@ func runServ(k *cli.Context) { repoUser, err := models.GetUserByName(repoUserName) if err != nil { - fmt.Println("You have no right to access") - log.Error("Get user failed", err) - return + println("You have no right to access") + qlog.Fatal("Get user failed", err) } // access check @@ -140,19 +140,16 @@ func runServ(k *cli.Context) { has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE) if err != nil { println("Inernel error:", err) - log.Error(err.Error()) - return + qlog.Fatal(err) } else if !has { println("You have no right to write this repository") - log.Error("User %s has no right to write repository %s", user.Name, repoPath) - return + qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath) } case isRead: repo, err := models.GetRepositoryByName(repoUser.Id, repoName) if err != nil { println("Get repository error:", err) - log.Error("Get repository error: " + err.Error()) - return + qlog.Fatal("Get repository error: " + err.Error()) } if !repo.IsPrivate { @@ -162,32 +159,25 @@ func runServ(k *cli.Context) { has, err := models.HasAccess(user.Name, repoPath, models.AU_READABLE) if err != nil { println("Inernel error") - log.Error(err.Error()) - return + qlog.Fatal(err) } if !has { has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE) if err != nil { println("Inernel error") - log.Error(err.Error()) - return + qlog.Fatal(err) } } if !has { println("You have no right to access this repository") - log.Error("You have no right to access this repository") - return + qlog.Fatal("You have no right to access this repository") } default: println("Unknown command") - log.Error("Unknown command") - return + 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 @@ -197,7 +187,6 @@ func runServ(k *cli.Context) { if err = gitcmd.Run(); err != nil { println("execute command error:", err.Error()) - log.Error("execute command error: " + err.Error()) - return + qlog.Fatal("execute command error: " + err.Error()) } } @@ -1,6 +1,15 @@ -#!/bin/bash - +#!/bin/sh - +# Copyright 2014 The Gogs Authors. All rights reserved. +# Use of this source code is governed by a MIT-style +# license that can be found in the LICENSE file. # # start gogs web # -cd "$(dirname $0)" -./gogs web +IFS=' + ' +PATH=/bin:/usr/bin:/usr/local/bin +HOME=${HOME:?"need \$HOME variable"} +USER=$(whoami) +export USER HOME PATH + +cd "$(dirname $0)" && exec ./gogs web diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 7f56ed70..648eb7c4 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -9,16 +9,27 @@ <meta name="description" content="Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language" /> <meta name="keywords" content="go, git"> <meta name="_csrf" content="{{.CsrfToken}}" /> + {{if .Repository.IsGoget}}<meta name="go-import" content="{{AppDomain}} git {{.CloneLink.HTTPS}}">{{end}} <!-- Stylesheets --> + {{if IsProdMode}} + <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> + <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> + + <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> + <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> + {{else}} <link href="/css/bootstrap.min.css" rel="stylesheet" /> - <link href="/css/todc-bootstrap.min.css" rel="stylesheet" /> <link href="/css/font-awesome.min.css" rel="stylesheet" /> - <link href="/css/markdown.css" rel="stylesheet" /> - <link href="/css/gogs.css" rel="stylesheet" /> <script src="/js/jquery-1.10.1.min.js"></script> <script src="/js/bootstrap.min.js"></script> + {{end}} + + <link href="/css/todc-bootstrap.min.css" rel="stylesheet" /> + <link href="/css/markdown.css" rel="stylesheet" /> + <link href="/css/gogs.css" rel="stylesheet" /> + <script src="/js/lib.js"></script> <script src="/js/app.js"></script> <title>{{if .Title}}{{.Title}} - {{end}}{{AppName}}</title> diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 7d1f64e4..c0855d81 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -8,9 +8,18 @@ <a id="nav-avatar" class="nav-item navbar-right{{if .PageIsUserProfile}} active{{end}}" href="{{.SignedUser.HomeLink}}" data-toggle="tooltip" data-placement="bottom" title="{{.SignedUserName}}"> <img src="{{.SignedUser.AvatarLink}}?s=28" alt="user-avatar" title="username"/> </a> - <a class="navbar-right nav-item{{if .PageIsNewRepo}} active{{end}}" href="/repo/create" data-toggle="tooltip" data-placement="bottom" title="New Repository"><i class="fa fa-plus fa-lg"></i></a> <a class="navbar-right nav-item{{if .PageIsUserSetting}} active{{end}}" href="/user/setting" data-toggle="tooltip" data-placement="bottom" title="Setting"><i class="fa fa-cogs fa-lg"></i></a> {{if .IsAdmin}}<a class="navbar-right nav-item{{if .PageIsAdmin}} active{{end}}" href="/admin" data-toggle="tooltip" data-placement="bottom" title="Admin"><i class="fa fa-gear fa-lg"></i></a>{{end}} + <div class="navbar-right nav-item pull-right{{if .PageIsNewRepo}} active{{end}}" id="nav-repo-new" data-toggle="tooltip" data-placement="bottom" title="New Repo"> + <button type="button" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-plus-square fa-lg"></i></button> + <div class="dropdown-menu"> + <ul class="list-unstyled"> + <li><a href="/repo/create"><i class="fa fa-book"></i>Repository</a></li> + <li><a href="/repo/mirror"><i class="fa fa-clipboard"></i>Mirror</a></li> + <li><a href="#"><i class="fa fa-users"></i>Organization</a></li> + </ul> + </div> + </div> {{else}}<a id="nav-signin" class="nav-item navbar-right navbar-btn btn btn-danger" href="/user/login/">Sign In</a> <a id="nav-signup" class="nav-item navbar-right" href="/user/sign_up/">Sign Up</a>{{end}} </nav> diff --git a/templates/install.tmpl b/templates/install.tmpl index 1fbc74bc..c70cfa3e 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -156,11 +156,11 @@ <label class="col-md-3 control-label">SMTP Host: </label> <div class="col-md-8"> - <input name="smtp_host" type="text" class="form-control" placeholder="Type SMTP host address" value="{{.smtp_host}}"> + <input name="smtp_host" type="text" class="form-control" placeholder="Type SMTP host address and port" value="{{.smtp_host}}"> </div> </div> <div class="form-group"> - <label class="col-md-3 control-label">Email: </label> + <label class="col-md-3 control-label">Username: </label> <div class="col-md-8"> <input name="mailer_user" type="text" class="form-control" placeholder="Type SMTP user e-mail address" value="{{.mailer_user}}"> diff --git a/templates/issue/create.tmpl b/templates/issue/create.tmpl index 01784cd2..5375040b 100644 --- a/templates/issue/create.tmpl +++ b/templates/issue/create.tmpl @@ -19,7 +19,7 @@ </div> <ul class="nav nav-tabs" data-init="tabs"> <li class="active issue-write"><a href="#issue-textarea" data-toggle="tab">Write</a></li> - <li class="issue-preview"><a href="#issue-preview" data-toggle="tab" data-ajax="/api/v1/markdown?repo=repo_id&issue=new" data-ajax-name="issue-preview" data-ajax-method="post" data-preview="#issue-preview">Preview</a></li> + <li class="issue-preview"><a href="#issue-preview" data-toggle="tab" data-ajax="/api/v1/markdown?repoLink={{.RepoLink}}" data-ajax-name="issue-preview" data-ajax-method="post" data-preview="#issue-preview">Preview</a></li> </ul> <div class="tab-content"> <div class="tab-pane" id="issue-textarea"> diff --git a/templates/issue/view.tmpl b/templates/issue/view.tmpl index e619451c..16d60d35 100644 --- a/templates/issue/view.tmpl +++ b/templates/issue/view.tmpl @@ -72,7 +72,7 @@ </div> <ul class="nav nav-tabs" data-init="tabs"> <li class="active issue-write"><a href="#issue-textarea" data-toggle="tab">Write</a></li> - <li class="issue-preview"><a href="#issue-preview" data-toggle="tab" data-ajax="/api/v1/markdown?repo=repo_id&issue=issue_id&comment=new" data-ajax-name="issue-preview" data-ajax-method="post" data-preview="#issue-preview">Preview</a></li> + <li class="issue-preview"><a href="#issue-preview" data-toggle="tab" data-ajax="/api/v1/markdown?repoLink={{.RepoLink}}" data-ajax-name="issue-preview" data-ajax-method="post" data-preview="#issue-preview">Preview</a></li> </ul> <div class="tab-content"> <div class="tab-pane" id="issue-textarea"> diff --git a/templates/mail/auth/reset_passwd.tmpl b/templates/mail/auth/reset_passwd.tmpl new file mode 100644 index 00000000..11861f4e --- /dev/null +++ b/templates/mail/auth/reset_passwd.tmpl @@ -0,0 +1,33 @@ +<!DOCTYPE html> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +<title>{{.User.Name}}, please reset your password</title> +</head> +<body style="background:#eee;"> +<div style="color:#333; font:12px/1.5 Tahoma,Arial,sans-serif;; text-shadow:1px 1px #fff; padding:0; margin:0;"> + <div style="width:600px;margin:0 auto; padding:40px 0 20px;"> + <div style="border:1px solid #d9d9d9;border-radius:3px; background:#fff; box-shadow: 0px 2px 5px rgba(0, 0, 0,.05); -webkit-box-shadow: 0px 2px 5px rgba(0, 0, 0,.05);"> + <div style="padding: 20px 15px;"> + <h1 style="font-size:20px; padding:10px 0 20px; margin:0; border-bottom:1px solid #ddd;"><img src="{{.AppUrl}}/{{.AppLogo}}" style="height: 32px; margin-bottom: -10px;"> <a style="color:#333;text-decoration:none;" target="_blank" href="{{.AppUrl}}">{{.AppName}}</a></h1> + <div style="padding:40px 15px;"> + <div style="font-size:16px; padding-bottom:30px; font-weight:bold;"> + Hi <span style="color: #00BFFF;">{{.User.Name}}</span>, + </div> + <div style="font-size:14px; padding:0 15px;"> + <p style="margin:0;padding:0 0 9px 0;">Please click following link to reset your password within <b>{{.ActiveCodeLives}} hours</b>.</p> + <p style="margin:0;padding:0 0 9px 0;"> + <a href="{{.AppUrl}}user/reset_password?code={{.Code}}">{{.AppUrl}}user/reset_password?code={{.Code}}</a> + </p> + <p style="margin:0;padding:0 0 9px 0;">Copy and paste it to your browser if the link is not working.</p> + </div> + </div> + </div> + </div> + <div style="color:#aaa;padding:10px;text-align:center;"> + © 2014 <a style="color:#888;text-decoration:none;" target="_blank" href="http://gogits.org">Gogs: Go Git Service</a> + </div> + </div> +</div> +</body> +</html>
\ No newline at end of file diff --git a/templates/mail/auth/reset_password.html b/templates/mail/auth/reset_password.html deleted file mode 100644 index 40a9efa8..00000000 --- a/templates/mail/auth/reset_password.html +++ /dev/null @@ -1,25 +0,0 @@ -{{template "mail/base.html" .}} -{{define "title"}} - {{if eq .Lang "zh-CN"}} - {{.User.NickName}},重置账户密码 - {{end}} - {{if eq .Lang "en-US"}} - {{.User.NickName}}, reset your password - {{end}} -{{end}} -{{define "body"}} - {{if eq .Lang "zh-CN"}} - <p style="margin:0;padding:0 0 9px 0;">点击链接重置密码,{{.ResetPwdCodeLives}} 分钟内有效</p> - <p style="margin:0;padding:0 0 9px 0;"> - <a href="{{.AppUrl}}reset/{{.Code}}">{{.AppUrl}}reset/{{.Code}}</a> - </p> - <p style="margin:0;padding:0 0 9px 0;">如果链接点击无反应,请复制到浏览器打开。</p> - {{end}} - {{if eq .Lang "en-US"}} - <p style="margin:0;padding:0 0 9px 0;">Please click following link to reset your password in {{.ResetPwdCodeLives}} hours</p> - <p style="margin:0;padding:0 0 9px 0;"> - <a href="{{.AppUrl}}reset/{{.Code}}">{{.AppUrl}}reset/{{.Code}}</a> - </p> - <p style="margin:0;padding:0 0 9px 0;">Copy and paste it to your browser if it's not working.</p> - {{end}} -{{end}}
\ No newline at end of file diff --git a/templates/release/new.tmpl b/templates/release/new.tmpl new file mode 100644 index 00000000..fe5aa179 --- /dev/null +++ b/templates/release/new.tmpl @@ -0,0 +1,66 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +{{template "repo/nav" .}} +{{template "repo/toolbar" .}} +<div id="body" class="container"> + <div id="release"> + <h4 id="release-head">New Release</h4> + <form id="release-new-form" action="" class="form form-inline"> + <div class="form-group"> + <input id="release-tag-name" type="text" class="form-control" placeholder="tag name"/> + <span class="target-at">@</span> + <div class="btn-group" id="release-new-target-select"> + <button type="button" class="btn btn-default"><i class="fa fa-code-fork fa-lg fa-m"></i> + <span class="target-text">Target : </span> + <strong id="release-new-target-name"> master</strong> + </button> + <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> + <span class="caret"></span> + </button> + <div class="dropdown-menu clone-group-btn" id="release-new-target-branch-list"> + <ul class="list-group"> + <li class="list-group-item"> + <a href="#" rel="master"><i class="fa fa-code-fork"></i>master</a> + </li> + </ul> + </div> + </div> + <p class="help-block">Choose an existing tag without release notes</p> + </div> + <div class="form-group" style="display: block"> + <input class="form-control input-lg" id="release-new-title" name="title" type="text" placeholder="release title"/> + </div> + <div class="form-group col-md-8" style="display: block" id="release-new-content-div"> + <div class="md-help pull-right"> + Content with <a href="https://help.github.com/articles/markdown-basics">Markdown</a> + </div> + <ul class="nav nav-tabs" data-init="tabs"> + <li class="release-write active"><a href="#release-textarea" data-toggle="tab">Write</a></li> + <li class="release-preview"><a href="#release-preview" data-toggle="tab" data-ajax="/api/v1/markdown?repo=repo_id&release=new" data-ajax-name="release-preview" data-ajax-method="post" data-preview="#release-preview">Preview</a></li> + </ul> + <div class="tab-content"> + <div class="tab-pane active" id="release-textarea"> + <div class="form-group"> + <textarea class="form-control" name="content" id="release-new-content" rows="10" placeholder="Write some content" data-ajax-rel="release-preview" data-ajax-val="val" data-ajax-field="content"></textarea> + </div> + </div> + <div class="tab-pane release-preview-content" id="release-preview">loading...</div> + </div> + </div> + <div class="text-right form-group col-md-8" style="display: block"> + <hr/> + <label for="release-new-pre-release"> + <input id="release-new-pre-release" type="checkbox" name="is-pre-release" value="true"/> + <strong>This is a pre-release</strong> + </label> + <p class="help-block">We’ll point out that this release is identified as non-production ready.</p> + </div> + <div class="text-right form-group col-md-8" style="display: block"> + <input type="hidden" value="id" name="repo-id"> + <button class="btn-success btn">Publish release</button> + <input class="btn btn-default" type="submit" name="is-draft" value="Save Draft"/> + </div> + </form> + </div> +</div> +{{template "base/footer" .}}
\ No newline at end of file diff --git a/templates/repo/mirror.tmpl b/templates/repo/mirror.tmpl new file mode 100644 index 00000000..2ac21dd6 --- /dev/null +++ b/templates/repo/mirror.tmpl @@ -0,0 +1,81 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +<div class="container" id="body"> + <form action="/repo/create" method="post" class="form-horizontal card" id="repo-create"> + {{.CsrfTokenHtml}} + <h3>Create Repository Mirror</h3> + <div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div> + <div class="form-group"> + <label class="col-md-2 control-label">From<strong class="text-danger">*</strong></label> + <div class="col-md-8"> + <select class="form-control" name="from"> + <option value="">GitHub</option> + </select> + </div> + </div> + <div class="form-group"> + <label class="col-md-2 control-label">URL<strong class="text-danger">*</strong></label> + <div class="col-md-8"> + <input name="url" type="text" class="form-control" placeholder="Type your mirror repository url link" required="required"> + </div> + </div> + <div class="form-group"> + <div class="col-md-offset-2 col-md-8"> + <a class="btn btn-default" data-toggle="collapse" data-target="#repo-import-auth">Need Authorization</a> + </div> + <div id="repo-import-auth" class="collapse"> + <div class="form-group"> + <label class="col-md-2 control-label">Username</label> + <div class="col-md-8"> + <input name="auth-username" type="text" class="form-control"> + </div> + </div> + <div class="form-group"> + <label class="col-md-2 control-label">Password</label> + <div class="col-md-8"> + <input name="auth-password" type="text" class="form-control"> + </div> + </div> + </div> + </div> + <hr/> + <div class="form-group"> + <label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label> + <div class="col-md-8"> + <p class="form-control-static">{{.SignedUserName}}</p> + <input type="hidden" value="{{.SignedUserId}}" name="userId"/> + </div> + </div> + + <div class="form-group {{if .Err_RepoName}}has-error has-feedback{{end}}"> + <label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label> + <div class="col-md-8"> + <input name="repo" type="text" class="form-control" placeholder="Type your repository name" value="{{.repo}}" required="required"> + <span class="help-block">Great repository names are short and memorable. </span> + </div> + </div> + + <div class="form-group"> + <label class="col-md-2 control-label">Visibility<strong class="text-danger">*</strong></label> + <div class="col-md-8"> + <p class="form-control-static">Public</p> + <input type="hidden" value="public" name="visibility"/> + </div> + </div> + + <div class="form-group {{if .Err_Description}}has-error has-feedback{{end}}"> + <label class="col-md-2 control-label">Description</label> + <div class="col-md-8"> + <textarea name="desc" class="form-control" placeholder="Type your repository description">{{.desc}}</textarea> + </div> + </div> + + <div class="form-group"> + <div class="col-md-offset-2 col-md-8"> + <button type="submit" class="btn btn-lg btn-primary">Mirror repository</button> + <a href="/" class="text-danger">Cancel</a> + </div> + </div> + </form> +</div> +{{template "base/footer" .}}
\ No newline at end of file diff --git a/templates/repo/setting.tmpl b/templates/repo/setting.tmpl index 6e2d3bec..1adf0090 100644 --- a/templates/repo/setting.tmpl +++ b/templates/repo/setting.tmpl @@ -23,9 +23,10 @@ {{.CsrfTokenHtml}} <input type="hidden" name="action" value="update"> <div class="form-group"> - <label class="col-md-3 text-right">Name</label> + <label class="col-md-3 text-right" for="repo-setting-name">Name</label> <div class="col-md-9"> - <input class="form-control" name="name" value="{{.Repository.Name}}" title="{{.Repository.Name}}" /> + <input class="form-control" name="name" value="{{.Repository.Name}}" title="{{.Repository.Name}}" id="repo-setting-name"/> + <p class="help-block hidden"><span class="text-danger">Cautious : </span>your repository name is changing !</p> </div> </div> @@ -42,6 +43,7 @@ <input type="url" class="form-control" name="site" value="{{.Repository.Website}}" /> </div> </div> + <hr> <!-- <div class="form-group"> <label class="col-md-3 text-right">Default Branch</label> <div class="col-md-9"> @@ -50,6 +52,18 @@ </select> </div> </div> --> + + <div class="form-group"> + <div class="col-md-offset-3 col-md-9"> + <div class="checkbox"> + <label style="line-height: 15px;"> + <input type="checkbox" name="goget" {{if .Repository.IsGoget}}checked{{end}}> + <strong>Enable 'go get' meta</strong> + </label> + </div> + </div> + </div> + <div class="form-group"> <div class="col-md-9 col-md-offset-3"> <button class="btn btn-primary" type="submit">Save Options</button> diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index fc0a3bd9..3f639153 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -9,6 +9,20 @@ <h4>Quick Guide</h4> </div> <div class="panel-body guide-content text-center"> + <form action="{{.RepoLink}}/import" method="post"> + {{.CsrfTokenHtml}} + <h3>Clone from existing repository</h3> + <div class="input-group col-md-6 col-md-offset-3"> + <span class="input-group-btn"> + <button class="btn btn-default" type="button">URL</button> + </span> + <input name="passwd" type="password" class="form-control" placeholder="Type existing repository address" required="required"> + <span class="input-group-btn"> + <button type="submit" class="btn btn-default" type="button">Clone</button> + </span> + </div> + </form> + <h3>Clone this repository</h3> <div class="input-group col-md-8 col-md-offset-2 guide-buttons"> <span class="input-group-btn"> diff --git a/templates/repo/toolbar.tmpl b/templates/repo/toolbar.tmpl index 54842048..9c137e51 100644 --- a/templates/repo/toolbar.tmpl +++ b/templates/repo/toolbar.tmpl @@ -11,11 +11,11 @@ <li class="{{if .IsRepoToolbarIssues}}active{{end}}"><a href="{{.RepoLink}}/issues">{{if .Repository.NumOpenIssues}}<span class="badge">{{.Repository.NumOpenIssues}}</span> {{end}}Issues <!--<span class="badge">42</span>--></a></li> {{if .IsRepoToolbarIssues}} <li class="tmp">{{if .IsRepoToolbarIssuesList}}<a href="{{.RepoLink}}/issues/new"><button class="btn btn-primary btn-sm">New Issue</button> - </a>{{else}}<a href="{{.RepoLink}}/issues"><button class="btn btn-primary btn-sm">Issues List</button></a>{{end}}</li> + </a>{{end}}</li> {{end}} <li class="{{if .IsRepoToolbarReleases}}active{{end}}"><a href="{{.RepoLink}}/releases">{{if .Repository.NumReleases}}<span class="badge">{{.Repository.NumReleases}}</span> {{end}}Releases</a></li> {{if .IsRepoToolbarReleases}} - <li class="tmp"><a href="{{.RepoLink}}/releases/new"><button class="btn btn-primary btn-sm">New Release</button></a></li> + <li class="tmp">{{if not .IsRepoReleaseNew}}<a href="{{.RepoLink}}/releases/new"><button class="btn btn-primary btn-sm">New Release</button></a>{{end}}</li> {{end}} <!-- <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">More <b class="caret"></b></a> diff --git a/templates/status/401.tmpl b/templates/status/401.tmpl new file mode 100644 index 00000000..98995381 --- /dev/null +++ b/templates/status/401.tmpl @@ -0,0 +1,6 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +<div class="container"> + 401 Unauthorized +</div> +{{template "base/footer" .}}
\ No newline at end of file diff --git a/templates/user/dashboard.tmpl b/templates/user/dashboard.tmpl index bc0853fb..e2d7a509 100644 --- a/templates/user/dashboard.tmpl +++ b/templates/user/dashboard.tmpl @@ -29,7 +29,16 @@ <div id="feed-right" class="col-md-4"> <div class="panel panel-default repo-panel"> <div class="panel-heading">Your Repositories - <a class="btn btn-success pull-right btn-sm" href="/repo/create"><i class="fa fa-plus-square"></i>New Repo</a> + <div class="btn-group pull-right" id="user-dashboard-repo-new"> + <button type="button" class="btn btn-success btn-sm dropdown-toggle" data-toggle="dropdown"><i class="fa fa-plus-square"></i>New</button> + <div class="dropdown-menu dropdown-menu-right"> + <ul class="list-unstyled"> + <li><a href="/repo/create"><i class="fa fa-book"></i>Repository</a></li> + <li><a href="/repo/mirror"><i class="fa fa-clipboard"></i>Mirror</a></li> + <li><a href="#"><i class="fa fa-users"></i>Organization</a></li> + </ul> + </div> + </div> </div> <div class="panel-body"> <ul class="list-group">{{range .MyRepos}} diff --git a/templates/user/forgot_passwd.tmpl b/templates/user/forgot_passwd.tmpl new file mode 100644 index 00000000..a099ff27 --- /dev/null +++ b/templates/user/forgot_passwd.tmpl @@ -0,0 +1,32 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +<div id="body" class="container"> + <form action="/user/forget_password" method="post" class="form-horizontal card" id="login-card"> + {{.CsrfTokenHtml}} + <h3>Reset Your Password</h3> + <div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div> + {{if .IsResetSent}} + <p>A confirmation e-mail has been sent to <b>{{.Email}}</b>, please check your inbox within {{.Hours}} hours.</p> + <hr/> + <a href="http://{{Mail2Domain .Email}}" class="btn btn-lg btn-success">Sign in to your e-mail</a> + {{else if .IsResetRequest}} + <div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> + <label class="col-md-3 control-label">Email: </label> + <div class="col-md-7"> + <input name="email" class="form-control" placeholder="Type your e-mail address" required="required"> + </div> + </div> + <hr/> + <div class="form-group"> + <div class="col-md-offset-4 col-md-6"> + <button type="submit" class="btn btn-lg btn-primary">Click here to send reset confirmation e-mail</button> + </div> + </div> + {{else if .IsResetDisable}} + <p>Sorry, mail service is not enabled.</p> + {{else if .ResendLimited}} + <p>Sorry, you are sending e-mail too frequently, please wait 3 minutes.</p> + {{end}} + </form> +</div> +{{template "base/footer" .}}
\ No newline at end of file diff --git a/templates/user/reset_passwd.tmpl b/templates/user/reset_passwd.tmpl new file mode 100644 index 00000000..9190c7c1 --- /dev/null +++ b/templates/user/reset_passwd.tmpl @@ -0,0 +1,26 @@ +{{template "base/head" .}} +{{template "base/navbar" .}} +<div id="body" class="container"> + <form action="/user/reset_password?code={{.Code}}" method="post" class="form-horizontal card" id="login-card"> + {{.CsrfTokenHtml}} + <h3>Reset Your Pasword</h3> + <div class="alert alert-danger form-error{{if .HasError}}{{else}} hidden{{end}}">{{.ErrorMsg}}</div> + {{if .IsResetForm}} + <div class="form-group"> + <label class="col-md-4 control-label">Password: </label> + <div class="col-md-6"> + <input name="passwd" type="password" class="form-control" placeholder="Type your password" required="required"> + </div> + </div> + <hr/> + <div class="form-group"> + <div class="col-md-offset-4 col-md-6"> + <button type="submit" class="btn btn-lg btn-primary">Click here to reset your password</button> + </div> + </div> + {{else}} + <p>Sorry, your confirmation code has been exipired or not valid.</p> + {{end}} + </form> +</div> +{{template "base/footer" .}}
\ No newline at end of file diff --git a/templates/user/setting.tmpl b/templates/user/setting.tmpl index b32689fe..d5828338 100644 --- a/templates/user/setting.tmpl +++ b/templates/user/setting.tmpl @@ -10,9 +10,10 @@ {{if .IsSuccess}}<p class="alert alert-success">Your profile has been successfully updated.</p>{{else if .HasError}}<p class="alert alert-danger form-error">{{.ErrorMsg}}</p>{{end}} <p>Your Email will be public and used for Account related notifications and any web based operations made via the web.</p> <div class="form-group"> - <label class="col-md-2 control-label">Username<strong class="text-danger">*</strong></label> + <label class="col-md-2 control-label" for="user-setting-username">Username<strong class="text-danger">*</strong></label> <div class="col-md-8"> - <input name="username" class="form-control" placeholder="Type your user name" required="required" value="{{.SignedUser.Name}}" title="{{.SignedUser.Name}}"> + <input name="username" class="form-control" placeholder="Type your user name" required="required" value="{{.SignedUser.Name}}" title="{{.SignedUser.Name}}" id="user-setting-username"> + <p class="help-block hidden"><span class="text-danger">Cautious : </span>your username is changing !</p> </div> </div> diff --git a/templates/user/signin.tmpl b/templates/user/signin.tmpl index b6c39af1..eb4cb9cc 100644 --- a/templates/user/signin.tmpl +++ b/templates/user/signin.tmpl @@ -33,7 +33,7 @@ <div class="form-group"> <div class="col-md-offset-4 col-md-6"> <button type="submit" class="btn btn-lg btn-primary">Log In</button> - <a href="/forget-password/">Forgot your password?</a> + <a href="/user/forget_password/">Forgot your password?</a> </div> </div> @@ -43,9 +43,12 @@ </div> </div> + {{if .OauthEnabled}} <div class="form-group text-center" id="social-login"> - <a class="btn btn-danger btn-lg" href="/user/sign_up">Register new account</a> + <h4>Log In with Social Accounts</h4> + {{if .OauthGitHubEnabled}}<a href="/user/login/github"><i class="fa fa-github-square fa-3x"></i></a>{{end}} </div> + {{end}} </form> </div> {{template "base/footer" .}}
\ No newline at end of file @@ -6,7 +6,6 @@ package main import ( "container/list" - "fmt" "os" "os/exec" "path" @@ -14,11 +13,11 @@ import ( "strings" "github.com/codegangsta/cli" + qlog "github.com/qiniu/log" + "github.com/gogits/git" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" - //"github.com/qiniu/log" ) var CmdUpdate = cli.Command{ @@ -31,46 +30,23 @@ gogs serv provide access auth for repositories`, } func newUpdateLogger(execDir string) { - level := "0" logPath := execDir + "/log/update.log" os.MkdirAll(path.Dir(logPath), os.ModePerm) - log.NewLogger(0, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, logPath)) - log.Trace("start logging...") -} - -// for command: ./gogs update -func runUpdate(c *cli.Context) { - execDir, _ := base.ExecDir() - newLogger(execDir) - - base.NewConfigContext() - models.LoadModelsConfig() - - if models.UseSQLite3 { - os.Chdir(execDir) - } - models.SetEngine() - - args := c.Args() - if len(args) != 3 { - log.Error("received less 3 parameters") - return + f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm) + if err != nil { + qlog.Fatal(err) } - refName := args[0] - if refName == "" { - log.Error("refName is empty, shouldn't use") - return - } - oldCommitId := args[1] - newCommitId := args[2] + qlog.SetOutput(f) + qlog.Info("Start logging update...") +} +func update(refName, oldCommitId, newCommitId string) { isNew := strings.HasPrefix(oldCommitId, "0000000") if isNew && strings.HasPrefix(newCommitId, "0000000") { - log.Error("old rev and new rev both 000000") - return + qlog.Fatal("old rev and new rev both 000000") } userName := os.Getenv("userName") @@ -86,20 +62,17 @@ func runUpdate(c *cli.Context) { repo, err := git.OpenRepository(f) if err != nil { - log.Error("runUpdate.Open repoId: %v", err) - return + qlog.Fatalf("runUpdate.Open repoId: %v", err) } newOid, err := git.NewOidFromString(newCommitId) if err != nil { - log.Error("runUpdate.Ref repoId: %v", err) - return + qlog.Fatalf("runUpdate.Ref repoId: %v", err) } newCommit, err := repo.LookupCommit(newOid) if err != nil { - log.Error("runUpdate.Ref repoId: %v", err) - return + qlog.Fatalf("runUpdate.Ref repoId: %v", err) } var l *list.List @@ -107,39 +80,33 @@ func runUpdate(c *cli.Context) { if isNew { l, err = repo.CommitsBefore(newCommit.Id()) if err != nil { - log.Error("Find CommitsBefore erro:", err) - return + qlog.Fatalf("Find CommitsBefore erro:", err) } } else { oldOid, err := git.NewOidFromString(oldCommitId) if err != nil { - log.Error("runUpdate.Ref repoId: %v", err) - return + qlog.Fatalf("runUpdate.Ref repoId: %v", err) } oldCommit, err := repo.LookupCommit(oldOid) if err != nil { - log.Error("runUpdate.Ref repoId: %v", err) - return + qlog.Fatalf("runUpdate.Ref repoId: %v", err) } l = repo.CommitsBetween(newCommit, oldCommit) } if err != nil { - log.Error("runUpdate.Commit repoId: %v", err) - return + qlog.Fatalf("runUpdate.Commit repoId: %v", err) } sUserId, err := strconv.Atoi(userId) if err != nil { - log.Error("runUpdate.Parse userId: %v", err) - return + qlog.Fatalf("runUpdate.Parse userId: %v", err) } repos, err := models.GetRepositoryByName(int64(sUserId), repoName) if err != nil { - log.Error("runUpdate.GetRepositoryByName userId: %v", err) - return + qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err) } commits := make([]*base.PushCommit, 0) @@ -163,6 +130,35 @@ func runUpdate(c *cli.Context) { //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()}) if err = models.CommitRepoAction(int64(sUserId), userName, actEmail, repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil { - log.Error("runUpdate.models.CommitRepoAction: %v", err) + qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err) } } + +// for command: ./gogs update +func runUpdate(c *cli.Context) { + execDir, _ := base.ExecDir() + newUpdateLogger(execDir) + + base.NewConfigContext() + models.LoadModelsConfig() + + if models.UseSQLite3 { + os.Chdir(execDir) + } + + models.SetEngine() + + args := c.Args() + if len(args) != 3 { + qlog.Fatal("received less 3 parameters") + } + + refName := args[0] + if refName == "" { + qlog.Fatal("refName is empty, shouldn't use") + } + oldCommitId := args[1] + newCommitId := args[2] + + update(refName, oldCommitId, newCommitId) +} @@ -11,16 +11,16 @@ import ( "github.com/codegangsta/cli" "github.com/go-martini/martini" - // "github.com/martini-contrib/oauth2" - // "github.com/martini-contrib/sessions" - "github.com/gogits/binding" + qlog "github.com/qiniu/log" + "github.com/gogits/binding" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/avatar" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/modules/oauth2" "github.com/gogits/gogs/routers" "github.com/gogits/gogs/routers/admin" "github.com/gogits/gogs/routers/api/v1" @@ -51,29 +51,32 @@ func newMartini() *martini.ClassicMartini { } func runWeb(*cli.Context) { - fmt.Println("Server is running...") routers.GlobalInit() - log.Info("%s %s", base.AppName, base.AppVer) m := newMartini() // Middlewares. m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}})) - - // scope := "https://api.github.com/user" - // oauth2.PathCallback = "/oauth2callback" - // m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123")))) - // m.Use(oauth2.Github(&oauth2.Options{ - // ClientId: "09383403ff2dc16daaa1", - // ClientSecret: "5f6e7101d30b77952aab22b75eadae17551ea6b5", - // RedirectURL: base.AppUrl + oauth2.PathCallback, - // Scopes: []string{scope}, - // })) - m.Use(middleware.InitContext()) + if base.OauthService != nil { + if base.OauthService.GitHub.Enabled { + m.Use(oauth2.Github(&oauth2.Options{ + ClientId: base.OauthService.GitHub.ClientId, + ClientSecret: base.OauthService.GitHub.ClientSecret, + RedirectURL: base.AppUrl + oauth2.PathCallback[1:], + Scopes: []string{base.OauthService.GitHub.Scopes}, + })) + } + } + reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true}) ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView}) + ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{ + SignInRequire: base.Service.RequireSignInView, + DisableCsrf: true, + }) + reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true}) // Routers. @@ -92,9 +95,11 @@ func runWeb(*cli.Context) { m.Get("/avatar/:hash", avt.ServeHTTP) m.Group("/user", func(r martini.Router) { - // r.Any("/login/github", user.SocialSignIn) r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn) + r.Any("/login/github", user.SocialSignIn) r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) + r.Any("/forget_password", user.ForgotPasswd) + r.Any("/reset_password", user.ResetPasswd) }, reqSignOut) m.Group("/user", func(r martini.Router) { r.Any("/logout", user.SignOut) @@ -116,6 +121,7 @@ func runWeb(*cli.Context) { m.Get("/user/:username", ignSignIn, user.Profile) m.Any("/repo/create", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) + m.Any("/repo/mirror", reqSignIn, binding.BindIgnErr(auth.CreateRepoForm{}), repo.Mirror) adminReq := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true, AdminRequire: true}) @@ -148,6 +154,7 @@ func runWeb(*cli.Context) { r.Get("/issues", repo.Issues) r.Get("/issues/:index", repo.ViewIssue) r.Get("/releases", repo.Releases) + r.Any("/releases/new", repo.ReleasesNew) r.Get("/pulls", repo.Pulls) r.Get("/branches", repo.Branches) }, ignSignIn, middleware.RepoAssignment(true)) @@ -164,17 +171,26 @@ func runWeb(*cli.Context) { m.Group("/:username", func(r martini.Router) { r.Any("/:reponame/**", repo.Http) r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single) - }, ignSignIn) + }, ignSignInAndCsrf) // Not found handler. m.NotFound(routers.NotFound) + protocol := base.Cfg.MustValue("server", "PROTOCOL", "http") listenAddr := fmt.Sprintf("%s:%s", base.Cfg.MustValue("server", "HTTP_ADDR"), base.Cfg.MustValue("server", "HTTP_PORT", "3000")) - log.Info("Listen: %s", listenAddr) - if err := http.ListenAndServe(listenAddr, m); err != nil { - fmt.Println(err.Error()) - //log.Critical(err.Error()) // not working now + + if protocol == "http" { + log.Info("Listen: http://%s", listenAddr) + if err := http.ListenAndServe(listenAddr, m); err != nil { + qlog.Error(err.Error()) + } + } else if protocol == "https" { + log.Info("Listen: https://%s", listenAddr) + if err := http.ListenAndServeTLS(listenAddr, base.Cfg.MustValue("server", "CERT_FILE"), + base.Cfg.MustValue("server", "KEY_FILE"), m); err != nil { + qlog.Error(err.Error()) + } } } |