aboutsummaryrefslogtreecommitdiff
path: root/pkg/git
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-08-04 14:44:36 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-08-04 14:44:36 +0200
commit60004cfb97fe0eb64d1c8310e7c9caae96b8adbe (patch)
tree2267cf703f6870b05d68c73be7af1fc57190e770 /pkg/git
parentaa559681fcf8df993029370f28e0011315ca8761 (diff)
downloadcerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.tar.gz
cerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.tar.bz2
cerrado-60004cfb97fe0eb64d1c8310e7c9caae96b8adbe.zip
feat: Add diff view
Adds a very simple diff view for a commit
Diffstat (limited to 'pkg/git')
-rw-r--r--pkg/git/git.go39
1 files changed, 39 insertions, 0 deletions
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 {