mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-08 14:03:24 +09:00
refactor: remove Ctx field from git.Repository (#38500)
This commit is contained in:
@@ -90,7 +90,7 @@ func hookPostReceiveSyncDatabaseBranches(ctx *gitea_context.PrivateContext, opts
|
||||
commitIDs = append(commitIDs, update.NewCommitID)
|
||||
}
|
||||
|
||||
if err = repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, branchNames, commitIDs, gitRepo.GetCommit); err != nil {
|
||||
if err = repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, gitRepo, branchNames, commitIDs); err != nil {
|
||||
ctx.PrivateError(http.StatusInternalServerError, err, "failed to sync branch to DB")
|
||||
return false
|
||||
}
|
||||
@@ -296,10 +296,10 @@ func hookPostReceiveSyncRepoDefaultBranch(ctx *gitea_context.PrivateContext, opt
|
||||
}
|
||||
|
||||
// if default branch doesn't exist, try to guess one from existing git repo
|
||||
_, err = gitRepo.GetBranchCommitID(repo.DefaultBranch)
|
||||
_, err = gitRepo.GetBranchCommitID(ctx, repo.DefaultBranch)
|
||||
if errors.Is(err, util.ErrNotExist) {
|
||||
for _, guessBranchName := range []string{"main", "master"} {
|
||||
if _, err = gitRepo.GetBranchCommitID(guessBranchName); err == nil {
|
||||
if _, err = gitRepo.GetBranchCommitID(ctx, guessBranchName); err == nil {
|
||||
repo.DefaultBranch = guessBranchName
|
||||
err = repo_model.UpdateDefaultBranch(ctx, repo)
|
||||
if err != nil {
|
||||
|
||||
@@ -226,7 +226,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
|
||||
|
||||
// 3. Enforce require signed commits
|
||||
if protectBranch.RequireSignedCommits {
|
||||
err := verifyCommits(oldCommitID, newCommitID, gitRepo, ctx.env)
|
||||
err := verifyCommits(ctx, oldCommitID, newCommitID, gitRepo, ctx.env)
|
||||
if err != nil {
|
||||
if !isErrUnverifiedCommit(err) {
|
||||
log.Error("Unable to check commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
|
||||
@@ -252,7 +252,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
|
||||
|
||||
globs := protectBranch.GetProtectedFilePatterns()
|
||||
if len(globs) > 0 {
|
||||
_, err := pull_service.CheckFileProtection(gitRepo, branchName, oldCommitID, newCommitID, globs, 1, ctx.env)
|
||||
_, err := pull_service.CheckFileProtection(ctx, gitRepo, branchName, oldCommitID, newCommitID, globs, 1, ctx.env)
|
||||
if err != nil {
|
||||
if !pull_service.IsErrFilePathProtected(err) {
|
||||
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
|
||||
@@ -302,7 +302,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
|
||||
// Allow commits that only touch unprotected files
|
||||
globs := protectBranch.GetUnprotectedFilePatterns()
|
||||
if len(globs) > 0 {
|
||||
unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env)
|
||||
unprotectedFilesOnly, err := pull_service.CheckUnprotectedFiles(ctx, gitRepo, branchName, oldCommitID, newCommitID, globs, ctx.env)
|
||||
if err != nil {
|
||||
log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
|
||||
ctx.JSON(http.StatusInternalServerError, private.Response{
|
||||
|
||||
@@ -5,6 +5,7 @@ package private
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"gitea.dev/modules/git"
|
||||
@@ -15,9 +16,9 @@ import (
|
||||
|
||||
// This file contains commit verification functions for refs passed across in hooks
|
||||
|
||||
func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []string) error {
|
||||
func verifyCommits(ctx context.Context, oldCommitID, newCommitID string, repo *git.Repository, env []string) error {
|
||||
var command *gitcmd.Command
|
||||
objectFormat, _ := repo.GetObjectFormat()
|
||||
objectFormat, _ := repo.GetObjectFormat(ctx)
|
||||
if oldCommitID == objectFormat.EmptyObjectID().String() {
|
||||
// When creating a new branch, the oldCommitID is empty, by using "newCommitID --not --all":
|
||||
// List commits that are reachable by following the newCommitID, exclude "all" existing heads/tags commits
|
||||
@@ -32,22 +33,22 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
|
||||
|
||||
err := command.WithEnv(env).
|
||||
WithDir(repo.Path).
|
||||
WithPipelineFunc(func(ctx gitcmd.Context) error {
|
||||
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
|
||||
return ctx.CancelPipeline(err)
|
||||
WithPipelineFunc(func(gitCtx gitcmd.Context) error {
|
||||
err := readAndVerifyCommitsFromShaReader(ctx, stdoutReader, repo, env)
|
||||
return gitCtx.CancelPipeline(err)
|
||||
}).
|
||||
Run(repo.Ctx)
|
||||
Run(ctx)
|
||||
if err != nil && !isErrUnverifiedCommit(err) {
|
||||
log.Error("Unable to check commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func readAndVerifyCommitsFromShaReader(input io.ReadCloser, repo *git.Repository, env []string) error {
|
||||
func readAndVerifyCommitsFromShaReader(ctx context.Context, input io.ReadCloser, repo *git.Repository, env []string) error {
|
||||
scanner := bufio.NewScanner(input)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
err := readAndVerifyCommit(line, repo, env)
|
||||
err := readAndVerifyCommit(ctx, line, repo, env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,7 +56,7 @@ func readAndVerifyCommitsFromShaReader(input io.ReadCloser, repo *git.Repository
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
|
||||
func readAndVerifyCommit(ctx context.Context, sha string, repo *git.Repository, env []string) error {
|
||||
commitID := git.MustIDFromString(sha)
|
||||
cmd := gitcmd.NewCommand("cat-file", "commit").AddDynamicArguments(sha)
|
||||
stdoutReader, stdoutReaderClose := cmd.MakeStdoutPipe()
|
||||
@@ -63,18 +64,18 @@ func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
|
||||
|
||||
return cmd.WithEnv(env).
|
||||
WithDir(repo.Path).
|
||||
WithPipelineFunc(func(ctx gitcmd.Context) error {
|
||||
WithPipelineFunc(func(gitCtx gitcmd.Context) error {
|
||||
commit, err := git.CommitFromReader(commitID, stdoutReader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
verification := asymkey_service.ParseCommitWithSignature(ctx, commit)
|
||||
if !verification.Verified {
|
||||
return ctx.CancelPipeline(&errUnverifiedCommit{commit.ID.String()})
|
||||
return gitCtx.CancelPipeline(&errUnverifiedCommit{commit.ID.String()})
|
||||
}
|
||||
return nil
|
||||
}).
|
||||
Run(repo.Ctx)
|
||||
Run(ctx)
|
||||
}
|
||||
|
||||
type errUnverifiedCommit struct {
|
||||
|
||||
@@ -17,13 +17,13 @@ var testReposDir = "tests/repos/"
|
||||
func TestVerifyCommits(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
gitRepo, err := git.OpenRepository(t.Context(), testReposDir+"repo1_hook_verification")
|
||||
gitRepo, err := git.OpenRepository(testReposDir + "repo1_hook_verification")
|
||||
if err != nil {
|
||||
defer gitRepo.Close()
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
objectFormat, err := gitRepo.GetObjectFormat()
|
||||
objectFormat, err := gitRepo.GetObjectFormat(t.Context())
|
||||
assert.NoError(t, err)
|
||||
|
||||
testCases := []struct {
|
||||
@@ -37,7 +37,7 @@ func TestVerifyCommits(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
err = verifyCommits(tc.base, tc.head, gitRepo, nil)
|
||||
err = verifyCommits(t.Context(), tc.base, tc.head, gitRepo, nil)
|
||||
if tc.verified {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user