From e08161a3021236d8249a3b426967666b916f7d1e Mon Sep 17 00:00:00 2001
From: Unknwon <u@gogs.io>
Date: Tue, 28 Feb 2017 22:58:52 -0500
Subject: hook: fix can’t be executed while run as service on Windows (#4207)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 cmd/hook.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

(limited to 'cmd/hook.go')

diff --git a/cmd/hook.go b/cmd/hook.go
index fede8649..95db18f0 100644
--- a/cmd/hook.go
+++ b/cmd/hook.go
@@ -117,7 +117,8 @@ func runHookPreReceive(c *cli.Context) error {
 		}
 
 		// Check force push
-		output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).Run()
+		output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).
+			RunInDir(models.RepoPath(os.Getenv(http.ENV_REPO_OWNER_NAME), os.Getenv(http.ENV_REPO_NAME)))
 		if err != nil {
 			fail("Internal error", "Fail to detect force push: %v", err)
 		} else if len(output) > 0 {
@@ -131,6 +132,7 @@ func runHookPreReceive(c *cli.Context) error {
 	}
 
 	hookCmd := exec.Command(customHooksPath)
+	hookCmd.Dir = models.RepoPath(os.Getenv(http.ENV_REPO_OWNER_NAME), os.Getenv(http.ENV_REPO_NAME))
 	hookCmd.Stdout = os.Stdout
 	hookCmd.Stdin = buf
 	hookCmd.Stderr = os.Stderr
@@ -159,6 +161,7 @@ func runHookUpdate(c *cli.Context) error {
 	}
 
 	hookCmd := exec.Command(customHooksPath, args...)
+	hookCmd.Dir = models.RepoPath(os.Getenv(http.ENV_REPO_OWNER_NAME), os.Getenv(http.ENV_REPO_NAME))
 	hookCmd.Stdout = os.Stdout
 	hookCmd.Stdin = os.Stdin
 	hookCmd.Stderr = os.Stderr
@@ -231,6 +234,7 @@ func runHookPostReceive(c *cli.Context) error {
 	}
 
 	hookCmd := exec.Command(customHooksPath)
+	hookCmd.Dir = models.RepoPath(os.Getenv(http.ENV_REPO_OWNER_NAME), os.Getenv(http.ENV_REPO_NAME))
 	hookCmd.Stdout = os.Stdout
 	hookCmd.Stdin = buf
 	hookCmd.Stderr = os.Stderr
-- 
cgit v1.2.3