Some code improvements (#14266)

This commit is contained in:
Lunny Xiao 2021-01-07 03:23:57 +08:00 committed by GitHub
parent 8688c2be95
commit 3c96a37162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 3 deletions

View File

@ -246,6 +246,7 @@ func TestCantMergeConflict(t *testing.T) {
err = pull.Merge(pr, user1, gitRepo, models.MergeStyleRebase, "CONFLICT") err = pull.Merge(pr, user1, gitRepo, models.MergeStyleRebase, "CONFLICT")
assert.Error(t, err, "Merge should return an error due to conflict") assert.Error(t, err, "Merge should return an error due to conflict")
assert.True(t, models.IsErrRebaseConflicts(err), "Merge error is not a conflict error") assert.True(t, models.IsErrRebaseConflicts(err), "Merge error is not a conflict error")
gitRepo.Close()
}) })
} }
@ -329,5 +330,6 @@ func TestCantMergeUnrelated(t *testing.T) {
err = pull.Merge(pr, user1, gitRepo, models.MergeStyleMerge, "UNRELATED") err = pull.Merge(pr, user1, gitRepo, models.MergeStyleMerge, "UNRELATED")
assert.Error(t, err, "Merge should return an error due to unrelated") assert.Error(t, err, "Merge should return an error due to unrelated")
assert.True(t, models.IsErrMergeUnrelatedHistories(err), "Merge error is not a unrelated histories error") assert.True(t, models.IsErrMergeUnrelatedHistories(err), "Merge error is not a unrelated histories error")
gitRepo.Close()
}) })
} }

View File

@ -137,6 +137,9 @@ func fixPublisherIDforTagReleases(x *xorm.Engine) error {
return err return err
} }
} }
if gitRepo != nil {
gitRepo.Close()
}
if err := sess.Commit(); err != nil { if err := sess.Commit(); err != nil {
return err return err

View File

@ -413,9 +413,8 @@ func PutHandler(ctx *context.Context) {
} }
contentStore := &ContentStore{ObjectStorage: storage.LFS} contentStore := &ContentStore{ObjectStorage: storage.LFS}
bodyReader := ctx.Req.Body().ReadCloser() defer ctx.Req.Request.Body.Close()
defer bodyReader.Close() if err := contentStore.Put(meta, ctx.Req.Request.Body); err != nil {
if err := contentStore.Put(meta, bodyReader); err != nil {
// Put will log the error itself // Put will log the error itself
ctx.Resp.WriteHeader(500) ctx.Resp.WriteHeader(500)
if err == errSizeMismatch || err == errHashMismatch { if err == errSizeMismatch || err == errHashMismatch {

View File

@ -120,13 +120,16 @@ func LFSLocks(ctx *context.Context) {
}); err != nil { }); err != nil {
log.Error("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err) log.Error("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err)
ctx.ServerError("LFSLocks", fmt.Errorf("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err)) ctx.ServerError("LFSLocks", fmt.Errorf("Failed to clone repository: %s (%v)", ctx.Repo.Repository.FullName(), err))
return
} }
gitRepo, err := git.OpenRepository(tmpBasePath) gitRepo, err := git.OpenRepository(tmpBasePath)
if err != nil { if err != nil {
log.Error("Unable to open temporary repository: %s (%v)", tmpBasePath, err) 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)) ctx.ServerError("LFSLocks", fmt.Errorf("Failed to open new temporary repository in: %s %v", tmpBasePath, err))
return
} }
defer gitRepo.Close()
filenames := make([]string, len(lfsLocks)) filenames := make([]string, len(lfsLocks))
@ -137,6 +140,7 @@ func LFSLocks(ctx *context.Context) {
if err := gitRepo.ReadTreeToIndex(ctx.Repo.Repository.DefaultBranch); err != nil { if err := gitRepo.ReadTreeToIndex(ctx.Repo.Repository.DefaultBranch); err != nil {
log.Error("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err) log.Error("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err)
ctx.ServerError("LFSLocks", fmt.Errorf("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err)) ctx.ServerError("LFSLocks", fmt.Errorf("Unable to read the default branch to the index: %s (%v)", ctx.Repo.Repository.DefaultBranch, err))
return
} }
name2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{ name2attribute2info, err := gitRepo.CheckAttribute(git.CheckAttributeOpts{
@ -147,6 +151,7 @@ func LFSLocks(ctx *context.Context) {
if err != nil { if err != nil {
log.Error("Unable to check attributes in %s (%v)", tmpBasePath, err) log.Error("Unable to check attributes in %s (%v)", tmpBasePath, err)
ctx.ServerError("LFSLocks", err) ctx.ServerError("LFSLocks", err)
return
} }
lockables := make([]bool, len(lfsLocks)) lockables := make([]bool, len(lfsLocks))
@ -166,6 +171,7 @@ func LFSLocks(ctx *context.Context) {
if err != nil { if err != nil {
log.Error("Unable to lsfiles in %s (%v)", tmpBasePath, err) log.Error("Unable to lsfiles in %s (%v)", tmpBasePath, err)
ctx.ServerError("LFSLocks", err) ctx.ServerError("LFSLocks", err)
return
} }
filemap := make(map[string]bool, len(filelist)) filemap := make(map[string]bool, len(filelist))

View File

@ -670,6 +670,8 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err
if err != nil { if err != nil {
return false, err return false, err
} }
defer baseGitRepo.Close()
baseCommit, err := baseGitRepo.GetBranchCommit(branchName) baseCommit, err := baseGitRepo.GetBranchCommit(branchName)
if err != nil { if err != nil {
return false, err return false, err
@ -682,6 +684,8 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err
if err != nil { if err != nil {
return false, err return false, err
} }
defer headGitRepo.Close()
headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch) headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
if err != nil { if err != nil {
return false, err return false, err