From e1d49daedea9400efee6cf539e36b24edb616874 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 30 Apr 2025 23:16:42 -0700 Subject: [PATCH 1/2] Fix bug --- routers/web/repo/compare.go | 5 +++-- routers/web/repo/pull.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 34d9710acbbce..f367311ab9aac 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -407,7 +407,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ctx.ServerError("OpenRepository", err) return nil } - defer ci.HeadGitRepo.Close() + // if it's not the same git repo, the HeadGitRepo should be closed out of the function } else { ctx.NotFound(nil) return nil @@ -727,7 +727,8 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor func CompareDiff(ctx *context.Context) { ci := ParseCompareInfo(ctx) defer func() { - if ci != nil && ci.HeadGitRepo != nil { + // If it's the same repo, the git repo should be managed by the context + if !ctx.Repo.PullRequest.SameRepo && ci != nil && ci.HeadGitRepo != nil { ci.HeadGitRepo.Close() } }() diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 6a6324026963f..734d54ad680ab 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1297,7 +1297,7 @@ func CompareAndPullRequestPost(ctx *context.Context) { ci := ParseCompareInfo(ctx) defer func() { - if ci != nil && ci.HeadGitRepo != nil { + if !ctx.Repo.PullRequest.SameRepo && ci != nil && ci.HeadGitRepo != nil { ci.HeadGitRepo.Close() } }() From e6bd151efea6ae1ee72a90232a847f40f29f9241 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 1 May 2025 09:25:11 -0700 Subject: [PATCH 2/2] improve --- routers/web/repo/compare.go | 11 ++--------- routers/web/repo/pull.go | 5 ----- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index f367311ab9aac..8b99dd95da109 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -402,12 +402,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 } - // if it's not the same git repo, the HeadGitRepo should be closed out of the function } else { ctx.NotFound(nil) return nil @@ -726,12 +725,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 it's the same repo, the git repo should be managed by the context - 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 734d54ad680ab..43ddc265cfafb 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1296,11 +1296,6 @@ 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 }