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.7 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
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Open(logPath)
  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. newLogger(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. return
  77. }
  78. newCommit, err := repo.LookupCommit(newOid)
  79. if err != nil {
  80. qlog.Fatalf("runUpdate.Ref repoId: %v", err)
  81. return
  82. }
  83. var l *list.List
  84. // if a new branch
  85. if isNew {
  86. l, err = repo.CommitsBefore(newCommit.Id())
  87. if err != nil {
  88. qlog.Fatalf("Find CommitsBefore erro:", err)
  89. return
  90. }
  91. } else {
  92. oldOid, err := git.NewOidFromString(oldCommitId)
  93. if err != nil {
  94. qlog.Fatalf("runUpdate.Ref repoId: %v", err)
  95. return
  96. }
  97. oldCommit, err := repo.LookupCommit(oldOid)
  98. if err != nil {
  99. qlog.Fatalf("runUpdate.Ref repoId: %v", err)
  100. return
  101. }
  102. l = repo.CommitsBetween(newCommit, oldCommit)
  103. }
  104. if err != nil {
  105. qlog.Fatalf("runUpdate.Commit repoId: %v", err)
  106. return
  107. }
  108. sUserId, err := strconv.Atoi(userId)
  109. if err != nil {
  110. qlog.Fatalf("runUpdate.Parse userId: %v", err)
  111. return
  112. }
  113. repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
  114. if err != nil {
  115. qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err)
  116. return
  117. }
  118. commits := make([]*base.PushCommit, 0)
  119. var maxCommits = 3
  120. var actEmail string
  121. for e := l.Front(); e != nil; e = e.Next() {
  122. commit := e.Value.(*git.Commit)
  123. if actEmail == "" {
  124. actEmail = commit.Committer.Email
  125. }
  126. commits = append(commits,
  127. &base.PushCommit{commit.Id().String(),
  128. commit.Message(),
  129. commit.Author.Email,
  130. commit.Author.Name})
  131. if len(commits) >= maxCommits {
  132. break
  133. }
  134. }
  135. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  136. if err = models.CommitRepoAction(int64(sUserId), userName, actEmail,
  137. repos.Id, repoName, git.BranchName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
  138. qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
  139. }
  140. }