update.go 3.5 KB

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