新增: 根据filepath获取合并请求文件更改内容

This commit is contained in:
yystopf 2024-11-05 16:11:16 +08:00
parent cfd6d422ea
commit 123bc33bfa
3 changed files with 75 additions and 2 deletions

View File

@ -22,7 +22,7 @@ import (
) )
var ( var (
Version = "v2.6, by v1.21.0 " Version = "v2.7, by v1.21.0 "
Tags = "" Tags = ""
MakeVersion = "" MakeVersion = ""
) )

View File

@ -165,7 +165,10 @@ func Routers() *web.Route {
m.Combo("").Get(repo.GetPullRequest). m.Combo("").Get(repo.GetPullRequest).
Patch(bind(gitea_api.EditPullRequestOption{}), repo.EditPullRequest) Patch(bind(gitea_api.EditPullRequestOption{}), repo.EditPullRequest)
m.Get("/commits", context.ReferencesGitRepo(), repo.GetPullCommits) 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.Group("/versions", func() {
m.Get("", repo.ListPullRequestVersions) m.Get("", repo.ListPullRequestVersions)
m.Get("/{versionId}/diff", context.ReferencesGitRepo(), repo.GetPullRequestVersionDiff) m.Get("/{versionId}/diff", context.ReferencesGitRepo(), repo.GetPullRequestVersionDiff)

View File

@ -402,3 +402,73 @@ func GetPullFiles(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, fileDiff) 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)
}