summaryrefslogtreecommitdiffstats
path: root/src/createchangelog/main.go
blob: 1706b5e2be74f6c3e359251d05f193c2b376d6e4 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"go/doc"
	"html/template"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"sort"
	"strings"

	"github.com/hashicorp/go-version"
	"github.com/eidolon/wordwrap"
)

type commandWithCapturedOutput struct {
	cmd    *exec.Cmd
	stdout bytes.Buffer
	stderr bytes.Buffer
}

func newCommandWithCapturedOutput(cmd *exec.Cmd) *commandWithCapturedOutput {
	result := &commandWithCapturedOutput{}
	result.cmd = cmd
	result.cmd.Stdout = &result.stdout
	result.cmd.Stderr = &result.stderr
	return result
}

func (c *commandWithCapturedOutput) run() (string, error) {
	err := c.cmd.Run()
	if err != nil {
		return "", fmt.Errorf("Error running %s: %s\nStdout: %s\nStderr: %s", strings.Join(c.cmd.Args, " "), err, c.stdout.String(), c.stderr.String())
	}
	return c.stdout.String(), err
}

func (c *commandWithCapturedOutput) runWithSpaceTrimmed() (string, error) {
	output, err := c.run()
	return strings.TrimSpace(output), err
}

func (c *commandWithCapturedOutput) runWithLines() ([]string, error) {
	output, err := c.run()
	if err != nil {
		return nil, err
	}
	var lines []string

	scanner := bufio.NewScanner(bytes.NewBufferString(output))
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("Error parsing line output: %s", err)
	}

	return lines, err
}

type repository string

func (repo repository) gitCommand(command string, parameters ...string) *commandWithCapturedOutput {
	parameters = append([]string{"--git-dir=" + string(repo), command}, parameters...)
	return newCommandWithCapturedOutput(exec.Command("git", parameters...))
}

func (repo repository) tags() ([]string, error) {
	return repo.gitCommand("tag", "-l").runWithLines()
}

func (repo repository) revList(revisionRange string) ([]string, error) {
	return repo.gitCommand("rev-list", revisionRange).runWithLines()
}

func (repo repository) catFile(objectType string, sha1 string) (string, error) {
	return repo.gitCommand("cat-file", objectType, sha1).run()
}

func detectCurrentQtVersionFromQMakeConf() (*version.Version, error) {
	qmakeConfFile, err := os.Open(".qmake.conf")
	if err != nil {
		defer qmakeConfFile.Close()
		return nil, fmt.Errorf("Error opening .qmake.conf - are you in the right directory?")
	}

	moduleVersionRegExp := regexp.MustCompile(`\s*MODULE_VERSION\s*=\s*([0-9\.]*).*`)

	scanner := bufio.NewScanner(qmakeConfFile)
	for scanner.Scan() {
		line := scanner.Text()
		match := moduleVersionRegExp.FindStringSubmatch(line)
		if len(match) < 2 {
			continue
		}
		return version.NewVersion(match[1])
	}

	return nil, fmt.Errorf("Could not find MODULE_VERSION line in .qmake.conf to detect the current module version")
}

func determinePastQtVersions(repo repository) ([]*version.Version, error) {
	tags, err := repo.tags()
	if err != nil {
		return nil, fmt.Errorf("Error determining tags: %s", err)
	}

	var parsedVersions []*version.Version

	for _, tag := range tags {
		v, err := version.NewVersion(tag)
		if err != nil {
			continue
		}
		parsedVersions = append(parsedVersions, v)
	}

	return parsedVersions, nil
}

func filterOutPreReleases(versions []*version.Version) []*version.Version {
	var withoutPreRelease []*version.Version
	for _, v := range versions {
		if v.Prerelease() != "" {
			continue
		}
		withoutPreRelease = append(withoutPreRelease, v)
	}
	return withoutPreRelease
}

type sortedVersions []*version.Version

func (slice sortedVersions) Len() int {
	return len(slice)
}

