Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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