update.go 4.4 KB

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