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.

156 lines
3.4 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
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
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
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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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 main
  5. import (
  6. "container/list"
  7. "os"
  8. "os/exec"
  9. "strconv"
  10. "strings"
  11. "github.com/codegangsta/cli"
  12. //"github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/git"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/qiniu/log"
  17. )
  18. var CmdUpdate = cli.Command{
  19. Name: "update",
  20. Usage: "This command just should be called by ssh shell",
  21. Description: `
  22. gogs serv provide access auth for repositories`,
  23. Action: runUpdate,
  24. Flags: []cli.Flag{},
  25. }
  26. // for command: ./gogs update
  27. func runUpdate(c *cli.Context) {
  28. base.NewConfigContext()
  29. models.LoadModelsConfig()
  30. models.SetEngine()
  31. w, _ := os.Create("update.log")
  32. defer w.Close()
  33. log.SetOutput(w)
  34. args := c.Args()
  35. //log.Info(args)
  36. if len(args) != 3 {
  37. log.Error("received less 3 parameters")
  38. return
  39. }
  40. refName := args[0]
  41. if refName == "" {
  42. log.Error("refName is empty, shouldn't use")
  43. return
  44. }
  45. oldCommitId := args[1]
  46. newCommitId := args[2]
  47. isNew := strings.HasPrefix(oldCommitId, "0000000")
  48. if isNew &&
  49. strings.HasPrefix(newCommitId, "0000000") {
  50. log.Error("old rev and new rev both 000000")
  51. return
  52. }
  53. userName := os.Getenv("userName")
  54. userId := os.Getenv("userId")
  55. //repoId := os.Getenv("repoId")
  56. repoName := os.Getenv("repoName")
  57. f := models.RepoPath(userName, repoName)
  58. gitUpdate := exec.Command("git", "update-server-info")
  59. gitUpdate.Dir = f
  60. gitUpdate.Run()
  61. repo, err := git.OpenRepository(f)
  62. if err != nil {
  63. log.Error("runUpdate.Open repoId: %v", err)
  64. return
  65. }
  66. newOid, err := git.NewOidFromString(newCommitId)
  67. if err != nil {
  68. log.Error("runUpdate.Ref repoId: %v", err)
  69. return
  70. }
  71. newCommit, err := repo.LookupCommit(newOid)
  72. if err != nil {
  73. log.Error("runUpdate.Ref repoId: %v", err)
  74. return
  75. }
  76. var l *list.List
  77. // if a new branch
  78. if isNew {
  79. l, err = repo.CommitsBefore(newCommit.Id())
  80. if err != nil {
  81. log.Error("Find CommitsBefore erro:", err)
  82. return
  83. }
  84. } else {
  85. oldOid, err := git.NewOidFromString(oldCommitId)
  86. if err != nil {
  87. log.Error("runUpdate.Ref repoId: %v", err)
  88. return
  89. }
  90. oldCommit, err := repo.LookupCommit(oldOid)
  91. if err != nil {
  92. log.Error("runUpdate.Ref repoId: %v", err)
  93. return
  94. }
  95. l = repo.CommitsBetween(newCommit, oldCommit)
  96. }
  97. if err != nil {
  98. log.Error("runUpdate.Commit repoId: %v", err)
  99. return
  100. }
  101. sUserId, err := strconv.Atoi(userId)
  102. if err != nil {
  103. log.Error("runUpdate.Parse userId: %v", err)
  104. return
  105. }
  106. repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
  107. if err != nil {
  108. log.Error("runUpdate.GetRepositoryByName userId: %v", err)
  109. return
  110. }
  111. commits := make([]*base.PushCommit, 0)
  112. var maxCommits = 3
  113. var actEmail string
  114. for e := l.Front(); e != nil; e = e.Next() {
  115. commit := e.Value.(*git.Commit)
  116. if actEmail == "" {
  117. actEmail = commit.Committer.Email
  118. }
  119. commits = append(commits,
  120. &base.PushCommit{commit.Id().String(),
  121. commit.Message(),
  122. commit.Author.Email,
  123. commit.Author.Name})
  124. if len(commits) >= maxCommits {
  125. break
  126. }
  127. }
  128. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  129. if err = models.CommitRepoAction(int64(sUserId), userName, actEmail,
  130. repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
  131. log.Error("runUpdate.models.CommitRepoAction: %v", err)
  132. }
  133. }