新增:获取commit下diff内容

This commit is contained in:
yystopf 2022-12-26 14:48:17 +08:00
parent f1088ea158
commit 966797489d
2 changed files with 35 additions and 0 deletions

View File

@ -70,6 +70,9 @@ func Routers(ctx gocontext.Context) *web.Route {
m.Group("/branches", func() {
m.Get("/branches_slice", context.ReferencesGitRepo(), repo.ListBranchesSlice)
}, reqRepoReader(unit_model.TypeCode))
m.Group("/commits", func() {
m.Get("/{sha}/diff", repo.GetCommitDiff)
}, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode))
m.Group("/tags", func() {
m.Get("", repo.ListTags)
}, reqRepoReader(unit_model.TypeCode), context.ReferencesGitRepo(true))

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/gitdiff"
hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
)
@ -237,3 +238,34 @@ func getCommit(ctx *context.APIContext, indentifier string) {
}
ctx.JSON(http.StatusOK, json)
}
func GetCommitDiff(ctx *context.APIContext) {
commitID := ctx.Params(":sha")
gitRepo := ctx.Repo.GitRepo
commit, err := gitRepo.GetCommit(commitID)
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound(commitID)
return
}
ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err)
return
}
if len(commitID) != 40 {
commitID = commit.ID.String()
}
diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
AfterCommitID: commitID,
MaxLines: setting.Git.MaxGitDiffLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: setting.Git.MaxGitDiffFiles,
})
if err != nil {
ctx.NotFound("GetDIff", err)
return
}
ctx.JSON(http.StatusOK, &diff)
}