From 60004cfb97fe0eb64d1c8310e7c9caae96b8adbe Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 4 Aug 2024 14:44:36 +0200 Subject: feat: Add diff view Adds a very simple diff view for a commit --- pkg/git/git.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'pkg/git') diff --git a/pkg/git/git.go b/pkg/git/git.go index a9c42ce..6221e33 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -179,6 +179,45 @@ func (g *GitRepository) Branches() ([]*plumbing.Reference, error) { return branches, nil } +func (g *GitRepository) Diff() (string, error) { + err := g.validateRef() + if err != nil { + return "", err + } + + c, err := g.repository.CommitObject(g.ref) + if err != nil { + return "", err + } + + commitTree, err := c.Tree() + if err != nil { + return "", err + } + + patch := &object.Patch{} + parentTree := &object.Tree{} + if c.NumParents() != 0 { + parent, err := c.Parents().Next() + if err == nil { + parentTree, err = parent.Tree() + if err == nil { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return "", err + } + } + } + } else { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return "", err + } + } + + return patch.String(), nil +} + func (g *GitRepository) Tree(path string) (*object.Tree, error) { err := g.validateRef() if err != nil { -- cgit v1.2.3