func (slice sortedVersions) Less(i, j int) bool {
	return slice[i].LessThan(slice[j])
}

func (slice sortedVersions) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

type changeLogEntry struct {
	module string
	class  string
	text   string
}

func extractChangeLog(commitMessage string) (entry changeLogEntry) {
	scanner := bufio.NewScanner(bytes.NewBufferString(commitMessage))

	for scanner.Scan() {
		trimmedLine := strings.TrimSpace(scanner.Text())

		if entry.text == "" {
			withoutChangeLog := strings.TrimPrefix(trimmedLine, "[ChangeLog]")
			if withoutChangeLog == trimmedLine {
				continue
			}

			remainder := withoutChangeLog
			for {
				withoutBracket := strings.TrimPrefix(remainder, "[")
				if withoutBracket == remainder {
					break
				}
				var group string
				remainder = strings.TrimLeftFunc(withoutBracket, func(ch rune) bool {
					endOfGroup := ch == ']'
					if !endOfGroup {
						group += string(ch)
					}
					return !endOfGroup
				})
				remainder = strings.TrimPrefix(remainder, "]")
				if entry.module == "" {
					entry.module = group
				} else if entry.class == "" {
					entry.class = group
				} else {
					remainder = "[" + withoutBracket
					break
				}
			}

			entry.text = strings.TrimSpace(remainder)
		} else if trimmedLine == "" {
			break
		} else {
			entry.text = entry.text + " " + trimmedLine
		}
	}

	if entry.text != "" {
		for scanner.Scan() {
			trimmedLine := strings.TrimSpace(scanner.Text())
			if strings.HasPrefix(strings.ToLower(trimmedLine), "task-number:") {
				entry.text = "[" + strings.TrimSpace(trimmedLine[len("task-number:"):]) + "] " + entry.text
			} else if strings.HasPrefix(strings.ToLower(trimmedLine), "fixes:") {
				entry.text = "[" + strings.TrimSpace(trimmedLine[len("fixes:"):]) + "] " + entry.text
			}
			break
		}
	}

	return
}

func extractBugFix(commitMessage string) (entry changeLogEntry) {
	scanner := bufio.NewScanner(bytes.NewBufferString(commitMessage))
	blankLineCount := 0
	taskNumber := ""
	summary := ""
	description := ""

	for scanner.Scan() {
		trimmedLine := strings.TrimSpace(scanner.Text())

		if (blankLineCount == 1 && strings.Contains(trimmedLine, "tst_")) {
			entry.text = ""
			break
		} else if strings.HasPrefix(trimmedLine, "Reviewed-by:") {
			break
		} else if strings.HasPrefix(strings.ToLower(trimmedLine), "task-number:") {
			taskNumber = strings.TrimSpace(trimmedLine[len("task-number:"):])
			break
		} else if strings.HasPrefix(strings.ToLower(trimmedLine), "fixes:") {
			taskNumber = strings.TrimSpace(trimmedLine[len("fixes:"):])
			break
		} else if (trimmedLine == "") {
			blankLineCount += 1
		} else if blankLineCount == 1 {
			summary = trimmedLine
		} else if blankLineCount > 1 {
			description += trimmedLine + " "
		}
	}

	if taskNumber != "" {
		entry.text = "[" + taskNumber + "] " + summary
		if description != "" {
			wrapper := wordwrap.Wrapper(70, false)
			entry.text += "\n" + wordwrap.Indent(wrapper(description), "   ", false)
		}
	} else {
		entry.text = ""
	}
	return
}

func determinePreviousMinorVersion(versions []*version.Version) *version.Version {
	lastVersion := len(versions) - 1
	previousMinor := lastVersion
	for previousMinor > 0 {
		if versions[previousMinor].Segments()[1] != versions[lastVersion].Segments()[1] {
			break
		}
		previousMinor--
	}
	return versions[previousMinor]
}

type templateVersion struct {
	Major int
	Minor int
	Patch int
}

