@@ -20,9 +20,9 @@ import de.bixilon.kutil.os.OSTypes
2020import de.bixilon.kutil.os.PlatformInfo
2121import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
2222import de.bixilon.kutil.stream.InputStreamUtil.copy
23- import org.ajoberstar.grgit.Commit
24- import org.ajoberstar.grgit.Grgit
25- import org.ajoberstar.grgit.operation.LogOp
23+ import de.bixilon.kutil.stream.InputStreamUtil.readAsString
24+ import de.bixilon.kutil.string.WhitespaceUtil.removeMultipleWhitespaces
25+ import de.bixilon.kutil.string.WhitespaceUtil.trimWhitespaces
2626import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2727import org.gradle.api.tasks.testing.logging.TestLogEvent
2828import org.gradle.jvm.tasks.Jar
@@ -50,7 +50,6 @@ plugins {
5050 kotlin(" jvm" ) version " 2.3.0"
5151 `jvm- test- suite`
5252 application
53- id(" org.ajoberstar.grgit.service" ) version " 5.3.3"
5453 id(" com.github.ben-manes.versions" ) version " 0.53.0"
5554}
5655
@@ -442,31 +441,63 @@ tasks.test {
442441 useJUnitPlatform()
443442}
444443
445- var git: Grgit ? = null
446- var commit: Commit ? = null
444+ fun getEnv (name : String ): String? = System .getenv(name)?.takeIf { it.isNotBlank() }
447445
446+ data class GitStatus (
447+ val commit : String ,
448+ val branch : String ,
449+ val clean : Boolean ,
450+ val tag : String? ,
451+ )
452+
453+ fun loadGitFromEnv (): GitStatus ? {
454+ val commit = getEnv(" CI_COMMIT_SHA" ) ? : return null
455+ val branch = getEnv(" CI_COMMIT_BRANCH" ) ? : return null
456+ val tag = getEnv(" CI_COMMIT_TAG" )
457+ return GitStatus (commit, branch, true , tag)
458+ }
459+
460+ val hasGit by lazy { project.rootDir.resolve(" .git" ).exists() }
461+
462+ fun executeGit (vararg args : String ): String? {
463+ if (! hasGit) return null
464+
465+ val process = ProcessBuilder ()
466+ .command(* (arrayOf(" git" ) + args))
467+ .directory(project.rootDir)
468+ .redirectOutput(ProcessBuilder .Redirect .PIPE )
469+ .start()
470+ if (process.waitFor() != 0 ) return null
471+
472+ return process.inputStream.readAsString()
473+ .trimWhitespaces()
474+ .trim { it == ' \n ' || it == ' \r ' }
475+ .removeMultipleWhitespaces()
476+ .takeIf { it.isNotBlank() }
477+ }
478+
479+ fun loadGitFromGit (): GitStatus ? {
480+ val commit = executeGit(" rev-parse" , " HEAD" ) ? : return null
481+ val branch = executeGit(" branch" , " --show-current" ) ? : return null
482+ val clean = executeGit(" status" , " --porcelain" ) == null
483+ val tag = executeGit(" describe" , " --exact-match" , " --tags" )
484+
485+ return GitStatus (commit, branch, clean, tag)
486+ }
487+
488+ val git by lazy { loadGitFromEnv() ? : loadGitFromGit() }
489+ val GitStatus .commitShort get() = commit.substring(0 , 10 )
448490
449- fun Commit.shortId () = id.substring(0 , 10 )
450491
451492fun loadGit () {
452- try {
453- git = Grgit .open(mapOf (" currentDir" to project.rootDir))
454- } catch (error: Throwable ) {
455- logger.warn(" Can not open git folder: $error " )
456- return
457- }
458- val git = git!!
459- commit = git.log { LogOp (git.repository).apply { maxCommits = 1 } }.first()
460- val commit = commit!!
461- val tag = git.tag.list().find { it.commit == commit }
462- var nextVersion = if (tag != null ) {
493+ val git = git ? : return
494+ var nextVersion = if (git.tag != null ) {
463495 stable = true
464- tag.name
496+ git.tag
465497 } else {
466- commit.shortId()
498+ git.commitShort
467499 }
468- val status = git.status()
469- if (! status.isClean) {
500+ if (! git.clean) {
470501 nextVersion + = " -dirty"
471502 }
472503 if (project.version != nextVersion) {
@@ -481,13 +512,12 @@ val versionJsonTask = tasks.register("versionJson") {
481512 outputs.upToDateWhen { false }
482513
483514 doFirst {
484- fun generateGit (git : Grgit , commit : Commit ): Map <String , Any > {
485- val status = git.status()
515+ fun generateGit (git : GitStatus ): Map <String , Any > {
486516 return mapOf (
487- " branch" to ( System .getenv()[ " CI_COMMIT_BRANCH " ] ? : git.branch.current().name) ,
488- " commit" to commit.id ,
489- " commit_short" to commit.shortId() ,
490- " dirty" to ! status.isClean ,
517+ " branch" to git.branch,
518+ " commit" to git.commit ,
519+ " commit_short" to git.commitShort ,
520+ " dirty" to ! git.clean ,
491521 )
492522 }
493523
@@ -499,15 +529,7 @@ val versionJsonTask = tasks.register("versionJson") {
499529 " updates" to updates,
500530 )
501531 )
502- try {
503- val git = git
504- val commit = commit
505- if (git != null && commit != null ) {
506- versionInfo[" git" ] = generateGit(git, commit)
507- }
508- } catch (exception: Throwable ) {
509- exception.printStackTrace()
510- }
532+ git?.let { versionInfo[" git" ] = generateGit(it) }
511533 val file = project.layout.buildDirectory.get().asFile.resolve(" resources/main/assets/minosoft/version.json" )
512534 file.writeText(groovy.json.JsonOutput .toJson(versionInfo))
513535 }
0 commit comments