From 2f078988ddd5f933289b7055d07e3d967c178346 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 4 May 2025 11:52:00 -0700 Subject: [PATCH 1/2] Fix bug when visiting comparation page (#34334) The `ci.HeadGitRepo` was opened and closed in the function `ParseCompareInfo` but reused in the function `PrepareCompareDiff`. --- routers/web/repo/compare.go | 10 ++-------- routers/web/repo/pull.go | 5 ----- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 278974bec3ecf..2889635fbc68b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -400,12 +400,11 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ci.HeadRepo = ctx.Repo.Repository ci.HeadGitRepo = ctx.Repo.GitRepo } else if has { - ci.HeadGitRepo, err = gitrepo.OpenRepository(ctx, ci.HeadRepo) + ci.HeadGitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ci.HeadRepo) if err != nil { - ctx.ServerError("OpenRepository", err) + ctx.ServerError("RepositoryFromRequestContextOrOpen", err) return nil } - defer ci.HeadGitRepo.Close() } else { ctx.NotFound("ParseCompareInfo", nil) return nil @@ -707,11 +706,6 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor // CompareDiff show different from one commit to another commit func CompareDiff(ctx *context.Context) { ci := ParseCompareInfo(ctx) - defer func() { - if ci != nil && ci.HeadGitRepo != nil { - ci.HeadGitRepo.Close() - } - }() if ctx.Written() { return } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e068ac45c6588..e5a56cbd1280c 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1262,11 +1262,6 @@ func CompareAndPullRequestPost(ctx *context.Context) { ) ci := ParseCompareInfo(ctx) - defer func() { - if ci != nil && ci.HeadGitRepo != nil { - ci.HeadGitRepo.Close() - } - }() if ctx.Written() { return } From f1ae24f5f6d723a9c2d34bdfc2d8d509a0bcddad Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 4 May 2025 12:30:10 -0700 Subject: [PATCH 2/2] Fix bug --- routers/web/repo/compare.go | 9 +++++++-- routers/web/repo/pull.go | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 2889635fbc68b..9d241d3b511ed 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -400,9 +400,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ci.HeadRepo = ctx.Repo.Repository ci.HeadGitRepo = ctx.Repo.GitRepo } else if has { - ci.HeadGitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ci.HeadRepo) + ci.HeadGitRepo, err = gitrepo.OpenRepository(ctx, ci.HeadRepo) if err != nil { - ctx.ServerError("RepositoryFromRequestContextOrOpen", err) + ctx.ServerError("OpenRepository", err) return nil } } else { @@ -706,6 +706,11 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor // CompareDiff show different from one commit to another commit func CompareDiff(ctx *context.Context) { ci := ParseCompareInfo(ctx) + defer func() { + if !ctx.Repo.PullRequest.SameRepo && ci != nil && ci.HeadGitRepo != nil { + ci.HeadGitRepo.Close() + } + }() if ctx.Written() { return } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e5a56cbd1280c..ac08bf2a4ff5d 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1262,6 +1262,11 @@ func CompareAndPullRequestPost(ctx *context.Context) { ) ci := ParseCompareInfo(ctx) + defer func() { + if !ctx.Repo.PullRequest.SameRepo && ci != nil && ci.HeadGitRepo != nil { + ci.HeadGitRepo.Close() + } + }() if ctx.Written() { return }