update.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package models
  2. import (
  3. "container/list"
  4. "fmt"
  5. "os/exec"
  6. "strings"
  7. git "gitlab.com/gitote/git-module"
  8. )
  9. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  10. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  11. return &PushCommit{
  12. Sha1: commit.ID.String(),
  13. Message: commit.Message(),
  14. AuthorEmail: commit.Author.Email,
  15. AuthorName: commit.Author.Name,
  16. CommitterEmail: commit.Committer.Email,
  17. CommitterName: commit.Committer.Name,
  18. Timestamp: commit.Committer.When,
  19. }
  20. }
  21. func ListToPushCommits(l *list.List) *PushCommits {
  22. if l == nil {
  23. return &PushCommits{}
  24. }
  25. commits := make([]*PushCommit, 0)
  26. var actEmail string
  27. for e := l.Front(); e != nil; e = e.Next() {
  28. commit := e.Value.(*git.Commit)
  29. if actEmail == "" {
  30. actEmail = commit.Committer.Email
  31. }
  32. commits = append(commits, CommitToPushCommit(commit))
  33. }
  34. return &PushCommits{l.Len(), commits, "", nil}
  35. }
  36. type PushUpdateOptions struct {
  37. OldCommitID string
  38. NewCommitID string
  39. RefFullName string
  40. PusherID int64
  41. PusherName string
  42. RepoUserName string
  43. RepoName string
  44. }
  45. // PushUpdate must be called for any push actions in order to
  46. // generates necessary push action history feeds.
  47. func PushUpdate(opts PushUpdateOptions) (err error) {
  48. isNewRef := opts.OldCommitID == git.EMPTY_SHA
  49. isDelRef := opts.NewCommitID == git.EMPTY_SHA
  50. if isNewRef && isDelRef {
  51. return fmt.Errorf("Old and new revisions are both %s", git.EMPTY_SHA)
  52. }
  53. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  54. gitUpdate := exec.Command("git", "update-server-info")
  55. gitUpdate.Dir = repoPath
  56. if err = gitUpdate.Run(); err != nil {
  57. return fmt.Errorf("Fail to call 'git update-server-info': %v", err)
  58. }
  59. gitRepo, err := git.OpenRepository(repoPath)
  60. if err != nil {
  61. return fmt.Errorf("OpenRepository: %v", err)
  62. }
  63. owner, err := GetUserByName(opts.RepoUserName)
  64. if err != nil {
  65. return fmt.Errorf("GetUserByName: %v", err)
  66. }
  67. repo, err := GetRepositoryByName(owner.ID, opts.RepoName)
  68. if err != nil {
  69. return fmt.Errorf("GetRepositoryByName: %v", err)
  70. }
  71. if err = repo.UpdateSize(); err != nil {
  72. return fmt.Errorf("UpdateSize: %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. }