forked from
tangled.org/core
Monorepo for Tangled
1package types
2
3import (
4 "github.com/bluekeyes/go-gitdiff/gitdiff"
5 "github.com/go-git/go-git/v5/plumbing/object"
6)
7
8type TextFragment struct {
9 Header string `json:"comment"`
10 Lines []gitdiff.Line `json:"lines"`
11}
12
13type Diff struct {
14 Name struct {
15 Old string `json:"old"`
16 New string `json:"new"`
17 } `json:"name"`
18 TextFragments []gitdiff.TextFragment `json:"text_fragments"`
19 IsBinary bool `json:"is_binary"`
20 IsNew bool `json:"is_new"`
21 IsDelete bool `json:"is_delete"`
22 IsCopy bool `json:"is_copy"`
23 IsRename bool `json:"is_rename"`
24}
25
26type DiffStat struct {
27 Insertions int64
28 Deletions int64
29}
30
31func (d *Diff) Stats() DiffStat {
32 var stats DiffStat
33 for _, f := range d.TextFragments {
34 stats.Insertions += f.LinesAdded
35 stats.Deletions += f.LinesDeleted
36 }
37 return stats
38}
39
40// A nicer git diff representation.
41type NiceDiff struct {
42 Commit struct {
43 Message string `json:"message"`
44 Author object.Signature `json:"author"`
45 This string `json:"this"`
46 Parent string `json:"parent"`
47 PGPSignature string `json:"pgp_signature"`
48 Committer object.Signature `json:"committer"`
49 Tree string `json:"tree"`
50 ChangedId string `json:"change_id"`
51 } `json:"commit"`
52 Stat struct {
53 FilesChanged int `json:"files_changed"`
54 Insertions int `json:"insertions"`
55 Deletions int `json:"deletions"`
56 } `json:"stat"`
57 Diff []Diff `json:"diff"`
58}
59
60type DiffTree struct {
61 Rev1 string `json:"rev1"`
62 Rev2 string `json:"rev2"`
63 Patch string `json:"patch"`
64 Diff []*gitdiff.File `json:"diff"`
65}
66
67func (d *NiceDiff) ChangedFiles() []string {
68 files := make([]string, len(d.Diff))
69
70 for i, f := range d.Diff {
71 if f.IsDelete {
72 files[i] = f.Name.Old
73 } else {
74 files[i] = f.Name.New
75 }
76 }
77
78 return files
79}
80
81// ObjectCommitToNiceDiff is a compatibility function to convert a
82// commit object into a NiceDiff structure.
83func ObjectCommitToNiceDiff(c *object.Commit) NiceDiff {
84 var niceDiff NiceDiff
85
86 // set commit information
87 niceDiff.Commit.Message = c.Message
88 niceDiff.Commit.Author = c.Author
89 niceDiff.Commit.This = c.Hash.String()
90 niceDiff.Commit.Committer = c.Committer
91 niceDiff.Commit.Tree = c.TreeHash.String()
92 niceDiff.Commit.PGPSignature = c.PGPSignature
93
94 changeId, ok := c.ExtraHeaders["change-id"]
95 if ok {
96 niceDiff.Commit.ChangedId = string(changeId)
97 }
98
99 // set parent hash if available
100 if len(c.ParentHashes) > 0 {
101 niceDiff.Commit.Parent = c.ParentHashes[0].String()
102 }
103
104 // XXX: Stats and Diff fields are typically populated
105 // after fetching the actual diff information, which isn't
106 // directly available in the commit object itself.
107
108 return niceDiff
109}