-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smithy.go
251 lines (214 loc) · 5.11 KB
/
smithy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"os"
"path"
"sort"
"strings"
"time"
"github.com/alecthomas/chroma/formatters/html"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
)
type RepositoryWithName struct {
Name string
Path string
Repository *git.Repository
}
type RepositoryByName []RepositoryWithName
func (r RepositoryByName) Len() int { return len(r) }
func (r RepositoryByName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r RepositoryByName) Less(i, j int) bool {
res := strings.Compare(r[i].Name, r[j].Name)
return res < 0
}
type ReferenceByName []*plumbing.Reference
func (r ReferenceByName) Len() int { return len(r) }
func (r ReferenceByName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ReferenceByName) Less(i, j int) bool {
res := strings.Compare(r[i].Name().String(), r[j].Name().String())
return res < 0
}
type Smithy struct {
Root string
repos map[string]RepositoryWithName
template *template.Template
}
func NewSmithy(root string) Smithy {
return Smithy{
Root: root,
}
}
func (sc *Smithy) AddRepository(rwn RepositoryWithName) {
sc.repos[rwn.Name] = rwn
}
func (sc *Smithy) LoadAllRepositories() (err error) {
files, err := os.ReadDir(sc.Root)
if err != nil {
return
}
sc.repos = make(map[string]RepositoryWithName)
for _, f := range files {
repoPath := path.Join(sc.Root, f.Name())
r, err := git.PlainOpen(repoPath)
if err != nil {
continue
}
key := f.Name()
rwn := RepositoryWithName{
Name: f.Name(),
Repository: r,
Path: repoPath,
}
sc.repos[key] = rwn
}
return
}
func (sc *Smithy) GetRepositories() []RepositoryWithName {
var repos []RepositoryWithName
for _, repo := range sc.repos {
repos = append(repos, repo)
}
sort.Sort(RepositoryByName(repos))
return repos
}
func (sc *Smithy) FindRepo(slug string) (RepositoryWithName, bool) {
value, exists := sc.repos[slug]
return value, exists
}
type Commit struct {
Commit *object.Commit
Subject string
ShortHash string
}
func (c *Commit) CommitDate() string {
return c.Commit.Author.When.Format(time.DateTime)
}
func ReferenceCollector(it storer.ReferenceIter) ([]*plumbing.Reference, error) {
var refs []*plumbing.Reference
for {
b, err := it.Next()
if err == io.EOF {
break
}
if err != nil {
return refs, err
}
refs = append(refs, b)
}
sort.Sort(ReferenceByName(refs))
return refs, nil
}
func ListBranches(r *git.Repository) ([]*plumbing.Reference, error) {
it, err := r.Branches()
if err != nil {
return []*plumbing.Reference{}, err
}
return ReferenceCollector(it)
}
func ListTags(r *git.Repository) ([]*plumbing.Reference, error) {
it, err := r.Tags()
if err != nil {
return []*plumbing.Reference{}, err
}
return ReferenceCollector(it)
}
func GetReadmeFromCommit(commit *object.Commit) (*object.File, error) {
options := []string{
"readme",
"README",
"readme.md",
"README.md",
"readme.txt",
"README.txt",
"readme.markdown",
"README.markdown",
}
for _, opt := range options {
f, err := commit.File(opt)
if err == nil {
return f, nil
}
}
return nil, errors.New("no valid readme")
}
func FormatMarkdown(input string) string {
var buf bytes.Buffer
markdown := goldmark.New(
goldmark.WithExtensions(
highlighting.NewHighlighting(
highlighting.WithFormatOptions(
html.WithClasses(true),
),
),
),
)
if err := markdown.Convert([]byte(input), &buf); err != nil {
return input
}
return buf.String()
}
func FindMainBranch(repo *git.Repository) (string, *plumbing.Hash, error) {
branches, _ := ListBranches(repo)
if len(branches) == 0 {
return "", nil, errors.New("no branches found")
}
var branch string
for _, br := range branches {
if br.Name().Short() == "main" || br.Name().Short() == "master" {
branch = br.Name().Short()
break
}
}
if branch == "" {
branch = branches[0].Name().Short()
}
revision, err := repo.ResolveRevision(plumbing.Revision(branch))
return branch, revision, err
}
func GetChanges(commit *object.Commit) (object.Changes, error) {
var changes object.Changes
var parentTree *object.Tree
parent, err := commit.Parent(0)
if err == nil {
parentTree, err = parent.Tree()
if err != nil {
return changes, err
}
}
currentTree, err := commit.Tree()
if err != nil {
return changes, err
}
return object.DiffTree(parentTree, currentTree)
}
// PatchHTML returns an HTML representation of a patch
func PatchHTML(p object.Patch) string {
buf := bytes.NewBuffer(nil)
ue := NewUnifiedEncoder(buf, DefaultContextLines)
err := ue.Encode(p)
if err != nil {
fmt.Println("PatchHTML error")
}
return buf.String()
}
// FormatChanges spits out something similar to `git diff`
func FormatChanges(changes object.Changes) (string, error) {
var s []string
for _, change := range changes {
patch, err := change.Patch()
if err != nil {
return "", err
}
s = append(s, PatchHTML(*patch))
}
return strings.Join(s, "\n\n\n\n"), nil
}