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.

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