2022-12-12 11:47:49 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bluekeyes/go-gitdiff/gitdiff"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TextFragment struct {
|
|
|
|
Header string
|
|
|
|
Lines []gitdiff.Line
|
|
|
|
}
|
|
|
|
|
|
|
|
type Diff struct {
|
|
|
|
Name struct {
|
|
|
|
Old string
|
|
|
|
New string
|
|
|
|
}
|
|
|
|
TextFragments []TextFragment
|
2022-12-19 06:06:50 +00:00
|
|
|
IsBinary bool
|
2022-12-19 12:37:13 +00:00
|
|
|
IsNew bool
|
|
|
|
IsDelete bool
|
2022-12-12 11:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A nicer git diff representation.
|
|
|
|
type NiceDiff struct {
|
|
|
|
Commit struct {
|
|
|
|
Message string
|
|
|
|
Author object.Signature
|
|
|
|
This string
|
|
|
|
Parent string
|
|
|
|
}
|
|
|
|
Stat struct {
|
|
|
|
FilesChanged int
|
|
|
|
Insertions int
|
|
|
|
Deletions int
|
|
|
|
}
|
|
|
|
Diff []Diff
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GitRepo) Diff() (*NiceDiff, error) {
|
|
|
|
c, err := g.r.CommitObject(g.h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("commit object: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-18 05:12:29 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-12-12 11:47:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String()))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nd := NiceDiff{}
|
|
|
|
nd.Commit.This = c.Hash.String()
|
2022-12-18 05:12:29 +00:00
|
|
|
|
|
|
|
if parent.Hash.IsZero() {
|
|
|
|
nd.Commit.Parent = ""
|
|
|
|
} else {
|
|
|
|
nd.Commit.Parent = parent.Hash.String()
|
|
|
|
}
|
2022-12-12 11:47:49 +00:00
|
|
|
nd.Commit.Author = c.Author
|
|
|
|
nd.Commit.Message = c.Message
|
|
|
|
|
|
|
|
for _, d := range diffs {
|
2022-12-17 17:05:48 +00:00
|
|
|
ndiff := Diff{}
|
2022-12-12 11:47:49 +00:00
|
|
|
ndiff.Name.New = d.NewName
|
|
|
|
ndiff.Name.Old = d.OldName
|
2022-12-19 06:06:50 +00:00
|
|
|
ndiff.IsBinary = d.IsBinary
|
2022-12-19 12:37:13 +00:00
|
|
|
ndiff.IsNew = d.IsNew
|
|
|
|
ndiff.IsDelete = d.IsDelete
|
2022-12-12 11:47:49 +00:00
|
|
|
|
|
|
|
for _, tf := range d.TextFragments {
|
|
|
|
ndiff.TextFragments = append(ndiff.TextFragments, TextFragment{
|
|
|
|
Header: tf.Header(),
|
|
|
|
Lines: tf.Lines,
|
|
|
|
})
|
|
|
|
for _, l := range tf.Lines {
|
|
|
|
switch l.Op {
|
|
|
|
case gitdiff.OpAdd:
|
|
|
|
nd.Stat.Insertions += 1
|
|
|
|
case gitdiff.OpDelete:
|
|
|
|
nd.Stat.Deletions += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nd.Diff = append(nd.Diff, ndiff)
|
|
|
|
}
|
|
|
|
|
|
|
|
nd.Stat.FilesChanged = len(diffs)
|
|
|
|
|
|
|
|
return &nd, nil
|
|
|
|
}
|