Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

update.go 2.1KB

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. "strconv"
  8. "strings"
  9. "github.com/urfave/cli"
  10. "code.gitea.io/git"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. // CmdUpdate represents the available update sub-command.
  16. var CmdUpdate = cli.Command{
  17. Name: "update",
  18. Usage: "This command should only be called by Git hook",
  19. Description: `Update get pushed info and insert into database`,
  20. Action: runUpdate,
  21. Flags: []cli.Flag{
  22. cli.StringFlag{
  23. Name: "config, c",
  24. Value: "custom/conf/app.ini",
  25. Usage: "Custom configuration file path",
  26. },
  27. },
  28. }
  29. func runUpdate(c *cli.Context) error {
  30. if c.IsSet("config") {
  31. setting.CustomConf = c.String("config")
  32. }
  33. setup("update.log")
  34. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  35. log.GitLogger.Trace("SSH_ORIGINAL_COMMAND is empty")
  36. return nil
  37. }
  38. args := c.Args()
  39. if len(args) != 3 {
  40. log.GitLogger.Fatal(2, "Arguments received are not equal to three")
  41. } else if len(args[0]) == 0 {
  42. log.GitLogger.Fatal(2, "First argument 'refName' is empty, shouldn't use")
  43. }
  44. // protected branch check
  45. branchName := strings.TrimPrefix(args[0], git.BranchPrefix)
  46. repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
  47. log.GitLogger.Trace("pushing to %d %v", repoID, branchName)
  48. accessMode := models.ParseAccessMode(os.Getenv(models.ProtectedBranchAccessMode))
  49. // skip admin or owner AccessMode
  50. if accessMode == models.AccessModeWrite {
  51. protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
  52. if err != nil {
  53. log.GitLogger.Fatal(2, "retrieve protected branches information failed")
  54. }
  55. if protectBranch != nil {
  56. log.GitLogger.Fatal(2, "protected branches can not be pushed to")
  57. }
  58. }
  59. task := models.UpdateTask{
  60. UUID: os.Getenv("GITEA_UUID"),
  61. RefName: args[0],
  62. OldCommitID: args[1],
  63. NewCommitID: args[2],
  64. }
  65. if err := models.AddUpdateTask(&task); err != nil {
  66. log.GitLogger.Fatal(2, "AddUpdateTask: %v", err)
  67. }
  68. return nil
  69. }