Skip to content

Commit e0a0e8c

Browse files
author
Igor Stepin
committed
minor: added -pre flag for pre-releases cases
1 parent 4b19355 commit e0a0e8c

8 files changed

+101
-17
lines changed

git-parse-commits.main.kts

+29-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.github.ajalt.clikt.parameters.options.flag
2020
import com.github.ajalt.clikt.parameters.options.option
2121
import kotlinx.serialization.SerialName
2222
import kotlinx.serialization.Serializable
23-
import kotlinx.serialization.encodeToString
2423
import kotlinx.serialization.json.Json
2524
import java.io.IOException
2625

@@ -35,25 +34,25 @@ fun fatal(
3534
Runtime.getRuntime().exit(exitCode)
3635
}
3736

38-
fun run(vararg cmd: String): String {
37+
fun run(cmd: List<String>): String {
3938
try {
40-
val process = Runtime.getRuntime().exec(cmd)
39+
val process = Runtime.getRuntime().exec(cmd.toTypedArray())
4140

4241
val result = process.waitFor()
4342
val stdout = process.inputStream.bufferedReader().readText()
4443
if (result != 0) {
4544
val stderr = process.errorStream.bufferedReader().readText()
4645
fatal(
4746
exitCode = 2,
48-
"fatal: failed command ${cmd.toList()}",
47+
"fatal: failed command $cmd",
4948
"output: $stdout",
5049
"error output: $stderr",
5150
"tip: most common reason: incorrect git commit SHA",
5251
)
5352
}
5453
return stdout.trim()
5554
} catch (e: IOException) {
56-
println("fatal: failed command ${cmd.toList()}")
55+
println("fatal: failed command $cmd")
5756
throw e
5857
}
5958
}
@@ -67,11 +66,12 @@ data class AutomaticVersion(
6766

6867
fun gitDescribe(
6968
tagPrefix: String,
69+
allowPreReleases: Boolean,
7070
abbrev: Int,
7171
commitish: String,
7272
): AutomaticVersion {
73-
val version =
74-
run(
73+
val cmd =
74+
mutableListOf(
7575
"git",
7676
"describe",
7777
"--tags",
@@ -80,11 +80,13 @@ fun gitDescribe(
8080
"--candidates=50",
8181
"--match",
8282
"$tagPrefix*.*.*",
83-
"--exclude",
84-
"$tagPrefix*[-+]*",
85-
commitish,
8683
)
87-
var automatic = false
84+
if (!allowPreReleases) {
85+
cmd.add("--exclude")
86+
cmd.add("$tagPrefix*[-+]*")
87+
}
88+
cmd.add(commitish)
89+
val version = run(cmd)
8890
if (SHA_RE.matches(version)) {
8991
return AutomaticVersion("0.1.0-${version.take(8)}", true)
9092
}
@@ -95,10 +97,11 @@ fun lastReleaseVersion(
9597
tagPrefix: String,
9698
asTag: Boolean,
9799
lastRevision: String,
100+
allowPreReleases: Boolean,
98101
): AutomaticVersion {
99102
val last = lastRevision.ifEmpty { "HEAD" }
100103

101-
val automaticVersion = gitDescribe(tagPrefix, 0, "$last^")
104+
val automaticVersion = gitDescribe(tagPrefix, allowPreReleases, 0, "$last^")
102105
if (asTag) {
103106
return automaticVersion
104107
}
@@ -181,8 +184,9 @@ class GitCommitsParser {
181184
scope: String?,
182185
initialRevision: String,
183186
lastRevision: String,
187+
allowPreReleases: Boolean,
184188
): ParsedInfo {
185-
val lastReleaseInfo = lastReleaseVersion(tagPrefix, true, lastRevision)
189+
val lastReleaseInfo = lastReleaseVersion(tagPrefix, true, lastRevision, allowPreReleases)
186190
val initialWithDefault =
187191
initialRevision.ifEmpty {
188192
if (lastReleaseInfo.notFound) {
@@ -200,7 +204,7 @@ class GitCommitsParser {
200204
lastReleaseInfo.version
201205
}
202206

203-
val commitsJson = run("jc", "git", "log", "--no-merges", range)
207+
val commitsJson = run(listOf("jc", "git", "log", "--no-merges", range))
204208
val rawCommits = json.decodeFromString<List<RawCommit>>(commitsJson)
205209

206210
var parsedCommits = rawCommits.map { parseCommit(it) }
@@ -439,6 +443,7 @@ class CliConfig(
439443
val scope: String,
440444
val initialRevision: String,
441445
val lastRevision: String,
446+
val allowPreReleases: Boolean,
442447
)
443448

444449
class GitParseCommits : CliktCommand(name = "git-parse-commits") {
@@ -469,9 +474,14 @@ class GitParseCommits : CliktCommand(name = "git-parse-commits") {
469474
"--last-revision",
470475
help = "Stop on this revision",
471476
).default("HEAD")
477+
private val allowPreReleases by option(
478+
"-pre",
479+
"--allow-pre-releases",
480+
help = "Don't drop pre-release tags",
481+
).flag()
472482

473483
override fun run() {
474-
currentContext.obj = CliConfig(json, tagPrefix, tag, scope, initialRevision, lastRevision)
484+
currentContext.obj = CliConfig(json, tagPrefix, tag, scope, initialRevision, lastRevision, allowPreReleases)
475485
}
476486
}
477487

@@ -499,7 +509,7 @@ class CurrentVersion : CliktCommand(name = "currentVersion") {
499509

500510
override fun run() {
501511
val last = config.lastRevision.ifEmpty { "HEAD" }
502-
var automaticVersion = gitDescribe(config.tagPrefix, 7, last)
512+
val automaticVersion = gitDescribe(config.tagPrefix, config.allowPreReleases, 7, last)
503513
val version =
504514
if (config.asTag) {
505515
automaticVersion.version
@@ -527,6 +537,7 @@ class LastReleaseVersion : CliktCommand(name = "lastReleaseVersion") {
527537
config.tagPrefix,
528538
config.asTag,
529539
config.lastRevision,
540+
config.allowPreReleases,
530541
)
531542
val version = automaticVersion.version
532543
val result =
@@ -554,6 +565,7 @@ class ReleaseVersion(
554565
config.scope,
555566
config.initialRevision,
556567
config.lastRevision,
568+
config.allowPreReleases,
557569
)
558570
val version = parsedInfo.version
559571
val result =
@@ -591,6 +603,7 @@ class ReleaseNotes(
591603
config.scope,
592604
config.initialRevision,
593605
config.lastRevision,
606+
config.allowPreReleases,
594607
)
595608
val result =
596609
if (config.asJson) {

tests/results/5-pre-currentVersion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.0-my-prelease.1
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.0

tests/results/5-pre-releaseNotes.json

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"version": {
3+
"last": "0.2.0",
4+
"increment": "minor",
5+
"current": "0.3.0"
6+
},
7+
"commits": [
8+
{
9+
"commit": "9636bacdfeb2a24645e888ece673dc4f7a31dfd6",
10+
"author": "Author One",
11+
"author_email": "[email protected]",
12+
"date": "Sun Mar 2 13:29:08 2025 +0400",
13+
"raw": "- chore(compontent1): even better",
14+
"headers": [
15+
{
16+
"type": "chore",
17+
"scope": "compontent1",
18+
"description": "even better",
19+
"group": "Other",
20+
"increment": "patch"
21+
}
22+
],
23+
"notes": {}
24+
},
25+
{
26+
"commit": "42deb55eaaae17114d56aa8cca84faad57c2ffcb",
27+
"author": "Author One",
28+
"author_email": "[email protected]",
29+
"date": "Sun Mar 2 13:29:08 2025 +0400",
30+
"raw": "- feat(compontent1): best feature ever",
31+
"headers": [
32+
{
33+
"type": "feat",
34+
"scope": "compontent1",
35+
"description": "best feature ever",
36+
"group": "Features",
37+
"increment": "minor"
38+
}
39+
],
40+
"notes": {}
41+
},
42+
{
43+
"commit": "7446243939319133d4461c35f52945a8a1ef9778",
44+
"author": "Author One",
45+
"author_email": "[email protected]",
46+
"date": "Sun Mar 2 13:29:08 2025 +0400",
47+
"raw": "- fix: now it's better for sure",
48+
"headers": [
49+
{
50+
"type": "fix",
51+
"scope": null,
52+
"description": "now it's better for sure",
53+
"group": "Fixes",
54+
"increment": "patch"
55+
}
56+
],
57+
"notes": {}
58+
}
59+
]
60+
}

tests/results/5-pre-releaseVersion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.3.0
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.2.0-my-prelease.1

tests/results/X-help

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ Usage: git-parse-commits [<options>] <command> [<args>]...
55
Options:
66
-j, --json Output in json format
77
-t, --tag-prefix=<text> Prefix for tags (optional)
8-
--tag Add tag prefix to versions (only if tag prefix is defined)
8+
--tag Add tag prefix to versions (only if tag prefix
9+
is defined)
910
-s, --scope=<text> Scope to filter release note items
1011
-i, --initial-revision=<text> Start range from next revision
1112
-l, --last-revision=<text> Stop on this revision
13+
-pre, --allow-pre-releases Don't drop pre-release tags
1214
-h, --help Show this message and exit
1315

1416
Commands:

tests/run-tests.sh

+5
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ cd ../repo5
108108
../../git-parse-commits -l test1 lastReleaseVersion > ../results/5-test1-lastReleaseVersion
109109
../../git-parse-commits -l test1 releaseVersion > ../results/5-test1-releaseVersion
110110
../../git-parse-commits -l test1 -j releaseNotes | jq . > ../results/5-test1-releaseNotes.json
111+
../../git-parse-commits -l 0.2.0-my-prelease.1 -pre currentVersion > ../results/5-pre-currentVersion
112+
../../git-parse-commits -l 0.2.0-my-prelease.1 -pre lastReleaseVersion > ../results/5-pre-lastReleaseVersion
113+
../../git-parse-commits -l 0.2.0-my-prelease.1 -pre releaseVersion > ../results/5-pre-releaseVersion
114+
../../git-parse-commits -l 0.2.0-my-prelease.1 -pre -j releaseNotes | jq . > ../results/5-pre-releaseNotes.json
111115
../../git-parse-commits currentVersion > ../results/5-currentVersion
112116
../../git-parse-commits lastReleaseVersion > ../results/5-lastReleaseVersion
117+
../../git-parse-commits -pre lastReleaseVersion > ../results/5-pre2-lastReleaseVersion
113118
../../git-parse-commits releaseVersion > ../results/5-releaseVersion
114119
../../git-parse-commits -j releaseNotes | jq . > ../results/5-releaseNotes.json
115120

0 commit comments

Comments
 (0)