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.

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