Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make git.OpenRepository accept Context #19260

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
log.Trace("Processing next %d repos of %d", len(repos), count)
for _, repo := range repos {
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
log.Warn("OpenRepository: %v", err)
continue
Expand Down
5 changes: 3 additions & 2 deletions integrations/api_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) {
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)

gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, err := git.OpenRepository(context.Background(), repo.RepoPath())
6543 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
defer gitRepo.Close()

Expand Down Expand Up @@ -167,7 +168,7 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)

gitRepo, err := git.OpenRepository(repo.RepoPath())
gitRepo, err := git.OpenRepository(context.Background(), repo.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

Expand Down
5 changes: 3 additions & 2 deletions integrations/api_repo_file_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
stdCtx "context"
"encoding/base64"
"fmt"
"net/http"
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestAPICreateFile(t *testing.T) {
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
resp := session.MakeRequest(t, req, http.StatusCreated)
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath)
var fileResponse api.FileResponse
Expand Down Expand Up @@ -286,7 +287,7 @@ func TestAPICreateFile(t *testing.T) {
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
resp = session.MakeRequest(t, req, http.StatusCreated)
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}).(*repo_model.Repository) // public repo
gitRepo, _ := git.OpenRepository(emptyRepo.RepoPath())
gitRepo, _ := git.OpenRepository(stdCtx.Background(), emptyRepo.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath)
DecodeJSON(t, resp, &fileResponse)
Expand Down
3 changes: 2 additions & 1 deletion integrations/api_repo_file_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
stdCtx "context"
"encoding/base64"
"fmt"
"net/http"
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestAPIUpdateFile(t *testing.T) {
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
req := NewRequestWithJSON(t, "PUT", url, &updateFileOptions)
resp := session.MakeRequest(t, req, http.StatusOK)
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath)
var fileResponse api.FileResponse
Expand Down
3 changes: 2 additions & 1 deletion integrations/api_repo_get_contents_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -75,7 +76,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
err := repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, repo1.DefaultBranch, newBranch)
assert.NoError(t, err)
// Get the commit ID of the default branch
gitRepo, err := git.OpenRepository(repo1.RepoPath())
gitRepo, err := git.OpenRepository(context.Background(), repo1.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

Expand Down
3 changes: 2 additions & 1 deletion integrations/api_repo_get_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -76,7 +77,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
err := repo_service.CreateNewBranch(git.DefaultContext, user2, repo1, repo1.DefaultBranch, newBranch)
assert.NoError(t, err)
// Get the commit ID of the default branch
gitRepo, err := git.OpenRepository(repo1.RepoPath())
gitRepo, err := git.OpenRepository(context.Background(), repo1.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

Expand Down
3 changes: 2 additions & 1 deletion integrations/api_repo_git_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"fmt"
"net/http"
"testing"
Expand All @@ -31,7 +32,7 @@ func TestAPIGitTags(t *testing.T) {
git.NewCommand(git.DefaultContext, "config", "user.name", user.Name).RunInDir(repo.RepoPath())
git.NewCommand(git.DefaultContext, "config", "user.email", user.Email).RunInDir(repo.RepoPath())

gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(context.Background(), repo.RepoPath())
defer gitRepo.Close()

commit, _ := gitRepo.GetBranchCommit("master")
Expand Down
3 changes: 2 additions & 1 deletion integrations/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"encoding/hex"
"fmt"
"math/rand"
Expand Down Expand Up @@ -624,7 +625,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
return
}

gitRepo, err := git.OpenRepository(dstPath)
gitRepo, err := git.OpenRepository(context.Background(), dstPath)
if !assert.NoError(t, err) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion integrations/mirror_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestMirrorPull(t *testing.T) {
mirror, err := repository.MigrateRepositoryGitData(ctx, user, mirrorRepo, opts, nil)
assert.NoError(t, err)

gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepository(context.Background(), repoPath)
assert.NoError(t, err)
defer gitRepo.Close()

Expand Down
4 changes: 2 additions & 2 deletions integrations/mirror_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func testMirrorPush(t *testing.T, u *url.URL) {
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
assert.True(t, ok)

srcGitRepo, err := git.OpenRepository(srcRepo.RepoPath())
srcGitRepo, err := git.OpenRepository(context.Background(), srcRepo.RepoPath())
assert.NoError(t, err)
defer srcGitRepo.Close()

srcCommit, err := srcGitRepo.GetBranchCommit("master")
assert.NoError(t, err)

mirrorGitRepo, err := git.OpenRepository(mirrorRepo.RepoPath())
mirrorGitRepo, err := git.OpenRepository(context.Background(), mirrorRepo.RepoPath())
assert.NoError(t, err)
defer mirrorGitRepo.Close()

Expand Down
5 changes: 3 additions & 2 deletions integrations/pull_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package integrations

import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -238,7 +239,7 @@ func TestCantMergeConflict(t *testing.T) {
BaseBranch: "base",
}).(*models.PullRequest)

gitRepo, err := git.OpenRepository(repo_model.RepoPath(user1.Name, repo1.Name))
gitRepo, err := git.OpenRepository(context.Background(), repo_model.RepoPath(user1.Name, repo1.Name))
assert.NoError(t, err)

err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "CONFLICT")
Expand Down Expand Up @@ -333,7 +334,7 @@ func TestCantMergeUnrelated(t *testing.T) {
session.MakeRequest(t, req, http.StatusCreated)

// Now this PR could be marked conflict - or at least a race may occur - so drop down to pure code at this point...
gitRepo, err := git.OpenRepository(path)
gitRepo, err := git.OpenRepository(context.Background(), path)
assert.NoError(t, err)
pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{
HeadRepoID: repo1.ID,
Expand Down
9 changes: 5 additions & 4 deletions integrations/repofiles_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package integrations

import (
"context"
"net/url"
"path/filepath"
"testing"
Expand Down Expand Up @@ -202,7 +203,7 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {

// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(context.Background(), repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
Expand Down Expand Up @@ -238,7 +239,7 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {

// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(context.Background(), repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
Expand Down Expand Up @@ -273,7 +274,7 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {

// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(context.Background(), repo.RepoPath())
defer gitRepo.Close()

commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
Expand Down Expand Up @@ -323,7 +324,7 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {

// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(context.Background(), repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(repo.DefaultBranch)
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v156.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func fixPublisherIDforTagReleases(x *xorm.Engine) error {
return err
}
}
gitRepo, err = git.OpenRepositoryCtx(git.DefaultContext, repoPath(repo.OwnerName, repo.Name))
gitRepo, err = git.OpenRepository(git.DefaultContext, repoPath(repo.OwnerName, repo.Name))
if err != nil {
log.Error("Error whilst opening git repo for [%d]%s/%s. Error: %v", repo.ID, repo.OwnerName, repo.Name, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion models/migrations/v82.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
userCache[repo.OwnerID] = user
}

gitRepo, err = git.OpenRepositoryCtx(git.DefaultContext, RepoPath(user.Name, repo.Name))
gitRepo, err = git.OpenRepository(git.DefaultContext, RepoPath(user.Name, repo.Name))
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions models/organization/team_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"

"xorm.io/builder"
)

Expand Down
1 change: 1 addition & 0 deletions models/organization/team_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"

"xorm.io/builder"
)

Expand Down
4 changes: 2 additions & 2 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func ReferencesGitRepo(allowEmpty bool) func(ctx *APIContext) (cancel context.Ca
// For API calls.
if ctx.Repo.GitRepo == nil {
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
gitRepo, err := git.OpenRepository(ctx, repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err)
return
Expand Down Expand Up @@ -388,7 +388,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {

if ctx.Repo.GitRepo == nil {
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath)
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, repoPath)
if err != nil {
ctx.InternalServerError(err)
return
Expand Down
4 changes: 2 additions & 2 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
return
}

gitRepo, err := git.OpenRepositoryCtx(ctx, repo_model.RepoPath(userName, repoName))
gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(userName, repoName))
if err != nil {
if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") {
log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err)
Expand Down Expand Up @@ -846,7 +846,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context

if ctx.Repo.GitRepo == nil {
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, repoPath)
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, repoPath)
if err != nil {
ctx.ServerError("RepoRef Invalid repo "+repoPath, err)
return
Expand Down
8 changes: 4 additions & 4 deletions modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_mo
},
}

gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
Expand All @@ -111,7 +111,7 @@ func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_mo
}

if pr.Flow == models.PullRequestFlowAGit {
gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err)
return nil
Expand All @@ -138,7 +138,7 @@ func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_mo
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, p.AccessMode)

headGitRepo, err := git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err)
return nil
Expand Down Expand Up @@ -174,7 +174,7 @@ func ToAPIPullRequest(ctx context.Context, pr *models.PullRequest, doer *user_mo
}

if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 {
baseGitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
Expand Down
2 changes: 1 addition & 1 deletion modules/doctor/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool

if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
numRepos++
r, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath())
r, err := git.OpenRepository(git.DefaultContext, repo.RepoPath())
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions modules/git/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestBlob_Data(t *testing.T) {
output := "file2\n"
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path)
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
if !assert.NoError(t, err) {
t.Fatal()
}
Expand All @@ -39,7 +39,7 @@ func TestBlob_Data(t *testing.T) {

func Benchmark_Blob_Data(b *testing.B) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path)
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
if err != nil {
b.Fatal(err)
}
Expand Down
Loading