Skip to content

Commit 1ebb30e

Browse files
authored
Pass gitRepo down to GetRawDiff, since its used for main repo and wiki (#19461)
as per #19449 (comment) pass gitRepo down to GetRawDiff, since its used for main repo and wiki
1 parent 7c164d5 commit 1ebb30e

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

modules/git/diff.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const (
2828
)
2929

3030
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
31-
func GetRawDiff(ctx context.Context, repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
32-
return GetRawDiffForFile(ctx, repoPath, "", commitID, diffType, "", writer)
31+
func GetRawDiff(repo *Repository, commitID string, diffType RawDiffType, writer io.Writer) error {
32+
return GetRepoRawDiffForFile(repo, "", commitID, diffType, "", writer)
3333
}
3434

3535
// GetReverseRawDiff dumps the reverse diff results of repository in given commit ID to io.Writer.
@@ -46,17 +46,6 @@ func GetReverseRawDiff(ctx context.Context, repoPath, commitID string, writer io
4646
return nil
4747
}
4848

49-
// GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer.
50-
func GetRawDiffForFile(ctx context.Context, repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
51-
repo, closer, err := RepositoryFromContextOrOpen(ctx, repoPath)
52-
if err != nil {
53-
return fmt.Errorf("RepositoryFromContextOrOpen: %v", err)
54-
}
55-
defer closer.Close()
56-
57-
return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer)
58-
}
59-
6049
// GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository
6150
func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
6251
commit, err := repo.GetCommit(endCommit)

routers/api/v1/repo/commits.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net/http"
1212
"strconv"
1313

14-
repo_model "code.gitea.io/gitea/models/repo"
1514
user_model "code.gitea.io/gitea/models/user"
1615
"code.gitea.io/gitea/modules/context"
1716
"code.gitea.io/gitea/modules/convert"
@@ -268,17 +267,12 @@ func DownloadCommitDiffOrPatch(ctx *context.APIContext) {
268267
// "$ref": "#/responses/string"
269268
// "404":
270269
// "$ref": "#/responses/notFound"
271-
repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
272-
// TODO: use gitRepo from context
273-
if err := git.GetRawDiff(
274-
ctx,
275-
repoPath,
276-
ctx.Params(":sha"),
277-
git.RawDiffType(ctx.Params(":diffType")),
278-
ctx.Resp,
279-
); err != nil {
270+
sha := ctx.Params(":sha")
271+
diffType := git.RawDiffType(ctx.Params(":diffType"))
272+
273+
if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, diffType, ctx.Resp); err != nil {
280274
if git.IsErrNotExist(err) {
281-
ctx.NotFound(ctx.Params(":sha"))
275+
ctx.NotFound(sha)
282276
return
283277
}
284278
ctx.Error(http.StatusInternalServerError, "DownloadCommitDiffOrPatch", err)

routers/web/repo/cherry_pick.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func CherryPickPost(ctx *context.Context) {
151151
return
152152
}
153153
} else {
154-
if err := git.GetRawDiff(ctx, ctx.Repo.Repository.RepoPath(), sha, git.RawDiffType("patch"), buf); err != nil {
154+
if err := git.GetRawDiff(ctx.Repo.GitRepo, sha, git.RawDiffType("patch"), buf); err != nil {
155155
if git.IsErrNotExist(err) {
156156
ctx.NotFound("GetRawDiff", errors.New("commit "+ctx.Params(":sha")+" does not exist."))
157157
return

routers/web/repo/commit.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ package repo
77

88
import (
99
"errors"
10+
"fmt"
1011
"net/http"
1112
"strings"
1213

1314
"code.gitea.io/gitea/models"
1415
asymkey_model "code.gitea.io/gitea/models/asymkey"
1516
"code.gitea.io/gitea/models/db"
16-
repo_model "code.gitea.io/gitea/models/repo"
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/base"
1919
"code.gitea.io/gitea/modules/charset"
@@ -381,15 +381,24 @@ func Diff(ctx *context.Context) {
381381

382382
// RawDiff dumps diff results of repository in given commit ID to io.Writer
383383
func RawDiff(ctx *context.Context) {
384-
var repoPath string
384+
var gitRepo *git.Repository
385385
if ctx.Data["PageIsWiki"] != nil {
386-
repoPath = ctx.Repo.Repository.WikiPath()
386+
wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
387+
if err != nil {
388+
ctx.ServerError("OpenRepository", err)
389+
return
390+
}
391+
defer wikiRepo.Close()
392+
gitRepo = wikiRepo
387393
} else {
388-
repoPath = repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
394+
gitRepo = ctx.Repo.GitRepo
395+
if gitRepo == nil {
396+
ctx.ServerError("GitRepo not open", fmt.Errorf("no open git repo for '%s'", ctx.Repo.Repository.FullName()))
397+
return
398+
}
389399
}
390400
if err := git.GetRawDiff(
391-
ctx,
392-
repoPath,
401+
gitRepo,
393402
ctx.Params(":sha"),
394403
git.RawDiffType(ctx.Params(":ext")),
395404
ctx.Resp,

0 commit comments

Comments
 (0)