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.6 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if models.UseSQLite3 {
  31. execDir, _ := base.ExecDir()
  32. os.Chdir(execDir)
  33. }
  34. models.SetEngine()
  35. w, _ := os.Create("update.log")
  36. defer w.Close()
  37. log.SetOutput(w)
  38. args := c.Args()
  39. //log.Info(args)
  40. if len(args) != 3 {
  41. log.Error("received less 3 parameters")
  42. return
  43. }
  44. refName := args[0]
  45. if refName == "" {
  46. log.Error("refName is empty, shouldn't use")
  47. return
  48. }
  49. oldCommitId := args[1]
  50. newCommitId := args[2]
  51. isNew := strings.HasPrefix(oldCommitId, "0000000")
  52. if isNew &&
  53. strings.HasPrefix(newCommitId, "0000000") {
  54. log.Error("old rev and new rev both 000000")
  55. return
  56. }
  57. userName := os.Getenv("userName")
  58. userId := os.Getenv("userId")
  59. //repoId := os.Getenv("repoId")
  60. repoName := os.Getenv("repoName")
  61. f := models.RepoPath(userName, repoName)
  62. gitUpdate := exec.Command("git", "update-server-info")
  63. gitUpdate.Dir = f
  64. gitUpdate.Run()
  65. repo, err := git.OpenRepository(f)
  66. if err != nil {
  67. log.Error("runUpdate.Open repoId: %v", err)
  68. return
  69. }
  70. newOid, err := git.NewOidFromString(newCommitId)
  71. if err != nil {
  72. log.Error("runUpdate.Ref repoId: %v", err)
  73. return
  74. }
  75. newCommit, err := repo.LookupCommit(newOid)
  76. if err != nil {
  77. log.Error("runUpdate.Ref repoId: %v", err)
  78. return
  79. }
  80. var l *list.List
  81. // if a new branch
  82. if isNew {
  83. l, err = repo.CommitsBefore(newCommit.Id())
  84. if err != nil {
  85. log.Error("Find CommitsBefore erro:", err)
  86. return
  87. }
  88. } else {
  89. oldOid, err := git.NewOidFromString(oldCommitId)
  90. if err != nil {
  91. log.Error("runUpdate.Ref repoId: %v", err)
  92. return
  93. }
  94. oldCommit, err := repo.LookupCommit(oldOid)
  95. if err != nil {
  96. log.Error("runUpdate.Ref repoId: %v", err)
  97. return
  98. }
  99. l = repo.CommitsBetween(newCommit, oldCommit)
  100. }
  101. if err != nil {
  102. log.Error("runUpdate.Commit repoId: %v", err)
  103. return
  104. }
  105. sUserId, err := strconv.Atoi(userId)
  106. if err != nil {
  107. log.Error("runUpdate.Parse userId: %v", err)
  108. return
  109. }
  110. repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
  111. if err != nil {
  112. log.Error("runUpdate.GetRepositoryByName userId: %v", err)
  113. return
  114. }
  115. commits := make([]*base.PushCommit, 0)
  116. var maxCommits = 3
  117. var actEmail string
  118. for e := l.Front(); e != nil; e = e.Next() {
  119. commit := e.Value.(*git.Commit)
  120. if actEmail == "" {
  121. actEmail = commit.Committer.Email
  122. }
  123. commits = append(commits,
  124. &base.PushCommit{commit.Id().String(),
  125. commit.Message(),
  126. commit.Author.Email,
  127. commit.Author.Name})
  128. if len(commits) >= maxCommits {
  129. break
  130. }
  131. }
  132. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  133. if err = models.CommitRepoAction(int64(sUserId), userName, actEmail,
  134. repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
  135. log.Error("runUpdate.models.CommitRepoAction: %v", err)
  136. }
  137. }