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.

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