diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-23 11:40:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-23 11:40:44 +0800 |
commit | 0e6b9ea786a77a4df4aa9fff1b96b9483bcdded5 (patch) | |
tree | 83ce2a577016f3a6fd478cce8e384f9b3118f3f6 /cmd | |
parent | 4f3880ff151fcbda019a1a9467bc39da621fbe07 (diff) | |
download | gitea-0e6b9ea786a77a4df4aa9fff1b96b9483bcdded5.tar.gz gitea-0e6b9ea786a77a4df4aa9fff1b96b9483bcdded5.zip |
Take back control of hooks (#1006)
* git: delegate all server-side Git hooks (#1623)
* create hooks directories
* take control hooks back
* fix lint
* bug fixed and minor changes
* fix imports style
* fix migration scripts
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/hook.go | 106 | ||||
-rw-r--r-- | cmd/serv.go (renamed from cmd/serve.go) | 6 |
2 files changed, 108 insertions, 4 deletions
diff --git a/cmd/hook.go b/cmd/hook.go new file mode 100644 index 0000000000..15ad74f8e0 --- /dev/null +++ b/cmd/hook.go @@ -0,0 +1,106 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "fmt" + "os" + + "code.gitea.io/gitea/models" + + "github.com/urfave/cli" +) + +var ( + // CmdHook represents the available hooks sub-command. + CmdHook = cli.Command{ + Name: "hook", + Usage: "Delegate commands to corresponding Git hooks", + Description: "This should only be called by Git", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "config, c", + Value: "custom/conf/app.ini", + Usage: "Custom configuration file path", + }, + }, + Subcommands: []cli.Command{ + subcmdHookPreReceive, + subcmdHookUpadte, + subcmdHookPostReceive, + }, + } + + subcmdHookPreReceive = cli.Command{ + Name: "pre-receive", + Usage: "Delegate pre-receive Git hook", + Description: "This command should only be called by Git", + Action: runHookPreReceive, + } + subcmdHookUpadte = cli.Command{ + Name: "update", + Usage: "Delegate update Git hook", + Description: "This command should only be called by Git", + Action: runHookUpdate, + } + subcmdHookPostReceive = cli.Command{ + Name: "post-receive", + Usage: "Delegate post-receive Git hook", + Description: "This command should only be called by Git", + Action: runHookPostReceive, + } +) + +func runHookPreReceive(c *cli.Context) error { + if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { + return nil + } + if err := setup("hooks/pre-receive.log"); err != nil { + fail("Hook pre-receive init failed", fmt.Sprintf("setup: %v", err)) + } + + return nil +} + +func runHookUpdate(c *cli.Context) error { + if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { + return nil + } + + if err := setup("hooks/update.log"); err != nil { + fail("Hook update init failed", fmt.Sprintf("setup: %v", err)) + } + + args := c.Args() + if len(args) != 3 { + fail("Arguments received are not equal to three", "Arguments received are not equal to three") + } else if len(args[0]) == 0 { + fail("First argument 'refName' is empty", "First argument 'refName' is empty") + } + + uuid := os.Getenv(envUpdateTaskUUID) + if err := models.AddUpdateTask(&models.UpdateTask{ + UUID: uuid, + RefName: args[0], + OldCommitID: args[1], + NewCommitID: args[2], + }); err != nil { + fail("Internal error", "Fail to add update task '%s': %v", uuid, err) + } + + return nil +} + +func runHookPostReceive(c *cli.Context) error { + if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { + return nil + } + + if err := setup("hooks/post-receive.log"); err != nil { + fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err)) + } + + return nil +} diff --git a/cmd/serve.go b/cmd/serv.go index f4a3c3d2c6..141a58a2ac 100644 --- a/cmd/serve.go +++ b/cmd/serv.go @@ -30,6 +30,7 @@ import ( const ( accessDenied = "Repository does not exist or you do not have access" lfsAuthenticateVerb = "git-lfs-authenticate" + envUpdateTaskUUID = "GITEA_UUID" ) // CmdServ represents the available serv sub-command. @@ -170,7 +171,6 @@ func runServ(c *cli.Context) error { var lfsVerb string if verb == lfsAuthenticateVerb { - if !setting.LFS.StartServer { fail("Unknown git command", "LFS authentication request over SSH denied, LFS support is disabled") } @@ -291,9 +291,7 @@ func runServ(c *cli.Context) error { } //LFS token authentication - if verb == lfsAuthenticateVerb { - url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, repoUser.Name, repo.Name) now := time.Now() @@ -326,7 +324,7 @@ func runServ(c *cli.Context) error { } uuid := gouuid.NewV4().String() - os.Setenv("GITEA_UUID", uuid) + os.Setenv(envUpdateTaskUUID, uuid) // Keep the old env variable name for backward compability os.Setenv("uuid", uuid) |