loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Do not allow deletion of internal references' (#2834) from gusted/forgejo-internal-reference into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2834
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>

+41 -2
+25 -2
cmd/hook.go
··· 293 293 return nil 294 294 } 295 295 296 + // runHookUpdate process the update hook: https://git-scm.com/docs/githooks#update 296 297 func runHookUpdate(c *cli.Context) error { 297 - // Update is empty and is kept only for backwards compatibility 298 - return nil 298 + // Now if we're an internal don't do anything else 299 + if isInternal, _ := strconv.ParseBool(os.Getenv(repo_module.EnvIsInternal)); isInternal { 300 + return nil 301 + } 302 + 303 + ctx, cancel := installSignals() 304 + defer cancel() 305 + 306 + // The last three arguments given to the hook are in order: reference name, old commit ID and new commit ID. 307 + args := os.Args[len(os.Args)-3:] 308 + refFullName := git.RefName(args[0]) 309 + newCommitID := args[2] 310 + 311 + // Only process pull references. 312 + if !refFullName.IsPull() { 313 + return nil 314 + } 315 + 316 + // Deletion of the ref means that the new commit ID is only composed of '0'. 317 + if strings.ContainsFunc(newCommitID, func(e rune) bool { return e != '0' }) { 318 + return nil 319 + } 320 + 321 + return fail(ctx, fmt.Sprintf("The deletion of %s is skipped as it's an internal reference.", refFullName), "") 299 322 } 300 323 301 324 func runHookPostReceive(c *cli.Context) error {
+16
tests/integration/git_test.go
··· 84 84 mediaTest(t, &httpContext, little, big, littleLFS, bigLFS) 85 85 86 86 t.Run("CreateAgitFlowPull", doCreateAgitFlowPull(dstPath, &httpContext, "master", "test/head")) 87 + t.Run("InternalReferences", doInternalReferences(&httpContext, dstPath)) 87 88 t.Run("BranchProtectMerge", doBranchProtectPRMerge(&httpContext, dstPath)) 88 89 t.Run("AutoMerge", doAutoPRMerge(&httpContext, dstPath)) 89 90 t.Run("CreatePRAndSetManuallyMerged", doCreatePRAndSetManuallyMerged(httpContext, httpContext, dstPath, "master", "test-manually-merge")) ··· 125 126 mediaTest(t, &sshContext, little, big, littleLFS, bigLFS) 126 127 127 128 t.Run("CreateAgitFlowPull", doCreateAgitFlowPull(dstPath, &sshContext, "master", "test/head2")) 129 + t.Run("InternalReferences", doInternalReferences(&sshContext, dstPath)) 128 130 t.Run("BranchProtectMerge", doBranchProtectPRMerge(&sshContext, dstPath)) 129 131 t.Run("MergeFork", func(t *testing.T) { 130 132 defer tests.PrintCurrentTest(t)() ··· 731 733 pr, err = doAPIGetPullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index)(t) 732 734 assert.NoError(t, err) 733 735 assert.True(t, pr.HasMerged) 736 + } 737 + } 738 + 739 + func doInternalReferences(ctx *APITestContext, dstPath string) func(t *testing.T) { 740 + return func(t *testing.T) { 741 + defer tests.PrintCurrentTest(t)() 742 + 743 + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: ctx.Username, Name: ctx.Reponame}) 744 + pr1 := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{HeadRepoID: repo.ID}) 745 + 746 + _, stdErr, gitErr := git.NewCommand(git.DefaultContext, "push", "origin").AddDynamicArguments(fmt.Sprintf(":refs/pull/%d/head", pr1.Index)).RunStdString(&git.RunOpts{Dir: dstPath}) 747 + assert.Error(t, gitErr) 748 + assert.Contains(t, stdErr, fmt.Sprintf("remote: Forgejo: The deletion of refs/pull/%d/head is skipped as it's an internal reference.", pr1.Index)) 749 + assert.Contains(t, stdErr, fmt.Sprintf("[remote rejected] refs/pull/%d/head (hook declined)", pr1.Index)) 734 750 } 735 751 } 736 752