This commit is contained in:
wxiaoguang
2026-06-15 14:22:38 +08:00
parent f25811942c
commit 0c8be7bee0
+9 -9
View File
@@ -47,23 +47,24 @@ type preReceiveContext struct {
opts *private.HookOptions opts *private.HookOptions
branchName string // this context should only contain shared variables, mutable variables like "current branch name" shouldn't be put here
} }
// CanWriteCode returns true if pusher can write code // CanWriteCode returns true if pusher can write code
func (ctx *preReceiveContext) CanWriteCode() bool { func (ctx *preReceiveContext) CanWriteCode(ref git.RefName) bool {
if !ctx.loadPusherAndPermission() { if !ctx.loadPusherAndPermission() {
return false return false
} }
// Must not be cached: CanMaintainerWriteToBranch is evaluated against ctx.branchName, which // Must not be cached: CanMaintainerWriteToBranch is evaluated against ctx.branchName, which
// differs for each ref in a batch push. Caching the first result would let a per-branch // differs for each ref in a batch push. Caching the first result would let a per-branch
// maintainer-edit grant on one ref authorize writes to every other ref in the same push. // maintainer-edit grant on one ref authorize writes to every other ref in the same push.
return issues_model.CanMaintainerWriteToBranch(ctx, ctx.userPerm, ctx.branchName, ctx.user) || ctx.deployKeyAccessMode >= perm_model.AccessModeWrite // FIXME: the ref can be a branch, a tag, or something else?
return issues_model.CanMaintainerWriteToBranch(ctx, ctx.userPerm, branchName, ctx.user) || ctx.deployKeyAccessMode >= perm_model.AccessModeWrite
} }
// AssertCanWriteCode returns true if pusher can write code // AssertCanWriteCode returns true if pusher can write code
func (ctx *preReceiveContext) AssertCanWriteCode() bool { func (ctx *preReceiveContext) AssertCanWriteCode(ref git.RefName) bool {
if !ctx.CanWriteCode() { if !ctx.CanWriteCode(ref) {
if ctx.Written() { if ctx.Written() {
return false return false
} }
@@ -125,7 +126,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
case git.DefaultFeatures().SupportProcReceive && refFullName.IsFor(): case git.DefaultFeatures().SupportProcReceive && refFullName.IsFor():
preReceiveFor(ourCtx, refFullName) preReceiveFor(ourCtx, refFullName)
default: default:
ourCtx.AssertCanWriteCode() ourCtx.AssertCanWriteCode(refFullName)
} }
if ctx.Written() { if ctx.Written() {
return return
@@ -137,9 +138,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, refFullName git.RefName) { func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, refFullName git.RefName) {
branchName := refFullName.BranchName() branchName := refFullName.BranchName()
ctx.branchName = branchName
if !ctx.AssertCanWriteCode() { if !ctx.AssertCanWriteCode(refFullName) {
return return
} }
@@ -400,7 +400,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
} }
func preReceiveTag(ctx *preReceiveContext, refFullName git.RefName) { func preReceiveTag(ctx *preReceiveContext, refFullName git.RefName) {
if !ctx.AssertCanWriteCode() { if !ctx.AssertCanWriteCode(refFullName) {
return return
} }