diff options
author | 6543 <6543@obermui.de> | 2020-09-18 14:09:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-18 20:09:26 +0800 |
commit | 1418288734b430330023e4e17747175e4fea2f41 (patch) | |
tree | 270f7bdbb6aee5f732a6d1295ce3d021d3e98874 /modules/convert/git_commit.go | |
parent | 5995326d51ab42014c6d77d3313233641c258318 (diff) | |
download | gitea-1418288734b430330023e4e17747175e4fea2f41.tar.gz gitea-1418288734b430330023e4e17747175e4fea2f41.zip |
Refactor: move Commit To APIFormat Code & Lot of StopWatch related things (#12729)
* move GitCommit to APIFormat convertion into convert package
* rename Commit convert functions
* move stopwatch to api convertion into convert package & rm unused code & extend test
* fix compare time
* Gitea not Gogs ;)
Diffstat (limited to 'modules/convert/git_commit.go')
-rw-r--r-- | modules/convert/git_commit.go | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go new file mode 100644 index 0000000000..55cbb3af80 --- /dev/null +++ b/modules/convert/git_commit.go @@ -0,0 +1,165 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "time" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" +) + +// ToCommitUser convert a git.Signature to an api.CommitUser +func ToCommitUser(sig *git.Signature) *api.CommitUser { + return &api.CommitUser{ + Identity: api.Identity{ + Name: sig.Name, + Email: sig.Email, + }, + Date: sig.When.UTC().Format(time.RFC3339), + } +} + +// ToCommitMeta convert a git.Tag to an api.CommitMeta +func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta { + return &api.CommitMeta{ + SHA: tag.Object.String(), + URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()), + } +} + +// ToPayloadCommit convert a git.Commit to api.PayloadCommit +func ToPayloadCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit { + authorUsername := "" + if author, err := models.GetUserByEmail(c.Author.Email); err == nil { + authorUsername = author.Name + } else if !models.IsErrUserNotExist(err) { + log.Error("GetUserByEmail: %v", err) + } + + committerUsername := "" + if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil { + committerUsername = committer.Name + } else if !models.IsErrUserNotExist(err) { + log.Error("GetUserByEmail: %v", err) + } + + return &api.PayloadCommit{ + ID: c.ID.String(), + Message: c.Message(), + URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()), + Author: &api.PayloadUser{ + Name: c.Author.Name, + Email: c.Author.Email, + UserName: authorUsername, + }, + Committer: &api.PayloadUser{ + Name: c.Committer.Name, + Email: c.Committer.Email, + UserName: committerUsername, + }, + Timestamp: c.Author.When, + Verification: ToVerification(c), + } +} + +// ToCommit convert a git.Commit to api.Commit +func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string]*models.User) (*api.Commit, error) { + + var apiAuthor, apiCommitter *api.User + + // Retrieve author and committer information + + var cacheAuthor *models.User + var ok bool + if userCache == nil { + cacheAuthor = (*models.User)(nil) + ok = false + } else { + cacheAuthor, ok = userCache[commit.Author.Email] + } + + if ok { + apiAuthor = cacheAuthor.APIFormat() + } else { + author, err := models.GetUserByEmail(commit.Author.Email) + if err != nil && !models.IsErrUserNotExist(err) { + return nil, err + } else if err == nil { + apiAuthor = author.APIFormat() + if userCache != nil { + userCache[commit.Author.Email] = author + } + } + } + + var cacheCommitter *models.User + if userCache == nil { + cacheCommitter = (*models.User)(nil) + ok = false + } else { + cacheCommitter, ok = userCache[commit.Committer.Email] + } + + if ok { + apiCommitter = cacheCommitter.APIFormat() + } else { + committer, err := models.GetUserByEmail(commit.Committer.Email) + if err != nil && !models.IsErrUserNotExist(err) { + return nil, err + } else if err == nil { + apiCommitter = committer.APIFormat() + 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/" + sha.String(), + SHA: sha.String(), + } + } + + return &api.Commit{ + CommitMeta: &api.CommitMeta{ + URL: repo.APIURL() + "/git/commits/" + commit.ID.String(), + SHA: commit.ID.String(), + }, + HTMLURL: repo.HTMLURL() + "/commit/" + commit.ID.String(), + RepoCommit: &api.RepoCommit{ + URL: repo.APIURL() + "/git/commits/" + commit.ID.String(), + Author: &api.CommitUser{ + Identity: api.Identity{ + Name: commit.Committer.Name, + Email: commit.Committer.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/" + commit.ID.String(), + SHA: commit.ID.String(), + }, + }, + Author: apiAuthor, + Committer: apiCommitter, + Parents: apiParents, + }, nil +} |