forked from
tangled.org/core
Monorepo for Tangled
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}
33
34func (d *Diff) Stats() DiffStat {
35 var stats DiffStat
36 for _, f := range d.TextFragments {
37 stats.Insertions += f.LinesAdded
38 stats.Deletions += f.LinesDeleted
39 }
40 return stats
41}
42
43// A nicer git diff representation.
44type NiceDiff struct {
45 Commit Commit `json:"commit"`
46 Stat struct {
47 FilesChanged int `json:"files_changed"`
48 Insertions int `json:"insertions"`
49 Deletions int `json:"deletions"`
50 } `json:"stat"`
51 Diff []Diff `json:"diff"`
52}
53
54type DiffTree struct {
55 Rev1 string `json:"rev1"`
56 Rev2 string `json:"rev2"`
57 Patch string `json:"patch"`
58 Diff []*gitdiff.File `json:"diff"`
59}
60
61func (d *NiceDiff) ChangedFiles() []string {
62 files := make([]string, len(d.Diff))
63
64 for i, f := range d.Diff {
65 if f.IsDelete {
66 files[i] = f.Name.Old
67 } else {
68 files[i] = f.Name.New
69 }
70 }
71
72 return files
73}
74
75// used by html elements as a unique ID for hrefs
76func (d *Diff) Id() string {
77 if d.IsDelete {
78 return d.Name.Old
79 }
80 return d.Name.New
81}
82
83func (d *Diff) Split() *SplitDiff {
84 fragments := make([]SplitFragment, len(d.TextFragments))
85 for i, fragment := range d.TextFragments {
86 leftLines, rightLines := SeparateLines(&fragment)
87 fragments[i] = SplitFragment{
88 Header: fragment.Header(),
89 LeftLines: leftLines,
90 RightLines: rightLines,
91 }
92 }
93
94 return &SplitDiff{
95 Name: d.Id(),
96 TextFragments: fragments,
97 }
98}