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
This commit is contained in:
Lunny Xiao 2017-03-01 23:01:03 +08:00 committed by GitHub
parent 9cb08a3cf5
commit cfdc62e7fa

View file

@ -69,6 +69,12 @@ func runHookPreReceive(c *cli.Context) error {
return nil 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 { if err := setup("hooks/pre-receive.log"); err != nil {
fail("Hook pre-receive init failed", fmt.Sprintf("setup: %v", err)) 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 // the environment setted on serv command
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64) repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true") isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
username := os.Getenv(models.EnvRepoUsername) //username := os.Getenv(models.EnvRepoUsername)
reponame := os.Getenv(models.EnvRepoName) //reponame := os.Getenv(models.EnvRepoName)
repoPath := models.RepoPath(username, reponame) //repoPath := models.RepoPath(username, reponame)
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
@ -96,10 +102,22 @@ func runHookPreReceive(c *cli.Context) error {
continue continue
} }
oldCommitID := string(fields[0]) //oldCommitID := string(fields[0])
newCommitID := string(fields[1]) newCommitID := string(fields[1])
refFullName := string(fields[2]) 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) branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName) protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
if err != nil { if err != nil {
@ -107,20 +125,13 @@ func runHookPreReceive(c *cli.Context) error {
} }
if protectBranch != nil { if protectBranch != nil {
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
}
// check and deletion // check and deletion
if newCommitID == git.EmptySHA { if newCommitID == git.EmptySHA {
fail(fmt.Sprintf("Branch '%s' is protected from deletion", branchName), "") 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), "")
} }
// 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), "")
} }
} }
@ -132,6 +143,12 @@ func runHookUpdate(c *cli.Context) error {
return nil 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 { if err := setup("hooks/update.log"); err != nil {
fail("Hook update init failed", fmt.Sprintf("setup: %v", err)) fail("Hook update init failed", fmt.Sprintf("setup: %v", err))
} }
@ -144,6 +161,12 @@ func runHookPostReceive(c *cli.Context) error {
return nil 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 { if err := setup("hooks/post-receive.log"); err != nil {
fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err)) fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err))
} }