diff options
author | John Cai <jcai@gitlab.com> | 2018-12-19 23:27:44 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2019-01-04 10:10:43 +0300 |
commit | 71818d640829c6a13508c4e1cb1d347cc1196c9b (patch) | |
tree | ad2a53db2e11ff8f344803a2157adf00d504fb2f | |
parent | c3dfc0b4d5cdcc24b15c05c5f32aa6f549991d0c (diff) |
adding a GetCommitMessage method by splitting the helper method parseRawCommit into sub-helper functions
-rw-r--r-- | internal/git/log/commit.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/internal/git/log/commit.go b/internal/git/log/commit.go index 0ded22414..d1da9836d 100644 --- a/internal/git/log/commit.go +++ b/internal/git/log/commit.go @@ -50,7 +50,40 @@ func GetCommitCatfile(c *catfile.Batch, revision string) (*gitalypb.GitCommit, e return parseRawCommit(raw, info) } +// GetCommitMessage looks up a commit message and returns it in its entirety. +func GetCommitMessage(ctx context.Context, repo *gitalypb.Repository, revision string) ([]byte, error) { + c, err := catfile.New(ctx, repo) + if err != nil { + return nil, err + } + info, err := c.Info(revision + "^{commit}") + if err != nil { + if catfile.IsNotFound(err) { + return nil, nil + } + + return nil, err + } + + r, err := c.Commit(info.Oid) + if err != nil { + return nil, err + } + + raw, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + _, body := splitRawCommit(raw) + return body, nil +} + func parseRawCommit(raw []byte, info *catfile.ObjectInfo) (*gitalypb.GitCommit, error) { + header, body := splitRawCommit(raw) + return buildCommit(header, body, info) +} + +func splitRawCommit(raw []byte) ([]byte, []byte) { split := bytes.SplitN(raw, []byte("\n\n"), 2) header := split[0] @@ -58,7 +91,10 @@ func parseRawCommit(raw []byte, info *catfile.ObjectInfo) (*gitalypb.GitCommit, if len(split) == 2 { body = split[1] } + return header, body +} +func buildCommit(header, body []byte, info *catfile.ObjectInfo) (*gitalypb.GitCommit, error) { commit := &gitalypb.GitCommit{ Id: info.Oid, Body: body, |