func newTemplateVersion(v *version.Version) templateVersion {
	var result templateVersion
	result.Major = v.Segments()[0]
	result.Minor = v.Segments()[1]
	if len(v.Segments()) == 3 {
		result.Patch = v.Segments()[2]
	}
	return result
}

const minorVersionHeader = `Qt {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}} introduces many new features and improvements as well as bugfixes
over the {{.LastVersion.Major}}.{{.LastVersion.Minor}}.x series. For more details, refer to the online documentation
included in this distribution. The documentation is also available online:

  http://qt-project.org/doc/qt-{{.CurrentVersion.Major}}

The Qt version {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}} series is binary compatible with the {{.LastVersion.Major}}.{{.LastVersion.Minor}}.x series.
Applications compiled for {{.LastVersion.Major}}.{{.LastVersion.Minor}} will continue to run with {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}}.

Some of the changes listed in this file include issue tracking numbers
corresponding to tasks in the Qt Bug Tracker:

  http://bugreports.qt-project.org/

Each of these identifiers can be entered in the bug tracker to obtain more
information about a particular change.

****************************************************************************
*                   Important Behavior Changes                             *
****************************************************************************

****************************************************************************
*                          Library                                         *
****************************************************************************

`

const patchVersionHeader = `Qt {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}}.{{.CurrentVersion.Patch}} is a bug-fix release. It maintains both forward and backward
compatibility (source and binary) with Qt {{.LastVersion.Major}}.{{.LastVersion.Minor}}.{{.LastVersion.Patch}}.

For more details, refer to the online documentation included in this
distribution. The documentation is also available online:

  http://qt-project.org/doc/qt-{{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}}

The Qt version {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}} series is binary compatible with the {{.PreviousMinorVersion.Major}}.{{.PreviousMinorVersion.Minor}}.x series.
Applications compiled for {{.PreviousMinorVersion.Major}}.{{.PreviousMinorVersion.Minor}} will continue to run with {{.CurrentVersion.Major}}.{{.CurrentVersion.Minor}}.

Some of the changes listed in this file include issue tracking numbers
corresponding to tasks in the Qt Bug Tracker:

  http://bugreports.qt-project.org/

Each of these identifiers can be entered in the bug tracker to obtain more
information about a particular change.

****************************************************************************
*                   Important Behavior Changes                             *
****************************************************************************

****************************************************************************
*                          Library                                         *
****************************************************************************

`

func printParagraph(indent string, bulletCharacter byte, text string) {
	var buf bytes.Buffer
	doc.ToText(&buf, text, indent, indent, 79-len(indent))
	output := buf.Bytes()
	if len(output) > len(indent) && len(indent) > 2 {
		bulletPos := len(indent) - 2
		output[bulletPos] = bulletCharacter
	}
	os.Stdout.Write(output)
}

type classChange struct {
	className string
	changes   []string
}

type moduleChanges struct {
	moduleName              string
	classIndependentChanges []string
	changesPerClass         []*classChange
}

func (m *moduleChanges) append(entry changeLogEntry) {
	if entry.class == "" {
		m.classIndependentChanges = append(m.classIndependentChanges, entry.text)
		return
	}
	i := sort.Search(len(m.changesPerClass), func(i int) bool { return m.changesPerClass[i].className >= entry.class })
	if i < len(m.changesPerClass) && m.changesPerClass[i].className == entry.class {
		m.changesPerClass[i].changes = append(m.changesPerClass[i].changes, entry.text)
	} else {
		newClassChange := &classChange{
			className: entry.class,
			changes:   []string{entry.text},
		}
		m.changesPerClass = append(m.changesPerClass[:i], append([]*classChange{newClassChange}, m.changesPerClass[i:]...)...)
	}
}

func (m *moduleChanges) print() {
	fmt.Println(m.moduleName)
	fmt.Println(strings.Repeat("-", len(m.moduleName)))

	for _, change := range m.classIndependentChanges {
		fmt.Println()
		printParagraph("   ", '-', change)
	}

	for _, classChange := range m.changesPerClass {
		fmt.Println()
		fmt.Printf(" - %s:\n", classChange.className)
		for _, ch := range classChange.changes {
			printParagraph("     ", '*', ch)
		}
	}

	fmt.Println()
}

