update.go 4.3 KB

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