update.go 3.6 KB

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