update.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "container/list"
  9. "fmt"
  10. "os/exec"
  11. "strings"
  12. git "gitlab.com/gitote/git-module"
  13. )
  14. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  15. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  16. return &PushCommit{
  17. Sha1: commit.ID.String(),
  18. Message: commit.Message(),
  19. AuthorEmail: commit.Author.Email,
  20. AuthorName: commit.Author.Name,
  21. CommitterEmail: commit.Committer.Email,
  22. CommitterName: commit.Committer.Name,
  23. Timestamp: commit.Committer.When,
  24. }
  25. }
  26. // ListToPushCommits transforms a list.List to PushCommits type.
  27. func ListToPushCommits(l *list.List) *PushCommits {
  28. if l == nil {
  29. return &PushCommits{}
  30. }
  31. commits := make([]*PushCommit, 0)
  32. var actEmail string
  33. for e := l.Front(); e != nil; e = e.Next() {
  34. commit := e.Value.(*git.Commit)
  35. if actEmail == "" {
  36. actEmail = commit.Committer.Email
  37. }
  38. commits = append(commits, CommitToPushCommit(commit))
  39. }
  40. return &PushCommits{l.Len(), commits, "", nil}
  41. }
  42. type PushUpdateOptions struct {
  43. OldCommitID string
  44. NewCommitID string
  45. RefFullName string
  46. PusherID int64
  47. PusherName string
  48. RepoUserName string
  49. RepoName string
  50. }
  51. // PushUpdate must be called for any push actions in order to
  52. // generates necessary push action history feeds.
  53. func PushUpdate(opts PushUpdateOptions) (err error) {
  54. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  55. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  56. if isNewRef && isDelRef {
  57. return fmt.Errorf("Old and new revisions are both %s", git.EMPTY_SHA)
  58. }
  59. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  60. gitUpdate := exec.Command("git", "update-server-info")
  61. gitUpdate.Dir = repoPath
  62. if err = gitUpdate.Run(); err != nil {
  63. return fmt.Errorf("Fail to call 'git update-server-info': %v", err)
  64. }
  65. gitRepo, err := git.OpenRepository(repoPath)
  66. if err != nil {
  67. return fmt.Errorf("OpenRepository: %v", err)
  68. }
  69. owner, err := GetUserByName(opts.RepoUserName)
  70. if err != nil {
  71. return fmt.Errorf("GetUserByName: %v", err)
  72. }
  73. repo, err := GetRepositoryByName(owner.ID, opts.RepoName)
  74. if err != nil {
  75. return fmt.Errorf("GetRepositoryByName: %v", err)
  76. }
  77. if err = repo.UpdateSize(); err != nil {
  78. return fmt.Errorf("UpdateSize: %v", err)
  79. }
  80. // Push tags
  81. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  82. if err := CommitRepoAction(CommitRepoActionOptions{
  83. PusherName: opts.PusherName,
  84. RepoOwnerID: owner.ID,
  85. RepoName: repo.Name,
  86. RefFullName: opts.RefFullName,
  87. OldCommitID: opts.OldCommitID,
  88. NewCommitID: opts.NewCommitID,
  89. Commits: &PushCommits{},
  90. }); err != nil {
  91. return fmt.Errorf("CommitRepoAction.(tag): %v", err)
  92. }
  93. return nil
  94. }
  95. var l *list.List
  96. // Skip read parent commits when delete branch
  97. if !isDelRef {
  98. // Push new branch
  99. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  100. if err != nil {
  101. return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
  102. }
  103. if isNewRef {
  104. l, err = newCommit.CommitsBeforeLimit(10)
  105. if err != nil {
  106. return fmt.Errorf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
  107. }
  108. } else {
  109. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  110. if err != nil {
  111. return fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  112. }
  113. }
  114. }
  115. if err := CommitRepoAction(CommitRepoActionOptions{
  116. PusherName: opts.PusherName,
  117. RepoOwnerID: owner.ID,
  118. RepoName: repo.Name,
  119. RefFullName: opts.RefFullName,
  120. OldCommitID: opts.OldCommitID,
  121. NewCommitID: opts.NewCommitID,
  122. Commits: ListToPushCommits(l),
  123. }); err != nil {
  124. return fmt.Errorf("CommitRepoAction.(branch): %v", err)
  125. }
  126. return nil
  127. }