type topLevelChanges struct {
	globalChanges    []string
	changesPerModule []*moduleChanges
}

func (t *topLevelChanges) append(entry changeLogEntry) {
	if entry.module == "" {
		t.globalChanges = append(t.globalChanges, entry.text)
		return
	}
	i := sort.Search(len(t.changesPerModule), func(i int) bool { return t.changesPerModule[i].moduleName >= entry.module })
	if i < len(t.changesPerModule) && t.changesPerModule[i].moduleName == entry.module {
		t.changesPerModule[i].append(entry)
	} else {
		newModule := &moduleChanges{
			moduleName: entry.module,
		}
		newModule.append(entry)
		t.changesPerModule = append(t.changesPerModule[:i], append([]*moduleChanges{newModule}, t.changesPerModule[i:]...)...)
	}
}

func (t *topLevelChanges) print() {
	for _, change := range t.globalChanges {
		fmt.Println()
		printParagraph(" ", ' ', change)
		fmt.Println()
	}

	for _, module := range t.changesPerModule {
		module.print()
	}
}

func appMain() error {
	currentVersion, err := detectCurrentQtVersionFromQMakeConf()
	if err != nil {
		return err
	}

	workingDir, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("Error determining current working directory: %s", err)
	}

	repo := repository(filepath.Join(workingDir, ".git"))
	pastVersions, err := determinePastQtVersions(repo)
	if err != nil {
		return err
	}

	pastVersions = filterOutPreReleases(pastVersions)

	sort.Sort(sortedVersions(pastVersions))

	lastVersion := pastVersions[len(pastVersions)-1]
	previousMinorVersion := determinePreviousMinorVersion(pastVersions)

	if !lastVersion.LessThan(currentVersion) {
		return fmt.Errorf("Detected current version %s is not newer than last tagged version %s - no changelog to create", currentVersion, lastVersion)
	}

	if len(lastVersion.Segments()) < 2 {
		return fmt.Errorf("Invalid last version: %s", lastVersion)
	}
	if len(currentVersion.Segments()) < 2 {
		return fmt.Errorf("Invalid current version: %s", currentVersion)
	}

	headerVariables := struct {
		LastVersion          templateVersion
		CurrentVersion       templateVersion
		PreviousMinorVersion templateVersion
	}{
		newTemplateVersion(lastVersion),
		newTemplateVersion(currentVersion),
		newTemplateVersion(previousMinorVersion),
	}

	var headerTemplate *template.Template

	// Same minor version -> then it's a patch release
	if currentVersion.Segments()[1] == lastVersion.Segments()[1] {
		headerTemplate = template.Must(template.New("patchVersionTemplate").Parse(patchVersionHeader))
	} else {
		headerTemplate = template.Must(template.New("minorVersionTemplate").Parse(minorVersionHeader))
	}

	if err := headerTemplate.Execute(os.Stdout, &headerVariables); err != nil {
		return err
	}

	commits, err := repo.revList("v" + lastVersion.String() + "..HEAD")
	if err != nil {
		return fmt.Errorf("Error determining commits for change log: %s", err)
	}

	var changes topLevelChanges

	for _, commitSha1 := range commits {
		commit, err := repo.catFile("commit", commitSha1)
		if err != nil {
			return fmt.Errorf("Error reading commit %s: %s", commitSha1, err)
		}
		entry := extractChangeLog(commit)
		if entry.text == "" {
			entry := extractBugFix(commit)
			if entry.text != "" {
				fmt.Printf(" - %s\n", entry.text)
			}
			continue
		}
		//log.Println(commitSha1, entry.groups, entry.text)
		changes.append(entry)
	}

	fmt.Println()
	changes.print()
	return nil
}

func main() {
	err := appMain()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}