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.

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