update.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 db
  5. import (
  6. "context"
  7. "fmt"
  8. "os/exec"
  9. "strings"
  10. "github.com/gogs/git-module"
  11. "github.com/pkg/errors"
  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 CommitsToPushCommits(commits []*git.Commit) *PushCommits {
  26. if len(commits) == 0 {
  27. return &PushCommits{}
  28. }
  29. pcs := make([]*PushCommit, len(commits))
  30. for i := range commits {
  31. pcs[i] = CommitToPushCommit(commits[i])
  32. }
  33. return &PushCommits{len(pcs), pcs, "", nil}
  34. }
  35. type PushUpdateOptions struct {
  36. OldCommitID string
  37. NewCommitID string
  38. FullRefspec string
  39. PusherID int64
  40. PusherName string
  41. RepoUserName string
  42. RepoName string
  43. }
  44. // PushUpdate must be called for any push actions in order to
  45. // generates necessary push action history feeds.
  46. func PushUpdate(opts PushUpdateOptions) (err error) {
  47. ctx := context.TODO()
  48. isNewRef := strings.HasPrefix(opts.OldCommitID, git.EmptyID)
  49. isDelRef := strings.HasPrefix(opts.NewCommitID, git.EmptyID)
  50. if isNewRef && isDelRef {
  51. return fmt.Errorf("both old and new revisions are %q", git.EmptyID)
  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("run 'git update-server-info': %v", err)
  58. }
  59. gitRepo, err := git.Open(repoPath)
  60. if err != nil {
  61. return fmt.Errorf("open repository: %v", err)
  62. }
  63. owner, err := Users.GetByUsername(ctx, 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.FullRefspec, git.RefsTags) {
  76. err := Actions.PushTag(ctx,
  77. PushTagOptions{
  78. Owner: owner,
  79. Repo: repo,
  80. PusherName: opts.PusherName,
  81. RefFullName: opts.FullRefspec,
  82. NewCommitID: opts.NewCommitID,
  83. },
  84. )
  85. if err != nil {
  86. return errors.Wrap(err, "create action for push tag")
  87. }
  88. return nil
  89. }
  90. var commits []*git.Commit
  91. // Skip read parent commits when delete branch
  92. if !isDelRef {
  93. // Push new branch
  94. newCommit, err := gitRepo.CatFileCommit(opts.NewCommitID)
  95. if err != nil {
  96. return fmt.Errorf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
  97. }
  98. if isNewRef {
  99. commits, err = newCommit.Ancestors(git.LogOptions{MaxCount: 9})
  100. if err != nil {
  101. return fmt.Errorf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
  102. }
  103. commits = append([]*git.Commit{newCommit}, commits...)
  104. } else {
  105. commits, err = newCommit.CommitsAfter(opts.OldCommitID)
  106. if err != nil {
  107. return fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  108. }
  109. }
  110. }
  111. err = Actions.CommitRepo(ctx,
  112. CommitRepoOptions{
  113. Owner: owner,
  114. Repo: repo,
  115. PusherName: opts.PusherName,
  116. RefFullName: opts.FullRefspec,
  117. OldCommitID: opts.OldCommitID,
  118. NewCommitID: opts.NewCommitID,
  119. Commits: CommitsToPushCommits(commits),
  120. },
  121. )
  122. if err != nil {
  123. return errors.Wrap(err, "create action for commit push")
  124. }
  125. return nil
  126. }