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.

146 lines
3.2 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
  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. "os"
  7. "os/exec"
  8. "strings"
  9. "strconv"
  10. "container/list"
  11. "github.com/codegangsta/cli"
  12. //"github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/models"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/qiniu/log"
  16. "github.com/gogits/git"
  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. //level := "0"
  29. //os.MkdirAll("log", os.ModePerm)
  30. //log.NewLogger(10000, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, "log/serv.log"))
  31. //log.Info("start update logging...")
  32. w, _ := os.Create("update.log")
  33. defer w.Close()
  34. log.SetOutput(w)
  35. args := c.Args()
  36. if len(args) != 3 {
  37. log.Error("received less 3 parameters")
  38. return
  39. }
  40. refName := args[0]
  41. oldCommitId := args[1]
  42. newCommitId := args[2]
  43. userName := os.Getenv("userName")
  44. userId := os.Getenv("userId")
  45. //repoId := os.Getenv("repoId")
  46. repoName := os.Getenv("repoName")
  47. log.Info("username", userName)
  48. log.Info("repoName", repoName)
  49. f := models.RepoPath(userName, repoName)
  50. log.Info("f", f)
  51. gitUpdate := exec.Command("git", "update-server-info")
  52. gitUpdate.Dir = f
  53. gitUpdate.Run()
  54. repo, err := git.OpenRepository(f)
  55. if err != nil {
  56. log.Error("runUpdate.Open repoId: %v", err)
  57. return
  58. }
  59. ref, err := repo.LookupReference(refName)
  60. if err != nil {
  61. log.Error("runUpdate.Ref repoId: %v", err)
  62. return
  63. }
  64. oldOid, err := git.NewOidFromString(oldCommitId)
  65. if err != nil {
  66. log.Error("runUpdate.Ref repoId: %v", err)
  67. return
  68. }
  69. oldCommit, err := repo.LookupCommit(oldOid)
  70. if err != nil {
  71. log.Error("runUpdate.Ref repoId: %v", err)
  72. return
  73. }
  74. newOid, err := git.NewOidFromString(newCommitId)
  75. if err != nil {
  76. log.Error("runUpdate.Ref repoId: %v", err)
  77. return
  78. }
  79. newCommit, err := repo.LookupCommit(newOid)
  80. if err != nil {
  81. log.Error("runUpdate.Ref repoId: %v", err)
  82. return
  83. }
  84. var l *list.List
  85. // if a new branch
  86. if strings.HasPrefix(oldCommitId, "0000000") {
  87. l, err = ref.AllCommits()
  88. } else {
  89. l = ref.CommitsBetween(newCommit, oldCommit)
  90. }
  91. if err != nil {
  92. log.Error("runUpdate.Commit repoId: %v", err)
  93. return
  94. }
  95. sUserId, err := strconv.Atoi(userId)
  96. if err != nil {
  97. log.Error("runUpdate.Parse userId: %v", err)
  98. return
  99. }
  100. repos, err := models.GetRepositoryByName(int64(sUserId), repoName)
  101. if err != nil {
  102. log.Error("runUpdate.GetRepositoryByName userId: %v", err)
  103. return
  104. }
  105. commits := make([][]string, 0)
  106. var maxCommits = 3
  107. for e := l.Front(); e != nil; e = e.Next() {
  108. commit := e.Value.(*git.Commit)
  109. commits = append(commits, []string{commit.Id().String(), commit.Message()})
  110. if len(commits) >= maxCommits {
  111. break
  112. }
  113. }
  114. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  115. if err = models.CommitRepoAction(int64(sUserId), userName,
  116. repos.Id, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
  117. log.Error("runUpdate.models.CommitRepoAction: %v", err)
  118. }
  119. }