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 PGPSignature string `json:"pgp_signature"`
48 Committer object.Signature `json:"committer"`
49 Tree string `json:"tree"`
50 ChangedId string `json:"change_id"`
51 } `json:"commit"`
52 Stat struct {
53 FilesChanged int `json:"files_changed"`
54 Insertions int `json:"insertions"`
55 Deletions int `json:"deletions"`
56 } `json:"stat"`
57 Diff []Diff `json:"diff"`
58}
59
60type DiffTree struct {
61 Rev1 string `json:"rev1"`
62 Rev2 string `json:"rev2"`
63 Patch string `json:"patch"`
64 Diff []*gitdiff.File `json:"diff"`
65}
66
67func (d *NiceDiff) ChangedFiles() []string {
68 files := make([]string, len(d.Diff))
69
70 for i, f := range d.Diff {
71 if f.IsDelete {
72 files[i] = f.Name.Old
73 } else {
74 files[i] = f.Name.New
75 }
76 }
77
78 return files
79}