Skip to content

Commit

Permalink
Comment force push detect to fix bug go-gitea#1073 (go-gitea#1077)
Browse files Browse the repository at this point in the history
* umcomment force push detect to fix bug go-gitea#1073

* fix go-gitea#1086

* handle global config set and fix go-gitea#1086
  • Loading branch information
lunny authored Mar 1, 2017
1 parent 9cb08a3 commit cfdc62e
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ 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))
}

// 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)
Expand All @@ -96,31 +102,36 @@ 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 {
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
}

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), "")
}
}
}

Expand All @@ -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))
}
Expand All @@ -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))
}
Expand Down

0 comments on commit cfdc62e

Please sign in to comment.