You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "errors"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/gogits/git"
  11. )
  12. var (
  13. ErrReleaseAlreadyExist = errors.New("Release already exist")
  14. )
  15. // Release represents a release of repository.
  16. type Release struct {
  17. Id int64
  18. RepoId int64
  19. PublisherId int64
  20. Publisher *User `xorm:"-"`
  21. Title string
  22. TagName string
  23. LowerTagName string
  24. SHA1 string
  25. NumCommits int
  26. NumCommitsBehind int `xorm:"-"`
  27. Note string `xorm:"TEXT"`
  28. IsPrerelease bool
  29. Created time.Time `xorm:"created"`
  30. }
  31. // GetReleasesByRepoId returns a list of releases of repository.
  32. func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) {
  33. err = orm.Desc("created").Find(&rels, Release{RepoId: repoId})
  34. return rels, err
  35. }
  36. // IsReleaseExist returns true if release with given tag name already exists.
  37. func IsReleaseExist(repoId int64, tagName string) (bool, error) {
  38. if len(tagName) == 0 {
  39. return false, nil
  40. }
  41. return orm.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)})
  42. }
  43. // CreateRelease creates a new release of repository.
  44. func CreateRelease(repoPath string, rel *Release, gitRepo *git.Repository) error {
  45. isExist, err := IsReleaseExist(rel.RepoId, rel.TagName)
  46. if err != nil {
  47. return err
  48. } else if isExist {
  49. return ErrReleaseAlreadyExist
  50. }
  51. if !git.IsTagExist(repoPath, rel.TagName) {
  52. _, stderr, err := com.ExecCmdDir(repoPath, "git", "tag", rel.TagName, "-m", rel.Title)
  53. if err != nil {
  54. return err
  55. } else if strings.Contains(stderr, "fatal:") {
  56. return errors.New(stderr)
  57. }
  58. } else {
  59. commit, err := gitRepo.GetCommitOfTag(rel.TagName)
  60. if err != nil {
  61. return err
  62. }
  63. rel.NumCommits, err = commit.CommitsCount()
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. rel.LowerTagName = strings.ToLower(rel.TagName)
  69. _, err = orm.InsertOne(rel)
  70. return err
  71. }