update.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/git"
  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 Update(refName, oldCommitID, newCommitID, userName, repoUserName, repoName string, userID int64) error {
  42. isNew := strings.HasPrefix(oldCommitID, "0000000")
  43. if isNew &&
  44. strings.HasPrefix(newCommitID, "0000000") {
  45. return fmt.Errorf("old rev and new rev both 000000")
  46. }
  47. f := RepoPath(repoUserName, repoName)
  48. gitUpdate := exec.Command("git", "update-server-info")
  49. gitUpdate.Dir = f
  50. gitUpdate.Run()
  51. isDel := strings.HasPrefix(newCommitID, "0000000")
  52. if isDel {
  53. log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userID)
  54. return nil
  55. }
  56. gitRepo, err := git.OpenRepository(f)
  57. if err != nil {
  58. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  59. }
  60. user, err := GetUserByName(repoUserName)
  61. if err != nil {
  62. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  63. }
  64. repo, err := GetRepositoryByName(user.Id, repoName)
  65. if err != nil {
  66. return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err)
  67. }
  68. // Push tags.
  69. if strings.HasPrefix(refName, "refs/tags/") {
  70. tagName := git.RefEndName(refName)
  71. tag, err := gitRepo.GetTag(tagName)
  72. if err != nil {
  73. log.GitLogger.Fatal(4, "runUpdate.GetTag: %v", err)
  74. }
  75. var actEmail string
  76. if tag.Tagger != nil {
  77. actEmail = tag.Tagger.Email
  78. } else {
  79. cmt, err := tag.Commit()
  80. if err != nil {
  81. log.GitLogger.Fatal(4, "runUpdate.GetTag Commit: %v", err)
  82. }
  83. actEmail = cmt.Committer.Email
  84. }
  85. commit := &PushCommits{}
  86. if err = CommitRepoAction(userID, user.Id, userName, actEmail,
  87. repo.ID, repoUserName, repoName, refName, commit, oldCommitID, newCommitID); err != nil {
  88. log.GitLogger.Fatal(4, "CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  89. }
  90. return err
  91. }
  92. newCommit, err := gitRepo.GetCommit(newCommitID)
  93. if err != nil {
  94. return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err)
  95. }
  96. // Push new branch.
  97. var l *list.List
  98. if isNew {
  99. l, err = newCommit.CommitsBefore()
  100. if err != nil {
  101. return fmt.Errorf("CommitsBefore: %v", err)
  102. }
  103. } else {
  104. l, err = newCommit.CommitsBeforeUntil(oldCommitID)
  105. if err != nil {
  106. return fmt.Errorf("CommitsBeforeUntil: %v", err)
  107. }
  108. }
  109. if err != nil {
  110. return fmt.Errorf("runUpdate.Commit repoId: %v", err)
  111. }
  112. // Push commits.
  113. commits := make([]*PushCommit, 0)
  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. &PushCommit{commit.ID.String(),
  122. commit.Message(),
  123. commit.Author.Email,
  124. commit.Author.Name,
  125. })
  126. }
  127. if err = CommitRepoAction(userID, user.Id, userName, actEmail,
  128. repo.ID, repoUserName, repoName, refName, &PushCommits{l.Len(), commits, "", nil}, oldCommitID, newCommitID); err != nil {
  129. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  130. }
  131. return nil
  132. }