Skip to content

Commit fb5af37

Browse files
authored
Add Close() method to gogitRepository (#8901) (#8958)
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
1 parent 2ef3752 commit fb5af37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+275
-31
lines changed

cmd/admin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,20 @@ func runRepoSyncReleases(c *cli.Context) error {
374374

375375
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
376376
log.Warn(" SyncReleasesWithTags: %v", err)
377+
gitRepo.Close()
377378
continue
378379
}
379380

380381
count, err = getReleaseCount(repo.ID)
381382
if err != nil {
382383
log.Warn(" GetReleaseCountByRepoID: %v", err)
384+
gitRepo.Close()
383385
continue
384386
}
385387

386388
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
387389
repo.FullName(), oldnum, count)
390+
gitRepo.Close()
388391
}
389392
}
390393

docs/content/doc/advanced/migrations.en-us.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Uploader interface {
6767
CreateComment(issueNumber int64, comment *Comment) error
6868
CreatePullRequest(pr *PullRequest) error
6969
Rollback() error
70+
Close()
7071
}
7172

72-
```
73+
```

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ require (
8383
github.com/mattn/go-sqlite3 v1.11.0
8484
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
8585
github.com/microcosm-cc/bluemonday v0.0.0-20161012083705-f77f16ffc87a
86-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
87-
github.com/modern-go/reflect2 v1.0.1 // indirect
8886
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae // indirect
8987
github.com/msteinert/pam v0.0.0-20151204160544-02ccfbfaf0cc
9088
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5

integrations/api_releases_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) {
5151

5252
gitRepo, err := git.OpenRepository(repo.RepoPath())
5353
assert.NoError(t, err)
54+
defer gitRepo.Close()
5455

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

113114
gitRepo, err := git.OpenRepository(repo.RepoPath())
114115
assert.NoError(t, err)
116+
defer gitRepo.Close()
115117

116118
err = gitRepo.CreateTag("v0.0.1", "master")
117119
assert.NoError(t, err)

integrations/api_repo_file_create_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func TestAPICreateFile(t *testing.T) {
139139
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
140140
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
141141
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
142+
gitRepo.Close()
142143
}
143144

144145
// Test creating a file in a new branch

integrations/api_repo_file_update_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func TestAPIUpdateFile(t *testing.T) {
143143
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
144144
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
145145
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
146+
gitRepo.Close()
146147
}
147148

148149
// Test updating a file in a new branch

integrations/api_repo_get_contents_list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
7474
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
7575
// Get the commit ID of the default branch
7676
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
77+
defer gitRepo.Close()
78+
7779
commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
7880
// Make a new tag in repo1
7981
newTag := "test_tag"

integrations/api_repo_get_contents_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
7575
repo1.CreateNewBranch(user2, repo1.DefaultBranch, newBranch)
7676
// Get the commit ID of the default branch
7777
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
78+
defer gitRepo.Close()
79+
7880
commitID, _ := gitRepo.GetBranchCommitID(repo1.DefaultBranch)
7981
// Make a new tag in repo1
8082
newTag := "test_tag"

integrations/api_repo_git_tags_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func TestAPIGitTags(t *testing.T) {
2929
git.NewCommand("config", "user.email", user.Email).RunInDir(repo.RepoPath())
3030

3131
gitRepo, _ := git.OpenRepository(repo.RepoPath())
32+
defer gitRepo.Close()
33+
3234
commit, _ := gitRepo.GetBranchCommit("master")
3335
lTagName := "lightweightTag"
3436
gitRepo.CreateTag(lTagName, commit.ID.String())

integrations/repofiles_delete_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
7373
test.LoadRepoCommit(t, ctx)
7474
test.LoadUser(t, ctx, 2)
7575
test.LoadGitRepo(t, ctx)
76+
defer ctx.Repo.GitRepo.Close()
7677
repo := ctx.Repo.Repository
7778
doer := ctx.User
7879
opts := getDeleteRepoFileOptions(repo)
@@ -111,6 +112,8 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
111112
test.LoadRepoCommit(t, ctx)
112113
test.LoadUser(t, ctx, 2)
113114
test.LoadGitRepo(t, ctx)
115+
defer ctx.Repo.GitRepo.Close()
116+
114117
repo := ctx.Repo.Repository
115118
doer := ctx.User
116119
opts := getDeleteRepoFileOptions(repo)
@@ -139,6 +142,8 @@ func TestDeleteRepoFileErrors(t *testing.T) {
139142
test.LoadRepoCommit(t, ctx)
140143
test.LoadUser(t, ctx, 2)
141144
test.LoadGitRepo(t, ctx)
145+
defer ctx.Repo.GitRepo.Close()
146+
142147
repo := ctx.Repo.Repository
143148
doer := ctx.User
144149

0 commit comments

Comments
 (0)