Skip to content

Commit

Permalink
Add Close() method to gogitRepository (#8901) (#8958)
Browse files Browse the repository at this point in the history
Backport #8901 - Adjusted slightly for 1.9

In investigating #7947 it has become clear that the storage component of go-git repositories needs closing.

This PR adds this Close function and adds the Close functions as necessary.

In TransferOwnership the ctx.Repo.GitRepo is closed if it is open to help prevent the risk of multiple open files.

Fixes #7947
  • Loading branch information
zeripath committed Nov 13, 2019
1 parent 2ef3752 commit fb5af37
Show file tree
Hide file tree
Showing 71 changed files with 275 additions and 31 deletions.
3 changes: 3 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,20 @@ func runRepoSyncReleases(c *cli.Context) error {

if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
log.Warn(" SyncReleasesWithTags: %v", err)
gitRepo.Close()
continue
}

count, err = getReleaseCount(repo.ID)
if err != nil {
log.Warn(" GetReleaseCountByRepoID: %v", err)
gitRepo.Close()
continue
}

log.Trace(" repo %s releases synchronized to tags: from %d to %d",
repo.FullName(), oldnum, count)
gitRepo.Close()
}
}

Expand Down
3 changes: 2 additions & 1 deletion docs/content/doc/advanced/migrations.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Uploader interface {
CreateComment(issueNumber int64, comment *Comment) error
CreatePullRequest(pr *PullRequest) error
Rollback() error
Close()
}

