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.

281 lines
7.0 KiB

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