update.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 cmd
  5. import (
  6. "os"
  7. "path"
  8. "strconv"
  9. "github.com/codegangsta/cli"
  10. qlog "github.com/qiniu/log"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. )
  14. var CmdUpdate = cli.Command{
  15. Name: "update",
  16. Usage: "This command should only be called by SSH shell",
  17. Description: `Update get pushed info and insert into database`,
  18. Action: runUpdate,
  19. Flags: []cli.Flag{},
  20. }
  21. func newUpdateLogger(execDir string) {
  22. logPath := execDir + "/log/update.log"
  23. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  24. f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
  25. if err != nil {
  26. qlog.Fatal(err)
  27. }
  28. qlog.SetOutput(f)
  29. qlog.Info("Start logging update...")
  30. }
  31. func updateEnv(refName, oldCommitId, newCommitId string) {
  32. os.Setenv("refName", refName)
  33. os.Setenv("oldCommitId", oldCommitId)
  34. os.Setenv("newCommitId", newCommitId)
  35. qlog.Error("set envs:", refName, oldCommitId, newCommitId)
  36. }
  37. // for command: ./gogs update
  38. func runUpdate(c *cli.Context) {
  39. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  40. if cmd == "" {
  41. return
  42. }
  43. execDir, _ := base.ExecDir()
  44. newUpdateLogger(execDir)
  45. base.NewConfigContext()
  46. models.LoadModelsConfig()
  47. if models.UseSQLite3 {
  48. os.Chdir(execDir)
  49. }
  50. models.SetEngine()
  51. args := c.Args()
  52. if len(args) != 3 {
  53. qlog.Fatal("received less 3 parameters")
  54. }
  55. if args[0] == "" {
  56. qlog.Fatal("refName is empty, shouldn't use")
  57. }
  58. //updateEnv(args[0], args[1], args[2])
  59. userName := os.Getenv("userName")
  60. userId, _ := strconv.ParseInt(os.Getenv("userId"), 10, 64)
  61. //repoId := os.Getenv("repoId")
  62. repoUserName := os.Getenv("repoUserName")
  63. repoName := os.Getenv("repoName")
  64. models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId)
  65. }