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.

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