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.

106 lines
2.6 KiB

  1. // Copyright 2017 The Gitea 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. "fmt"
  7. "os"
  8. "code.gitea.io/gitea/models"
  9. "github.com/urfave/cli"
  10. )
  11. var (
  12. // CmdHook represents the available hooks sub-command.
  13. CmdHook = cli.Command{
  14. Name: "hook",
  15. Usage: "Delegate commands to corresponding Git hooks",
  16. Description: "This should only be called by Git",
  17. Flags: []cli.Flag{
  18. cli.StringFlag{
  19. Name: "config, c",
  20. Value: "custom/conf/app.ini",
  21. Usage: "Custom configuration file path",
  22. },
  23. },
  24. Subcommands: []cli.Command{
  25. subcmdHookPreReceive,
  26. subcmdHookUpadte,
  27. subcmdHookPostReceive,
  28. },
  29. }
  30. subcmdHookPreReceive = cli.Command{
  31. Name: "pre-receive",
  32. Usage: "Delegate pre-receive Git hook",
  33. Description: "This command should only be called by Git",
  34. Action: runHookPreReceive,
  35. }
  36. subcmdHookUpadte = cli.Command{
  37. Name: "update",
  38. Usage: "Delegate update Git hook",
  39. Description: "This command should only be called by Git",
  40. Action: runHookUpdate,
  41. }
  42. subcmdHookPostReceive = cli.Command{
  43. Name: "post-receive",
  44. Usage: "Delegate post-receive Git hook",
  45. Description: "This command should only be called by Git",
  46. Action: runHookPostReceive,
  47. }
  48. )
  49. func runHookPreReceive(c *cli.Context) error {
  50. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  51. return nil
  52. }
  53. if err := setup("hooks/pre-receive.log"); err != nil {
  54. fail("Hook pre-receive init failed", fmt.Sprintf("setup: %v", err))
  55. }
  56. return nil
  57. }
  58. func runHookUpdate(c *cli.Context) error {
  59. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  60. return nil
  61. }
  62. if err := setup("hooks/update.log"); err != nil {
  63. fail("Hook update init failed", fmt.Sprintf("setup: %v", err))
  64. }
  65. args := c.Args()
  66. if len(args) != 3 {
  67. fail("Arguments received are not equal to three", "Arguments received are not equal to three")
  68. } else if len(args[0]) == 0 {
  69. fail("First argument 'refName' is empty", "First argument 'refName' is empty")
  70. }
  71. uuid := os.Getenv(envUpdateTaskUUID)
  72. if err := models.AddUpdateTask(&models.UpdateTask{
  73. UUID: uuid,
  74. RefName: args[0],
  75. OldCommitID: args[1],
  76. NewCommitID: args[2],
  77. }); err != nil {
  78. fail("Internal error", "Fail to add update task '%s': %v", uuid, err)
  79. }
  80. return nil
  81. }
  82. func runHookPostReceive(c *cli.Context) error {
  83. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  84. return nil
  85. }
  86. if err := setup("hooks/post-receive.log"); err != nil {
  87. fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err))
  88. }
  89. return nil
  90. }