diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go new file mode 100644 index 0000000..7c63263 --- /dev/null +++ b/modules/convert/git_commit.go @@ -0,0 +1,26 @@ +package convert + +import ( + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + gitea_convert "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/git" + hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" +) + +func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*hat_api.Commit, error) { + giteaApiCommit, err := gitea_convert.ToCommit(repo, gitRepo, commit, userCache, stat) + if err != nil { + return nil, err + } + + err = commit.LoadBranchName() + if err != nil { + return nil, err + } + + return &hat_api.Commit{ + Commit: giteaApiCommit, + Branch: commit.Branch, + }, nil +} diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go new file mode 100644 index 0000000..8cc0f00 --- /dev/null +++ b/modules/structs/repo_commit.go @@ -0,0 +1,10 @@ +package structs + +import ( + gitea_api "code.gitea.io/gitea/modules/structs" +) + +type Commit struct { + *gitea_api.Commit + Branch string `json:"branch"` +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 3095376..a5bac64 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -118,6 +118,11 @@ func Routers(ctx gocontext.Context) *web.Route { m.Get("", repo.GetContentsList) m.Get("/*", repo.GetContents) }, reqRepoReader(unit.TypeCode)) + m.Group("/git", func() { + m.Group("/commits", func() { + m.Get("/{sha}", repo.GetSingleCommit) + }) + }, context.ReferencesGitRepo(), reqRepoReader(unit.TypeCode)) }, repoAssignment()) }) m.Group("/users", func() { diff --git a/routers/hat/repo/commits.go b/routers/hat/repo/commits.go index 79b44ac..9b4a8a0 100644 --- a/routers/hat/repo/commits.go +++ b/routers/hat/repo/commits.go @@ -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" + hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git" ) @@ -207,3 +208,32 @@ func GetFileAllCommits(ctx *context.APIContext) { ctx.JSON(http.StatusOK, apiCommits) } + +func GetSingleCommit(ctx *context.APIContext) { + sha := ctx.Params(":sha") + if !git.IsValidRefPattern(sha) { + ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha)) + return + } + + getCommit(ctx, sha) +} + +func getCommit(ctx *context.APIContext, indentifier string) { + commit, err := ctx.Repo.GitRepo.GetCommit(indentifier) + if err != nil { + if git.IsErrNotExist(err) { + ctx.NotFound(indentifier) + return + } + ctx.Error(http.StatusInternalServerError, "gitRepo.GetCommit", err) + return + } + + json, err := hat_convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, true) + if err != nil { + ctx.Error(http.StatusInternalServerError, "toCommit", err) + return + } + ctx.JSON(http.StatusOK, json) +}