From 123bc33bfa169370097258650079cefff7fadf78 Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 5 Nov 2024 16:11:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20=E6=A0=B9=E6=8D=AEfilepat?= =?UTF-8?q?h=E8=8E=B7=E5=8F=96=E5=90=88=E5=B9=B6=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9B=B4=E6=94=B9=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 +- routers/hat/hat.go | 5 ++- routers/hat/repo/pull.go | 70 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 88b3ce5..f77fcf2 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ import ( ) var ( - Version = "v2.6, by v1.21.0 " + Version = "v2.7, by v1.21.0 " Tags = "" MakeVersion = "" ) diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 3810af0..21dffa4 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -165,7 +165,10 @@ func Routers() *web.Route { m.Combo("").Get(repo.GetPullRequest). Patch(bind(gitea_api.EditPullRequestOption{}), repo.EditPullRequest) m.Get("/commits", context.ReferencesGitRepo(), repo.GetPullCommits) - m.Get("/files", context.ReferencesGitRepo(), repo.GetPullFiles) + m.Group("/files", func() { + m.Get("", repo.GetPullFiles) + m.Get("/*", repo.GetPullFilesByPath) + }, context.ReferencesGitRepo()) m.Group("/versions", func() { m.Get("", repo.ListPullRequestVersions) m.Get("/{versionId}/diff", context.ReferencesGitRepo(), repo.GetPullRequestVersionDiff) diff --git a/routers/hat/repo/pull.go b/routers/hat/repo/pull.go index 24274d4..90b9c9b 100644 --- a/routers/hat/repo/pull.go +++ b/routers/hat/repo/pull.go @@ -402,3 +402,73 @@ func GetPullFiles(ctx *context.APIContext) { ctx.JSON(http.StatusOK, fileDiff) } + +func GetPullFilesByPath(ctx *context.APIContext) { + path := ctx.Params("*") + pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + if err != nil { + if issues_model.IsErrPullRequestNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err) + } + return + } + + if err := pr.LoadBaseRepo(ctx); err != nil { + ctx.InternalServerError(err) + return + } + + if err := pr.LoadHeadRepo(ctx); err != nil { + ctx.InternalServerError(err) + return + } + + baseGitRepo := ctx.Repo.GitRepo + + var prInfo *git.CompareInfo + if pr.HasMerged { + prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.MergeBase, pr.GetGitRefName(), true, false) + } else { + prInfo, err = baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(), pr.BaseBranch, pr.GetGitRefName(), true, false) + } + if err != nil { + ctx.ServerError("GetCompareInfo", err) + return + } + + headCommitID, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + ctx.ServerError("GetRefCommitID", err) + return + } + + startCommitID := prInfo.MergeBase + endCommitID := headCommitID + + diff, err := gitdiff.GetDiff(baseGitRepo, &gitdiff.DiffOptions{ + BeforeCommitID: startCommitID, + AfterCommitID: endCommitID, + SkipTo: ctx.FormString("skip-to"), + MaxLines: setting.Git.MaxGitDiffLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: -1, + WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.FormString("whitespace")), + }, path) + + if err != nil { + ctx.ServerError("GetDiff", err) + return + } + + fileDiff := struct { + *gitdiff.Diff + LatestSha string + }{ + Diff: diff, + LatestSha: endCommitID, + } + + ctx.JSON(http.StatusOK, fileDiff) +}