update.go 4.2 KB

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