Skip to content

Commit af6bce3

Browse files
lunnylafriks
authored andcommitted
fix detect force push failure on deletion of protected branches (#5522)
1 parent 6db7dbd commit af6bce3

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

cmd/hook.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,15 @@ func runHookPreReceive(c *cli.Context) error {
112112
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
113113
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
114114
if err != nil {
115-
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
115+
fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
116116
}
117117

118118
if protectBranch != nil && protectBranch.IsProtected() {
119+
// check and deletion
120+
if newCommitID == git.EmptySHA {
121+
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
122+
}
123+
119124
// detect force push
120125
if git.EmptySHA != oldCommitID {
121126
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
@@ -126,17 +131,12 @@ func runHookPreReceive(c *cli.Context) error {
126131
}
127132
}
128133

129-
// check and deletion
130-
if newCommitID == git.EmptySHA {
131-
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
132-
} else {
133-
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
134-
canPush, err := private.CanUserPush(protectBranch.ID, userID)
135-
if err != nil {
136-
fail("Internal error", "Fail to detect user can push: %v", err)
137-
} else if !canPush {
138-
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
139-
}
134+
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
135+
canPush, err := private.CanUserPush(protectBranch.ID, userID)
136+
if err != nil {
137+
fail("Internal error", "Fail to detect user can push: %v", err)
138+
} else if !canPush {
139+
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
140140
}
141141
}
142142
}

0 commit comments

Comments
 (0)