update.go 3.7 KB

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