From cfdc62e7fa4acb562b67eaa790fe2b6049ee8acb Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Wed, 1 Mar 2017 23:01:03 +0800
Subject: [PATCH] Comment force push detect to fix bug #1073 (#1077)

* umcomment force push detect to fix bug #1073

* fix #1086

* handle global config set and fix #1086
---
 cmd/hook.go | 59 +++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/cmd/hook.go b/cmd/hook.go
index 8f7f8c3c4..d120f21b2 100644
--- a/cmd/hook.go
+++ b/cmd/hook.go
@@ -69,6 +69,12 @@ func runHookPreReceive(c *cli.Context) error {
 		return nil
 	}
 
+	if c.IsSet("config") {
+		setting.CustomConf = c.String("config")
+	} else if c.GlobalIsSet("config") {
+		setting.CustomConf = c.GlobalString("config")
+	}
+
 	if err := setup("hooks/pre-receive.log"); err != nil {
 		fail("Hook pre-receive init failed", fmt.Sprintf("setup: %v", err))
 	}
@@ -76,9 +82,9 @@ func runHookPreReceive(c *cli.Context) error {
 	// the environment setted on serv command
 	repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
 	isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
-	username := os.Getenv(models.EnvRepoUsername)
-	reponame := os.Getenv(models.EnvRepoName)
-	repoPath := models.RepoPath(username, reponame)
+	//username := os.Getenv(models.EnvRepoUsername)
+	//reponame := os.Getenv(models.EnvRepoName)
+	//repoPath := models.RepoPath(username, reponame)
 
 	buf := bytes.NewBuffer(nil)
 	scanner := bufio.NewScanner(os.Stdin)
@@ -96,10 +102,22 @@ func runHookPreReceive(c *cli.Context) error {
 			continue
 		}
 
-		oldCommitID := string(fields[0])
+		//oldCommitID := string(fields[0])
 		newCommitID := string(fields[1])
 		refFullName := string(fields[2])
 
+		// FIXME: when we add feature to protected branch to deny force push, then uncomment below
+		/*var isForce bool
+		// detect force push
+		if git.EmptySHA != oldCommitID {
+			output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
+			if err != nil {
+				fail("Internal error", "Fail to detect force push: %v", err)
+			} else if len(output) > 0 {
+				isForce = true
+			}
+		}*/
+
 		branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
 		protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
 		if err != nil {
@@ -107,20 +125,13 @@ func runHookPreReceive(c *cli.Context) error {
 		}
 
 		if protectBranch != nil {
-			fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
-		}
-
-		// check and deletion
-		if newCommitID == git.EmptySHA {
-			fail(fmt.Sprintf("Branch '%s' is protected from deletion", branchName), "")
-		}
-
-		// Check force push
-		output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
-		if err != nil {
-			fail("Internal error", "Fail to detect force push: %v", err)
-		} else if len(output) > 0 {
-			fail(fmt.Sprintf("Branch '%s' is protected from force push", branchName), "")
+			// check and deletion
+			if newCommitID == git.EmptySHA {
+				fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
+			} else {
+				fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
+				//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
+			}
 		}
 	}
 
@@ -132,6 +143,12 @@ func runHookUpdate(c *cli.Context) error {
 		return nil
 	}
 
+	if c.IsSet("config") {
+		setting.CustomConf = c.String("config")
+	} else if c.GlobalIsSet("config") {
+		setting.CustomConf = c.GlobalString("config")
+	}
+
 	if err := setup("hooks/update.log"); err != nil {
 		fail("Hook update init failed", fmt.Sprintf("setup: %v", err))
 	}
@@ -144,6 +161,12 @@ func runHookPostReceive(c *cli.Context) error {
 		return nil
 	}
 
+	if c.IsSet("config") {
+		setting.CustomConf = c.String("config")
+	} else if c.GlobalIsSet("config") {
+		setting.CustomConf = c.GlobalString("config")
+	}
+
 	if err := setup("hooks/post-receive.log"); err != nil {
 		fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err))
 	}