update.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "fmt"
  7. "os/exec"
  8. "strings"
  9. "github.com/gogs/git-module"
  10. )
  11. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  12. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  13. return &PushCommit{
  14. Sha1: commit.ID.String(),
  15. Message: commit.Message,
  16. AuthorEmail: commit.Author.Email,
  17. AuthorName: commit.Author.Name,
  18. CommitterEmail: commit.Committer.Email,
  19. CommitterName: commit.Committer.Name,
  20. Timestamp: commit.Committer.When,
  21. }
  22. }
  23. func CommitsToPushCommits(commits []*git.Commit) *PushCommits {
  24. if len(commits) == 0 {
  25. return &PushCommits{}
  26. }
  27. pcs := make([]*PushCommit, len(commits))
  28. for i := range commits {
  29. pcs[i] = CommitToPushCommit(commits[i])
  30. }
  31. return &PushCommits{len(pcs), pcs, "", nil}
  32. }
  33. type PushUpdateOptions struct {
  34. OldCommitID string
  35. NewCommitID string
  36. FullRefspec string
  37. PusherID int64
  38. PusherName string
  39. RepoUserName string
  40. RepoName string
  41. }
  42. // PushUpdate must be called for any push actions in order to
  43. // generates necessary push action history feeds.
  44. func PushUpdate(opts PushUpdateOptions) (err error) {
  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 := GetUserByName(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. if err := CommitRepoAction(CommitRepoActionOptions{
  74. PusherName: opts.PusherName,
  75. RepoOwnerID: owner.ID,
  76. RepoName: repo.Name,
  77. RefFullName: opts.FullRefspec,
  78. OldCommitID: opts.OldCommitID,
  79. NewCommitID: opts.NewCommitID,
  80. Commits: &PushCommits{},
  81. }); err != nil {
  82. return fmt.Errorf("CommitRepoAction.(tag): %v", err)
  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 fmt.Errorf("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 fmt.Errorf("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 fmt.Errorf("CommitsBeforeUntil [commit_id: %s]: %v", opts.OldCommitID, err)
  104. }
  105. }
  106. }
  107. if err := CommitRepoAction(CommitRepoActionOptions{
  108. PusherName: opts.PusherName,
  109. RepoOwnerID: owner.ID,
  110. RepoName: repo.Name,
  111. RefFullName: opts.FullRefspec,
  112. OldCommitID: opts.OldCommitID,
  113. NewCommitID: opts.NewCommitID,
  114. Commits: CommitsToPushCommits(commits),
  115. }); err != nil {
  116. return fmt.Errorf("CommitRepoAction.(branch): %v", err)
  117. }
  118. return nil
  119. }