Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

update.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "container/list"
  7. "fmt"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/modules/cache"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // env keys for git hooks need
  16. const (
  17. EnvRepoName = "GITEA_REPO_NAME"
  18. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  19. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  20. EnvPusherName = "GITEA_PUSHER_NAME"
  21. EnvPusherID = "GITEA_PUSHER_ID"
  22. )
  23. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  24. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  25. return &PushCommit{
  26. Sha1: commit.ID.String(),
  27. Message: commit.Message(),
  28. AuthorEmail: commit.Author.Email,
  29. AuthorName: commit.Author.Name,
  30. CommitterEmail: commit.Committer.Email,
  31. CommitterName: commit.Committer.Name,
  32. Timestamp: commit.Author.When,
  33. }
  34. }
  35. // ListToPushCommits transforms a list.List to PushCommits type.
  36. func ListToPushCommits(l *list.List) *PushCommits {
  37. var commits []*PushCommit
  38. var actEmail string
  39. for e := l.Front(); e != nil; e = e.Next() {
  40. commit := e.Value.(*git.Commit)
  41. if actEmail == "" {
  42. actEmail = commit.Committer.Email
  43. }
  44. commits = append(commits, CommitToPushCommit(commit))
  45. }
  46. return &PushCommits{l.Len(), commits, "", nil}
  47. }
  48. // PushUpdateOptions defines the push update options
  49. type PushUpdateOptions struct {
  50. PusherID int64
  51. PusherName string
  52. RepoUserName string
  53. RepoName string
  54. RefFullName string
  55. OldCommitID string
  56. NewCommitID string
  57. }
  58. // PushUpdate must be called for any push actions in order to
  59. // generates necessary push action history feeds.
  60. func PushUpdate(branch string, opt PushUpdateOptions) error {
  61. repo, err := pushUpdate(opt)
  62. if err != nil {
  63. return err
  64. }
  65. pusher, err := GetUserByID(opt.PusherID)
  66. if err != nil {
  67. return err
  68. }
  69. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  70. go AddTestPullRequestTask(pusher, repo.ID, branch, true)
  71. return nil
  72. }
  73. func pushUpdateDeleteTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
  74. rel, err := GetRelease(repo.ID, tagName)
  75. if err != nil {
  76. if IsErrReleaseNotExist(err) {
  77. return nil
  78. }
  79. return fmt.Errorf("GetRelease: %v", err)
  80. }
  81. if rel.IsTag {
  82. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  83. return fmt.Errorf("Delete: %v", err)
  84. }
  85. } else {
  86. rel.IsDraft = true
  87. rel.NumCommits = 0
  88. rel.Sha1 = ""
  89. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  90. return fmt.Errorf("Update: %v", err)
  91. }
  92. }
  93. return nil
  94. }
  95. func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
  96. rel, err := GetRelease(repo.ID, tagName)
  97. if err != nil && !IsErrReleaseNotExist(err) {
  98. return fmt.Errorf("GetRelease: %v", err)
  99. }
  100. tag, err := gitRepo.GetTag(tagName)
  101. if err != nil {
  102. return fmt.Errorf("GetTag: %v", err)
  103. }
  104. commit, err := tag.Commit()
  105. if err != nil {
  106. return fmt.Errorf("Commit: %v", err)
  107. }
  108. sig := tag.Tagger
  109. if sig == nil {
  110. sig = commit.Author
  111. }
  112. if sig == nil {
  113. sig = commit.Committer
  114. }
  115. var author *User
  116. var createdAt = time.Unix(1, 0)
  117. if sig != nil {
  118. author, err = GetUserByEmail(sig.Email)
  119. if err != nil && !IsErrUserNotExist(err) {
  120. return fmt.Errorf("GetUserByEmail: %v", err)
  121. }
  122. createdAt = sig.When
  123. }
  124. commitsCount, err := commit.CommitsCount()
  125. if err != nil {
  126. return fmt.Errorf("CommitsCount: %v", err)
  127. }
  128. if rel == nil {
  129. rel = &Release{
  130. RepoID: repo.ID,
  131. Title: "",
  132. TagName: tagName,
  133. LowerTagName: strings.ToLower(tagName),
  134. Target: "",
  135. Sha1: commit.ID.String(),
  136. NumCommits: commitsCount,
  137. Note: "",
  138. IsDraft: false,
  139. IsPrerelease: false,
  140. IsTag: true,
  141. Created: createdAt,
  142. CreatedUnix: createdAt.Unix(),
  143. }
  144. if author != nil {
  145. rel.PublisherID = author.ID
  146. }
  147. if _, err = x.InsertOne(rel); err != nil {
  148. return fmt.Errorf("InsertOne: %v", err)
  149. }
  150. } else {
  151. rel.Sha1 = commit.ID.String()
  152. rel.Created = createdAt
  153. rel.CreatedUnix = createdAt.Unix()
  154. rel.NumCommits = commitsCount
  155. rel.IsDraft = false
  156. if rel.IsTag && author != nil {
  157. rel.PublisherID = author.ID
  158. }
  159. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  160. return fmt.Errorf("Update: %v", err)
  161. }
  162. }
  163. return nil
  164. }
  165. func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
  166. isNewRef := opts.OldCommitID == git.EmptySHA
  167. isDelRef := opts.NewCommitID == git.EmptySHA
  168. if isNewRef && isDelRef {
  169. return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  170. }
  171. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  172. gitUpdate := exec.Command("git", "update-server-info")
  173. gitUpdate.Dir = repoPath
  174. if err = gitUpdate.Run(); err != nil {
  175. return nil, fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  176. }
  177. owner, err := GetUserByName(opts.RepoUserName)
  178. if err != nil {
  179. return nil, fmt.Errorf("GetUserByName: %v", err)
  180. }
  181. repo, err = GetRepositoryByName(owner.ID, opts.RepoName)
  182. if err != nil {
  183. return nil, fmt.Errorf("GetRepositoryByName: %v", err)
  184. }
  185. gitRepo, err := git.OpenRepository(repoPath)
  186. if err != nil {
  187. return nil, fmt.Errorf("OpenRepository: %v", err)
  188. }
  189. if err = repo.UpdateSize(); err != nil {
  190. log.Error(4, "Failed to update size for repository: %v", err)
  191. }
  192. var commits = &PushCommits{}
  193. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  194. // If is tag reference
  195. tagName := opts.RefFullName[len(git.TagPrefix):]
  196. if isDelRef {
  197. err = pushUpdateDeleteTag(repo, gitRepo, tagName)
  198. if err != nil {
  199. return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err)
  200. }
  201. } else {
  202. // Clear cache for tag commit count
  203. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  204. err = pushUpdateAddTag(repo, gitRepo, tagName)
  205. if err != nil {
  206. return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
  207. }
  208. }
  209. } else if !isDelRef {
  210. // If is branch reference
  211. // Clear cache for branch commit count
  212. cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true))
  213. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  214. if err != nil {
  215. return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
  216. }
  217. // Push new branch.
  218. var l *list.List
  219. if isNewRef {
  220. l, err = newCommit.CommitsBeforeLimit(10)
  221. if err != nil {
  222. return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  223. }
  224. } else {
  225. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  226. if err != nil {
  227. return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  228. }
  229. }
  230. commits = ListToPushCommits(l)
  231. }
  232. if opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
  233. UpdateRepoIndexer(repo)
  234. }
  235. if err := CommitRepoAction(CommitRepoActionOptions{
  236. PusherName: opts.PusherName,
  237. RepoOwnerID: owner.ID,
  238. RepoName: repo.Name,
  239. RefFullName: opts.RefFullName,
  240. OldCommitID: opts.OldCommitID,
  241. NewCommitID: opts.NewCommitID,
  242. Commits: commits,
  243. }); err != nil {
  244. return nil, fmt.Errorf("CommitRepoAction: %v", err)
  245. }
  246. return repo, nil
  247. }