1package types
2
3import (
4 "github.com/bluekeyes/go-gitdiff/gitdiff"
5 "github.com/go-git/go-git/v5/plumbing/object"
6)
7
8type TextFragment struct {
9 Header string `json:"comment"`
10 Lines []gitdiff.Line `json:"lines"`
11}
12
13type Diff struct {
14 Name struct {
15 Old string `json:"old"`
16 New string `json:"new"`
17 } `json:"name"`
18 TextFragments []gitdiff.TextFragment `json:"text_fragments"`
19 IsBinary bool `json:"is_binary"`
20 IsNew bool `json:"is_new"`
21 IsDelete bool `json:"is_delete"`
22 IsCopy bool `json:"is_copy"`
23 IsRename bool `json:"is_rename"`
24}
25
26type DiffStat struct {
27 Insertions int64
28 Deletions int64
29}
30
31func (d *Diff) Stats() DiffStat {
32 var stats DiffStat
33 for _, f := range d.TextFragments {
34 stats.Insertions += f.LinesAdded
35 stats.Deletions += f.LinesDeleted
36 }
37 return stats
38}
39
40// A nicer git diff representation.
41type NiceDiff struct {
42 Commit struct {
43 Message string `json:"message"`
44 Author object.Signature `json:"author"`
45 This string `json:"this"`
46 Parent string `json:"parent"`
47 } `json:"commit"`
48 Stat struct {
49 FilesChanged int `json:"files_changed"`
50 Insertions int `json:"insertions"`
51 Deletions int `json:"deletions"`
52 } `json:"stat"`
53 Diff []Diff `json:"diff"`
54}
55
56type DiffTree struct {
57 Rev1 string `json:"rev1"`
58 Rev2 string `json:"rev2"`
59 Patch string `json:"patch"`
60 Diff []*gitdiff.File `json:"diff"`
61}
62
63func (d *NiceDiff) ChangedFiles() []string {
64 files := make([]string, len(d.Diff))
65
66 for i, f := range d.Diff {
67 if f.IsDelete {
68 files[i] = f.Name.Old
69 } else {
70 files[i] = f.Name.New
71 }
72 }
73
74 return files
75}