diff --git a/routers/hat/hat.go b/routers/hat/hat.go index c86d039..ff16739 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -67,6 +67,7 @@ func Routers() *web.Route { Delete(repo.DeleteWiki) }) }) + m.Get("/commits_slice", repo.GetAllCommitsSliceByTime) }, repoAssignment()) }) }) diff --git a/routers/hat/repo/commitgs.go b/routers/hat/repo/commitgs.go new file mode 100644 index 0000000..8444e64 --- /dev/null +++ b/routers/hat/repo/commitgs.go @@ -0,0 +1,123 @@ +package repo + +import ( + "fmt" + "math" + "net/http" + "strconv" + + "code.gitea.io/gitea/models/repo" + user "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/convert" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/routers/api/v1/utils" +) + +func GetAllCommitsSliceByTime(ctx *context.APIContext) { + if ctx.Repo.Repository.IsEmpty { + ctx.JSON(http.StatusConflict, api.APIError{ + Message: "Git Repository is empty.", + URL: setting.API.SwaggerURL, + }) + return + } + + gitRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath()) + if err != nil { + ctx.Error(http.StatusInternalServerError, "OpenRepository", err) + return + } + + defer gitRepo.Close() + + listOptions := utils.GetListOptions(ctx) + if listOptions.Page <= 0 { + listOptions.Page = 1 + } + + if listOptions.PageSize > setting.Git.CommitsRangeSize { + listOptions.PageSize = setting.Git.CommitsRangeSize + } + + sha := ctx.Params("sha") + var baseCommit *git.Commit + if len(sha) == 0 { + head, err := gitRepo.GetHEADBranch() + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err) + return + } + + baseCommit, err = gitRepo.GetBranchCommit(head.Name) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) + return + } + + } else { + baseCommit, err = gitRepo.GetCommit(sha) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + } + + commitsCountTotal, err := baseCommit.CommitsCount() + if err != nil { + ctx.Error(http.StatusInternalServerError, "CommitsCount", err) + return + } + + pageCount := int(math.Ceil(float64(commitsCountTotal))) + + commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize) + if err != nil { + ctx.Error(http.StatusInternalServerError, "CommitsByRange", err) + return + } + + userCache := make(map[string]*user.User) + + apiCommits := make([]*responseCommit, len(commits)) + apiCommitsList := []responseCommit{} + for i, commitPoniter := range commits { + apiCommits[i], err = toResponseCommit(ctx.Repo.Repository, gitRepo, commitPoniter, userCache) + if err != nil { + ctx.Error(http.StatusInternalServerError, "ToCommit", err) + return + } + apiCommitsList = append(apiCommitsList, *apiCommits[i]) + } + + ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) + ctx.RespHeader().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10)) + ctx.RespHeader().Set("X-PageCount", strconv.Itoa(pageCount)) + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount)) + + ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize) + ctx.RespHeader().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal)) + ctx.RespHeader().Set("Access-Control-Expose-Headers", "X-Total-Count, X-PerPage, X-Total, X-PageCount, X-HasMore, Link") + + ctx.JSON(http.StatusOK, apiCommitsList) +} + +type responseCommit struct { + *api.Commit + CommitDate string `json:"commit_date"` +} + +func toResponseCommit(repo *repo.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user.User) (*responseCommit, error) { + apiCommit, err := convert.ToCommit(repo, gitRepo, commit, userCache) + if err != nil { + return nil, err + } + + return &responseCommit{ + Commit: apiCommit, + CommitDate: apiCommit.Created.Format("2006-01-02"), + }, nil +}