```
```
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ require (
github.com/mattn/go-sqlite3 v1.11.0
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
github.com/microcosm-cc/bluemonday v0.0.0-20161012083705-f77f16ffc87a
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae // indirect
github.com/msteinert/pam v0.0.0-20151204160544-02ccfbfaf0cc
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) {

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

err = gitRepo.CreateTag("v0.0.1", "master")
assert.NoError(t, err)
Expand Down Expand Up @@ -112,6 +113,7 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {

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

err = gitRepo.CreateTag("v0.0.1", "master")
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions integrations/api_repo_file_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestAPICreateFile(t *testing.T) {
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
gitRepo.Close()
}

// Test creating a file in a new branch
Expand Down
1 change: 1 addition & 0 deletions integrations/api_repo_file_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func TestAPIUpdateFile(t *testing.T) {
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
gitRepo.Close()
}

// Test updating a file in a new branch
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_repo_get_contents_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
// Get the commit ID of the default branch
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
// Make a new tag in repo1
newTag := "test_tag"
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_repo_get_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
// Get the commit ID of the default branch
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
// Make a new tag in repo1
newTag := "test_tag"
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_repo_git_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestAPIGitTags(t *testing.T) {
git.NewCommand("config", "user.email", user.Email).RunInDir(repo.RepoPath())

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

commit, _ := gitRepo.GetBranchCommit("master")
lTagName := "lightweightTag"
gitRepo.CreateTag(lTagName, commit.ID.String())
Expand Down
5 changes: 5 additions & 0 deletions integrations/repofiles_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()
repo := ctx.Repo.Repository
doer := ctx.User
opts := getDeleteRepoFileOptions(repo)
Expand Down Expand Up @@ -111,6 +112,8 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User
opts := getDeleteRepoFileOptions(repo)
Expand Down Expand Up @@ -139,6 +142,8 @@ func TestDeleteRepoFileErrors(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User

Expand Down
18 changes: 18 additions & 0 deletions integrations/repofiles_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User
opts := getCreateRepoFileOptions(repo)
Expand All @@ -201,6 +203,8 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
// asserts
assert.Nil(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
Expand All @@ -220,6 +224,8 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User
opts := getUpdateRepoFileOptions(repo)
Expand All @@ -230,6 +236,8 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
// asserts
assert.Nil(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch)
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commitID, opts.TreePath)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
Expand All @@ -249,6 +257,8 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User
opts := getUpdateRepoFileOptions(repo)
Expand All @@ -261,6 +271,8 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {
// asserts
assert.Nil(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
defer gitRepo.Close()

commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.TreePath)
// assert that the old file no longer exists in the last commit of the branch
Expand Down Expand Up @@ -288,6 +300,8 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User
opts := getUpdateRepoFileOptions(repo)
Expand All @@ -300,6 +314,8 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
// asserts
assert.Nil(t, err)
gitRepo, _ := git.OpenRepository(repo.RepoPath())
defer gitRepo.Close()

commitID, _ := gitRepo.GetBranchCommitID(repo.DefaultBranch)
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commitID, opts.TreePath)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
Expand All @@ -315,6 +331,8 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)
defer ctx.Repo.GitRepo.Close()

repo := ctx.Repo.Repository
doer := ctx.User

Expand Down
3 changes: 3 additions & 0 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
if err != nil {
log.Error("GetBranchCommitID[%s]: %v", opts.RefFullName, err)
}
gitRepo.Close()
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
Ref: refName,
Sha: shaSum,
Expand Down Expand Up @@ -797,6 +798,8 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
if err != nil {
log.Error("GetTagCommitID[%s]: %v", opts.RefFullName, err)
}
gitRepo.Close()

if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
Ref: refName,
Sha: shaSum,
Expand Down
2 changes: 2 additions & 0 deletions models/git_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
if err != nil {
return nil, err
}
defer gitRepo.Close()

commit, err := gitRepo.GetCommit(afterCommitID)
if err != nil {
Expand Down Expand Up @@ -747,6 +748,7 @@ func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiff
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
}
defer repo.Close()

commit, err := repo.GetCommit(endCommit)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions models/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func BenchmarkGetCommitGraph(b *testing.B) {
if err != nil {
b.Error("Could not open repository")
}
defer currentRepo.Close()

for i := 0; i < b.N; i++ {
graph, err := GetCommitGraph(currentRepo)
Expand Down
1 change: 1 addition & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ func CreateCodeComment(doer *User, repo *Repository, issue *Issue, content, tree
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()

// FIXME validate treePath
// Get latest commit referencing the commented line
Expand Down
1 change: 1 addition & 0 deletions models/migrations/v39.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func releaseAddColumnIsTagAndSyncTags(x *xorm.Engine) error {
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
log.Warn("SyncReleasesWithTags: %v", err)
}
gitRepo.Close()
}
if len(repos) < pageSize {
break
Expand Down
1 change: 1 addition & 0 deletions models/migrations/v82.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
if err != nil {
return err
}
defer gitRepo.Close()
gitRepoCache[release.RepoID] = gitRepo
}

Expand Down
5 changes: 5 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
if err != nil {
return nil, err
}
defer headGitRepo.Close()

lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
if err != nil {
Expand Down Expand Up @@ -527,6 +528,7 @@ func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()

commit, err := gitRepo.GetCommit(mergeCommit[:40])
if err != nil {
Expand Down Expand Up @@ -920,6 +922,7 @@ func (pr *PullRequest) UpdatePatch() (err error) {
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
}
defer headGitRepo.Close()

// Add a temporary remote.
tmpRemote := com.ToStr(time.Now().UnixNano())
Expand Down Expand Up @@ -961,6 +964,7 @@ func (pr *PullRequest) PushToBaseRepo() (err error) {
if err != nil {
return fmt.Errorf("OpenRepository: %v", err)
}
defer headGitRepo.Close()

tmpRemoteName := fmt.Sprintf("tmp-pull-%d", pr.ID)
if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), false); err != nil {
Expand Down Expand Up @@ -1150,6 +1154,7 @@ func checkForInvalidation(requests PullRequestList, repoID int64, doer *User, br
if err != nil {
log.Error("PullRequestList.InvalidateCodeComments: %v", err)
}
gitRepo.Close()
}()
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions models/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestRelease_Create(t *testing.T) {

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

assert.NoError(t, CreateRelease(gitRepo, &Release{
RepoID: repo.ID,
Expand Down Expand Up @@ -115,6 +116,7 @@ func TestRelease_MirrorDelete(t *testing.T) {

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

findOptions := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
initCount, err := GetReleaseCountByRepoID(mirror.ID, findOptions)
Expand Down
1 change: 1 addition & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
if err != nil {
return repo, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()

repo.IsEmpty, err = gitRepo.IsEmpty()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions models/repo_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func GetActivityStats(repo *Repository, timeFrom time.Time, releases, issues, pr
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()

code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch)
if err != nil {
return nil, fmt.Errorf("FillFromGit: %v", err)
Expand All @@ -79,6 +81,8 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
defer gitRepo.Close()

code, err := gitRepo.GetCodeActivityStats(timeFrom, "")
if err != nil {
return nil, fmt.Errorf("FillFromGit: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions models/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
if err != nil {
return nil, err
}
defer gitRepo.Close()

return gitRepo.GetBranch(branch)
}
Expand All @@ -38,6 +39,7 @@ func (repo *Repository) CheckBranchName(name string) error {
if err != nil {
return err
}
defer gitRepo.Close()

branches, err := repo.GetBranches()
if err != nil {
Expand Down Expand Up @@ -94,6 +96,7 @@ func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName st
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)
}
defer gitRepo.Close()

if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
Expand Down Expand Up @@ -140,6 +143,7 @@ func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName
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)
}
defer gitRepo.Close()

if err = gitRepo.CreateBranch(branchName, commit); err != nil {
log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
Expand Down
Loading

0 comments on commit fb5af37

Please sign in to comment.