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.

202 lines
5.0 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
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().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. // Trim '--' prefix to prevent command line argument vulnerability.
  58. rel.TagName = strings.TrimPrefix(rel.TagName, "--")
  59. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  60. if strings.Contains(err.Error(), "is not a valid tag name") {
  61. return ErrInvalidTagName{rel.TagName}
  62. }
  63. return err
  64. }
  65. } else {
  66. commit, err := gitRepo.GetTagCommit(rel.TagName)
  67. if err != nil {
  68. return fmt.Errorf("GetTagCommit: %v", err)
  69. }
  70. rel.NumCommits, err = commit.CommitsCount()
  71. if err != nil {
  72. return fmt.Errorf("CommitsCount: %v", err)
  73. }
  74. }
  75. }
  76. return nil
  77. }
  78. // CreateRelease creates a new release of repository.
  79. func CreateRelease(gitRepo *git.Repository, rel *Release) error {
  80. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  81. if err != nil {
  82. return err
  83. } else if isExist {
  84. return ErrReleaseAlreadyExist{rel.TagName}
  85. }
  86. if err = createTag(gitRepo, rel); err != nil {
  87. return err
  88. }
  89. rel.LowerTagName = strings.ToLower(rel.TagName)
  90. _, err = x.InsertOne(rel)
  91. return err
  92. }
  93. // GetRelease returns release by given ID.
  94. func GetRelease(repoID int64, tagName string) (*Release, error) {
  95. isExist, err := IsReleaseExist(repoID, tagName)
  96. if err != nil {
  97. return nil, err
  98. } else if !isExist {
  99. return nil, ErrReleaseNotExist{0, tagName}
  100. }
  101. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  102. _, err = x.Get(rel)
  103. return rel, err
  104. }
  105. // GetReleaseByID returns release with given ID.
  106. func GetReleaseByID(id int64) (*Release, error) {
  107. rel := new(Release)
  108. has, err := x.Id(id).Get(rel)
  109. if err != nil {
  110. return nil, err
  111. } else if !has {
  112. return nil, ErrReleaseNotExist{id, ""}
  113. }
  114. return rel, nil
  115. }
  116. // GetReleasesByRepoID returns a list of releases of repository.
  117. func GetReleasesByRepoID(repoID int64) (rels []*Release, err error) {
  118. err = x.Desc("created_unix").Find(&rels, Release{RepoID: repoID})
  119. return rels, err
  120. }
  121. type ReleaseSorter struct {
  122. rels []*Release
  123. }
  124. func (rs *ReleaseSorter) Len() int {
  125. return len(rs.rels)
  126. }
  127. func (rs *ReleaseSorter) Less(i, j int) bool {
  128. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  129. if diffNum != 0 {
  130. return diffNum > 0
  131. }
  132. return rs.rels[i].Created.After(rs.rels[j].Created)
  133. }
  134. func (rs *ReleaseSorter) Swap(i, j int) {
  135. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  136. }
  137. // SortReleases sorts releases by number of commits and created time.
  138. func SortReleases(rels []*Release) {
  139. sorter := &ReleaseSorter{rels: rels}
  140. sort.Sort(sorter)
  141. }
  142. // UpdateRelease updates information of a release.
  143. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  144. if err = createTag(gitRepo, rel); err != nil {
  145. return err
  146. }
  147. _, err = x.Id(rel.ID).AllCols().Update(rel)
  148. return err
  149. }
  150. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  151. func DeleteReleaseByID(id int64) error {
  152. rel, err := GetReleaseByID(id)
  153. if err != nil {
  154. return fmt.Errorf("GetReleaseByID: %v", err)
  155. }
  156. repo, err := GetRepositoryByID(rel.RepoID)
  157. if err != nil {
  158. return fmt.Errorf("GetRepositoryByID: %v", err)
  159. }
  160. _, stderr, err := process.ExecDir(-1, repo.RepoPath(),
  161. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  162. "git", "tag", "-d", rel.TagName)
  163. if err != nil && !strings.Contains(stderr, "not found") {
  164. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  165. }
  166. if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil {
  167. return fmt.Errorf("Delete: %v", err)
  168. }
  169. return nil
  170. }