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) +}