update.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const (
  22. MAX_COMMITS int = 5
  23. )
  24. func AddUpdateTask(task *UpdateTask) error {
  25. _, err := x.Insert(task)
  26. return err
  27. }
  28. func GetUpdateTasksByUuid(uuid string) ([]*UpdateTask, error) {
  29. task := &UpdateTask{
  30. Uuid: uuid,
  31. }
  32. tasks := make([]*UpdateTask, 0)
  33. err := x.Find(&tasks, task)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return tasks, nil
  38. }
  39. func DelUpdateTasksByUuid(uuid string) error {
  40. _, err := x.Delete(&UpdateTask{Uuid: uuid})
  41. return err
  42. }
  43. func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) error {
  44. isNew := strings.HasPrefix(oldCommitId, "0000000")
  45. if isNew &&
  46. strings.HasPrefix(newCommitId, "0000000") {
  47. return fmt.Errorf("old rev and new rev both 000000")
  48. }
  49. f := RepoPath(repoUserName, repoName)
  50. gitUpdate := exec.Command("git", "update-server-info")
  51. gitUpdate.Dir = f
  52. gitUpdate.Run()
  53. isDel := strings.HasPrefix(newCommitId, "0000000")
  54. if isDel {
  55. log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId)
  56. return nil
  57. }
  58. repo, err := git.OpenRepository(f)
  59. if err != nil {
  60. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  61. }
  62. ru, err := GetUserByName(repoUserName)
  63. if err != nil {
  64. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  65. }
  66. repos, err := GetRepositoryByName(ru.Id, repoName)
  67. if err != nil {
  68. return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err)
  69. }
  70. // Push tags.
  71. if strings.HasPrefix(refName, "refs/tags/") {
  72. tagName := git.RefEndName(refName)
  73. tag, err := repo.GetTag(tagName)
  74. if err != nil {
  75. log.GitLogger.Fatal(4, "runUpdate.GetTag: %v", err)
  76. }
  77. var actEmail string
  78. if tag.Tagger != nil {
  79. actEmail = tag.Tagger.Email
  80. } else {
  81. cmt, err := tag.Commit()
  82. if err != nil {
  83. log.GitLogger.Fatal(4, "runUpdate.GetTag Commit: %v", err)
  84. }
  85. actEmail = cmt.Committer.Email
  86. }
  87. commit := &base.PushCommits{}
  88. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  89. repos.Id, repoUserName, repoName, refName, commit, oldCommitId, newCommitId); err != nil {
  90. log.GitLogger.Fatal(4, "CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  91. }
  92. return err
  93. }
  94. newCommit, err := repo.GetCommit(newCommitId)
  95. if err != nil {
  96. return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err)
  97. }
  98. // Push new branch.
  99. var l *list.List
  100. if isNew {
  101. l, err = newCommit.CommitsBefore()
  102. if err != nil {
  103. return fmt.Errorf("Find CommitsBefore erro: %v", err)
  104. }
  105. } else {
  106. l, err = newCommit.CommitsBeforeUntil(oldCommitId)
  107. if err != nil {
  108. return fmt.Errorf("Find CommitsBeforeUntil erro: %v", err)
  109. }
  110. }
  111. if err != nil {
  112. return fmt.Errorf("runUpdate.Commit repoId: %v", err)
  113. }
  114. // Push commits.
  115. commits := make([]*base.PushCommit, 0)
  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) >= MAX_COMMITS {
  128. break
  129. }
  130. }
  131. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  132. repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits, ""}, oldCommitId, newCommitId); err != nil {
  133. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  134. }
  135. return nil
  136. }