at master 2.5 kB view raw
1package types 2 3import ( 4 "github.com/bluekeyes/go-gitdiff/gitdiff" 5 "github.com/go-git/go-git/v5/plumbing/object" 6) 7 8type DiffOpts struct { 9 Split bool `json:"split"` 10} 11 12type TextFragment struct { 13 Header string `json:"comment"` 14 Lines []gitdiff.Line `json:"lines"` 15} 16 17type Diff struct { 18 Name struct { 19 Old string `json:"old"` 20 New string `json:"new"` 21 } `json:"name"` 22 TextFragments []gitdiff.TextFragment `json:"text_fragments"` 23 IsBinary bool `json:"is_binary"` 24 IsNew bool `json:"is_new"` 25 IsDelete bool `json:"is_delete"` 26 IsCopy bool `json:"is_copy"` 27 IsRename bool `json:"is_rename"` 28} 29 30type DiffStat struct { 31 Insertions int64 32 Deletions int64 33} 34 35func (d *Diff) Stats() DiffStat { 36 var stats DiffStat 37 for _, f := range d.TextFragments { 38 stats.Insertions += f.LinesAdded 39 stats.Deletions += f.LinesDeleted 40 } 41 return stats 42} 43 44// A nicer git diff representation. 45type NiceDiff struct { 46 Commit struct { 47 Message string `json:"message"` 48 Author object.Signature `json:"author"` 49 This string `json:"this"` 50 Parent string `json:"parent"` 51 PGPSignature string `json:"pgp_signature"` 52 Committer object.Signature `json:"committer"` 53 Tree string `json:"tree"` 54 ChangedId string `json:"change_id"` 55 } `json:"commit"` 56 Stat struct { 57 FilesChanged int `json:"files_changed"` 58 Insertions int `json:"insertions"` 59 Deletions int `json:"deletions"` 60 } `json:"stat"` 61 Diff []Diff `json:"diff"` 62} 63 64type DiffTree struct { 65 Rev1 string `json:"rev1"` 66 Rev2 string `json:"rev2"` 67 Patch string `json:"patch"` 68 Diff []*gitdiff.File `json:"diff"` 69} 70 71func (d *NiceDiff) ChangedFiles() []string { 72 files := make([]string, len(d.Diff)) 73 74 for i, f := range d.Diff { 75 if f.IsDelete { 76 files[i] = f.Name.Old 77 } else { 78 files[i] = f.Name.New 79 } 80 } 81 82 return files 83} 84 85// used by html elements as a unique ID for hrefs 86func (d *Diff) Id() string { 87 return d.Name.New 88} 89 90func (d *Diff) Split() *SplitDiff { 91 fragments := make([]SplitFragment, len(d.TextFragments)) 92 for i, fragment := range d.TextFragments { 93 leftLines, rightLines := SeparateLines(&fragment) 94 fragments[i] = SplitFragment{ 95 Header: fragment.Header(), 96 LeftLines: leftLines, 97 RightLines: rightLines, 98 } 99 } 100 101 return &SplitDiff{ 102 Name: d.Id(), 103 TextFragments: fragments, 104 } 105}