You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

update.go 3.5 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }