diff --git a/models/models.go b/models/models.go index f1d43531..0e20a1ab 100644 --- a/models/models.go +++ b/models/models.go @@ -33,7 +33,7 @@ var ( func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), - new(Mirror)) + new(Mirror), new(Release)) } func LoadModelsConfig() { diff --git a/models/release.go b/models/release.go new file mode 100644 index 00000000..08d74d3f --- /dev/null +++ b/models/release.go @@ -0,0 +1,83 @@ +// 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 ( + "errors" + "strings" + "time" + + "github.com/Unknwon/com" + "github.com/gogits/git" +) + +var ( + ErrReleaseAlreadyExist = errors.New("Release already exist") +) + +// Release represents a release of repository. +type Release struct { + Id int64 + RepoId int64 + PublisherId int64 + Publisher *User `xorm:"-"` + Title string + TagName string + LowerTagName string + SHA1 string + NumCommits int + NumCommitsBehind int `xorm:"-"` + Note string `xorm:"TEXT"` + IsPrerelease bool + Created time.Time `xorm:"created"` +} + +// GetReleasesByRepoId returns a list of releases of repository. +func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) { + err = orm.Desc("created").Find(&rels, Release{RepoId: repoId}) + return rels, err +} + +// IsReleaseExist returns true if release with given tag name already exists. +func IsReleaseExist(repoId int64, tagName string) (bool, error) { + if len(tagName) == 0 { + return false, nil + } + + return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)}) +} + +// CreateRelease creates a new release of repository. +func CreateRelease(repoPath string, rel *Release, gitRepo *git.Repository) error { + isExist, err := IsReleaseExist(rel.RepoId, rel.TagName) + if err != nil { + return err + } else if isExist { + return ErrReleaseAlreadyExist + } + + if !git.IsTagExist(repoPath, rel.TagName) { + _, stderr, err := com.ExecCmdDir(repoPath, "git", "tag", rel.TagName, "-m", "\""+rel.Title+"\"") + if err != nil { + return err + } else if strings.Contains(stderr, "fatal:") { + return errors.New(stderr) + } + } else { + commit, err := gitRepo.GetCommitOfTag(rel.TagName) + if err != nil { + return err + } + + rel.NumCommits, err = commit.CommitsCount() + if err != nil { + return err + } + } + + rel.LowerTagName = strings.ToLower(rel.TagName) + _, err = orm.InsertOne(rel) + return err +} diff --git a/modules/auth/release.go b/modules/auth/release.go new file mode 100644 index 00000000..a29028e0 --- /dev/null +++ b/modules/auth/release.go @@ -0,0 +1,50 @@ +// 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 auth + +import ( + "net/http" + "reflect" + + "github.com/go-martini/martini" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" +) + +type NewReleaseForm struct { + TagName string `form:"tag_name" binding:"Required"` + Title string `form:"title" binding:"Required"` + Content string `form:"content" binding:"Required"` + Prerelease bool `form:"prerelease"` +} + +func (f *NewReleaseForm) Name(field string) string { + names := map[string]string{ + "TagName": "Tag name", + "Title": "Release title", + "Content": "Release content", + } + return names[field] +} + +func (f *NewReleaseForm) Validate(errors *base.BindingErrors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("NewReleaseForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/modules/base/markdown.go b/modules/base/markdown.go index cc180775..95b4b212 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -166,3 +166,7 @@ func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { // fmt.Println(string(body)) return body } + +func RenderMarkdownString(raw, urlPrefix string) string { + return string(RenderMarkdown([]byte(raw), urlPrefix)) +} diff --git a/routers/repo/release.go b/routers/repo/release.go index 3d549bf7..a4baa479 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -5,25 +5,152 @@ package repo import ( + "sort" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" ) +type ReleaseSorter struct { + rels []*models.Release +} + +func (rs *ReleaseSorter) Len() int { + return len(rs.rels) +} + +func (rs *ReleaseSorter) Less(i, j int) bool { + return rs.rels[i].NumCommits > rs.rels[j].NumCommits +} + +func (rs *ReleaseSorter) Swap(i, j int) { + rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i] +} + func Releases(ctx *middleware.Context) { ctx.Data["Title"] = "Releases" ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoReleaseNew"] = false - tags, err := ctx.Repo.GitRepo.GetTags() + rawTags, err := ctx.Repo.GitRepo.GetTags() if err != nil { - ctx.Handle(404, "repo.Releases(GetTags)", err) + ctx.Handle(500, "release.Releases(GetTags)", err) return } - ctx.Data["Releases"] = tags + + rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id) + if err != nil { + ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err) + return + } + + commitsCount, err := ctx.Repo.Commit.CommitsCount() + if err != nil { + ctx.Handle(500, "release.Releases(CommitsCount)", err) + return + } + + var tags ReleaseSorter + tags.rels = make([]*models.Release, len(rawTags)) + for i, rawTag := range rawTags { + for _, rel := range rels { + if rel.TagName == rawTag { + rel.Publisher, err = models.GetUserById(rel.PublisherId) + if err != nil { + ctx.Handle(500, "release.Releases(GetUserById)", err) + return + } + rel.NumCommitsBehind = commitsCount - rel.NumCommits + rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) + tags.rels[i] = rel + break + } + } + + if tags.rels[i] == nil { + commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag) + if err != nil { + ctx.Handle(500, "release.Releases(GetCommitOfTag)", err) + return + } + + tags.rels[i] = &models.Release{ + Title: rawTag, + TagName: rawTag, + SHA1: commit.Id.String(), + } + tags.rels[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) + if err != nil { + ctx.Handle(500, "release.Releases(CommitsCount)", err) + return + } + tags.rels[i].NumCommitsBehind = commitsCount - tags.rels[i].NumCommits + tags.rels[i].Created = commit.Author.When + } + } + + sort.Sort(&tags) + + ctx.Data["Releases"] = tags.rels ctx.HTML(200, "release/list") } func ReleasesNew(ctx *middleware.Context) { + if !ctx.Repo.IsOwner { + ctx.Handle(404, "release.ReleasesNew", nil) + return + } + ctx.Data["Title"] = "New Release" ctx.Data["IsRepoToolbarReleases"] = true ctx.Data["IsRepoReleaseNew"] = true ctx.HTML(200, "release/new") } + +func ReleasesNewPost(ctx *middleware.Context, form auth.NewReleaseForm) { + if !ctx.Repo.IsOwner { + ctx.Handle(404, "release.ReleasesNew", nil) + return + } + + ctx.Data["Title"] = "New Release" + ctx.Data["IsRepoToolbarReleases"] = true + ctx.Data["IsRepoReleaseNew"] = true + + if ctx.HasError() { + ctx.HTML(200, "release/new") + return + } + + commitsCount, err := ctx.Repo.Commit.CommitsCount() + if err != nil { + ctx.Handle(500, "release.ReleasesNewPost(CommitsCount)", err) + return + } + + rel := &models.Release{ + RepoId: ctx.Repo.Repository.Id, + PublisherId: ctx.User.Id, + Title: form.Title, + TagName: form.TagName, + SHA1: ctx.Repo.Commit.Id.String(), + NumCommits: commitsCount, + Note: form.Content, + IsPrerelease: form.Prerelease, + } + + if err = models.CreateRelease(models.RepoPath(ctx.User.Name, ctx.Repo.Repository.Name), + rel, ctx.Repo.GitRepo); err != nil { + if err == models.ErrReleaseAlreadyExist { + ctx.RenderWithErr("Release with this tag name has already existed", "release/new", &form) + } else { + ctx.Handle(500, "release.ReleasesNewPost(IsReleaseExist)", err) + } + return + } + log.Trace("%s Release created: %s/%s:%s", ctx.Req.RequestURI, ctx.User.LowerName, ctx.Repo.Repository.Name, form.TagName) + + ctx.Redirect(ctx.Repo.RepoLink + "/releases") +} diff --git a/templates/release/list.tmpl b/templates/release/list.tmpl index 9541265c..dd37e9c1 100644 --- a/templates/release/list.tmpl +++ b/templates/release/list.tmpl @@ -10,50 +10,47 @@
- 1 week ago - 0 commits since this tag + + {{if .Created}}{{TimeSince .Created}}{{end}} + {{.NumCommitsBehind}} commits since this release
-- Source Code (ZIP) - Source Code (TAR.GZ) + Source Code (ZIP) +