update.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if err = repo.UpdateSize(); err != nil {
  75. return fmt.Errorf("UpdateSize: %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. var l *list.List
  93. // Skip read parent commits when delete branch
  94. if !isDelRef {
  95. // Push new branch
  96. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  97. if err != nil {
  98. return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
  99. }
  100. if isNewRef {
  101. l, err = newCommit.CommitsBeforeLimit(10)
  102. if err != nil {
  103. return fmt.Errorf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
  104. }
  105. } else {
  106. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  107. if err != nil {
  108. return fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  109. }
  110. }
  111. }
  112. if err := CommitRepoAction(CommitRepoActionOptions{
  113. PusherName: opts.PusherName,
  114. RepoOwnerID: owner.ID,
  115. RepoName: repo.Name,
  116. RefFullName: opts.RefFullName,
  117. OldCommitID: opts.OldCommitID,
  118. NewCommitID: opts.NewCommitID,
  119. Commits: ListToPushCommits(l),
  120. }); err != nil {
  121. return fmt.Errorf("CommitRepoAction.(branch): %v", err)
  122. }
  123. return nil
  124. }