Monorepo for Tangled
0

Configure Feed

Select the types of activity you want to include in your feed.

at master 3.0 kB View raw
1package types 2 3import ( 4 "net/url" 5 6 "github.com/bluekeyes/go-gitdiff/gitdiff" 7 "tangled.org/core/appview/filetree" 8) 9 10type DiffOpts struct { 11 Split bool `json:"split"` 12 RefreshUrl string `json:"refresh_url,omitempty"` 13 Target string `json:"target,omitempty"` 14 Field string `json:"field,omitempty"` 15} 16 17func (d DiffOpts) Encode() string { 18 values := make(url.Values) 19 if d.Split { 20 values.Set("diff", "split") 21 } else { 22 values.Set("diff", "unified") 23 } 24 return values.Encode() 25} 26 27// A nicer git diff representation. 28type NiceDiff struct { 29 Commit Commit `json:"commit"` 30 Stat DiffStat `json:"stat"` 31 Diff []Diff `json:"diff"` 32} 33 34type Diff struct { 35 Name struct { 36 Old string `json:"old"` 37 New string `json:"new"` 38 } `json:"name"` 39 TextFragments []gitdiff.TextFragment `json:"text_fragments"` 40 IsBinary bool `json:"is_binary"` 41 IsNew bool `json:"is_new"` 42 IsDelete bool `json:"is_delete"` 43 IsCopy bool `json:"is_copy"` 44 IsRename bool `json:"is_rename"` 45} 46 47func (d Diff) Stats() DiffFileStat { 48 var stats DiffFileStat 49 for _, f := range d.TextFragments { 50 stats.Insertions += f.LinesAdded 51 stats.Deletions += f.LinesDeleted 52 } 53 return stats 54} 55 56type DiffStat struct { 57 Insertions int64 `json:"insertions"` 58 Deletions int64 `json:"deletions"` 59 FilesChanged int `json:"files_changed"` 60} 61 62type DiffFileStat struct { 63 Insertions int64 64 Deletions int64 65} 66 67type DiffTree struct { 68 Rev1 string `json:"rev1"` 69 Rev2 string `json:"rev2"` 70 Patch string `json:"patch"` 71 Diff []*gitdiff.File `json:"diff"` 72} 73 74type DiffFileName struct { 75 Old string 76 New string 77} 78 79func (d NiceDiff) ChangedFiles() []DiffFileRenderer { 80 drs := make([]DiffFileRenderer, len(d.Diff)) 81 for i, s := range d.Diff { 82 drs[i] = s 83 } 84 return drs 85} 86 87func (d NiceDiff) FileTree() *filetree.FileTreeNode { 88 fs := make([]string, len(d.Diff)) 89 for i, s := range d.Diff { 90 fs[i] = s.Id() 91 } 92 return filetree.FileTree(fs) 93} 94 95func (d NiceDiff) Stats() DiffStat { 96 return d.Stat 97} 98 99func (d Diff) Id() string { 100 if d.IsDelete { 101 return d.Name.Old 102 } 103 return d.Name.New 104} 105 106func (d Diff) Names() DiffFileName { 107 var n DiffFileName 108 if d.IsDelete { 109 n.Old = d.Name.Old 110 return n 111 } else if d.IsCopy || d.IsRename { 112 n.Old = d.Name.Old 113 n.New = d.Name.New 114 return n 115 } else { 116 n.New = d.Name.New 117 return n 118 } 119} 120 121func (d Diff) CanRender() string { 122 if d.IsBinary { 123 return "This is a binary file and will not be displayed." 124 } 125 126 return "" 127} 128 129func (d Diff) Split() SplitDiff { 130 fragments := make([]SplitFragment, len(d.TextFragments)) 131 for i, fragment := range d.TextFragments { 132 leftLines, rightLines := SeparateLines(&fragment) 133 fragments[i] = SplitFragment{ 134 Header: fragment.Header(), 135 LeftLines: leftLines, 136 RightLines: rightLines, 137 } 138 } 139 140 return SplitDiff{ 141 Name: d.Id(), 142 TextFragments: fragments, 143 } 144}