Monorepo for Tangled
at op/vyrymqtwolsn 100 lines 2.2 kB view raw
1package types 2 3import ( 4 "github.com/bluekeyes/go-gitdiff/gitdiff" 5) 6 7type DiffOpts struct { 8 Split bool `json:"split"` 9} 10 11type TextFragment struct { 12 Header string `json:"comment"` 13 Lines []gitdiff.Line `json:"lines"` 14} 15 16type Diff struct { 17 Name struct { 18 Old string `json:"old"` 19 New string `json:"new"` 20 } `json:"name"` 21 TextFragments []gitdiff.TextFragment `json:"text_fragments"` 22 IsBinary bool `json:"is_binary"` 23 IsNew bool `json:"is_new"` 24 IsDelete bool `json:"is_delete"` 25 IsCopy bool `json:"is_copy"` 26 IsRename bool `json:"is_rename"` 27} 28 29type DiffStat struct { 30 Insertions int64 31 Deletions int64 32 FilesChanged int 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 stats.FilesChanged = len(d.TextFragments) 42 return stats 43} 44 45// A nicer git diff representation. 46type NiceDiff struct { 47 Commit Commit `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} 76 77// used by html elements as a unique ID for hrefs 78func (d Diff) Id() string { 79 if d.IsDelete { 80 return d.Name.Old 81 } 82 return d.Name.New 83} 84 85func (d Diff) Split() *SplitDiff { 86 fragments := make([]SplitFragment, len(d.TextFragments)) 87 for i, fragment := range d.TextFragments { 88 leftLines, rightLines := SeparateLines(&fragment) 89 fragments[i] = SplitFragment{ 90 Header: fragment.Header(), 91 LeftLines: leftLines, 92 RightLines: rightLines, 93 } 94 } 95 96 return &SplitDiff{ 97 Name: d.Id(), 98 TextFragments: fragments, 99 } 100}