diff options
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r-- | pkg/git/git.go | 39 |
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 { |