From 3872146a9d3e1e2deaae900069d011d79425d4a1 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Fri, 5 May 2023 14:52:20 +0800 Subject: [PATCH] =?UTF-8?q?commit=E5=A2=9E=E5=8A=A0=E5=8F=82=E6=95=B0stat?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/convert/git_commit.go | 136 +++++++++++++++++++++++++++++++++- routers/hat/repo/commits.go | 12 ++- 2 files changed, 142 insertions(+), 6 deletions(-) diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 7c63263..c9ac248 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -3,13 +3,16 @@ 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/convert" "code.gitea.io/gitea/modules/git" + api "code.gitea.io/gitea/modules/structs" hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" + "net/url" + "time" ) 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) + giteaApiCommit, err := ToCommitNotDiff(repo, gitRepo, commit, userCache, stat) if err != nil { return nil, err } @@ -24,3 +27,132 @@ func ToCommit(repo *repo_model.Repository, gitRepo *git.Repository, commit *git. Branch: commit.Branch, }, nil } + +func ToCommitNotDiff(repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, stat bool) (*api.Commit, error) { + var apiAuthor, apiCommitter *api.User + + // Retrieve author and committer information + + var cacheAuthor *user_model.User + var ok bool + if userCache == nil { + cacheAuthor = (*user_model.User)(nil) + ok = false + } else { + cacheAuthor, ok = userCache[commit.Author.Email] + } + + if ok { + apiAuthor = convert.ToUser(cacheAuthor, nil) + } else { + author, err := user_model.GetUserByEmail(commit.Author.Email) + if err != nil && !user_model.IsErrUserNotExist(err) { + return nil, err + } else if err == nil { + apiAuthor = convert.ToUser(author, nil) + if userCache != nil { + userCache[commit.Author.Email] = author + } + } + } + + var cacheCommitter *user_model.User + if userCache == nil { + cacheCommitter = (*user_model.User)(nil) + ok = false + } else { + cacheCommitter, ok = userCache[commit.Committer.Email] + } + + if ok { + apiCommitter = convert.ToUser(cacheCommitter, nil) + } else { + committer, err := user_model.GetUserByEmail(commit.Committer.Email) + if err != nil && !user_model.IsErrUserNotExist(err) { + return nil, err + } else if err == nil { + apiCommitter = convert.ToUser(committer, nil) + if userCache != nil { + userCache[commit.Committer.Email] = committer + } + } + } + + // Retrieve parent(s) of the commit + apiParents := make([]*api.CommitMeta, commit.ParentCount()) + for i := 0; i < commit.ParentCount(); i++ { + sha, _ := commit.ParentID(i) + apiParents[i] = &api.CommitMeta{ + URL: repo.APIURL() + "/git/commits/" + url.PathEscape(sha.String()), + SHA: sha.String(), + } + } + + res := &api.Commit{ + CommitMeta: &api.CommitMeta{ + URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()), + SHA: commit.ID.String(), + Created: commit.Committer.When, + }, + HTMLURL: repo.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()), + RepoCommit: &api.RepoCommit{ + URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()), + Author: &api.CommitUser{ + Identity: api.Identity{ + Name: commit.Author.Name, + Email: commit.Author.Email, + }, + Date: commit.Author.When.Format(time.RFC3339), + }, + Committer: &api.CommitUser{ + Identity: api.Identity{ + Name: commit.Committer.Name, + Email: commit.Committer.Email, + }, + Date: commit.Committer.When.Format(time.RFC3339), + }, + Message: commit.Message(), + Tree: &api.CommitMeta{ + URL: repo.APIURL() + "/git/trees/" + url.PathEscape(commit.ID.String()), + SHA: commit.ID.String(), + Created: commit.Committer.When, + }, + Verification: convert.ToVerification(commit), + }, + Author: apiAuthor, + Committer: apiCommitter, + Parents: apiParents, + } + + // Retrieve files affected by the commit + if stat { + fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String()) + if err != nil { + return nil, err + } + affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified)) + for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} { + for _, filename := range files { + affectedFileList = append(affectedFileList, &api.CommitAffectedFiles{ + Filename: filename, + }) + } + } + + //diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ + // AfterCommitID: commit.ID.String(), + //}) + //if err != nil { + // return nil, err + //} + + res.Files = affectedFileList + //res.Stats = &api.CommitStats{ + // Total: diff.TotalAddition + diff.TotalDeletion, + // Additions: diff.TotalAddition, + // Deletions: diff.TotalDeletion, + //} + } + + return res, nil +} diff --git a/routers/hat/repo/commits.go b/routers/hat/repo/commits.go index fd3698f..ff93fe5 100644 --- a/routers/hat/repo/commits.go +++ b/routers/hat/repo/commits.go @@ -87,8 +87,10 @@ func GetAllCommitsSliceByTime(ctx *context.APIContext) { apiCommits := make([]*responseCommit, len(commits)) apiCommitsList := []responseCommit{} + stat := ctx.FormString("stat") == "" || ctx.FormBool("stat") + for i, commitPoniter := range commits { - apiCommits[i], err = toResponseCommit(ctx.Repo.Repository, gitRepo, commitPoniter, userCache) + apiCommits[i], err = toResponseCommit(ctx.Repo.Repository, gitRepo, commitPoniter, userCache, stat) if err != nil { ctx.Error(http.StatusInternalServerError, "ToCommit", err) return @@ -114,8 +116,8 @@ type responseCommit struct { 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, true) +func toResponseCommit(repo *repo.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user.User, stat bool) (*responseCommit, error) { + apiCommit, err := convert.ToCommit(repo, gitRepo, commit, userCache, stat) if err != nil { return nil, err } @@ -190,8 +192,10 @@ func GetFileAllCommits(ctx *context.APIContext) { userCache := make(map[string]*user_model.User) apiCommits := make([]*api.Commit, len(commits)) + + stat := ctx.FormString("stat") == "" || ctx.FormBool("stat") for i, commit := range commits { - apiCommits[i], err = convert.ToCommit(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, true) + apiCommits[i], err = hat_convert.ToCommitNotDiff(ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, stat) if err != nil { ctx.Error(http.StatusInternalServerError, "ToCommit", err) return