update.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, 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. // PushUpdateOptions defines the push update options
  43. type PushUpdateOptions struct {
  44. OldCommitID string
  45. NewCommitID string
  46. RefFullName string
  47. PusherID int64
  48. PusherName string
  49. RepoUserName string
  50. RepoName string
  51. }
  52. // PushUpdate must be called for any push actions in order to
  53. // generates necessary push action history feeds.
  54. func PushUpdate(opts PushUpdateOptions) (err error) {
  55. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  56. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  57. if isNewRef && isDelRef {
  58. return fmt.Errorf("Old and new revisions are both %s", git.EMPTY_SHA)
  59. }
  60. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  61. gitUpdate := exec.Command("git", "update-server-info")
  62. gitUpdate.Dir = repoPath
  63. if err = gitUpdate.Run(); err != nil {
  64. return fmt.Errorf("Fail to call 'git update-server-info': %v", err)
  65. }
  66. gitRepo, err := git.OpenRepository(repoPath)
  67. if err != nil {
  68. return fmt.Errorf("OpenRepository: %v", err)
  69. }
  70. owner, err := GetUserByName(opts.RepoUserName)
  71. if err != nil {
  72. return fmt.Errorf("GetUserByName: %v", err)
  73. }
  74. repo, err := GetRepositoryByName(owner.ID, opts.RepoName)
  75. if err != nil {
  76. return fmt.Errorf("GetRepositoryByName: %v", err)
  77. }
  78. if err = repo.UpdateSize(); err != nil {
  79. return fmt.Errorf("UpdateSize: %v", err)
  80. }
  81. // Push tags
  82. if strings.HasPrefix(opts.RefFullName, git.TAG_PREFIX) {
  83. if err := CommitRepoAction(CommitRepoActionOptions{
  84. PusherName: opts.PusherName,
  85. RepoOwnerID: owner.ID,
  86. RepoName: repo.Name,
  87. RefFullName: opts.RefFullName,
  88. OldCommitID: opts.OldCommitID,
  89. NewCommitID: opts.NewCommitID,
  90. Commits: &PushCommits{},
  91. }); err != nil {
  92. return fmt.Errorf("CommitRepoAction.(tag): %v", err)
  93. }
  94. return nil
  95. }
  96. var l *list.List
  97. // Skip read parent commits when delete branch
  98. if !isDelRef {
  99. // Push new branch
  100. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  101. if err != nil {
  102. return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
  103. }
  104. if isNewRef {
  105. l, err = newCommit.CommitsBeforeLimit(10)
  106. if err != nil {
  107. return fmt.Errorf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
  108. }
  109. } else {
  110. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  111. if err != nil {
  112. return fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  113. }
  114. }
  115. }
  116. if err := CommitRepoAction(CommitRepoActionOptions{
  117. PusherName: opts.PusherName,
  118. RepoOwnerID: owner.ID,
  119. RepoName: repo.Name,
  120. RefFullName: opts.RefFullName,
  121. OldCommitID: opts.OldCommitID,
  122. NewCommitID: opts.NewCommitID,
  123. Commits: ListToPushCommits(l),
  124. }); err != nil {
  125. return fmt.Errorf("CommitRepoAction.(branch): %v", err)
  126. }
  127. return nil
  128. }