mirror of
https://github.com/go-gitea/gitea
synced 2024-08-20 06:11:16 +02:00
fix detect force push failure on deletion of protected branches (#5522)
This commit is contained in:
parent
6db7dbd333
commit
af6bce3ad5
24
cmd/hook.go
24
cmd/hook.go
@ -112,10 +112,15 @@ func runHookPreReceive(c *cli.Context) error {
|
|||||||
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
|
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
|
||||||
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
|
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
|
fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if protectBranch != nil && protectBranch.IsProtected() {
|
if protectBranch != nil && protectBranch.IsProtected() {
|
||||||
|
// check and deletion
|
||||||
|
if newCommitID == git.EmptySHA {
|
||||||
|
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
|
||||||
|
}
|
||||||
|
|
||||||
// detect force push
|
// detect force push
|
||||||
if git.EmptySHA != oldCommitID {
|
if git.EmptySHA != oldCommitID {
|
||||||
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
|
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
|
||||||
@ -126,17 +131,12 @@ func runHookPreReceive(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check and deletion
|
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
|
||||||
if newCommitID == git.EmptySHA {
|
canPush, err := private.CanUserPush(protectBranch.ID, userID)
|
||||||
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
|
if err != nil {
|
||||||
} else {
|
fail("Internal error", "Fail to detect user can push: %v", err)
|
||||||
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
|
} else if !canPush {
|
||||||
canPush, err := private.CanUserPush(protectBranch.ID, userID)
|
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
|
||||||
if err != nil {
|
|
||||||
fail("Internal error", "Fail to detect user can push: %v", err)
|
|
||||||
} else if !canPush {
|
|
||||||
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user