update.go 3.6 KB

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