summaryrefslogtreecommitdiffstats
path: root/releaser/git.go
blob: 7d2d43e2aa871f7bfa0bc55823c901ece79b2056 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2017-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package releaser

import (
	"fmt"
	"os/exec"
	"regexp"
	"sort"
	"strconv"
	"strings"
)

var issueRe = regexp.MustCompile(`(?i)[Updates?|Closes?|Fix.*|See] #(\d+)`)

const (
	notesChanges    = "notesChanges"
	templateChanges = "templateChanges"
	coreChanges     = "coreChanges"
	outChanges      = "outChanges"
	otherChanges    = "otherChanges"
)

type changeLog struct {
	Version      string
	Enhancements map[string]gitInfos
	Fixes        map[string]gitInfos
	Notes        gitInfos
	All          gitInfos
	Docs         gitInfos

	// Overall stats
	Repo             *gitHubRepo
	ContributorCount int
	ThemeCount       int
}

func newChangeLog(infos, docInfos gitInfos) *changeLog {
	return &changeLog{
		Enhancements: make(map[string]gitInfos),
		Fixes:        make(map[string]gitInfos),
		All:          infos,
		Docs:         docInfos,
	}
}

func (l *changeLog) addGitInfo(isFix bool, info gitInfo, category string) {
	var (
		infos   gitInfos
		found   bool
		segment map[string]gitInfos
	)

	if category == notesChanges {
		l.Notes = append(l.Notes, info)
		return
	} else if isFix {
		segment = l.Fixes
	} else {
		segment = l.Enhancements
	}

	infos, found = segment[category]
	if !found {
		infos = gitInfos{}
	}

	infos = append(infos, info)
	segment[category] = infos
}

func gitInfosToChangeLog(infos, docInfos gitInfos) *changeLog {
	log := newChangeLog(infos, docInfos)
	for _, info := range infos {
		los := strings.ToLower(info.Subject)
		isFix := strings.Contains(los, "fix")
		category := otherChanges

		// TODO(bep) improve
		if regexp.MustCompile("(?i)deprecate").MatchString(los) {
			category = notesChanges
		} else if regexp.MustCompile("(?i)tpl|tplimpl:|layout").MatchString(los) {
			category = templateChanges
		} else if regexp.MustCompile("(?i)hugolib:").MatchString(los) {
			category = coreChanges
		} else if regexp.MustCompile("(?i)out(put)?:|media:|Output|Media").MatchString(los) {
			category = outChanges
		}

		// Trim package prefix.
		colonIdx := strings.Index(info.Subject, ":")
		if colonIdx != -1 && colonIdx < (len(info.Subject)/2) {
			info.Subject = info.Subject[colonIdx+1:]
		}

		info.Subject = strings.TrimSpace(info.Subject)

		log.addGitInfo(isFix, info, category)
	}

	return log
}

type gitInfo struct {
	Hash    string
	Author  string
	Subject string
	Body    string

	GitHubCommit *gitHubCommit
}

func (g gitInfo) Issues() []int {
	return extractIssues(g.Body)
}

func (g gitInfo) AuthorID() string {
	if g.GitHubCommit != nil {
		return g.GitHubCommit.Author.Login
	}
	return g.Author
}

func extractIssues(body string) []int {
	var i []int
	m := issueRe.FindAllStringSubmatch(body, -1)
	for _, mm := range m {
		issueID, err := strconv.Atoi(mm[1])
		if err != nil {
			continue
		}
		i = append(i, issueID)
	}
	return i
}

type gitInfos []gitInfo

func git(args ...string) (string, error) {
	cmd := exec.Command("git", args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("git failed: %q: %q (%q)", err, out, args)
	}
	return string(out), nil
}

func getGitInfos(tag, repo, repoPath string, remote bool) (gitInfos, error) {
	return getGitInfosBefore("HEAD", tag, repo, repoPath, remote)
}

type countribCount struct {
	Author       string
	GitHubAuthor gitHubAuthor
	Count        int
}

func (c countribCount) AuthorLink() string {
	if c.GitHubAuthor.HTMLURL != "" {
		return fmt.Sprintf("[@%s](%s)", c.GitHubAuthor.Login, c.GitHubAuthor.HTMLURL)
	}

	if !strings.Contains(c.Author, "@") {
		return c.Author
	}

	return c.Author[:strings.Index(c.Author, "@")]
}

type contribCounts []countribCount

func (c contribCounts) Less(i, j int) bool { return c[i].Count > c[j].Count }
func (c contribCounts) Len() int           { return len(c) }
func (c contribCounts) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }

func (g gitInfos) ContribCountPerAuthor() contribCounts {
	var c contribCounts

	counters := make(map[string]countribCount)

	for _, gi := range g {
		authorID := gi.AuthorID()
		if count, ok := counters[authorID]; ok {
			count.Count = count.Count + 1
			counters[authorID] = count
		} else {
			var ghA gitHubAuthor
			if gi.GitHubCommit != nil {
				ghA = gi.GitHubCommit.Author
			}
			authorCount := countribCount{Count: 1, Author: gi.Author, GitHubAuthor: ghA}
			counters[authorID] = authorCount
		}
	}

	for _, v := range </