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 return d.Name.New
78}
79
80func (d *Diff) Split() *SplitDiff {
81 fragments := make([]SplitFragment, len(d.TextFragments))
82 for i, fragment := range d.TextFragments {
83 leftLines, rightLines := SeparateLines(&fragment)
84 fragments[i] = SplitFragment{
85 Header: fragment.Header(),
86 LeftLines: leftLines,
87 RightLines: rightLines,
88 }
89 }
90
91 return &SplitDiff{
92 Name: d.Id(),
93 TextFragments: fragments,
94 }
95}