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.

197 lines
4.8 KiB

10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 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. "fmt"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/git-module"
  12. "github.com/gogits/gogs/modules/process"
  13. )
  14. // Release represents a release of repository.
  15. type Release struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RepoID int64
  18. PublisherID int64
  19. Publisher *User `xorm:"-"`
  20. TagName string
  21. LowerTagName string
  22. Target string
  23. Title string
  24. Sha1 string `xorm:"VARCHAR(40)"`
  25. NumCommits int64
  26. NumCommitsBehind int64 `xorm:"-"`
  27. Note string `xorm:"TEXT"`
  28. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  29. IsPrerelease bool
  30. Created time.Time `xorm:"-"`
  31. CreatedUnix int64
  32. }
  33. func (r *Release) BeforeInsert() {
  34. r.CreatedUnix = time.Now().UTC().Unix()
  35. }
  36. func (r *Release) AfterSet(colName string, _ xorm.Cell) {
  37. switch colName {
  38. case "created_unix":
  39. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  40. }
  41. }
  42. // IsReleaseExist returns true if release with given tag name already exists.
  43. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  44. if len(tagName) == 0 {
  45. return false, nil
  46. }
  47. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  48. }
  49. func createTag(gitRepo *git.Repository, rel *Release) error {
  50. // Only actual create when publish.
  51. if !rel.IsDraft {
  52. if !gitRepo.IsTagExist(rel.TagName) {
  53. commit, err := gitRepo.GetBranchCommit(rel.Target)
  54. if err != nil {
  55. return fmt.Errorf("GetBranchCommit: %v", err)
  56. }
  57. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  58. return err
  59. }
  60. } else {
  61. commit, err := gitRepo.GetTagCommit(rel.TagName)
  62. if err != nil {
  63. return fmt.Errorf("GetTagCommit: %v", err)
  64. }
  65. rel.NumCommits, err = commit.CommitsCount()
  66. if err != nil {
  67. return fmt.Errorf("CommitsCount: %v", err)
  68. }
  69. }
  70. }
  71. return nil
  72. }
  73. // CreateRelease creates a new release of repository.
  74. func CreateRelease(gitRepo *git.Repository, rel *Release) error {
  75. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  76. if err != nil {
  77. return err
  78. } else if isExist {
  79. return ErrReleaseAlreadyExist{rel.TagName}
  80. }
  81. if err = createTag(gitRepo, rel); err != nil {
  82. return err
  83. }
  84. rel.LowerTagName = strings.ToLower(rel.TagName)
  85. _, err = x.InsertOne(rel)
  86. return err
  87. }
  88. // GetRelease returns release by given ID.
  89. func GetRelease(repoID int64, tagName string) (*Release, error) {
  90. isExist, err := IsReleaseExist(repoID, tagName)
  91. if err != nil {
  92. return nil, err
  93. } else if !isExist {
  94. return nil, ErrReleaseNotExist{0, tagName}
  95. }
  96. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  97. _, err = x.Get(rel)
  98. return rel, err
  99. }
  100. // GetReleaseByID returns release with given ID.
  101. func GetReleaseByID(id int64) (*Release, error) {
  102. rel := new(Release)
  103. has, err := x.Id(id).Get(rel)
  104. if err != nil {
  105. return nil, err
  106. } else if !has {
  107. return nil, ErrReleaseNotExist{id, ""}
  108. }
  109. return rel, nil
  110. }
  111. // GetReleasesByRepoID returns a list of releases of repository.
  112. func GetReleasesByRepoID(repoID int64) (rels []*Release, err error) {
  113. err = x.Desc("created_unix").Find(&rels, Release{RepoID: repoID})
  114. return rels, err
  115. }
  116. type ReleaseSorter struct {
  117. rels []*Release
  118. }
  119. func (rs *ReleaseSorter) Len() int {
  120. return len(rs.rels)
  121. }
  122. func (rs *ReleaseSorter) Less(i, j int) bool {
  123. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  124. if diffNum != 0 {
  125. return diffNum > 0
  126. }
  127. return rs.rels[i].Created.After(rs.rels[j].Created)
  128. }
  129. func (rs *ReleaseSorter) Swap(i, j int) {
  130. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  131. }
  132. // SortReleases sorts releases by number of commits and created time.
  133. func SortReleases(rels []*Release) {
  134. sorter := &ReleaseSorter{rels: rels}
  135. sort.Sort(sorter)
  136. }
  137. // UpdateRelease updates information of a release.
  138. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  139. if err = createTag(gitRepo, rel); err != nil {
  140. return err
  141. }
  142. _, err = x.Id(rel.ID).AllCols().Update(rel)
  143. return err
  144. }
  145. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  146. func DeleteReleaseByID(id int64) error {
  147. rel, err := GetReleaseByID(id)
  148. if err != nil {
  149. return fmt.Errorf("GetReleaseByID: %v", err)
  150. }
  151. repo, err := GetRepositoryByID(rel.RepoID)
  152. if err != nil {
  153. return fmt.Errorf("GetRepositoryByID: %v", err)
  154. }
  155. _, stderr, err := process.ExecDir(-1, repo.RepoPath(),
  156. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  157. "git", "tag", "-d", rel.TagName)
  158. if err != nil && !strings.Contains(stderr, "not found") {
  159. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  160. }
  161. if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil {
  162. return fmt.Errorf("Delete: %v", err)
  163. }
  164. return nil
  165. }