-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
295 lines (265 loc) · 6.33 KB
/
main.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
// Changelog is the root data structure available to the output template.
type Changelog struct {
Repo string
Commits chan DecoratedCommit
}
// DecoratedCommit is a go-git Commit struct with additional metadata.
type DecoratedCommit struct {
object.Commit
HashHexDigest string
Tags []string
}
const defaultTemplate = `# {{ .Repo }}
{{ range .Commits }}
{{ if .Tags }}
## {{ range .Tags }}{{ . }} {{ end }}
{{ .Author.When }}
{{ else }}{{ end }}
#### ` + "`{{ slice .HashHexDigest 0 7 }}`" + ` {{ .Message }}{{ end }}`
var (
onlyTag, serve, templatePath string
maxRevs int
)
func init() {
flag.StringVar(&serve, "http", "", "serves over HTTP at the given address")
flag.StringVar(&onlyTag, "tag", "", "show the changelog for only the given tag")
flag.StringVar(&templatePath, "template", "", "load the output template from the given file")
flag.IntVar(&maxRevs, "max-revs", 0, "max versions to show before exiting")
}
func collectTags(repo *git.Repository) (map[plumbing.Hash][]string, error) {
tagsByCommit := make(map[plumbing.Hash][]string)
refIter, err := repo.Tags()
if err != nil {
return nil, err
}
var (
ref *plumbing.Reference
commit *object.Commit
commitHash plumbing.Hash
)
for {
ref, err = refIter.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// Check if the tag is annotated or lightweight.
tag, err := repo.TagObject(ref.Hash())
switch err {
case nil:
// This is an annotated tag.
commit, err = tag.Commit()
if err == object.ErrUnsupportedObject {
// This tag doesn't point to a commit.
continue
}
commitHash = commit.Hash
case plumbing.ErrObjectNotFound:
// This is a lightweight tag and directly points to a commit.
commitHash = ref.Hash()
default:
return nil, err
}
tags := tagsByCommit[commitHash]
cleanTagName := strings.TrimPrefix(ref.Name().String(), "refs/tags/")
tagsByCommit[commitHash] = append(tags, cleanTagName)
}
return tagsByCommit, nil
}
func clone(repoURL, tag string) (repo *git.Repository, destDir string, err error) {
destDir, err = ioutil.TempDir("", "changelog_")
if err != nil {
return
}
args := []string{
"clone",
"--quiet",
"--bare",
"--single-branch",
}
if tag != "" {
args = append(args, "--branch", tag)
}
args = append(args, repoURL, destDir)
cmd := exec.Command("git", args...)
buf := &bytes.Buffer{}
cmd.Stderr = buf
if err = cmd.Run(); err != nil {
err = fmt.Errorf("%s: %s", err, buf)
return
}
repo, err = git.PlainOpen(destDir)
return
}
func writeChangelog(
repoPath,
tag string,
maxRevs int,
tmpl *template.Template,
out io.Writer,
) (err error) {
repo, repoDir, err := clone(repoPath, tag)
defer os.RemoveAll(repoDir)
if err != nil {
return err
}
if tag != "" && maxRevs == 0 {
maxRevs = 1
}
tagsByCommit, err := collectTags(repo)
if err != nil {
return err
}
commitIter, err := repo.Log(&git.LogOptions{})
defer commitIter.Close()
if err != nil {
return err
}
cl := Changelog{
Repo: repoPath,
Commits: make(chan DecoratedCommit, 32),
}
goErr := make(chan error)
go func() {
goErr <- tmpl.Execute(out, cl)
close(goErr)
}()
numTaggedCommits := 0
for {
commit, err := commitIter.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
commitTags, hasTags := tagsByCommit[commit.Hash]
decoratedCommit := DecoratedCommit{
Commit: *commit,
HashHexDigest: hex.EncodeToString(commit.Hash[:]),
Tags: commitTags,
}
if hasTags {
numTaggedCommits++
}
if maxRevs > 0 && numTaggedCommits > maxRevs {
break
}
select {
case cl.Commits <- decoratedCommit:
case err := <-goErr:
// Always fail here, because if the templating returned before the
// commit iterator, we have a problem.
return err
}
}
close(cl.Commits)
return <-goErr
}
func parseRequest(req *http.Request, route string) (repoURL, tag string, maxRevs int, err error) {
repoPath := strings.TrimPrefix(req.URL.Path, route)
var cloneScheme string
parts := strings.SplitN(repoPath, "/", 2)
if len(parts) == 2 {
cloneScheme, repoPath = parts[0], parts[1]
} else {
err = fmt.Errorf("invalid request: %v", req)
return
}
parts = strings.Split(repoPath, "@")
if len(parts) == 2 {
repoPath, tag = parts[0], parts[1]
} else if len(parts) > 2 {
err = fmt.Errorf("invalid request: %v", req)
return
}
maxRevsStr := req.URL.Query().Get("maxRevs")
if maxRevsStr != "" {
maxRevs, err = strconv.Atoi(maxRevsStr)
}
if cloneScheme == "ssh" {
repoPath = "git@" + repoPath
}
repoURL = fmt.Sprintf("%s://%s", cloneScheme, repoPath)
return
}
// SplitLines splits the input on newline characters.
func SplitLines(s string) []string {
return strings.Split(s, "\n")
}
func main() {
check := func(err error) {
if err != nil {
log.Fatal(err)
}
}
flag.Parse()
var (
err error
)
tmpl := template.New("changelog").Funcs(template.FuncMap{"SplitLines": SplitLines})
if templatePath == "" {
tmpl, err = template.New("changelog").Parse(defaultTemplate)
} else {
templateContents, err := ioutil.ReadFile(templatePath)
check(err)
tmpl, err = tmpl.Parse(string(templateContents))
}
check(err)
if serve == "" {
repoPath := flag.Arg(0)
if repoPath == "" {
flag.Usage()
fmt.Println("No repository path specified")
os.Exit(1)
}
check(writeChangelog(repoPath, onlyTag, maxRevs, tmpl, os.Stdout))
} else {
primaryRoute := "/"
http.HandleFunc(primaryRoute, func(w http.ResponseWriter, req *http.Request) {
repoURL, tag, maxRevs, err := parseRequest(req, primaryRoute)
if err != nil {
fmt.Fprintln(w, err)
log.Println(err)
return
}
log.Printf(
"%#v -> repo: %#v tag: %#v maxRevs: %d\n",
req.URL.String(),
repoURL,
tag,
maxRevs,
)
err = writeChangelog(repoURL, tag, maxRevs, tmpl, w)
if err != nil {
fmt.Fprintln(w, err)
log.Println(err)
}
})
// Ignore requests for a site icon.
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {})
log.Printf("listening at %s\n", serve)
check(http.ListenAndServe(serve, nil))
}
}