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.

update.go 3.8KB

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/base"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/log"
  13. )
  14. type UpdateTask struct {
  15. ID int64 `xorm:"pk autoincr"`
  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 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, fmt.Errorf("task does not exist: %s", 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. repo, err := git.OpenRepository(f)
  57. if err != nil {
  58. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  59. }
  60. ru, err := GetUserByName(repoUserName)
  61. if err != nil {
  62. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  63. }
  64. repos, err := GetRepositoryByName(ru.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 := repo.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 := &base.PushCommits{}
  86. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  87. repos.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 := repo.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([]*base.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. &base.PushCommit{commit.Id.String(),
  122. commit.Message(),
  123. commit.Author.Email,
  124. commit.Author.Name,
  125. })
  126. }
  127. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  128. repos.ID, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits, ""}, oldCommitId, newCommitId); err != nil {
  129. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  130. }
  131. return nil
  132. }