Skip to content

Commit 3c96a37

Browse files
authored
Some code improvements (#14266)
1 parent 8688c2b commit 3c96a37

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

integrations/pull_merge_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ func TestCantMergeConflict(t *testing.T) {
246246
err = pull.Merge(pr, user1, gitRepo, models.MergeStyleRebase, "CONFLICT")
247247
assert.Error(t, err, "Merge should return an error due to conflict")
248248
assert.True(t, models.IsErrRebaseConflicts(err), "Merge error is not a conflict error")
249+
gitRepo.Close()
249250
})
250251
}
251252

@@ -329,5 +330,6 @@ func TestCantMergeUnrelated(t *testing.T) {
329330
err = pull.Merge(pr, user1, gitRepo, models.MergeStyleMerge, "UNRELATED")
330331
assert.Error(t, err, "Merge should return an error due to unrelated")
331332
assert.True(t, models.IsErrMergeUnrelatedHistories(err), "Merge error is not a unrelated histories error")
333+
gitRepo.Close()
332334
})
333335
}

models/migrations/v156.go

+3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func fixPublisherIDforTagReleases(x *xorm.Engine) error {
137137
return err
138138
}
139139
}
140+
if gitRepo != nil {
141+
gitRepo.Close()
142+
}
140143

141144
if err := sess.Commit(); err != nil {
142145
return err

modules/lfs/server.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,8 @@ func PutHandler(ctx *context.Context) {
413413
}
414414

415415
contentStore := &ContentStore{ObjectStorage: storage.LFS}
416-
bodyReader := ctx.Req.Body().ReadCloser()
417-
defer bodyReader.Close()
418-
if err := contentStore.Put(meta, bodyReader); err != nil {
416+
defer ctx.Req.Request.Body.Close()
417+
if err := contentStore.Put(meta, ctx.Req.Request.Body); err != nil {
419418
// Put will log the error itself
420419
ctx.Resp.WriteHeader(500)
421420
if err == errSizeMismatch || err == errHashMismatch {

routers/repo/lfs.go

+6
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ func LFSLocks(ctx *context.Context) {
120120
}); err != nil {
121121
log.Error("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err)
122122
ctx.ServerError("LFSLocks", fmt.Errorf("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err))
123+
return
123124
}
124125

125126
gitRepo, err := git.OpenRepository(tmpBasePath)
126127
if err != nil {
127128
log.Error("Unable to open temporary repository: %s (%v)", tmpBasePath, err)
128129
ctx.ServerError("LFSLocks", fmt.Errorf("Failed to open new temporary repository in: %s %v", tmpBasePath, err))
130+
return
129131
}
132+
defer gitRepo.Close()
130133

131134
filenames := make([]string, len(lfsLocks))
132135

@@ -137,6 +140,7 @@ func LFSLocks(ctx *context.Context) {
137140
if err := gitRepo.ReadTreeToIndex(ctx.Repo.Repository.DefaultBranch); err != nil {
138141
log.Error("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err)
139142
ctx.ServerError("LFSLocks", fmt.Errorf("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err))
143+
return
140144
}
141145

142146
name2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{
@@ -147,6 +151,7 @@ func LFSLocks(ctx *context.Context) {
147151
if err != nil {
148152
log.Error("Unable to check attributes in %s (%v)", tmpBasePath, err)
149153
ctx.ServerError("LFSLocks", err)
154+
return
150155
}
151156

152157
lockables := make([]bool, len(lfsLocks))
@@ -166,6 +171,7 @@ func LFSLocks(ctx *context.Context) {
166171
if err != nil {
167172
log.Error("Unable to lsfiles in %s (%v)", tmpBasePath, err)
168173
ctx.ServerError("LFSLocks", err)
174+
return
169175
}
170176

171177
filemap := make(map[string]bool, len(filelist))

services/pull/pull.go

+4
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err
670670
if err != nil {
671671
return false, err
672672
}
673+
defer baseGitRepo.Close()
674+
673675
baseCommit, err := baseGitRepo.GetBranchCommit(branchName)
674676
if err != nil {
675677
return false, err
@@ -682,6 +684,8 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err
682684
if err != nil {
683685
return false, err
684686
}
687+
defer headGitRepo.Close()
688+
685689
headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
686690
if err != nil {
687691
return false, err

0 commit comments

Comments
 (0)