Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. git "github.com/gogits/git-module"
  11. "github.com/gogits/gogs/modules/log"
  12. )
  13. type UpdateTask struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. UUID string `xorm:"index"`
  16. RefName string
  17. OldCommitID string
  18. NewCommitID string
  19. }
  20. func AddUpdateTask(task *UpdateTask) error {
  21. _, err := x.Insert(task)
  22. return err
  23. }
  24. // GetUpdateTaskByUUID returns update task by given UUID.
  25. func GetUpdateTaskByUUID(uuid string) (*UpdateTask, error) {
  26. task := &UpdateTask{
  27. UUID: uuid,
  28. }
  29. has, err := x.Get(task)
  30. if err != nil {
  31. return nil, err
  32. } else if !has {
  33. return nil, ErrUpdateTaskNotExist{uuid}
  34. }
  35. return task, nil
  36. }
  37. func DeleteUpdateTaskByUUID(uuid string) error {
  38. _, err := x.Delete(&UpdateTask{UUID: uuid})
  39. return err
  40. }
  41. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  42. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  43. return &PushCommit{
  44. Sha1: commit.ID.String(),
  45. Message: commit.Message(),
  46. AuthorEmail: commit.Author.Email,
  47. AuthorName: commit.Author.Name,
  48. CommitterEmail: commit.Committer.Email,
  49. CommitterName: commit.Committer.Name,
  50. Timestamp: commit.Author.When,
  51. }
  52. }
  53. func ListToPushCommits(l *list.List) *PushCommits {
  54. commits := make([]*PushCommit, 0)
  55. var actEmail string
  56. for e := l.Front(); e != nil; e = e.Next() {
  57. commit := e.Value.(*git.Commit)
  58. if actEmail == "" {
  59. actEmail = commit.Committer.Email
  60. }
  61. commits = append(commits, CommitToPushCommit(commit))
  62. }
  63. return &PushCommits{l.Len(), commits, "", nil}
  64. }
  65. type PushUpdateOptions struct {
  66. RefName string
  67. OldCommitID string
  68. NewCommitID string
  69. PusherID int64
  70. PusherName string
  71. RepoUserName string
  72. RepoName string
  73. }
  74. // PushUpdate must be called for any push actions in order to
  75. // generates necessary push action history feeds.
  76. func PushUpdate(opts PushUpdateOptions) (err error) {
  77. isNewRef := strings.HasPrefix(opts.OldCommitID, "0000000")
  78. isDelRef := strings.HasPrefix(opts.NewCommitID, "0000000")
  79. if isNewRef && isDelRef {
  80. return fmt.Errorf("Old and new revisions both start with 000000")
  81. }
  82. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  83. gitUpdate := exec.Command("git", "update-server-info")
  84. gitUpdate.Dir = repoPath
  85. if err = gitUpdate.Run(); err != nil {
  86. return fmt.Errorf("Fail to call 'git update-server-info': %v", err)
  87. }
  88. if isDelRef {
  89. log.GitLogger.Info("Reference '%s' has been deleted from '%s/%s' by %d",
  90. opts.RefName, opts.RepoUserName, opts.RepoName, opts.PusherName)
  91. return nil
  92. }
  93. gitRepo, err := git.OpenRepository(repoPath)
  94. if err != nil {
  95. return fmt.Errorf("OpenRepository: %v", err)
  96. }
  97. repoUser, err := GetUserByName(opts.RepoUserName)
  98. if err != nil {
  99. return fmt.Errorf("GetUserByName: %v", err)
  100. }
  101. repo, err := GetRepositoryByName(repoUser.ID, opts.RepoName)
  102. if err != nil {
  103. return fmt.Errorf("GetRepositoryByName: %v", err)
  104. }
  105. // Push tags.
  106. if strings.HasPrefix(opts.RefName, "refs/tags/") {
  107. tag, err := gitRepo.GetTag(git.RefEndName(opts.RefName))
  108. if err != nil {
  109. return fmt.Errorf("gitRepo.GetTag: %v", err)
  110. }
  111. // When tagger isn't available, fall back to get committer email.
  112. var actEmail string
  113. if tag.Tagger != nil {
  114. actEmail = tag.Tagger.Email
  115. } else {
  116. cmt, err := tag.Commit()
  117. if err != nil {
  118. return fmt.Errorf("tag.Commit: %v", err)
  119. }
  120. actEmail = cmt.Committer.Email
  121. }
  122. commit := &PushCommits{}
  123. if err = CommitRepoAction(opts.PusherID, repoUser.ID, opts.PusherName, actEmail,
  124. repo.ID, opts.RepoUserName, opts.RepoName, opts.RefName, commit, opts.OldCommitID, opts.NewCommitID); err != nil {
  125. return fmt.Errorf("CommitRepoAction (tag): %v", err)
  126. }
  127. return err
  128. }
  129. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  130. if err != nil {
  131. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  132. }
  133. // Push new branch.
  134. var l *list.List
  135. if isNewRef {
  136. l, err = newCommit.CommitsBeforeLimit(10)
  137. if err != nil {
  138. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  139. }
  140. } else {
  141. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  142. if err != nil {
  143. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  144. }
  145. }
  146. if err = CommitRepoAction(opts.PusherID, repoUser.ID, opts.PusherName, repoUser.Email,
  147. repo.ID, opts.RepoUserName, opts.RepoName, opts.RefName, ListToPushCommits(l),
  148. opts.OldCommitID, opts.NewCommitID); err != nil {
  149. return fmt.Errorf("CommitRepoAction (branch): %v", err)
  150. }
  151. return nil
  152. }