update.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Committer.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. PusherID int64
  67. PusherName string
  68. RepoUserName string
  69. RepoName string
  70. RefFullName string
  71. OldCommitID string
  72. NewCommitID 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 := opts.OldCommitID == git.EMPTY_SHA
  78. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  79. if isNewRef && isDelRef {
  80. return fmt.Errorf("Old and new revisions are both %s", git.EMPTY_SHA)
  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 %s",
  90. opts.RefFullName, 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. owner, err := GetUserByName(opts.RepoUserName)
  98. if err != nil {
  99. return fmt.Errorf("GetUserByName: %v", err)
  100. }
  101. repo, err := GetRepositoryByName(owner.ID, opts.RepoName)
  102. if err != nil {
  103. return fmt.Errorf("GetRepositoryByName: %v", err)
  104. }
  105. // Push tags.
  106. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  107. if err := CommitRepoAction(CommitRepoActionOptions{
  108. PusherName: opts.PusherName,
  109. RepoOwnerID: owner.ID,
  110. RepoName: repo.Name,
  111. RefFullName: opts.RefFullName,
  112. OldCommitID: opts.OldCommitID,
  113. NewCommitID: opts.NewCommitID,
  114. Commits: &PushCommits{},
  115. }); err != nil {
  116. return fmt.Errorf("CommitRepoAction (tag): %v", err)
  117. }
  118. return nil
  119. }
  120. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  121. if err != nil {
  122. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  123. }
  124. // Push new branch.
  125. var l *list.List
  126. if isNewRef {
  127. l, err = newCommit.CommitsBeforeLimit(10)
  128. if err != nil {
  129. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  130. }
  131. } else {
  132. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  133. if err != nil {
  134. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  135. }
  136. }
  137. if err := CommitRepoAction(CommitRepoActionOptions{
  138. PusherName: opts.PusherName,
  139. RepoOwnerID: owner.ID,
  140. RepoName: repo.Name,
  141. RefFullName: opts.RefFullName,
  142. OldCommitID: opts.OldCommitID,
  143. NewCommitID: opts.NewCommitID,
  144. Commits: ListToPushCommits(l),
  145. }); err != nil {
  146. return fmt.Errorf("CommitRepoAction (branch): %v", err)
  147. }
  148. return nil
  149. }