You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
  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 models
  5. import (
  6. "container/list"
  7. "fmt"
  8. "os/exec"
  9. "strings"
  10. "code.gitea.io/git"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // env keys for git hooks need
  14. const (
  15. EnvRepoName = "GITEA_REPO_NAME"
  16. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  17. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  18. EnvPusherName = "GITEA_PUSHER_NAME"
  19. EnvPusherID = "GITEA_PUSHER_ID"
  20. )
  21. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  22. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  23. return &PushCommit{
  24. Sha1: commit.ID.String(),
  25. Message: commit.Message(),
  26. AuthorEmail: commit.Author.Email,
  27. AuthorName: commit.Author.Name,
  28. CommitterEmail: commit.Committer.Email,
  29. CommitterName: commit.Committer.Name,
  30. Timestamp: commit.Author.When,
  31. }
  32. }
  33. // ListToPushCommits transforms a list.List to PushCommits type.
  34. func ListToPushCommits(l *list.List) *PushCommits {
  35. var commits []*PushCommit
  36. var actEmail string
  37. for e := l.Front(); e != nil; e = e.Next() {
  38. commit := e.Value.(*git.Commit)
  39. if actEmail == "" {
  40. actEmail = commit.Committer.Email
  41. }
  42. commits = append(commits, CommitToPushCommit(commit))
  43. }
  44. return &PushCommits{l.Len(), commits, "", nil}
  45. }
  46. // PushUpdateOptions defines the push update options
  47. type PushUpdateOptions struct {
  48. PusherID int64
  49. PusherName string
  50. RepoUserName string
  51. RepoName string
  52. RefFullName string
  53. OldCommitID string
  54. NewCommitID string
  55. }
  56. // PushUpdate must be called for any push actions in order to
  57. // generates necessary push action history feeds.
  58. func PushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
  59. isNewRef := opts.OldCommitID == git.EmptySHA
  60. isDelRef := opts.NewCommitID == git.EmptySHA
  61. if isNewRef && isDelRef {
  62. return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  63. }
  64. repoPath := RepoPath(opts.RepoUserName, opts.RepoName)
  65. gitUpdate := exec.Command("git", "update-server-info")
  66. gitUpdate.Dir = repoPath
  67. if err = gitUpdate.Run(); err != nil {
  68. return nil, fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  69. }
  70. owner, err := GetUserByName(opts.RepoUserName)
  71. if err != nil {
  72. return nil, fmt.Errorf("GetUserByName: %v", err)
  73. }
  74. repo, err = GetRepositoryByName(owner.ID, opts.RepoName)
  75. if err != nil {
  76. return nil, fmt.Errorf("GetRepositoryByName: %v", err)
  77. }
  78. if isDelRef {
  79. log.GitLogger.Info("Reference '%s' has been deleted from '%s/%s' by %s",
  80. opts.RefFullName, opts.RepoUserName, opts.RepoName, opts.PusherName)
  81. return repo, nil
  82. }
  83. gitRepo, err := git.OpenRepository(repoPath)
  84. if err != nil {
  85. return nil, fmt.Errorf("OpenRepository: %v", err)
  86. }
  87. if err = repo.UpdateSize(); err != nil {
  88. log.Error(4, "Failed to update size for repository: %v", err)
  89. }
  90. // Push tags.
  91. if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
  92. if err := CommitRepoAction(CommitRepoActionOptions{
  93. PusherName: opts.PusherName,
  94. RepoOwnerID: owner.ID,
  95. RepoName: repo.Name,
  96. RefFullName: opts.RefFullName,
  97. OldCommitID: opts.OldCommitID,
  98. NewCommitID: opts.NewCommitID,
  99. Commits: &PushCommits{},
  100. }); err != nil {
  101. return nil, fmt.Errorf("CommitRepoAction (tag): %v", err)
  102. }
  103. return repo, nil
  104. }
  105. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  106. if err != nil {
  107. return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
  108. }
  109. // Push new branch.
  110. var l *list.List
  111. if isNewRef {
  112. l, err = newCommit.CommitsBeforeLimit(10)
  113. if err != nil {
  114. return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  115. }
  116. } else {
  117. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  118. if err != nil {
  119. return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  120. }
  121. }
  122. if err := CommitRepoAction(CommitRepoActionOptions{
  123. PusherName: opts.PusherName,
  124. RepoOwnerID: owner.ID,
  125. RepoName: repo.Name,
  126. RefFullName: opts.RefFullName,
  127. OldCommitID: opts.OldCommitID,
  128. NewCommitID: opts.NewCommitID,
  129. Commits: ListToPushCommits(l),
  130. }); err != nil {
  131. return nil, fmt.Errorf("CommitRepoAction (branch): %v", err)
  132. }
  133. return repo, nil
  134. }