fix: Allow direct commits for unprotected files with push restrictions (#37657)

Fixes an issue where users could not commit changes on a file which is
unprotected.

Fixes: #37655
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
Nicolas
2026-05-18 00:49:38 +02:00
committed by GitHub
co-authored by Claude Opus 4.7 Giteabot
parent 0dd8429cc6
commit 9648716f63
2 changed files with 46 additions and 0 deletions
+10
View File
@@ -173,6 +173,16 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
protectedBranch.Repo = targetRepo
canPushWithProtection = protectedBranch.CanUserPush(ctx, doer)
protectionRequireSigned = protectedBranch.RequireSignedCommits
// If branch-wide push is restricted, allow direct commit when the
// URL-derived tree path matches an unprotected file pattern. The
// pre-receive hook re-checks every path the commit actually touches
// (e.g. rename source and destination).
if !canPushWithProtection && ctx.Repo.TreePath != "" && protectedBranch.UnprotectedFilePatterns != "" {
globs := protectedBranch.GetUnprotectedFilePatterns()
if protectedBranch.IsUnprotectedFile(globs, ctx.Repo.TreePath) {
canPushWithProtection = true
}
}
}
targetGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, targetRepo)