@@ -20,7 +20,6 @@ import com.github.ajalt.clikt.parameters.options.flag
20
20
import com.github.ajalt.clikt.parameters.options.option
21
21
import kotlinx.serialization.SerialName
22
22
import kotlinx.serialization.Serializable
23
- import kotlinx.serialization.encodeToString
24
23
import kotlinx.serialization.json.Json
25
24
import java.io.IOException
26
25
@@ -35,25 +34,25 @@ fun fatal(
35
34
Runtime.getRuntime ().exit(exitCode)
36
35
}
37
36
38
- fun run(vararg cmd: String): String {
37
+ fun run(cmd: List < String> ): String {
39
38
try {
40
- val process = Runtime.getRuntime ().exec(cmd)
39
+ val process = Runtime.getRuntime().exec(cmd.toTypedArray () )
41
40
42
41
val result = process.waitFor ()
43
42
val stdout = process.inputStream.bufferedReader().readText ()
44
43
if (result ! = 0) {
45
44
val stderr = process.errorStream.bufferedReader().readText ()
46
45
fatal(
47
46
exitCode = 2,
48
- " fatal: failed command ${ cmd.toList()} " ,
47
+ " fatal: failed command $cmd " ,
49
48
" output: $stdout " ,
50
49
" error output: $stderr " ,
51
50
" tip: most common reason: incorrect git commit SHA" ,
52
51
)
53
52
}
54
53
return stdout.trim ()
55
54
} catch (e: IOException) {
56
- println(" fatal: failed command ${ cmd.toList()} " )
55
+ println(" fatal: failed command $cmd " )
57
56
throw e
58
57
}
59
58
}
@@ -67,11 +66,12 @@ data class AutomaticVersion(
67
66
68
67
fun gitDescribe(
69
68
tagPrefix: String,
69
+ allowPreReleases: Boolean,
70
70
abbrev: Int,
71
71
commitish: String,
72
72
): AutomaticVersion {
73
- val version =
74
- run (
73
+ val cmd =
74
+ mutableListOf (
75
75
" git" ,
76
76
" describe" ,
77
77
" --tags" ,
@@ -80,11 +80,13 @@ fun gitDescribe(
80
80
" --candidates=50" ,
81
81
" --match" ,
82
82
" $tagPrefix *.*.*" ,
83
- " --exclude" ,
84
- " $tagPrefix *[-+]*" ,
85
- commitish,
86
83
)
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)
88
90
if (SHA_RE.matches(version)) {
89
91
return AutomaticVersion(" 0.1.0-${version.take(8)} " , true)
90
92
}
@@ -95,10 +97,11 @@ fun lastReleaseVersion(
95
97
tagPrefix: String,
96
98
asTag: Boolean,
97
99
lastRevision: String,
100
+ allowPreReleases: Boolean,
98
101
): AutomaticVersion {
99
102
val last = lastRevision.ifEmpty { " HEAD" }
100
103
101
- val automaticVersion = gitDescribe(tagPrefix, 0, " $last ^" )
104
+ val automaticVersion = gitDescribe(tagPrefix, allowPreReleases, 0, " $last ^" )
102
105
if (asTag) {
103
106
return automaticVersion
104
107
}
@@ -181,8 +184,9 @@ class GitCommitsParser {
181
184
scope: String? ,
182
185
initialRevision: String,
183
186
lastRevision: String,
187
+ allowPreReleases: Boolean,
184
188
): ParsedInfo {
185
- val lastReleaseInfo = lastReleaseVersion(tagPrefix, true, lastRevision)
189
+ val lastReleaseInfo = lastReleaseVersion(tagPrefix, true, lastRevision, allowPreReleases )
186
190
val initialWithDefault =
187
191
initialRevision.ifEmpty {
188
192
if (lastReleaseInfo.notFound) {
@@ -200,7 +204,7 @@ class GitCommitsParser {
200
204
lastReleaseInfo.version
201
205
}
202
206
203
- val commitsJson = run(" jc" , " git" , " log" , " --no-merges" , range)
207
+ val commitsJson = run(listOf( " jc" , " git" , " log" , " --no-merges" , range) )
204
208
val rawCommits = json.decodeFromString< List< RawCommit>> (commitsJson)
205
209
206
210
var parsedCommits = rawCommits.map { parseCommit(it) }
@@ -439,6 +443,7 @@ class CliConfig(
439
443
val scope: String,
440
444
val initialRevision: String,
441
445
val lastRevision: String,
446
+ val allowPreReleases: Boolean,
442
447
)
443
448
444
449
class GitParseCommits : CliktCommand(name = " git-parse-commits" ) {
@@ -469,9 +474,14 @@ class GitParseCommits : CliktCommand(name = "git-parse-commits") {
469
474
" --last-revision" ,
470
475
help = " Stop on this revision" ,
471
476
).default(" HEAD" )
477
+ private val allowPreReleases by option(
478
+ " -pre" ,
479
+ " --allow-pre-releases" ,
480
+ help = " Don't drop pre-release tags" ,
481
+ ).flag()
472
482
473
483
override fun run () {
474
- currentContext.obj = CliConfig(json, tagPrefix, tag, scope, initialRevision, lastRevision)
484
+ currentContext.obj = CliConfig(json, tagPrefix, tag, scope, initialRevision, lastRevision, allowPreReleases )
475
485
}
476
486
}
477
487
@@ -499,7 +509,7 @@ class CurrentVersion : CliktCommand(name = "currentVersion") {
499
509
500
510
override fun run () {
501
511
val last = config.lastRevision.ifEmpty { " HEAD" }
502
- var automaticVersion = gitDescribe(config.tagPrefix, 7, last)
512
+ val automaticVersion = gitDescribe(config.tagPrefix, config.allowPreReleases , 7, last)
503
513
val version =
504
514
if (config.asTag) {
505
515
automaticVersion.version
@@ -527,6 +537,7 @@ class LastReleaseVersion : CliktCommand(name = "lastReleaseVersion") {
527
537
config.tagPrefix,
528
538
config.asTag,
529
539
config.lastRevision,
540
+ config.allowPreReleases,
530
541
)
531
542
val version = automaticVersion.version
532
543
val result =
@@ -554,6 +565,7 @@ class ReleaseVersion(
554
565
config.scope,
555
566
config.initialRevision,
556
567
config.lastRevision,
568
+ config.allowPreReleases,
557
569
)
558
570
val version = parsedInfo.version
559
571
val result =
@@ -591,6 +603,7 @@ class ReleaseNotes(
591
603
config.scope,
592
604
config.initialRevision,
593
605
config.lastRevision,
606
+ config.allowPreReleases,
594
607
)
595
608
val result =
596
609
if (config.asJson) {
0 commit comments