summaryrefslogtreecommitdiffstats
path: root/git/diff.go
diff options
context:
space:
mode:
authorLibravatar Anirudh Oppiliappan <x@icyphox.sh>2022-12-18 10:42:29 +0530
committerLibravatar Anirudh Oppiliappan <x@icyphox.sh>2022-12-18 10:42:53 +0530
commit60298a69538ccdda417613a09e5acbd917bfc53a (patch)
tree799e52039fca1d7a15138d315b7b1b60d47ba46c /git/diff.go
parente4d12fc667c9830dd5f08f827b2a70adff58a09b (diff)
downloadlegit-60298a69538ccdda417613a09e5acbd917bfc53a.tar.gz
legit-60298a69538ccdda417613a09e5acbd917bfc53a.tar.bz2
legit-60298a69538ccdda417613a09e5acbd917bfc53a.zip
git: rework diff to fetch initial commit
Diffstat (limited to 'git/diff.go')
-rw-r--r--git/diff.go40
1 files changed, 27 insertions, 13 deletions
diff --git a/git/diff.go b/git/diff.go
index 4b42b9a..38c00f2 100644
--- a/git/diff.go
+++ b/git/diff.go
@@ -44,19 +44,28 @@ func (g *GitRepo) Diff() (*NiceDiff, error) {
return nil, fmt.Errorf("commit object: %w", err)
}
- var parent *object.Commit
- if len(c.ParentHashes) > 0 {
- parent, err = c.Parent(0)
- if err != nil {
- return nil, fmt.Errorf("getting parent: %w", err)
+ patch := &object.Patch{}
+ commitTree, err := c.Tree()
+ parent := &object.Commit{}
+ if err == nil {
+ 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 nil, fmt.Errorf("patch: %w", err)
+ }
+ }
+ }
+ } else {
+ patch, err = parentTree.Patch(commitTree)
+ if err != nil {
+ return nil, fmt.Errorf("patch: %w", err)
+ }
}
- } else {
- parent = c
- }
-
- patch, err := parent.Patch(c)
- if err != nil {
- return nil, fmt.Errorf("patch: %w", err)
}
diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String()))
@@ -66,7 +75,12 @@ func (g *GitRepo) Diff() (*NiceDiff, error) {
nd := NiceDiff{}
nd.Commit.This = c.Hash.String()
- nd.Commit.Parent = parent.Hash.String()
+
+ if parent.Hash.IsZero() {
+ nd.Commit.Parent = ""
+ } else {
+ nd.Commit.Parent = parent.Hash.String()
+ }
nd.Commit.Author = c.Author
nd.Commit.Message = c.Message