release.go 4.6 KB

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