update.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package database
  2. import (
  3. "context"
  4. "os/exec"
  5. "strings"
  6. "github.com/cockroachdb/errors"
  7. "github.com/gogs/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 CommitsToPushCommits(commits []*git.Commit) *PushCommits {
  22. if len(commits) == 0 {
  23. return &PushCommits{}
  24. }
  25. pcs := make([]*PushCommit, len(commits))
  26. for i := range commits {
  27. pcs[i] = CommitToPushCommit(commits[i])
  28. }
  29. return &PushCommits{len(pcs), pcs, "", nil}
  30. }
  31. type PushUpdateOptions struct {
  32. OldCommitID string
  33. NewCommitID string
  34. FullRefspec string
  35. PusherID int64
  36. PusherName string
  37. RepoUserName string
  38. RepoName string
  39. }
  40. // PushUpdate must be called for any push actions in order to generate necessary
  41. // push action history feeds.
  42. func PushUpdate(opts PushUpdateOptions) (err error) {
  43. ctx := context.TODO()
  44. isNewRef := strings.HasPrefix(opts.OldCommitID, git.EmptyID)
  45. isDelRef := strings.HasPrefix(opts.NewCommitID, git.EmptyID)
  46. if isNewRef && isDelRef {
  47. return errors.Newf("both old and new revisions are %q", git.EmptyID)
  48. }
  49. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  50. gitUpdate := exec.Command("git", "update-server-info")
  51. gitUpdate.Dir = repoPath
  52. if err = gitUpdate.Run(); err != nil {
  53. return errors.Newf("run 'git update-server-info': %v", err)
  54. }
  55. gitRepo, err := git.Open(repoPath)
  56. if err != nil {
  57. return errors.Newf("open repository: %v", err)
  58. }
  59. owner, err := Handle.Users().GetByUsername(ctx, opts.RepoUserName)
  60. if err != nil {
  61. return errors.Newf("GetUserByName: %v", err)
  62. }
  63. repo, err := GetRepositoryByName(owner.ID, opts.RepoName)
  64. if err != nil {
  65. return errors.Newf("GetRepositoryByName: %v", err)
  66. }
  67. if err = repo.UpdateSize(); err != nil {
  68. return errors.Newf("UpdateSize: %v", err)
  69. }
  70. // Push tags
  71. if strings.HasPrefix(opts.FullRefspec, git.RefsTags) {
  72. err := Handle.Actions().PushTag(ctx,
  73. PushTagOptions{
  74. Owner: owner,
  75. Repo: repo,
  76. PusherName: opts.PusherName,
  77. RefFullName: opts.FullRefspec,
  78. NewCommitID: opts.NewCommitID,
  79. },
  80. )
  81. if err != nil {
  82. return errors.Wrap(err, "create action for push tag")
  83. }
  84. return nil
  85. }
  86. var commits []*git.Commit
  87. // Skip read parent commits when delete branch
  88. if !isDelRef {
  89. // Push new branch
  90. newCommit, err := gitRepo.CatFileCommit(opts.NewCommitID)
  91. if err != nil {
  92. return errors.Newf("GetCommit [commit_id: %s]: %v", opts.NewCommitID, err)
  93. }
  94. if isNewRef {
  95. commits, err = newCommit.Ancestors(git.LogOptions{MaxCount: 9})
  96. if err != nil {
  97. return errors.Newf("CommitsBeforeLimit [commit_id: %s]: %v", newCommit.ID, err)
  98. }
  99. commits = append([]*git.Commit{newCommit}, commits...)
  100. } else {
  101. commits, err = newCommit.CommitsAfter(opts.OldCommitID)
  102. if err != nil {
  103. return errors.Newf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  104. }
  105. }
  106. }
  107. err = Handle.Actions().CommitRepo(ctx,
  108. CommitRepoOptions{
  109. Owner: owner,
  110. Repo: repo,
  111. PusherName: opts.PusherName,
  112. RefFullName: opts.FullRefspec,
  113. OldCommitID: opts.OldCommitID,
  114. NewCommitID: opts.NewCommitID,
  115. Commits: CommitsToPushCommits(commits),
  116. },
  117. )
  118. if err != nil {
  119. return errors.Wrap(err, "create action for commit push")
  120. }
  121. return nil
  122. }