-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-parse-commits.main.kts
executable file
·741 lines (670 loc) · 21.9 KB
/
git-parse-commits.main.kts
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#!/bin/sh
///bin/echo >/dev/null <<EOC
/*
EOC
kotlinc -script -Xplugin="${KOTLIN_HOME}/lib/kotlinx-serialization-compiler-plugin.jar" -- "$0" "$@"
exit $?
*/
// NOTE: this script is for Kotlin 2.0 as kotlinx-serialization-json:1.7.0 require it
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:4.4.0")
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.requireObject
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.IOException
fun fatal(
exitCode: Int = 0,
vararg messages: String,
) {
for (message in messages) {
println(message)
}
Runtime.getRuntime().exit(exitCode)
}
fun run(vararg cmd: String): String {
try {
val process = Runtime.getRuntime().exec(cmd)
val result = process.waitFor()
val stdout = process.inputStream.bufferedReader().readText()
if (result != 0) {
val stderr = process.errorStream.bufferedReader().readText()
fatal(
exitCode = 2,
"fatal: failed command ${cmd.toList()}",
"output: $stdout",
"error output: $stderr",
"tip: most common reason: incorrect git commit SHA",
)
}
return stdout.trim()
} catch (e: IOException) {
println("fatal: failed command ${cmd.toList()}")
throw e
}
}
val SHA_RE = "^[0-9a-f]{5,40}$".toRegex()
data class AutomaticVersion(
val version: String,
val notFound: Boolean,
)
fun lastReleaseVersion(
tagPrefix: String,
asTag: Boolean,
lastRevision: String,
): AutomaticVersion {
val last = lastRevision.ifEmpty { "HEAD" }
var version =
if (tagPrefix.isEmpty()) {
run("git", "describe", "--tags", "--always", "--abbrev=0", "$last^")
} else {
run(
"git",
"describe",
"--tags",
"--always",
"--abbrev=0",
"--match",
"$tagPrefix*",
"$last^",
)
}
var automatic = false
if (SHA_RE.matches(version)) {
version = "0.1.0-${version.take(8)}"
automatic = true
} else if (!asTag) {
version = version.substring(tagPrefix.length)
}
return AutomaticVersion(version, automatic)
}
class GitCommitsParser {
@Serializable
enum class IncrementType(
val sort: Int,
) {
@SerialName("none")
NONE(0),
@SerialName("patch")
PATCH(1),
@SerialName("minor")
MINOR(2),
@SerialName("major")
MAJOR(3),
}
@Serializable
data class VersionInfo(
val last: String?,
val increment: IncrementType,
val current: String,
)
@Serializable
data class RawCommit(
val commit: String,
val author: String?,
@SerialName("author_email")
val authorEmail: String?,
val date: String,
val message: String,
val epoch: Int,
@SerialName("epoch_utc")
val epochUtc: Int?,
)
@Serializable
data class ChangeLogLine(
val type: String,
val scope: String?,
val description: String,
val group: String?,
var increment: IncrementType,
)
@Serializable
data class CommitInfo(
val commit: String,
val author: String,
@SerialName("author_email")
val authorEmail: String?,
val date: String,
val raw: String,
var headers: List<ChangeLogLine>,
val notes: Map<String, String>,
)
@Serializable
data class ParsedInfo(
val version: VersionInfo,
val commits: List<CommitInfo>,
)
private val json =
Json {
ignoreUnknownKeys = true
}
fun parse(
tagPrefix: String,
asTag: Boolean,
scope: String?,
initialRevision: String,
lastRevision: String,
): ParsedInfo {
val lastReleaseInfo = lastReleaseVersion(tagPrefix, true, lastRevision)
val initialWithDefault =
initialRevision.ifEmpty {
if (lastReleaseInfo.notFound) {
""
} else {
lastReleaseInfo.version
}
}
val lastWithDefault = lastRevision.ifEmpty { "HEAD" }
val range = prepareRange(initialWithDefault, lastWithDefault)
var lastRelease =
if (lastReleaseInfo.notFound && asTag && tagPrefix.isNotEmpty()) {
"${tagPrefix}${lastReleaseInfo.version}"
} else {
lastReleaseInfo.version
}
val commitsJson = run("jc", "git", "log", "--no-merges", range)
val rawCommits = json.decodeFromString<List<RawCommit>>(commitsJson)
var parsedCommits = rawCommits.map { parseCommit(it) }
if (!scope.isNullOrEmpty()) {
parsedCommits =
parsedCommits.filter { commit ->
commit.headers =
commit.headers.filter { it.scope.isNullOrEmpty() || it.scope == scope }
commit.headers.isNotEmpty()
}
}
val allHeaders =
if (parsedCommits.isEmpty()) {
emptyList()
} else {
parsedCommits.flatMap { commit -> commit.headers }
}
val increment =
if (allHeaders.isEmpty()) {
IncrementType.NONE
} else {
allHeaders.map { it.increment }.maxBy { it.sort }
}
if (tagPrefix.isNotEmpty() && lastRelease.startsWith(tagPrefix)) {
lastRelease = lastRelease.substring(tagPrefix.length)
}
var currentVersion = incrementVersion(lastRelease, increment)
if (asTag && tagPrefix.isNotEmpty()) {
currentVersion = tagPrefix + currentVersion
lastRelease = tagPrefix + lastRelease
}
return ParsedInfo(
version =
VersionInfo(
last = lastRelease,
increment = increment,
current = currentVersion,
),
commits = parsedCommits,
)
}
private fun prepareRange(
initial: String,
last: String,
): String {
if (initial.isEmpty()) {
return last
}
return "$initial..$last"
}
private fun parseCommit(raw: RawCommit): CommitInfo {
val (headers, notes) = parseMessage(raw.message)
return CommitInfo(
commit = raw.commit,
author = raw.author ?: "",
authorEmail = raw.authorEmail,
date = raw.date,
raw = raw.message,
headers = headers,
notes = notes,
)
}
val RELEASE_LINE_RE = "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$".toRegex()
private fun parseMessage(message: String): Pair<MutableList<ChangeLogLine>, MutableMap<String, String>> {
val lines = message.split("\n")
val headerDelimiterIndex = lines.indexOf("")
val footerDelimiterIndex =
if (headerDelimiterIndex != -1) {
lines.lastIndexOf("")
} else {
-1
}
val headerLines =
if (headerDelimiterIndex == -1) {
lines
} else {
lines.subList(0, headerDelimiterIndex)
}
val footerLines =
if (footerDelimiterIndex == -1) {
emptyList()
} else {
lines.asReversed().subList(0, footerDelimiterIndex)
}
var isMajor = false
val notes = mutableMapOf<String, String>()
footerLines.forEach {
val line = it.trim()
val delimiter = line.indexOf(":")
if (delimiter == -1) return@forEach
val key = line.take(delimiter).trim()
val value = line.substring(delimiter + 1).trim()
notes[key] = value
if (key == "BREAKING CHANGE") {
isMajor = true
}
}
val headers = mutableListOf<ChangeLogLine>()
headerLines.forEach { lineOriginal ->
var line = lineOriginal.trim()
if (line.startsWith("-") || line.startsWith("*")) {
line = line.substring(1).trim()
}
val parsed = RELEASE_LINE_RE.find(line)
if (parsed != null) {
var type = parsed.groupValues[1].lowercase()
var scope: String? = parsed.groupValues[2].lowercase()
if (scope == "*" || scope == "") scope = null
var increment =
if (type.endsWith("!")) {
type = type.take(type.length - 1)
IncrementType.MAJOR
} else if (isMajor) {
IncrementType.MAJOR
} else {
incrementFromType(type)
}
var group = groupFromType(type)
val description = parsed.groupValues[3]
if (line.contains("WIP")) {
type = "wip"
group = null
increment = IncrementType.NONE
}
headers += ChangeLogLine(type, scope, description, group, increment)
} else {
var type = "feat"
var increment = incrementFromType(type)
var group = groupFromType(type)
if (line.contains("WIP")) {
type = "wip"
group = null
increment = IncrementType.NONE
}
headers += ChangeLogLine(type, null, line, group, increment)
}
}
if (isMajor) {
val commitIncrement = headers.map { it.increment }.maxBy { it.sort }
if (commitIncrement != IncrementType.MAJOR) {
if (headers.isEmpty()) {
val type = "feat"
val group = groupFromType(type)
val line =
if (headerLines.isEmpty()) {
headerLines.first()
} else {
""
}
headers += ChangeLogLine(type, null, line, group, IncrementType.MAJOR)
} else {
headers[0].increment = IncrementType.MAJOR
}
}
}
return Pair(headers.asReversed(), notes)
}
private fun groupFromType(type: String): String? =
when (type) {
"fix", "refactor", "docs", "perf", "BREAKING CHANGE" -> "Fixes"
"chore", "ci", "build", "style", "test" -> "Other"
"skip", "wip", "minor" -> null
else -> "Features"
}
private fun incrementFromType(type: String): IncrementType =
when (type) {
"BREAKING CHANGE" -> IncrementType.MAJOR
"fix", "refactor", "docs", "perf", "chore", "ci", "build", "style",
"test",
-> IncrementType.PATCH
"skip", "wip", "minor" -> IncrementType.NONE
else -> IncrementType.MINOR
}
private fun incrementVersion(
version: String,
increment: IncrementType,
): String {
val versionParts = "\\.|\\-|\\+".toRegex().split(version)
if (versionParts[0].isEmpty() || versionParts[1].isEmpty() || versionParts[2].isEmpty()) {
fatal(
exitCode = 3,
"version '$version' looks incorrect.",
"tip: did you forget --tag-prefix option?",
)
}
var major = versionParts[0].toInt()
var minor = versionParts[1].toInt()
var patch = versionParts[2].toInt()
when (increment) {
IncrementType.NONE -> {}
IncrementType.PATCH -> patch += 1
IncrementType.MINOR -> {
minor += 1
patch = 0
}
IncrementType.MAJOR -> {
major += 1
minor = 0
patch = 0
}
}
return "$major.$minor.$patch"
}
}
class CliConfig(
val asJson: Boolean,
val tagPrefix: String,
val asTag: Boolean,
val scope: String,
val initialRevision: String,
val lastRevision: String,
)
class GitParseCommits :
CliktCommand(
name = "git-parse-commits",
printHelpOnEmptyArgs = true,
help = "Provides next release version and release notes from git commit messages.",
) {
private val json by option("-j", "--json", help = "Output in json format").flag()
private val tagPrefix by option(
"-t",
"--tag-prefix",
help = "Prefix for tags (optional)",
).default("")
private val tag by option(
help = "Add tag prefix to versions (only if tag prefix is defined)",
).flag()
private val scope by option(
"-s",
"--scope",
help = "Scope to filter release note items",
).default("")
private val initialRevision by option(
"-i",
"--initial-revision",
help = "Start range from next revision",
).default("")
private val lastRevision by option(
"-l",
"--last-revision",
help = "Stop on this revision",
).default("HEAD")
override fun run() {
currentContext.obj = CliConfig(json, tagPrefix, tag, scope, initialRevision, lastRevision)
}
}
class Version :
CliktCommand(
name = "version",
help = "Prints version of this tool",
) {
private val config by requireObject<CliConfig>()
override fun run() {
val version = "SNAPSHOT"
val result =
if (config.asJson) {
"""{"tool_version": "$version"}"""
} else {
version
}
println(result)
}
}
class CurrentVersion :
CliktCommand(
name = "currentVersion",
help = "Prints current version (useful for non-release builds)",
) {
private val config by requireObject<CliConfig>()
override fun run() {
val last = config.lastRevision.ifEmpty { "HEAD" }
var version =
if (config.tagPrefix.isEmpty()) {
run("git", "describe", "--tags", "--always", last)
} else {
run(
"git",
"describe",
"--tags",
"--always",
"--match",
"${config.tagPrefix}*",
last,
)
}
if (SHA_RE.matches(version)) {
version = "0.1.0-${version.take(8)}"
} else if (!config.asTag) {
version = version.substring(config.tagPrefix.length)
}
val result =
if (config.asJson) {
"""{"current_version": "$version"}"""
} else {
version
}
println(result)
}
}
class LastReleaseVersion :
CliktCommand(
name = "lastReleaseVersion",
help = "Prints version of last release",
) {
private val config by requireObject<CliConfig>()
override fun run() {
val automaticVersion =
lastReleaseVersion(
config.tagPrefix,
config.asTag,
config.lastRevision,
)
val version = automaticVersion.version
val result =
if (config.asJson) {
"""{"last_release_version": "$version"}"""
} else {
version
}
println(result)
}
}
class ReleaseVersion(
private val gitCommitsParser: GitCommitsParser,
) : CliktCommand(
name = "releaseVersion",
help = "Prints version of next release from git commit messages",
) {
private val config by requireObject<CliConfig>()
override fun run() {
val parsedInfo =
gitCommitsParser.parse(
config.tagPrefix,
config.asTag,
config.scope,
config.initialRevision,
config.lastRevision,
)
val version = parsedInfo.version
val result =
if (config.asJson) {
"""{"release_version": "${version.current}", "increment": "${version.increment.name.lowercase()}"}"""
} else {
version.current
}
println(result)
}
}
class ReleaseNotes(
private val gitCommitsParser: GitCommitsParser,
) : CliktCommand(
name = "releaseNotes",
help = "Prints release notes from git commit messages",
) {
private val asShort by option(
"-s",
"--short",
help = "Switch output to short format to be used as description of git tag",
).flag()
private val asOneline by option(
"-l",
"--one-line",
help = "Switch output to one-line format to be used as description of git tag",
).flag()
private val config by requireObject<CliConfig>()
override fun run() {
val parsedInfo =
gitCommitsParser.parse(
config.tagPrefix,
config.asTag,
config.scope,
config.initialRevision,
config.lastRevision,
)
val result =
if (config.asJson) {
Json.encodeToString<GitCommitsParser.ParsedInfo>(parsedInfo)
} else {
formatReleaseNotes(parsedInfo, asShort, asOneline)
}
println(result)
}
private fun formatReleaseNotes(
parsedInfo: GitCommitsParser.ParsedInfo,
asShortInitial: Boolean,
asOneline: Boolean,
): String {
val asShort = asShortInitial || asOneline
val linesPerGroup = getReleaseLinesPerGroup(parsedInfo)
val features = groupToText("Features", linesPerGroup["Features"], asShort)
val fixes = groupToText("Fixes", linesPerGroup["Fixes"], asShort)
val other = groupToText("Other", linesPerGroup["Other"], asShort)
if (asOneline) {
val result =
listOfNotNull(features, fixes, other)
.joinToString("\n")
.split("\n")
.firstOrNull()
if (result.isNullOrEmpty()) return ""
return "$result..."
}
val separator = if (asShort) "\n" else "\n\n"
return listOfNotNull(features, fixes, other).joinToString(separator)
}
data class ReleaseLine(
val sha: String,
val type: String,
val scope: String?,
val description: String,
) {
companion object {
fun of(
commit: GitCommitsParser.CommitInfo,
header: GitCommitsParser.ChangeLogLine,
) = ReleaseLine(
sha = commit.commit.take(7),
type = header.type,
scope = header.scope,
description = header.description,
)
}
}
private fun getReleaseLinesPerGroup(parsedInfo: GitCommitsParser.ParsedInfo): Map<String, Set<ReleaseLine>> {
val result = mutableMapOf<String, MutableSet<ReleaseLine>>()
parsedInfo.commits.forEach { commit ->
commit.headers.forEach { header ->
if (header.group != null) {
result.getOrPut(header.group) { mutableSetOf() }.add(
ReleaseLine.of(commit, header),
)
}
}
}
return result
}
private fun groupToText(
header: String,
lines: Set<ReleaseLine>?,
asShort: Boolean,
): String? {
if (asShort) {
return groupToTextShort(lines)
}
return groupToTextFull(header, lines)
}
private fun groupToTextShort(lines: Set<ReleaseLine>?): String? {
if (lines.isNullOrEmpty()) return null
val result = mutableSetOf<String>()
lines.forEach { line ->
val scope = if (line.scope != "*" && line.scope != null) line.scope else ""
var type =
if (scope.isNotEmpty()) {
"${line.type}($scope)"
} else {
line.type
}
if (type.isNotEmpty()) {
type += ": "
}
result.add("- ${type}${line.description}")
}
return result.joinToString("\n")
}
private fun groupToTextFull(
header: String,
lines: Set<ReleaseLine>?,
): String? {
if (lines.isNullOrEmpty()) return null
val result = mutableSetOf("### $header\n")
lines.forEach { line ->
val scope = if (line.scope != "*" && line.scope != null) line.scope else ""
var type =
if (line.type != "feat" && line.type != "fix") {
if (scope.isNotEmpty()) {
"${line.type}($scope)"
} else if (line.type.isNotEmpty()) {
line.type
} else {
scope
}
} else {
scope
}
if (type.isNotEmpty()) {
type += ": "
}
result.add("- (${line.sha}) ${type}${line.description}")
}
return result.joinToString("\n")
}
}
val gitCommitsParser = GitCommitsParser()
GitParseCommits()
.subcommands(
Version(),
CurrentVersion(),
LastReleaseVersion(),
ReleaseVersion(gitCommitsParser),
ReleaseNotes(gitCommitsParser),
).main(args)