From 197c4d4a5ba8a9540c49879324194f5f6be4689c Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 25 Mar 2014 08:53:11 -0400 Subject: Fix wrong serve command log location and commit repo action --- models/action.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'models') diff --git a/models/action.go b/models/action.go index 44d7aea8..dffc0e53 100644 --- a/models/action.go +++ b/models/action.go @@ -59,7 +59,7 @@ func (a Action) GetContent() string { // CommitRepoAction records action for commit repository. func CommitRepoAction(userId int64, userName string, repoId int64, repoName string, refName string, commits *base.PushCommits) error { - log.Trace("action.CommitRepoAction: %d/%s", userId, repoName) + log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName) bs, err := json.Marshal(commits) if err != nil { @@ -92,8 +92,8 @@ func CommitRepoAction(userId int64, userName string, }) if err != nil { log.Error("action.CommitRepoAction(notify watches): %d/%s", userId, repoName) + return err } - return err } // Update repository last update time. @@ -107,6 +107,8 @@ func CommitRepoAction(userId int64, userName string, log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName) return err } + + log.Trace("action.CommitRepoAction(end): %d/%s", userId, repoName) return nil } -- cgit v1.2.3 From d3b8e9daa1a22501c03564f2739f9fa8198fbdf1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 25 Mar 2014 14:04:57 -0400 Subject: Add notify watcher action --- README.md | 2 +- README_ZH.md | 2 +- models/action.go | 29 ++----------------- models/issue.go | 1 + models/repo.go | 71 ++++++++++++++++++++++++++++++++++------------- modules/base/tool.go | 21 ++++++++++---- routers/repo/issue.go | 40 ++++++++++++++++++++++---- templates/issue/list.tmpl | 46 ++++++++---------------------- 8 files changed, 118 insertions(+), 94 deletions(-) (limited to 'models') diff --git a/README.md b/README.md index a39a92a3..d99031c6 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ More importantly, Gogs only needs one binary to setup your own project hosting o - Create/delete/watch public repository. - User profile page. - Repository viewer. -- Gravatar support. +- Gravatar and cache support. - Mail service(register). - Administration panel. - Supports MySQL, PostgreSQL and SQLite3(binary release only). diff --git a/README_ZH.md b/README_ZH.md index 440f952f..0a4d3bdc 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -28,7 +28,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 - 创建/删除/关注公开仓库 - 用户个人信息页面 - 仓库浏览器 -- Gravatar 支持 +- Gravatar 以及缓存支持 - 邮件服务(注册) - 管理员面板 - 支持 MySQL、PostgreSQL 以及 SQLite3(仅限二进制版本) diff --git a/models/action.go b/models/action.go index dffc0e53..edf1bf58 100644 --- a/models/action.go +++ b/models/action.go @@ -19,6 +19,7 @@ const ( OP_STAR_REPO OP_FOLLOW_REPO OP_COMMIT_REPO + OP_CREATE_ISSUE OP_PULL_REQUEST ) @@ -67,34 +68,10 @@ func CommitRepoAction(userId int64, userName string, return err } - // Add feeds for user self and all watchers. - watches, err := GetWatches(repoId) - if err != nil { - log.Error("action.CommitRepoAction(get watches): %d/%s", userId, repoName) + if err = NotifyWatchers(userId, repoId, OP_COMMIT_REPO, userName, repoName, refName, string(bs)); err != nil { + log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName) return err } - watches = append(watches, Watch{UserId: userId}) - - for i := range watches { - if userId == watches[i].UserId && i > 0 { - continue // Do not add twice in case author watches his/her repository. - } - - _, err = orm.InsertOne(&Action{ - UserId: watches[i].UserId, - ActUserId: userId, - ActUserName: userName, - OpType: OP_COMMIT_REPO, - Content: string(bs), - RepoId: repoId, - RepoName: repoName, - RefName: refName, - }) - if err != nil { - log.Error("action.CommitRepoAction(notify watches): %d/%s", userId, repoName) - return err - } - } // Update repository last update time. repo, err := GetRepositoryByName(userId, repoName) diff --git a/models/issue.go b/models/issue.go index fe43a94b..2bdd083d 100644 --- a/models/issue.go +++ b/models/issue.go @@ -23,6 +23,7 @@ type Issue struct { Name string RepoId int64 `xorm:"index"` PosterId int64 + Poster *User `xorm:"-"` MilestoneId int64 AssigneeId int64 IsPull bool // Indicates whether is a pull request or not. diff --git a/models/repo.go b/models/repo.go index d5f9be72..824d5ba0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -262,27 +262,27 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep } /* - // hook/post-update - pu, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-update"), os.O_CREATE|os.O_WRONLY, 0777) - if err != nil { - return err - } - defer pu.Close() - // TODO: Windows .bat - if _, err = pu.WriteString(fmt.Sprintf("#!/usr/bin/env bash\n%s update\n", appPath)); err != nil { - return err - } + // hook/post-update + pu, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-update"), os.O_CREATE|os.O_WRONLY, 0777) + if err != nil { + return err + } + defer pu.Close() + // TODO: Windows .bat + if _, err = pu.WriteString(fmt.Sprintf("#!/usr/bin/env bash\n%s update\n", appPath)); err != nil { + return err + } - // hook/post-update - pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777) - if err != nil { - return err - } - defer pu2.Close() - // TODO: Windows .bat - if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil { - return err - } + // hook/post-update + pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777) + if err != nil { + return err + } + defer pu2.Close() + // TODO: Windows .bat + if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil { + return err + } */ // Initialize repository according to user's choice. @@ -506,6 +506,37 @@ func GetWatches(repoId int64) ([]Watch, error) { return watches, err } +// NotifyWatchers creates batch of actions for every watcher. +func NotifyWatchers(userId, repoId int64, opType int, userName, repoName, refName, content string) error { + // Add feeds for user self and all watchers. + watches, err := GetWatches(repoId) + if err != nil { + return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) + } + watches = append(watches, Watch{UserId: userId}) + + for i := range watches { + if userId == watches[i].UserId && i > 0 { + continue // Do not add twice in case author watches his/her repository. + } + + _, err = orm.InsertOne(&Action{ + UserId: watches[i].UserId, + ActUserId: userId, + ActUserName: userName, + OpType: opType, + Content: content, + RepoId: repoId, + RepoName: repoName, + RefName: refName, + }) + if err != nil { + return errors.New("repo.NotifyWatchers(create action): " + err.Error()) + } + } + return nil +} + // IsWatching checks if user has watched given repository. func IsWatching(userId, repoId int64) bool { has, _ := orm.Get(&Watch{0, repoId, userId}) diff --git a/modules/base/tool.go b/modules/base/tool.go index c23f5de6..8f38d492 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -486,15 +486,19 @@ func ActionIcon(opType int) string { return "plus-circle" case 5: // Commit repository. return "arrow-circle-o-right" + case 6: // Create issue. + return "exclamation-circle" default: return "invalid type" } } const ( - TPL_CREATE_REPO = `%s created repository %s` - TPL_COMMIT_REPO = `%s pushed to %s at %s/%s%s` - TPL_COMMIT_REPO_LI = `
user-avatar %s %s
` + TPL_CREATE_REPO = `%s created repository %s` + TPL_COMMIT_REPO = `%s pushed to %s at %s%s` + TPL_COMMIT_REPO_LI = `
user-avatar %s %s
` + TPL_CREATE_Issue = `%s opened issue %s#%s +
user-avatar %s
` ) type PushCommits struct { @@ -507,11 +511,12 @@ type PushCommits struct { func ActionDesc(act Actioner, avatarLink string) string { actUserName := act.GetActUserName() 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, actUserName, repoName, repoName) + 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 { @@ -519,13 +524,17 @@ func ActionDesc(act Actioner, avatarLink string) string { } buf := bytes.NewBuffer([]byte("\n")) for _, commit := range push.Commits { - buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, avatarLink, actUserName, repoName, commit[0], commit[0][:7], commit[1]) + "\n") + buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, avatarLink, repoLink, commit[0], commit[0][:7], commit[1]) + "\n") } if push.Len > 3 { buf.WriteString(fmt.Sprintf(`
%d other commits >>
`, actUserName, repoName, branch, push.Len)) } - return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, actUserName, repoName, branch, branch, actUserName, repoName, actUserName, repoName, + 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, infos[1]) default: return "invalid type" } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 4e832460..fc5bb986 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -23,13 +23,33 @@ func Issues(ctx *middleware.Context, params martini.Params) { milestoneId, _ := base.StrTo(params["milestone"]).Int() page, _ := base.StrTo(params["page"]).Int() - var err error - ctx.Data["Issues"], err = models.GetIssues(0, ctx.Repo.Repository.Id, 0, + // Get issues. + issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, 0, int64(milestoneId), page, params["state"] == "closed", false, params["labels"], params["sortType"]) if err != nil { ctx.Handle(200, "issue.Issues: %v", err) return } + + var closedCount int + // Get posters. + for i := range issues { + u, err := models.GetUserById(issues[i].PosterId) + if err != nil { + ctx.Handle(200, "issue.Issues(get poster): %v", err) + return + } + + if issues[i].IsClosed { + closedCount++ + } + issues[i].Poster = u + } + + ctx.Data["Issues"] = issues + ctx.Data["IssueCount"] = len(issues) + ctx.Data["OpenCount"] = len(issues) - closedCount + ctx.Data["ClosedCount"] = closedCount ctx.HTML(200, "issue/list") } @@ -54,12 +74,20 @@ 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, form.IssueName, form.Labels, form.Content, false) - if err == nil { - log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) - ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index)) + if err != nil { + ctx.Handle(200, "issue.CreateIssue", err) return } - ctx.Handle(200, "issue.CreateIssue", err) + + // Notify watchers. + if err = models.NotifyWatchers(ctx.User.Id, ctx.Repo.Repository.Id, models.OP_CREATE_ISSUE, + ctx.User.Name, ctx.Repo.Repository.Name, "", fmt.Sprintf("%d|%s", issue.Index, issue.Name)); err != nil { + ctx.Handle(200, "issue.CreateIssue", err) + return + } + + log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) + ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index)) } func ViewIssue(ctx *middleware.Context, params martini.Params) { diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index 685eaeeb..0df68838 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -6,7 +6,7 @@
@@ -14,46 +14,24 @@
{{range .Issues}} -
- {{end}} -
-
- -
- #123 -
Bug: When running tests after generating a beego app, templates do not load.
+
+ #{{.Index}} +
{{.Name}}

- - Obama - 3 days ago - 3 -

-
-
- #123 -
Bug: When running tests after generating a beego app, templates do not load.
-

- - Obama - 3 days ago - 3 + + {{.Poster.Name}} + {{TimeSince .Created}} + {{.NumComments}}

+ {{end}} +
-- cgit v1.2.3 From c1a3d4fefbbbf332cd1cedda66e93bf40cc9713d Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 25 Mar 2014 21:37:18 -0400 Subject: Add mail notify for creating issue --- gogs.go | 2 +- models/issue.go | 1 - modules/mailer/mail.go | 40 ++++++++++++++++++++++++++++++++++++++-- modules/mailer/mailer.go | 2 +- routers/repo/issue.go | 9 +++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) (limited to 'models') diff --git a/gogs.go b/gogs.go index b62580f8..f5a328ad 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true -const APP_VER = "0.1.7.0325" +const APP_VER = "0.1.8.0325" func init() { base.AppVer = APP_VER diff --git a/models/issue.go b/models/issue.go index 2bdd083d..2de65685 100644 --- a/models/issue.go +++ b/models/issue.go @@ -59,7 +59,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co Content: content, } _, err = orm.Insert(issue) - // TODO: newIssueAction return issue, err } diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go index 92acd20e..d0decbe0 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -6,6 +6,7 @@ package mailer import ( "encoding/hex" + "errors" "fmt" "github.com/gogits/gogs/models" @@ -15,12 +16,17 @@ import ( ) // Create New mail message use MailFrom and MailUser -func NewMailMessage(To []string, subject, body string) Message { - msg := NewHtmlMessage(To, base.MailService.User, subject, body) +func NewMailMessageFrom(To []string, from, subject, body string) Message { + msg := NewHtmlMessage(To, from, subject, body) msg.User = base.MailService.User return msg } +// Create New mail message use MailFrom and MailUser +func NewMailMessage(To []string, subject, body string) Message { + return NewMailMessageFrom(To, base.MailService.User, subject, body) +} + func GetMailTmplData(user *models.User) map[interface{}]interface{} { data := make(map[interface{}]interface{}, 10) data["AppName"] = base.AppName @@ -84,3 +90,33 @@ func SendActiveMail(r *middleware.Render, user *models.User) { SendAsync(&msg) } + +// SendNotifyMail sends mail notification of all watchers. +func SendNotifyMail(userId, repoId int64, userName, repoName, subject, content string) error { + watches, err := models.GetWatches(repoId) + if err != nil { + return errors.New("mail.NotifyWatchers(get watches): " + err.Error()) + } + + tos := make([]string, 0, len(watches)) + for i := range watches { + uid := watches[i].UserId + if userId == uid { + continue + } + u, err := models.GetUserById(uid) + if err != nil { + return errors.New("mail.NotifyWatchers(get user): " + err.Error()) + } + tos = append(tos, u.Email) + } + + if len(tos) == 0 { + return nil + } + + msg := NewMailMessageFrom(tos, userName, subject, content) + msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject) + SendAsync(&msg) + return nil +} diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index da63e01d..63861d87 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -33,7 +33,7 @@ func (m Message) Content() string { } // create mail content - content := "From: " + m.User + "<" + m.From + + content := "From: " + m.From + "<" + m.User + ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body return content } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index fc5bb986..242593ff 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -13,6 +13,7 @@ import ( "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/mailer" "github.com/gogits/gogs/modules/middleware" ) @@ -86,6 +87,14 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat return } + // Mail watchers. + if base.Service.NotifyMail { + if err = mailer.SendNotifyMail(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); err != nil { + ctx.Handle(200, "issue.CreateIssue", err) + return + } + } + log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index)) } -- cgit v1.2.3 From 87de66561c0ecc14f42a24242d850b586f143080 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 25 Mar 2014 23:53:01 -0400 Subject: Start working on diff page --- README.md | 2 +- README_ZH.md | 4 +- diff.txt | 137 +++++++++++++++++++ models/git.go | 272 +++++++++++++++++++++++++++++++++++++ models/repo.go | 242 +-------------------------------- routers/repo/commit.go | 22 ++- templates/repo/diff.tmpl | 342 +++++++++++++++++++++-------------------------- 7 files changed, 591 insertions(+), 430 deletions(-) create mode 100644 diff.txt create mode 100644 models/git.go (limited to 'models') diff --git a/README.md b/README.md index 5a57eb5f..debe834b 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language ![Demo](http://gowalker.org/public/gogs_demo.gif) -##### Current version: 0.1.7 Alpha +##### Current version: 0.1.8 Alpha #### Other language version diff --git a/README_ZH.md b/README_ZH.md index 0a4d3bdc..ee9c3b7c 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。 ![Demo](http://gowalker.org/public/gogs_demo.gif) -##### 当前版本:0.1.7 Alpha +##### 当前版本:0.1.8 Alpha ## 开发目的 @@ -16,7 +16,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依 ## 项目概览 - 有关项目设计、开发说明、变更日志和路线图,请通过 [Wiki](https://github.com/gogits/gogs/wiki) 查看。 -- 您可以到 [Trello Broad](https://trello.com/b/uxAoeLUl/gogs-go-git-service) 跟随开发团队的脚步。 +- 您可以到 [Trello Board](https://trello.com/b/uxAoeLUl/gogs-go-git-service) 跟随开发团队的脚步。 - 想要先睹为快?通过 [在线体验](http://try.gogits.org/Unknown/gogs) 或查看 **安装部署 -> 二进制安装** 小节。 - 使用过程中遇到问题?尝试从 [故障排查](https://github.com/gogits/gogs/wiki/Troubleshooting) 页面获取帮助。 diff --git a/diff.txt b/diff.txt new file mode 100644 index 00000000..7b2522f8 --- /dev/null +++ b/diff.txt @@ -0,0 +1,137 @@ +commit c1a3d4fefbbbf332cd1cedda66e93bf40cc9713d +Author: Unknown +Date: Tue Mar 25 21:37:18 2014 -0400 + + Add mail notify for creating issue + +diff --git a/gogs.go b/gogs.go +index b62580f..f5a328a 100644 +--- a/gogs.go ++++ b/gogs.go +@@ -19,7 +19,7 @@ import ( + // Test that go1.2 tag above is included in builds. main.go refers to this definition. + const go12tag = true + +-const APP_VER = "0.1.7.0325" ++const APP_VER = "0.1.8.0325" + + func init() { + base.AppVer = APP_VER +diff --git a/models/issue.go b/models/issue.go +index 2bdd083..2de6568 100644 +--- a/models/issue.go ++++ b/models/issue.go +@@ -59,7 +59,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co + Content: content, + } + _, err = orm.Insert(issue) +- // TODO: newIssueAction + return issue, err + } + +diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go +index 92acd20..d0decbe 100644 +--- a/modules/mailer/mail.go ++++ b/modules/mailer/mail.go +@@ -6,6 +6,7 @@ package mailer + + import ( + "encoding/hex" ++ "errors" + "fmt" + + "github.com/gogits/gogs/models" +@@ -15,12 +16,17 @@ import ( + ) + + // Create New mail message use MailFrom and MailUser +-func NewMailMessage(To []string, subject, body string) Message { +- msg := NewHtmlMessage(To, base.MailService.User, subject, body) ++func NewMailMessageFrom(To []string, from, subject, body string) Message { ++ msg := NewHtmlMessage(To, from, subject, body) + msg.User = base.MailService.User + return msg + } + ++// Create New mail message use MailFrom and MailUser ++func NewMailMessage(To []string, subject, body string) Message { ++ return NewMailMessageFrom(To, base.MailService.User, subject, body) ++} ++ + func GetMailTmplData(user *models.User) map[interface{}]interface{} { + data := make(map[interface{}]interface{}, 10) + data["AppName"] = base.AppName +@@ -84,3 +90,33 @@ func SendActiveMail(r *middleware.Render, user *models.User) { + + SendAsync(&msg) + } ++ ++// SendNotifyMail sends mail notification of all watchers. ++func SendNotifyMail(userId, repoId int64, userName, repoName, subject, content string) error { ++ watches, err := models.GetWatches(repoId) ++ if err != nil { ++ return errors.New("mail.NotifyWatchers(get watches): " + err.Error()) ++ } ++ ++ tos := make([]string, 0, len(watches)) ++ for i := range watches { ++ uid := watches[i].UserId ++ if userId == uid { ++ continue ++ } ++ u, err := models.GetUserById(uid) ++ if err != nil { ++ return errors.New("mail.NotifyWatchers(get user): " + err.Error()) ++ } ++ tos = append(tos, u.Email) ++ } ++ ++ if len(tos) == 0 { ++ return nil ++ } ++ ++ msg := NewMailMessageFrom(tos, userName, subject, content) ++ msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject) ++ SendAsync(&msg) ++ return nil ++} +diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go +index da63e01..63861d8 100644 +--- a/modules/mailer/mailer.go ++++ b/modules/mailer/mailer.go +@@ -33,7 +33,7 @@ func (m Message) Content() string { + } + + // create mail content +- content := "From: " + m.User + "<" + m.From + ++ content := "From: " + m.From + "<" + m.User + + ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body + return content + } +diff --git a/routers/repo/issue.go b/routers/repo/issue.go +index fc5bb98..242593f 100644 +--- a/routers/repo/issue.go ++++ b/routers/repo/issue.go +@@ -13,6 +13,7 @@ import ( + "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" ++ "github.com/gogits/gogs/modules/mailer" + "github.com/gogits/gogs/modules/middleware" + ) + +@@ -86,6 +87,14 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat + return + } + ++ // Mail watchers. ++ if base.Service.NotifyMail { ++ if err = mailer.SendNotifyMail(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); err != nil { ++ ctx.Handle(200, "issue.CreateIssue", err) ++ return ++ } ++ } ++ + log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id) + ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index)) + } diff --git a/models/git.go b/models/git.go new file mode 100644 index 00000000..6a4bd610 --- /dev/null +++ b/models/git.go @@ -0,0 +1,272 @@ +// 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 ( + "container/list" + "fmt" + "path" + "strings" + + "github.com/Unknwon/com" + + "github.com/gogits/git" +) + +// RepoFile represents a file object in git repository. +type RepoFile struct { + *git.TreeEntry + Path string + Size int64 + Repo *git.Repository + Commit *git.Commit +} + +// LookupBlob returns the content of an object. +func (file *RepoFile) LookupBlob() (*git.Blob, error) { + if file.Repo == nil { + return nil, ErrRepoFileNotLoaded + } + + return file.Repo.LookupBlob(file.Id) +} + +// GetBranches returns all branches of given repository. +func GetBranches(userName, reposName string) ([]string, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + refs, err := repo.AllReferences() + if err != nil { + return nil, err + } + + brs := make([]string, len(refs)) + for i, ref := range refs { + brs[i] = ref.Name + } + return brs, nil +} + +func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + commit, err := repo.GetCommit(branchName, commitId) + if err != nil { + return nil, err + } + + parts := strings.Split(path.Clean(rpath), "/") + + var entry *git.TreeEntry + tree := commit.Tree + for i, part := range parts { + if i == len(parts)-1 { + entry = tree.EntryByName(part) + if entry == nil { + return nil, ErrRepoFileNotExist + } + } else { + tree, err = repo.SubTree(tree, part) + if err != nil { + return nil, err + } + } + } + + size, err := repo.ObjectSize(entry.Id) + if err != nil { + return nil, err + } + + repoFile := &RepoFile{ + entry, + rpath, + size, + repo, + commit, + } + + return repoFile, nil +} + +// GetReposFiles returns a list of file object in given directory of repository. +func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + + commit, err := repo.GetCommit(branchName, commitId) + if err != nil { + return nil, err + } + + var repodirs []*RepoFile + var repofiles []*RepoFile + commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int { + if dirname == rpath { + // TODO: size get method shoule be improved + size, err := repo.ObjectSize(entry.Id) + if err != nil { + 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 + } + } + } + } + } + } + + loop: + + rp := &RepoFile{ + entry, + path.Join(dirname, entry.Name), + size, + repo, + cm, + } + + if entry.IsFile() { + repofiles = append(repofiles, rp) + } else if entry.IsDir() { + repodirs = append(repodirs, rp) + } + } + return 0 + }) + + return append(repodirs, repofiles...), nil +} + +func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) { + repo, err := git.OpenRepository(RepoPath(userName, repoName)) + if err != nil { + return nil, err + } + + return repo.GetCommit(branchname, commitid) +} + +// GetCommits returns all commits of given branch of repository. +func GetCommits(userName, reposName, branchname string) (*list.List, error) { + repo, err := git.OpenRepository(RepoPath(userName, reposName)) + if err != nil { + return nil, err + } + r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) + if err != nil { + return nil, err + } + return r.AllCommits() +} + +type DiffFile struct { + Name string + Addition, Deletion int + Type string + Content []string +} + +type Diff struct { + NumFiles int // Number of file has been changed. + TotalAddition, TotalDeletion int + Files []*DiffFile +} + +func GetDiff(repoPath, commitid string) (*Diff, error) { + stdout, _, err := com.ExecCmdDir(repoPath, "git", "show", commitid) + if err != nil { + return nil, err + } + + // Sperate parts by file. + parts := strings.Split(stdout, "diff --git ") + + // First part is commit information. + // Check if it's a merge. + mergeIndex := strings.Index(parts[0], "merge") + if mergeIndex > -1 { + mergeCommit := strings.SplitN(strings.Split(parts[0], "\n")[1], "", 3)[2] + return GetDiff(repoPath, mergeCommit) + } + + diff := &Diff{NumFiles: len(parts[1:])} + diff.Files = make([]*DiffFile, 0, diff.NumFiles) + for _, part := range parts[1:] { + infos := strings.SplitN(part, "\n", 6) + infos[5] = strings.TrimSuffix(strings.TrimSuffix(infos[5], "\n"), "\n\\ No newline at end of file") + + file := &DiffFile{ + Name: strings.TrimPrefix(strings.Split(infos[0], " ")[0], "a/"), + Content: strings.Split(infos[5], "\n"), + } + diff.Files = append(diff.Files, file) + } + return diff, nil +} diff --git a/models/repo.go b/models/repo.go index 824d5ba0..868a5dc2 100644 --- a/models/repo.go +++ b/models/repo.go @@ -5,17 +5,14 @@ package models import ( - "container/list" "errors" "fmt" "io/ioutil" "os" "os/exec" - "path" "path/filepath" "regexp" "strings" - "sync" "time" "unicode/utf8" @@ -36,8 +33,6 @@ var ( ErrRepoFileNotLoaded = fmt.Errorf("repo file not loaded") ) -var gitInitLocker = sync.Mutex{} - var ( LanguageIgns, Licenses []string ) @@ -222,33 +217,21 @@ func extractGitBareZip(repoPath string) error { } // initRepoCommit temporarily changes with work directory. -func initRepoCommit(tmpPath string, sig *git.Signature) error { - gitInitLocker.Lock() - defer gitInitLocker.Unlock() - - // Change work directory. - curPath, err := os.Getwd() - if err != nil { - return err - } else if err = os.Chdir(tmpPath); err != nil { - return err - } - defer os.Chdir(curPath) - +func initRepoCommit(tmpPath string, sig *git.Signature) (err error) { var stderr string - if _, stderr, err = com.ExecCmd("git", "add", "--all"); err != nil { + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "add", "--all"); err != nil { return err } - log.Info("stderr(1): %s", stderr) - if _, stderr, err = com.ExecCmd("git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + log.Trace("stderr(1): %s", stderr) + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", "Init commit"); err != nil { return err } - log.Info("stderr(2): %s", stderr) - if _, stderr, err = com.ExecCmd("git", "push", "origin", "master"); err != nil { + log.Trace("stderr(2): %s", stderr) + if _, stderr, err = com.ExecCmdDir(tmpPath, "git", "push", "origin", "master"); err != nil { return err } - log.Info("stderr(3): %s", stderr) + log.Trace("stderr(3): %s", stderr) return nil } @@ -562,214 +545,3 @@ func UnWatchRepository() { func ForkRepository(reposName string, userId int64) { } - -// RepoFile represents a file object in git repository. -type RepoFile struct { - *git.TreeEntry - Path string - Size int64 - Repo *git.Repository - Commit *git.Commit -} - -// LookupBlob returns the content of an object. -func (file *RepoFile) LookupBlob() (*git.Blob, error) { - if file.Repo == nil { - return nil, ErrRepoFileNotLoaded - } - - return file.Repo.LookupBlob(file.Id) -} - -// GetBranches returns all branches of given repository. -func GetBranches(userName, reposName string) ([]string, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - - refs, err := repo.AllReferences() - if err != nil { - return nil, err - } - - brs := make([]string, len(refs)) - for i, ref := range refs { - brs[i] = ref.Name - } - return brs, nil -} - -func GetTargetFile(userName, reposName, branchName, commitId, rpath string) (*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - - commit, err := repo.GetCommit(branchName, commitId) - if err != nil { - return nil, err - } - - parts := strings.Split(path.Clean(rpath), "/") - - var entry *git.TreeEntry - tree := commit.Tree - for i, part := range parts { - if i == len(parts)-1 { - entry = tree.EntryByName(part) - if entry == nil { - return nil, ErrRepoFileNotExist - } - } else { - tree, err = repo.SubTree(tree, part) - if err != nil { - return nil, err - } - } - } - - size, err := repo.ObjectSize(entry.Id) - if err != nil { - return nil, err - } - - repoFile := &RepoFile{ - entry, - rpath, - size, - repo, - commit, - } - - return repoFile, nil -} - -// GetReposFiles returns a list of file object in given directory of repository. -func GetReposFiles(userName, reposName, branchName, commitId, rpath string) ([]*RepoFile, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - - commit, err := repo.GetCommit(branchName, commitId) - if err != nil { - return nil, err - } - - var repodirs []*RepoFile - var repofiles []*RepoFile - commit.Tree.Walk(func(dirname string, entry *git.TreeEntry) int { - if dirname == rpath { - // TODO: size get method shoule be improved - size, err := repo.ObjectSize(entry.Id) - if err != nil { - 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 - } - } - } - } - } - } - - loop: - - rp := &RepoFile{ - entry, - path.Join(dirname, entry.Name), - size, - repo, - cm, - } - - if entry.IsFile() { - repofiles = append(repofiles, rp) - } else if entry.IsDir() { - repodirs = append(repodirs, rp) - } - } - return 0 - }) - - return append(repodirs, repofiles...), nil -} - -func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) { - repo, err := git.OpenRepository(RepoPath(userName, repoName)) - if err != nil { - return nil, err - } - - return repo.GetCommit(branchname, commitid) -} - -// GetCommits returns all commits of given branch of repository. -func GetCommits(userName, reposName, branchname string) (*list.List, error) { - repo, err := git.OpenRepository(RepoPath(userName, reposName)) - if err != nil { - return nil, err - } - r, err := repo.LookupReference(fmt.Sprintf("refs/heads/%s", branchname)) - if err != nil { - return nil, err - } - return r.AllCommits() -} diff --git a/routers/repo/commit.go b/routers/repo/commit.go index e038998f..3d00f8d7 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -34,8 +34,24 @@ func Commits(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, "repo/commits") } -func Diff(ctx *middleware.Context,params martini.Params){ - ctx.Data["Title"] = "commit-sha" +func Diff(ctx *middleware.Context, params martini.Params) { + commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"]) + if err != nil { + ctx.Handle(404, "repo.Diff", err) + return + } + + diff, err := models.GetDiff(models.RepoPath(params["username"], params["reponame"]), params["commitid"]) + if err != nil { + ctx.Handle(404, "repo.Diff", err) + return + } + + shortSha := params["commitid"][:7] + ctx.Data["Title"] = commit.Message() + " · " + shortSha + ctx.Data["Commit"] = commit + ctx.Data["ShortSha"] = shortSha + ctx.Data["Diff"] = diff ctx.Data["IsRepoToolbarCommits"] = true - ctx.HTML(200,"repo/diff") + ctx.HTML(200, "repo/diff") } diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index 6a3c5fb3..2627c9e1 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -7,239 +7,203 @@
Browse Source -

bsongen: support for custom tags

+

{{.Commit.Message}}

- commit commit-sha + commit {{.ShortSha}}

- - author-name - times-ago + + {{.Commit.Author.Name}} + {{TimeSince .Commit.Author.When}}

- Show Diff Files + Show Diff Stats

- 5 changed files with 25 additions and 9 deletions. + {{.Diff.NumFiles}} changed files with {{.Diff.TotalAddition}} additions and {{.Diff.TotalDeletion}} deletions.

    + {{range .Diff.Files}}
  1. - 2 + {{.Addition}} - 4 + {{.Deletion}}
      - gopmweb.go -
  2. -
  3. -
    - 666 - - - - - 44 -
    -   - static/img/favicon.png -
  4. -
  5. -   - static/img/favicon.png -
  6. -
  7. -   - static/img/favicon.png + {{.Name}}
  8. + {{end}}
-
-
-
- BIN - - - - - -
- View File - data/test/bson_test/simple_type.png -
-
- - - - -
-
-
- + {{range .Diff.Files}}
- + 30 + + {{.Addition}} - - 4 + - {{.Deletion}}
View File - data/test/bson_test/simple_type.go + {{.Name}}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + {{range .Content}} + + + + + + {{end}} +
- 1 - - 1 - -
	"github.com/youtube/vitess/go/bson"
-
- 2 - - 2 - -
	"github.com/youtube/vitess/go/bson"
-
- 3 - - 3 - -
	"github.com/youtube/vitess/go/bson"
-
- + - - 4 - -
	"github.com/youtube/vitess/go/bson"
-
- + - - 5 - -
	"github.com/youtube/vitess/go/bson"
-
- 4 - - - - -
	"github.com/youtube/vitess/go/bson"
-
- 5 - - - - -
	"github.com/youtube/vitess/go/bson"
-
- 6 - - - - -
	"github.com/youtube/vitess/go/bson"
-
- 7 - - - - -
	"github.com/youtube/vitess/go/bson"
-
- 8 - - 6 - -
	"github.com/youtube/vitess/go/bson"
-
- 9 - - 7 - -
	"github.com/youtube/vitess/go/bson"
-
- 10 - - 8 - -
	"github.com/youtube/vitess/go/bson"
-
+ + + + +
{{.}}
+
+ {{end}} -
+
{{template "base/footer" .}} \ No newline at end of file -- cgit v1.2.3 From 06cf878471af02376dfcd02b9781982a89c27a2a Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 26 Mar 2014 00:13:01 -0400 Subject: More on diff page --- models/git.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'models') diff --git a/models/git.go b/models/git.go index 6a4bd610..b0b0dd2f 100644 --- a/models/git.go +++ b/models/git.go @@ -246,25 +246,27 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { } // Sperate parts by file. - parts := strings.Split(stdout, "diff --git ") + startIndex := strings.Index(stdout, "diff --git ") + 12 // First part is commit information. // Check if it's a merge. - mergeIndex := strings.Index(parts[0], "merge") + mergeIndex := strings.Index(stdout[:startIndex], "merge") if mergeIndex > -1 { - mergeCommit := strings.SplitN(strings.Split(parts[0], "\n")[1], "", 3)[2] + mergeCommit := strings.SplitN(strings.Split(stdout[:startIndex], "\n")[1], "", 3)[2] return GetDiff(repoPath, mergeCommit) } - diff := &Diff{NumFiles: len(parts[1:])} + parts := strings.Split(stdout[startIndex:], "diff --git ") + diff := &Diff{NumFiles: len(parts)} diff.Files = make([]*DiffFile, 0, diff.NumFiles) - for _, part := range parts[1:] { + for _, part := range parts { infos := strings.SplitN(part, "\n", 6) - infos[5] = strings.TrimSuffix(strings.TrimSuffix(infos[5], "\n"), "\n\\ No newline at end of file") + maxIndex := len(infos) - 1 + infos[maxIndex] = strings.TrimSuffix(strings.TrimSuffix(infos[maxIndex], "\n"), "\n\\ No newline at end of file") file := &DiffFile{ Name: strings.TrimPrefix(strings.Split(infos[0], " ")[0], "a/"), - Content: strings.Split(infos[5], "\n"), + Content: strings.Split(infos[maxIndex], "\n"), } diff.Files = append(diff.Files, file) } -- cgit v1.2.3