update.go 3.8 KB

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