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.

159 lines
3.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/log"
  13. )
  14. type UpdateTask struct {
  15. Id int64
  16. Uuid string `xorm:"index"`
  17. RefName string
  18. OldCommitId string
  19. NewCommitId string
  20. }
  21. func AddUpdateTask(task *UpdateTask) error {
  22. _, err := x.Insert(task)
  23. return err
  24. }
  25. func GetUpdateTasksByUuid(uuid string) ([]*UpdateTask, error) {
  26. task := &UpdateTask{
  27. Uuid: uuid,
  28. }
  29. tasks := make([]*UpdateTask, 0)
  30. err := x.Find(&tasks, task)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return tasks, nil
  35. }
  36. func DelUpdateTasksByUuid(uuid string) error {
  37. _, err := x.Delete(&UpdateTask{Uuid: uuid})
  38. return err
  39. }
  40. func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) error {
  41. isNew := strings.HasPrefix(oldCommitId, "0000000")
  42. if isNew &&
  43. strings.HasPrefix(newCommitId, "0000000") {
  44. return fmt.Errorf("old rev and new rev both 000000")
  45. }
  46. f := RepoPath(repoUserName, repoName)
  47. gitUpdate := exec.Command("git", "update-server-info")
  48. gitUpdate.Dir = f
  49. gitUpdate.Run()
  50. isDel := strings.HasPrefix(newCommitId, "0000000")
  51. if isDel {
  52. log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId)
  53. return nil
  54. }
  55. repo, err := git.OpenRepository(f)
  56. if err != nil {
  57. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  58. }
  59. ru, err := GetUserByName(repoUserName)
  60. if err != nil {
  61. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  62. }
  63. repos, err := GetRepositoryByName(ru.Id, repoName)
  64. if err != nil {
  65. return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err)
  66. }
  67. // Push tags.
  68. if strings.HasPrefix(refName, "refs/tags/") {
  69. tagName := git.RefEndName(refName)
  70. tag, err := repo.GetTag(tagName)
  71. if err != nil {
  72. log.GitLogger.Fatal(4, "runUpdate.GetTag: %v", err)
  73. }
  74. var actEmail string
  75. if tag.Tagger != nil {
  76. actEmail = tag.Tagger.Email
  77. } else {
  78. cmt, err := tag.Commit()
  79. if err != nil {
  80. log.GitLogger.Fatal(4, "runUpdate.GetTag Commit: %v", err)
  81. }
  82. actEmail = cmt.Committer.Email
  83. }
  84. commit := &base.PushCommits{}
  85. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  86. repos.Id, repoUserName, repoName, refName, commit, oldCommitId, newCommitId); err != nil {
  87. log.GitLogger.Fatal(4, "runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  88. }
  89. return err
  90. }
  91. newCommit, err := repo.GetCommit(newCommitId)
  92. if err != nil {
  93. return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err)
  94. }
  95. var l *list.List
  96. // if a new branch
  97. if isNew {
  98. l, err = newCommit.CommitsBefore()
  99. if err != nil {
  100. return fmt.Errorf("Find CommitsBefore erro: %v", err)
  101. }
  102. } else {
  103. l, err = newCommit.CommitsBeforeUntil(oldCommitId)
  104. if err != nil {
  105. return fmt.Errorf("Find CommitsBeforeUntil erro: %v", err)
  106. }
  107. }
  108. if err != nil {
  109. return fmt.Errorf("runUpdate.Commit repoId: %v", err)
  110. }
  111. // if commits push
  112. commits := make([]*base.PushCommit, 0)
  113. var maxCommits = 2
  114. var actEmail string
  115. for e := l.Front(); e != nil; e = e.Next() {
  116. commit := e.Value.(*git.Commit)
  117. if actEmail == "" {
  118. actEmail = commit.Committer.Email
  119. }
  120. commits = append(commits,
  121. &base.PushCommit{commit.Id.String(),
  122. commit.Message(),
  123. commit.Author.Email,
  124. commit.Author.Name})
  125. if len(commits) >= maxCommits {
  126. break
  127. }
  128. }
  129. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  130. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  131. repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}, oldCommitId, newCommitId); err != nil {
  132. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  133. }
  134. return nil
  135. }