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.

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