1
0
mirror of https://github.com/go-gitea/gitea synced 2024-12-24 03:35:55 +01:00

Make git.OpenRepository accept Context (#19260)

* OpenRepositoryCtx -> OpenRepository
* OpenRepository -> openRepositoryWithDefaultContext, only for internal usage
This commit is contained in:
6543 2022-03-29 21:13:41 +02:00 committed by GitHub
parent 889a8c268c
commit 3e88af898a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
89 changed files with 176 additions and 170 deletions

@ -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

@ -105,7 +105,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(git.DefaultContext, repo.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()
@ -167,7 +167,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(git.DefaultContext, repo.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

@ -5,6 +5,7 @@
package integrations
import (
stdCtx "context"
"encoding/base64"
"fmt"
"net/http"
@ -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
@ -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)

@ -5,6 +5,7 @@
package integrations
import (
stdCtx "context"
"encoding/base64"
"fmt"
"net/http"
@ -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

@ -75,7 +75,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(git.DefaultContext, repo1.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

@ -76,7 +76,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(git.DefaultContext, repo1.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()

@ -31,7 +31,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(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit("master")

@ -624,7 +624,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
return
}
gitRepo, err := git.OpenRepository(dstPath)
gitRepo, err := git.OpenRepository(git.DefaultContext, dstPath)
if !assert.NoError(t, err) {
return
}

@ -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(git.DefaultContext, repoPath)
assert.NoError(t, err)
defer gitRepo.Close()

@ -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(git.DefaultContext, 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(git.DefaultContext, mirrorRepo.RepoPath())
assert.NoError(t, err)
defer mirrorGitRepo.Close()

@ -238,7 +238,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(git.DefaultContext, repo_model.RepoPath(user1.Name, repo1.Name))
assert.NoError(t, err)
err = pull.Merge(git.DefaultContext, pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "CONFLICT")
@ -333,7 +333,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(git.DefaultContext, path)
assert.NoError(t, err)
pr := unittest.AssertExistsAndLoadBean(t, &models.PullRequest{
HeadRepoID: repo1.ID,

@ -90,7 +90,7 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.Pul
assert.NoError(t, err)
assert.NotEmpty(t, baseRepo)
headRepo, err := repo_service.ForkRepository(actor, forkOrg, repo_service.ForkRepoOptions{
headRepo, err := repo_service.ForkRepository(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{
BaseRepo: baseRepo,
Name: "repo-pr-update",
Description: "desc",

@ -202,7 +202,7 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
@ -238,7 +238,7 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
@ -273,7 +273,7 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {
// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
@ -323,7 +323,7 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
// asserts
assert.NoError(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commitID, _ := gitRepo.GetBranchCommitID(repo.DefaultBranch)

@ -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

@ -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
}

@ -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"
)

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"xorm.io/builder"
)

@ -247,8 +247,8 @@ func CanUserForkRepo(user *user_model.User, repo *repo_model.Repository) (bool,
}
// FindUserOrgForks returns the forked repositories for one user from a repository
func FindUserOrgForks(repoID, userID int64) ([]*repo_model.Repository, error) {
var cond builder.Cond = builder.And(
func FindUserOrgForks(ctx context.Context, repoID, userID int64) ([]*repo_model.Repository, error) {
cond := builder.And(
builder.Eq{"fork_id": repoID},
builder.In("owner_id",
builder.Select("org_id").
@ -258,23 +258,23 @@ func FindUserOrgForks(repoID, userID int64) ([]*repo_model.Repository, error) {
)
var repos []*repo_model.Repository
return repos, db.GetEngine(db.DefaultContext).Table("repository").Where(cond).Find(&repos)
return repos, db.GetEngine(ctx).Table("repository").Where(cond).Find(&repos)
}
// GetForksByUserAndOrgs return forked repos of the user and owned orgs
func GetForksByUserAndOrgs(user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) {
func GetForksByUserAndOrgs(ctx context.Context, user *user_model.User, repo *repo_model.Repository) ([]*repo_model.Repository, error) {
var repoList []*repo_model.Repository
if user == nil {
return repoList, nil
}
forkedRepo, err := repo_model.GetUserFork(repo.ID, user.ID)
forkedRepo, err := repo_model.GetUserFork(ctx, repo.ID, user.ID)
if err != nil {
return repoList, err
}
if forkedRepo != nil {
repoList = append(repoList, forkedRepo)
}
orgForks, err := FindUserOrgForks(repo.ID, user.ID)
orgForks, err := FindUserOrgForks(ctx, repo.ID, user.ID)
if err != nil {
return nil, err
}

@ -44,9 +44,9 @@ func HasForkedRepo(ownerID, repoID int64) bool {
}
// GetUserFork return user forked repository from this repository, if not forked return nil
func GetUserFork(repoID, userID int64) (*Repository, error) {
func GetUserFork(ctx context.Context, repoID, userID int64) (*Repository, error) {
var forkedRepo Repository
has, err := db.GetEngine(db.DefaultContext).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo)
has, err := db.GetEngine(ctx).Where("fork_id = ?", repoID).And("owner_id = ?", userID).Get(&forkedRepo)
if err != nil {
return nil, err
}

@ -7,6 +7,7 @@ package repo
import (
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
@ -19,14 +20,14 @@ func TestGetUserFork(t *testing.T) {
repo, err := GetRepositoryByID(10)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = GetUserFork(repo.ID, 13)
repo, err = GetUserFork(db.DefaultContext, repo.ID, 13)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = GetRepositoryByID(9)
assert.NoError(t, err)
assert.NotNil(t, repo)
repo, err = GetUserFork(repo.ID, 13)
repo, err = GetUserFork(db.DefaultContext, repo.ID, 13)
assert.NoError(t, err)
assert.Nil(t, repo)
}

@ -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
@ -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

@ -529,7 +529,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
}
ctx.Data["CanSignedUserFork"] = canSignedUserFork
userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx.Doer, ctx.Repo.Repository)
userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx, ctx.Doer, ctx.Repo.Repository)
if err != nil {
ctx.ServerError("GetForksByUserAndOrgs", err)
return
@ -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)
@ -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

@ -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
@ -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
@ -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
@ -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

@ -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(ctx, repo.RepoPath())
if err != nil {
return err
}

@ -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()
}
@ -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)
}

@ -112,7 +112,7 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
func TestEntries_GetCommitsInfo(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -123,7 +123,7 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
assert.NoError(t, err)
}
defer util.RemoveAll(clonedPath)
clonedRepo1, err := OpenRepository(clonedPath)
clonedRepo1, err := openRepositoryWithDefaultContext(clonedPath)
if err != nil {
assert.NoError(t, err)
}
@ -156,7 +156,7 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
}
defer util.RemoveAll(repoPath)
if repo, err = OpenRepository(repoPath); err != nil {
if repo, err = openRepositoryWithDefaultContext(repoPath); err != nil {
b.Fatal(err)
}
defer repo.Close()

@ -64,7 +64,7 @@ gpgsig -----BEGIN PGP SIGNATURE-----
empty commit`
sha := SHA1{0xfe, 0xaf, 0x4b, 0xa6, 0xbc, 0x63, 0x5f, 0xec, 0x44, 0x2f, 0x46, 0xdd, 0xd4, 0x51, 0x24, 0x16, 0xec, 0x43, 0xc2, 0xc2}
gitRepo, err := OpenRepository(filepath.Join(testReposDir, "repo1_bare"))
gitRepo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare"))
assert.NoError(t, err)
assert.NotNil(t, gitRepo)
@ -109,7 +109,7 @@ empty commit`, commitFromReader.Signature.Payload)
func TestHasPreviousCommit(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path)
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
commit, err := repo.GetCommit("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")

@ -51,7 +51,7 @@ func GetReverseRawDiff(ctx context.Context, repoPath, commitID string, writer io
func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath)
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
return fmt.Errorf("RepositoryFromContextOrOpen: %v", err)
}
defer closer.Close()

@ -14,7 +14,7 @@ import (
func TestGetNotes(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -27,7 +27,7 @@ func TestGetNotes(t *testing.T) {
func TestGetNestedNotes(t *testing.T) {
repoPath := filepath.Join(testReposDir, "repo3_notes")
repo, err := OpenRepository(repoPath)
repo, err := openRepositoryWithDefaultContext(repoPath)
assert.NoError(t, err)
defer repo.Close()
@ -42,7 +42,7 @@ func TestGetNestedNotes(t *testing.T) {
func TestGetNonExistentNotes(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()

@ -44,6 +44,6 @@ func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository,
return gitRepo, nopCloser(nil), nil
}
gitRepo, err := OpenRepositoryCtx(ctx, path)
gitRepo, err := OpenRepository(ctx, path)
return gitRepo, gitRepo, err
}

@ -35,13 +35,13 @@ type Repository struct {
Ctx context.Context
}
// OpenRepository opens the repository at the given path.
func OpenRepository(repoPath string) (*Repository, error) {
return OpenRepositoryCtx(DefaultContext, repoPath)
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
return OpenRepository(DefaultContext, repoPath)
}
// OpenRepositoryCtx opens the repository at the given path within the context.Context
func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error) {
// OpenRepository opens the repository at the given path within the context.Context
func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err

@ -36,13 +36,13 @@ type Repository struct {
Ctx context.Context
}
// OpenRepository opens the repository at the given path.
func OpenRepository(repoPath string) (*Repository, error) {
return OpenRepositoryCtx(DefaultContext, repoPath)
// openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
return OpenRepository(DefaultContext, repoPath)
}
// OpenRepositoryCtx opens the repository at the given path with the provided context.
func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error) {
// OpenRepository opens the repository at the given path with the provided context.
func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
repoPath, err := filepath.Abs(repoPath)
if err != nil {
return nil, err

@ -15,7 +15,7 @@ import (
func TestRepository_GetBlob_Found(t *testing.T) {
repoPath := filepath.Join(testReposDir, "repo1_bare")
r, err := OpenRepository(repoPath)
r, err := openRepositoryWithDefaultContext(repoPath)
assert.NoError(t, err)
defer r.Close()
@ -43,7 +43,7 @@ func TestRepository_GetBlob_Found(t *testing.T) {
func TestRepository_GetBlob_NotExist(t *testing.T) {
repoPath := filepath.Join(testReposDir, "repo1_bare")
r, err := OpenRepository(repoPath)
r, err := openRepositoryWithDefaultContext(repoPath)
assert.NoError(t, err)
defer r.Close()
@ -57,7 +57,7 @@ func TestRepository_GetBlob_NotExist(t *testing.T) {
func TestRepository_GetBlob_NoId(t *testing.T) {
repoPath := filepath.Join(testReposDir, "repo1_bare")
r, err := OpenRepository(repoPath)
r, err := openRepositoryWithDefaultContext(repoPath)
assert.NoError(t, err)
defer r.Close()

@ -89,7 +89,7 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) {
// GetBranchesByPath returns a branch by it's path
// if limit = 0 it will not limit
func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) {
gitRepo, err := OpenRepositoryCtx(ctx, path)
gitRepo, err := OpenRepository(ctx, path)
if err != nil {
return nil, 0, err
}

@ -88,7 +88,7 @@ func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refn
repo := RepositoryFromContext(ctx, repoPath)
if repo == nil {
var err error
repo, err = OpenRepositoryCtx(ctx, repoPath)
repo, err = OpenRepository(ctx, repoPath)
if err != nil {
return 0, err
}

@ -13,7 +13,7 @@ import (
func TestRepository_GetBranches(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -41,7 +41,7 @@ func TestRepository_GetBranches(t *testing.T) {
func BenchmarkRepository_GetBranches(b *testing.B) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
if err != nil {
b.Fatal(err)
}

@ -13,7 +13,7 @@ import (
func TestRepository_GetCommitBranches(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -40,7 +40,7 @@ func TestRepository_GetCommitBranches(t *testing.T) {
func TestGetTagCommitWithSignature(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -54,7 +54,7 @@ func TestGetTagCommitWithSignature(t *testing.T) {
func TestGetCommitWithBadCommitID(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -66,7 +66,7 @@ func TestGetCommitWithBadCommitID(t *testing.T) {
func TestIsCommitInBranch(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -81,7 +81,7 @@ func TestIsCommitInBranch(t *testing.T) {
func TestRepository_CommitsBetweenIDs(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()

@ -24,7 +24,7 @@ func TestGetFormatPatch(t *testing.T) {
}
defer util.RemoveAll(clonedPath)
repo, err := OpenRepository(clonedPath)
repo, err := openRepositoryWithDefaultContext(clonedPath)
if err != nil {
assert.NoError(t, err)
return
@ -52,7 +52,7 @@ func TestGetFormatPatch(t *testing.T) {
func TestReadPatch(t *testing.T) {
// Ensure we can read the patch files
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path)
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
if err != nil {
assert.NoError(t, err)
return
@ -91,7 +91,7 @@ func TestReadWritePullHead(t *testing.T) {
}
defer util.RemoveAll(clonedPath)
repo, err := OpenRepository(clonedPath)
repo, err := openRepositoryWithDefaultContext(clonedPath)
if err != nil {
assert.NoError(t, err)
return

@ -16,7 +16,7 @@ import (
func TestRepository_GetLanguageStats(t *testing.T) {
repoPath := filepath.Join(testReposDir, "language_stats_repo")
gitRepo, err := OpenRepository(repoPath)
gitRepo, err := openRepositoryWithDefaultContext(repoPath)
if !assert.NoError(t, err) {
t.Fatal()
}

@ -13,7 +13,7 @@ import (
func TestRepository_GetRefs(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()
@ -37,7 +37,7 @@ func TestRepository_GetRefs(t *testing.T) {
func TestRepository_GetRefsFiltered(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()

@ -14,7 +14,7 @@ import (
func TestRepository_GetCodeActivityStats(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
assert.NoError(t, err)
defer bareRepo1.Close()

@ -15,7 +15,7 @@ import (
func TestRepository_GetTags(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path)
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
if err != nil {
assert.NoError(t, err)
return
@ -44,7 +44,7 @@ func TestRepository_GetTag(t *testing.T) {
}
defer util.RemoveAll(clonedPath)
bareRepo1, err := OpenRepository(clonedPath)
bareRepo1, err := openRepositoryWithDefaultContext(clonedPath)
if err != nil {
assert.NoError(t, err)
return
@ -149,7 +149,7 @@ func TestRepository_GetAnnotatedTag(t *testing.T) {
}
defer util.RemoveAll(clonedPath)
bareRepo1, err := OpenRepository(clonedPath)
bareRepo1, err := openRepositoryWithDefaultContext(clonedPath)
if err != nil {
assert.NoError(t, err)
return

@ -23,7 +23,7 @@ func TestGetLatestCommitTime(t *testing.T) {
func TestRepoIsEmpty(t *testing.T) {
emptyRepo2Path := filepath.Join(testReposDir, "repo2_empty")
repo, err := OpenRepository(emptyRepo2Path)
repo, err := openRepositoryWithDefaultContext(emptyRepo2Path)
assert.NoError(t, err)
defer repo.Close()
isEmpty, err := repo.IsEmpty()

@ -57,7 +57,7 @@ func TestEntriesCustomSort(t *testing.T) {
}
func TestFollowLink(t *testing.T) {
r, err := OpenRepository("tests/repos/repo1_bare")
r, err := openRepositoryWithDefaultContext("tests/repos/repo1_bare")
assert.NoError(t, err)
defer r.Close()

@ -14,7 +14,7 @@ import (
)
func BenchmarkGetCommitGraph(b *testing.B) {
currentRepo, err := git.OpenRepository(".")
currentRepo, err := git.OpenRepository(git.DefaultContext, ".")
if err != nil || currentRepo == nil {
b.Error("Could not open repository")
}

@ -35,7 +35,7 @@ func (db *DBIndexer) Index(id int64) error {
return err
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
if err.Error() == "no such file or directory" {
return nil

@ -1079,7 +1079,7 @@ func sha1CurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
if !inCache {
if ctx.GitRepo == nil {
var err error
ctx.GitRepo, err = git.OpenRepositoryCtx(ctx.Ctx, ctx.Metas["repoPath"])
ctx.GitRepo, err = git.OpenRepository(ctx.Ctx, ctx.Metas["repoPath"])
if err != nil {
log.Error("unable to open repository: %s Error: %v", ctx.Metas["repoPath"], err)
return

@ -219,7 +219,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
repo.DefaultBranch = templateRepo.DefaultBranch
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}

@ -430,7 +430,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}

@ -126,7 +126,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
return repo, fmt.Errorf("error in MigrateRepositoryGitData(git update-server-info): %v", err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
gitRepo, err := git.OpenRepository(ctx, repoPath)
if err != nil {
return repo, fmt.Errorf("OpenRepository: %v", err)
}

@ -66,7 +66,7 @@ func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
// LoadRepoCommit loads a repo's commit into a test context.
func LoadRepoCommit(t *testing.T, ctx *context.Context) {
gitRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
gitRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := gitRepo.GetHEADBranch()
@ -88,7 +88,7 @@ func LoadUser(t *testing.T, ctx *context.Context, userID int64) {
func LoadGitRepo(t *testing.T, ctx *context.Context) {
assert.NoError(t, ctx.Repo.Repository.GetOwner(ctx))
var err error
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
assert.NoError(t, err)
}

@ -122,7 +122,7 @@ func GetArchive(ctx *context.APIContext) {
repoPath := repo_model.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
if ctx.Repo.GitRepo == nil {
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
gitRepo, err := git.OpenRepository(ctx, repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return

@ -136,7 +136,7 @@ func CreateFork(ctx *context.APIContext) {
name = *form.Name
}
fork, err := repo_service.ForkRepository(ctx.Doer, forker, repo_service.ForkRepoOptions{
fork, err := repo_service.ForkRepository(ctx, ctx.Doer, forker, repo_service.ForkRepoOptions{
BaseRepo: repo,
Name: name,
Description: repo.Description,

@ -55,7 +55,7 @@ func GetNote(ctx *context.APIContext) {
}
func getNote(ctx *context.APIContext, identifier string) {
gitRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
gitRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return

@ -887,7 +887,7 @@ func MergePullRequest(ctx *context.APIContext) {
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
headRepo = ctx.Repo.GitRepo
} else {
headRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
headRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
return
@ -981,7 +981,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
headRepo = ctx.Repo.Repository
headGitRepo = ctx.Repo.GitRepo
} else {
headGitRepo, err = git.OpenRepositoryCtx(ctx, repo_model.RepoPath(headUser.Name, headRepo.Name))
headGitRepo, err = git.OpenRepository(ctx, repo_model.RepoPath(headUser.Name, headRepo.Name))
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return nil, nil, nil, nil, "", ""

@ -711,7 +711,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
if ctx.Repo.GitRepo == nil && !repo.IsEmpty {
var err error
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
if err != nil {
ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err)
return err

@ -458,7 +458,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
// findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error.
// The caller is responsible for closing the returned repo again
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
if err != nil {
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {

@ -36,7 +36,7 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
if ctx.Repo.GitRepo == nil {
var err error
ctx.Repo.GitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.RepoPath())
ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath())
if err != nil {
return nil, "OpenRepository", err
}

@ -43,7 +43,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
return nil
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{

@ -290,7 +290,7 @@ func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, p
if pr.HasMerged {
baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
if !ok {
baseGitRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil

@ -265,7 +265,7 @@ func Diff(ctx *context.Context) {
)
if ctx.Data["PageIsWiki"] != nil {
gitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
if err != nil {
ctx.ServerError("Repo.GitRepo.GetCommit", err)
return

@ -389,7 +389,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
ci.HeadRepo = ctx.Repo.Repository
ci.HeadGitRepo = ctx.Repo.GitRepo
} else if has {
ci.HeadGitRepo, err = git.OpenRepositoryCtx(ctx, ci.HeadRepo.RepoPath())
ci.HeadGitRepo, err = git.OpenRepository(ctx, ci.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil
@ -661,7 +661,7 @@ func PrepareCompareDiff(
}
func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) {
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return nil, nil, err
}
@ -795,7 +795,7 @@ func ExcerptBlob(ctx *context.Context) {
gitRepo := ctx.Repo.GitRepo
if ctx.FormBool("wiki") {
var err error
gitRepo, err = git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return

@ -67,7 +67,7 @@ func TestGetClosestParentWithFiles(t *testing.T) {
repo := ctx.Repo.Repository
branch := repo.DefaultBranch
gitRepo, _ := git.OpenRepository(repo.RepoPath())
gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch)
expectedTreePath := ""

@ -124,7 +124,7 @@ func LFSLocks(ctx *context.Context) {
return
}
gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err := git.OpenRepository(ctx, tmpBasePath)
if err != nil {
log.Error("Unable to open temporary repository: %s (%v)", tmpBasePath, err)
ctx.ServerError("LFSLocks", fmt.Errorf("failed to open new temporary repository in: %s %v", tmpBasePath, err))

@ -227,7 +227,7 @@ func ForkPost(ctx *context.Context) {
}
}
repo, err := repo_service.ForkRepository(ctx.Doer, ctxUser, repo_service.ForkRepoOptions{
repo, err := repo_service.ForkRepository(ctx, ctx.Doer, ctxUser, repo_service.ForkRepoOptions{
BaseRepo: forkRepo,
Name: form.RepoName,
Description: form.Description,
@ -418,7 +418,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
if pull.BaseRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
baseGitRepo = ctx.Repo.GitRepo
} else {
baseGitRepo, err := git.OpenRepositoryCtx(ctx, pull.BaseRepo.RepoPath())
baseGitRepo, err := git.OpenRepository(ctx, pull.BaseRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil
@ -470,7 +470,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
var headBranchSha string
// HeadRepo may be missing
if pull.HeadRepo != nil {
headGitRepo, err := git.OpenRepositoryCtx(ctx, pull.HeadRepo.RepoPath())
headGitRepo, err := git.OpenRepository(ctx, pull.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil
@ -1070,7 +1070,7 @@ func MergePullRequest(ctx *context.Context) {
if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil {
headRepo = ctx.Repo.GitRepo
} else {
headRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
headRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
return
@ -1280,7 +1280,7 @@ func CleanUpPullRequest(ctx *context.Context) {
gitBaseRepo = ctx.Repo.GitRepo
} else {
// If not just open it
gitBaseRepo, err = git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
gitBaseRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err)
return
@ -1295,7 +1295,7 @@ func CleanUpPullRequest(ctx *context.Context) {
gitRepo = ctx.Repo.GitRepo
} else if pr.BaseRepoID != pr.HeadRepoID {
// Otherwise just load it up
gitRepo, err = git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
gitRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err)
return

@ -90,7 +90,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
}
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
wikiRepo, err := git.OpenRepositoryCtx(ctx, ctx.Repo.Repository.WikiPath())
wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil, nil, err

@ -26,7 +26,7 @@ const (
)
func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName string) *git.TreeEntry {
wikiRepo, err := git.OpenRepository(repo.WikiPath())
wikiRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
defer wikiRepo.Close()
commit, err := wikiRepo.GetBranchCommit("master")

@ -195,7 +195,7 @@ Loop:
return false, "", nil, &ErrWontSign{twofa}
}
case parentSigned:
gitRepo, err := git.OpenRepositoryCtx(ctx, repoWikiPath)
gitRepo, err := git.OpenRepository(ctx, repoWikiPath)
if err != nil {
return false, "", nil, err
}
@ -248,7 +248,7 @@ Loop:
return false, "", nil, &ErrWontSign{twofa}
}
case parentSigned:
gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err := git.OpenRepository(ctx, tmpBasePath)
if err != nil {
return false, "", nil, err
}
@ -322,7 +322,7 @@ Loop:
}
case baseSigned:
if gitRepo == nil {
gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err = git.OpenRepository(ctx, tmpBasePath)
if err != nil {
return false, "", nil, err
}
@ -338,7 +338,7 @@ Loop:
}
case headSigned:
if gitRepo == nil {
gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err = git.OpenRepository(ctx, tmpBasePath)
if err != nil {
return false, "", nil, err
}
@ -354,7 +354,7 @@ Loop:
}
case commitsSigned:
if gitRepo == nil {
gitRepo, err = git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err = git.OpenRepository(ctx, tmpBasePath)
if err != nil {
return false, "", nil, err
}

@ -690,7 +690,7 @@ func TestDiffLine_GetCommentSide(t *testing.T) {
}
func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
gitRepo, err := git.OpenRepository("./testdata/academic-module")
gitRepo, err := git.OpenRepository(git.DefaultContext, "./testdata/academic-module")
if !assert.NoError(t, err) {
return
}

@ -182,7 +182,7 @@ func (g *RepositoryDumper) CreateRepo(repo *base.Repository, opts base.MigrateOp
}
}
g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, g.gitPath())
g.gitRepo, err = git.OpenRepository(g.ctx, g.gitPath())
return err
}

@ -136,7 +136,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
if err != nil {
return err
}
g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, r.RepoPath())
g.gitRepo, err = git.OpenRepository(g.ctx, r.RepoPath())
return err
}

@ -247,7 +247,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
Author: &signature,
Message: "Initial Commit",
}))
fromGitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, fromRepo.RepoPath())
fromGitRepo, err := git.OpenRepository(git.DefaultContext, fromRepo.RepoPath())
assert.NoError(t, err)
defer fromGitRepo.Close()
baseSHA, err := fromGitRepo.GetBranchCommitID(baseRef)
@ -290,7 +290,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
Author: &signature,
Message: "branch2 commit",
}))
forkGitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, forkRepo.RepoPath())
forkGitRepo, err := git.OpenRepository(git.DefaultContext, forkRepo.RepoPath())
assert.NoError(t, err)
defer forkGitRepo.Close()
forkHeadSHA, err := forkGitRepo.GetBranchCommitID(forkHeadRef)

@ -281,7 +281,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
log.Error("SyncMirrors [repo: %-v]: %v", m.Repo, err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
gitRepo, err := git.OpenRepository(ctx, repoPath)
if err != nil {
log.Error("SyncMirrors [repo: %-v]: failed to OpenRepository: %v", m.Repo, err)
return nil, false
@ -438,7 +438,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
log.Trace("SyncMirrors [repo: %-v]: no branches updated", m.Repo)
} else {
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
gitRepo, err = git.OpenRepositoryCtx(ctx, m.Repo.RepoPath())
gitRepo, err = git.OpenRepository(ctx, m.Repo.RepoPath())
if err != nil {
log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
return false

@ -137,7 +137,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
if setting.LFS.StartServer {
log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo)
gitRepo, err := git.OpenRepositoryCtx(ctx, path)
gitRepo, err := git.OpenRepository(ctx, path)
if err != nil {
log.Error("OpenRepository: %v", err)
return errors.New("Unexpected error")

@ -121,7 +121,7 @@ func getMergeCommit(ctx context.Context, pr *models.PullRequest) (*git.Commit, e
mergeCommit = commitID[:40]
}
gitRepo, err := git.OpenRepositoryCtx(ctx, pr.BaseRepo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}

@ -69,7 +69,7 @@ func TestPatch(pr *models.PullRequest) error {
}
}()
gitRepo, err := git.OpenRepositoryCtx(ctx, tmpBasePath)
gitRepo, err := git.OpenRepository(ctx, tmpBasePath)
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
}

@ -83,7 +83,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, pull *mode
}
// add first push codes comment
baseGitRepo, err := git.OpenRepositoryCtx(prCtx, pr.BaseRepo.RepoPath())
baseGitRepo, err := git.OpenRepository(prCtx, pr.BaseRepo.RepoPath())
if err != nil {
return err
}
@ -224,7 +224,7 @@ func checkForInvalidation(ctx context.Context, requests models.PullRequestList,
if err != nil {
return fmt.Errorf("GetRepositoryByID: %v", err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("git.OpenRepository: %v", err)
}
@ -352,7 +352,7 @@ func checkIfPRContentChanged(ctx context.Context, pr *models.PullRequest, oldCom
return false, fmt.Errorf("LoadBaseRepo: %v", err)
}
headGitRepo, err := git.OpenRepositoryCtx(ctx, pr.HeadRepo.RepoPath())
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
return false, fmt.Errorf("OpenRepository: %v", err)
}
@ -752,7 +752,7 @@ func GetIssuesLastCommitStatus(ctx context.Context, issues models.IssueList) (ma
}
gitRepo, ok := gitRepos[issue.RepoID]
if !ok {
gitRepo, err = git.OpenRepositoryCtx(ctx, issue.Repo.RepoPath())
gitRepo, err = git.OpenRepository(ctx, issue.Repo.RepoPath())
if err != nil {
log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err)
continue

@ -127,7 +127,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
}
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath())
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
return nil, fmt.Errorf("RepositoryFromContextOrOpen: %v", err)
}
defer closer.Close()

@ -31,7 +31,7 @@ func TestRelease_Create(t *testing.T) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
repoPath := repo_model.RepoPath(user.Name, repo.Name)
gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath)
assert.NoError(t, err)
defer gitRepo.Close()
@ -135,7 +135,7 @@ func TestRelease_Update(t *testing.T) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
repoPath := repo_model.RepoPath(user.Name, repo.Name)
gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath)
assert.NoError(t, err)
defer gitRepo.Close()
@ -277,7 +277,7 @@ func TestRelease_createTag(t *testing.T) {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
repoPath := repo_model.RepoPath(user.Name, repo.Name)
gitRepo, err := git.OpenRepository(repoPath)
gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath)
assert.NoError(t, err)
defer gitRepo.Close()

@ -123,7 +123,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
repo.IsEmpty = false
// Don't bother looking this repo in the context it won't be there
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return fmt.Errorf("openRepository: %v", err)
}

@ -178,7 +178,7 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
return nil, fmt.Errorf("archiver.LoadRepo failed: %v", err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
return nil, err
}

@ -109,7 +109,7 @@ func TestGetFileResponseFromCommit(t *testing.T) {
repo := ctx.Repo.Repository
branch := repo.DefaultBranch
treePath := "README.md"
gitRepo, _ := git.OpenRepositoryCtx(ctx, repo.RepoPath())
gitRepo, _ := git.OpenRepository(ctx, repo.RepoPath())
defer gitRepo.Close()
commit, _ := gitRepo.GetBranchCommit(branch)
expectedFileResponse := getExpectedFileResponse()

@ -69,7 +69,7 @@ func (t *TemporaryUploadRepository) Clone(branch string) error {
return fmt.Errorf("Clone: %v %s", err, stderr)
}
}
gitRepo, err := git.OpenRepositoryCtx(t.ctx, t.basePath)
gitRepo, err := git.OpenRepository(t.ctx, t.basePath)
if err != nil {
return err
}
@ -82,7 +82,7 @@ func (t *TemporaryUploadRepository) Init() error {
if err := git.InitRepository(t.ctx, t.basePath, false); err != nil {
return err
}
gitRepo, err := git.OpenRepositoryCtx(t.ctx, t.basePath)
gitRepo, err := git.OpenRepository(t.ctx, t.basePath)
if err != nil {
return err
}

@ -30,8 +30,8 @@ type ForkRepoOptions struct {
}
// ForkRepository forks a repository
func ForkRepository(doer, owner *user_model.User, opts ForkRepoOptions) (_ *repo_model.Repository, err error) {
forkedRepo, err := repo_model.GetUserFork(opts.BaseRepo.ID, owner.ID)
func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts ForkRepoOptions) (*repo_model.Repository, error) {
forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID)
if err != nil {
return nil, err
}
@ -91,24 +91,24 @@ func ForkRepository(doer, owner *user_model.User, opts ForkRepoOptions) (_ *repo
panic(panicErr)
}()
err = db.WithTx(func(ctx context.Context) error {
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
err = db.WithTx(func(txCtx context.Context) error {
if err = models.CreateRepository(txCtx, doer, owner, repo, false); err != nil {
return err
}
if err = models.IncrementRepoForkNum(ctx, opts.BaseRepo.ID); err != nil {
if err = models.IncrementRepoForkNum(txCtx, opts.BaseRepo.ID); err != nil {
return err
}
// copy lfs files failure should not be ignored
if err = models.CopyLFS(ctx, repo, opts.BaseRepo); err != nil {
if err = models.CopyLFS(txCtx, repo, opts.BaseRepo); err != nil {
return err
}
needsRollback = true
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
if stdout, err := git.NewCommand(ctx,
if stdout, err := git.NewCommand(txCtx,
"clone", "--bare", oldRepoPath, repoPath).
SetDescription(fmt.Sprintf("ForkRepository(git clone): %s to %s", opts.BaseRepo.FullName(), repo.FullName())).
RunInDirTimeout(10*time.Minute, ""); err != nil {
@ -116,11 +116,11 @@ func ForkRepository(doer, owner *user_model.User, opts ForkRepoOptions) (_ *repo
return fmt.Errorf("git clone: %v", err)
}
if err := models.CheckDaemonExportOK(ctx, repo); err != nil {
if err := models.CheckDaemonExportOK(txCtx, repo); err != nil {
return fmt.Errorf("checkDaemonExportOK: %v", err)
}
if stdout, err := git.NewCommand(ctx, "update-server-info").
if stdout, err := git.NewCommand(txCtx, "update-server-info").
SetDescription(fmt.Sprintf("ForkRepository(git update-server-info): %s", repo.FullName())).
RunInDir(repoPath); err != nil {
log.Error("Fork Repository (git update-server-info) failed for %v:\nStdout: %s\nError: %v", repo, stdout, err)
@ -139,14 +139,14 @@ func ForkRepository(doer, owner *user_model.User, opts ForkRepoOptions) (_ *repo
}
// even if below operations failed, it could be ignored. And they will be retried
if err := models.UpdateRepoSize(db.DefaultContext, repo); err != nil {
if err := models.UpdateRepoSize(ctx, repo); err != nil {
log.Error("Failed to update size for repository: %v", err)
}
if err := repo_model.CopyLanguageStat(opts.BaseRepo, repo); err != nil {
log.Error("Copy language stat from oldRepo failed: %v", err)
}
gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.RepoPath())
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
if err != nil {
log.Error("Open created git repository failed: %v", err)
} else {

@ -11,6 +11,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"github.com/stretchr/testify/assert"
)
@ -22,7 +23,7 @@ func TestForkRepository(t *testing.T) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 13}).(*user_model.User)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}).(*repo_model.Repository)
fork, err := ForkRepository(user, user, ForkRepoOptions{
fork, err := ForkRepository(git.DefaultContext, user, user, ForkRepoOptions{
BaseRepo: repo,
Name: "test",
Description: "test",

@ -54,13 +54,13 @@ func SyncRepositoryHooks(ctx context.Context) error {
// GenerateGitHooks generates git hooks from a template repository
func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
generateGitRepo, err := git.OpenRepositoryCtx(ctx, generateRepo.RepoPath())
generateGitRepo, err := git.OpenRepository(ctx, generateRepo.RepoPath())
if err != nil {
return err
}
defer generateGitRepo.Close()
templateGitRepo, err := git.OpenRepositoryCtx(ctx, templateRepo.RepoPath())
templateGitRepo, err := git.OpenRepository(ctx, templateRepo.RepoPath())
if err != nil {
return err
}

@ -89,7 +89,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
repoPath := repo.RepoPath()
gitRepo, err := git.OpenRepositoryCtx(ctx, repoPath)
gitRepo, err := git.OpenRepository(ctx, repoPath)
if err != nil {
return fmt.Errorf("OpenRepository[%s]: %v", repoPath, err)
}

@ -156,7 +156,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, basePath)
gitRepo, err := git.OpenRepository(ctx, basePath)
if err != nil {
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
@ -305,7 +305,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
}
gitRepo, err := git.OpenRepositoryCtx(ctx, basePath)
gitRepo, err := git.OpenRepository(ctx, basePath)
if err != nil {
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)

@ -138,7 +138,7 @@ func TestRepository_AddWikiPage(t *testing.T) {
t.Parallel()
assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, wikiName, wikiContent, commitMsg))
// Now need to show that the page has been added:
gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master")
@ -183,7 +183,7 @@ func TestRepository_EditWikiPage(t *testing.T) {
assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", newWikiName, newWikiContent, commitMsg))
// Now need to show that the page has been added:
gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
masterTree, err := gitRepo.GetTree("master")
assert.NoError(t, err)
@ -207,7 +207,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home"))
// Now need to show that the page has been added:
gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
assert.NoError(t, err)
defer gitRepo.Close()
masterTree, err := gitRepo.GetTree("master")
@ -220,7 +220,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) {
func TestPrepareWikiFileName(t *testing.T) {
unittest.PrepareTestEnv(t)
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository)
gitRepo, err := git.OpenRepositoryCtx(git.DefaultContext, repo.WikiPath())
gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath())
defer gitRepo.Close()
assert.NoError(t, err)
@ -283,7 +283,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
err = git.InitRepository(git.DefaultContext, tmpDir, true)
assert.NoError(t, err)
gitRepo, err := git.OpenRepository(tmpDir)
gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir)
defer gitRepo.Close()
assert.NoError(t, err)