Skip to content

Commit 1a8fa24

Browse files
committed
prevent delete pull request head ref
fix go-gitea#18174 Signed-off-by: a1012112796 <[email protected]>
1 parent 832f987 commit 1a8fa24

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

modules/git/repo_branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const BranchPrefix = "refs/heads/"
1616

1717
// AGit Flow
1818

19-
// PullRequestPrefix sepcial ref to create a pull request: refs/for/<targe-branch>/<topic-branch>
19+
// AgitPullPrefix sepcial ref to create a pull request: refs/for/<targe-branch>/<topic-branch>
2020
// or refs/for/<targe-branch> -o topic='<topic-branch>'
21-
const PullRequestPrefix = "refs/for/"
21+
const AgitPullPrefix = "refs/for/"
2222

2323
// TODO: /refs/for-review for suggest change interface
2424

routers/private/hook_pre_receive.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
124124
preReceiveBranch(ourCtx, oldCommitID, newCommitID, refFullName)
125125
case strings.HasPrefix(refFullName, git.TagPrefix):
126126
preReceiveTag(ourCtx, oldCommitID, newCommitID, refFullName)
127-
case git.SupportProcReceive && strings.HasPrefix(refFullName, git.PullRequestPrefix):
127+
case git.SupportProcReceive && strings.HasPrefix(refFullName, git.AgitPullPrefix):
128128
preReceivePullRequest(ourCtx, oldCommitID, newCommitID, refFullName)
129+
case strings.HasPrefix(refFullName, git.PullPrefix) && newCommitID == git.EmptySHA:
130+
ctx.JSON(http.StatusForbidden, private.Response{
131+
Err: "delete head ref of pull request is not allowed",
132+
})
133+
return
129134
default:
130135
ourCtx.AssertCanWriteCode()
131136
}
@@ -409,7 +414,7 @@ func preReceivePullRequest(ctx *preReceiveContext, oldCommitID, newCommitID, ref
409414
return
410415
}
411416

412-
baseBranchName := refFullName[len(git.PullRequestPrefix):]
417+
baseBranchName := refFullName[len(git.AgitPullPrefix):]
413418

414419
baseBranchExist := false
415420
if ctx.Repo.GitRepo.IsBranchExist(baseBranchName) {

services/agit/agit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
5050
continue
5151
}
5252

53-
if !strings.HasPrefix(opts.RefFullNames[i], git.PullRequestPrefix) {
53+
if !strings.HasPrefix(opts.RefFullNames[i], git.AgitPullPrefix) {
5454
results = append(results, private.HookProcReceiveRefResult{
5555
IsNotMatched: true,
5656
OriginalRef: opts.RefFullNames[i],
5757
})
5858
continue
5959
}
6060

61-
baseBranchName := opts.RefFullNames[i][len(git.PullRequestPrefix):]
61+
baseBranchName := opts.RefFullNames[i][len(git.AgitPullPrefix):]
6262
curentTopicBranch := ""
6363
if !gitRepo.IsBranchExist(baseBranchName) {
6464
// try match refs/for/<target-branch>/<topic-branch>

0 commit comments

Comments
 (0)