update.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) error {
  15. //fmt.Println(refName, oldCommitId, newCommitId)
  16. //fmt.Println(userName, repoUserName, repoName)
  17. isNew := strings.HasPrefix(oldCommitId, "0000000")
  18. if isNew &&
  19. strings.HasPrefix(newCommitId, "0000000") {
  20. return fmt.Errorf("old rev and new rev both 000000")
  21. }
  22. f := RepoPath(repoUserName, repoName)
  23. gitUpdate := exec.Command("git", "update-server-info")
  24. gitUpdate.Dir = f
  25. gitUpdate.Run()
  26. isDel := strings.HasPrefix(newCommitId, "0000000")
  27. if isDel {
  28. log.GitLogger.Info("del rev", refName, "from", userName+"/"+repoName+".git", "by", userId)
  29. return nil
  30. }
  31. repo, err := git.OpenRepository(f)
  32. if err != nil {
  33. return fmt.Errorf("runUpdate.Open repoId: %v", err)
  34. }
  35. newCommit, err := repo.GetCommit(newCommitId)
  36. if err != nil {
  37. return fmt.Errorf("runUpdate GetCommit of newCommitId: %v", err)
  38. }
  39. var l *list.List
  40. // if a new branch
  41. if isNew {
  42. l, err = newCommit.CommitsBefore()
  43. if err != nil {
  44. return fmt.Errorf("Find CommitsBefore erro: %v", err)
  45. }
  46. } else {
  47. l, err = newCommit.CommitsBeforeUntil(oldCommitId)
  48. if err != nil {
  49. return fmt.Errorf("Find CommitsBeforeUntil erro: %v", err)
  50. }
  51. }
  52. if err != nil {
  53. return fmt.Errorf("runUpdate.Commit repoId: %v", err)
  54. }
  55. ru, err := GetUserByName(repoUserName)
  56. if err != nil {
  57. return fmt.Errorf("runUpdate.GetUserByName: %v", err)
  58. }
  59. repos, err := GetRepositoryByName(ru.Id, repoName)
  60. if err != nil {
  61. return fmt.Errorf("runUpdate.GetRepositoryByName userId: %v", err)
  62. }
  63. // if tags push
  64. if strings.HasPrefix(refName, "refs/tags/") {
  65. tagName := git.RefEndName(refName)
  66. tag, err := repo.GetTag(tagName)
  67. if err != nil {
  68. log.GitLogger.Fatal("runUpdate.GetTag: %v", err)
  69. }
  70. var actEmail string
  71. if tag.Tagger != nil {
  72. actEmail = tag.Tagger.Email
  73. } else {
  74. cmt, err := tag.Commit()
  75. if err != nil {
  76. log.GitLogger.Fatal("runUpdate.GetTag Commit: %v", err)
  77. }
  78. actEmail = cmt.Committer.Email
  79. }
  80. commit := &base.PushCommits{}
  81. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  82. repos.Id, repoUserName, repoName, refName, commit); err != nil {
  83. log.GitLogger.Fatal("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  84. }
  85. return err
  86. }
  87. // if commits push
  88. commits := make([]*base.PushCommit, 0)
  89. var maxCommits = 3
  90. var actEmail string
  91. for e := l.Front(); e != nil; e = e.Next() {
  92. commit := e.Value.(*git.Commit)
  93. if actEmail == "" {
  94. actEmail = commit.Committer.Email
  95. }
  96. commits = append(commits,
  97. &base.PushCommit{commit.Id.String(),
  98. commit.Message(),
  99. commit.Author.Email,
  100. commit.Author.Name})
  101. if len(commits) >= maxCommits {
  102. break
  103. }
  104. }
  105. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  106. if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
  107. repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
  108. return fmt.Errorf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
  109. }
  110. return nil
  111. }