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.

162 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
  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/git"
  11. "github.com/gogits/gogs/modules/base"
  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. //fmt.Println(refName, oldCommitId, newCommitId)
  42. //fmt.Println(userName, repoUserName, repoName)
  43. isNew := strings.HasPrefix(oldCommitId, "0000000")
  44. if isNew &&
  45. strings.HasPrefix(newCommitId, "0000000") {
  46. return fmt.Errorf("old rev and new rev both 000000")
  47. }
  48. f := RepoPath(repoUserName, repoName)
  49. gitUpdate := exec.Command("git", "update-server-info")
  50. gitUpdate.Dir = f
  51. gitUpdate.Run()
  52. isDel := strings.HasPrefix(newCommitId, "0000000")
  53. if isDel {
  54. log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId)
  55. return nil
  56. }
  57. repo, err := git.OpenRepository(f)
  58. if err != nil {
  59. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  60. }
  61. ru, err := GetUserByName(repoUserName)
  62. if err != nil {
  63. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  64. }
  65. repos, err := GetRepositoryByName(ru.Id, repoName)
  66. if err != nil {
  67. return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err)
  68. }
  69. // if tags push
  70. if strings.HasPrefix(refName, "refs/tags/") {
  71. tagName := git.RefEndName(refName)
  72. tag, err := repo.GetTag(tagName)
  73. if err != nil {
  74. log.GitLogger.Fatal("runUpdate.GetTag: %v", err)
  75. }
  76. var actEmail string
  77. if tag.Tagger != nil {
  78. actEmail = tag.Tagger.Email
  79. } else {
  80. cmt, err := tag.Commit()
  81. if err != nil {
  82. log.GitLogger.Fatal("runUpdate.GetTag Commit: %v", err)
  83. }
  84. actEmail = cmt.Committer.Email
  85. }
  86. commit := &base.PushCommits{}
  87. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  88. repos.Id, repoUserName, repoName, refName, commit); err != nil {
  89. log.GitLogger.Fatal("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  90. }
  91. return err
  92. }
  93. newCommit, err := repo.GetCommit(newCommitId)
  94. if err != nil {
  95. return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err)
  96. }
  97. var l *list.List
  98. // if a new branch
  99. if isNew {
  100. l, err = newCommit.CommitsBefore()
  101. if err != nil {
  102. return fmt.Errorf("Find CommitsBefore erro: %v", err)
  103. }
  104. } else {
  105. l, err = newCommit.CommitsBeforeUntil(oldCommitId)
  106. if err != nil {
  107. return fmt.Errorf("Find CommitsBeforeUntil erro: %v", err)
  108. }
  109. }
  110. if err != nil {
  111. return fmt.Errorf("runUpdate.Commit repoId: %v", err)
  112. }
  113. // if commits push
  114. commits := make([]*base.PushCommit, 0)
  115. var maxCommits = 3
  116. var actEmail string
  117. for e := l.Front(); e != nil; e = e.Next() {
  118. commit := e.Value.(*git.Commit)
  119. if actEmail == "" {
  120. actEmail = commit.Committer.Email
  121. }
  122. commits = append(commits,
  123. &base.PushCommit{commit.Id.String(),
  124. commit.Message(),
  125. commit.Author.Email,
  126. commit.Author.Name})
  127. if len(commits) >= maxCommits {
  128. break
  129. }
  130. }
  131. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  132. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  133. repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
  134. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  135. }
  136. return nil
  137. }