fix(autoversion): try to avoid running git
All checks were successful
ci/woodpecker/push/gradle Pipeline was successful
ci/woodpecker/push/pages Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-05-26 10:58:34 +02:00
parent c9cbbce3b2
commit 8decdad746
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 60 additions and 7 deletions

View File

@ -0,0 +1,29 @@
package io.gitlab.jfronny.scripts
import java.lang.reflect.Field
import java.util.*
private val processEnvironmentClass: Class<*> = Class.forName("java.lang.ProcessEnvironment")
private val theEnvironmentField: Field = processEnvironmentClass.getDeclaredField("theEnvironment").apply { isAccessible = true }
private val theCaseInsensitiveEnvironmentField: Field = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment").apply { isAccessible = true }
@Throws(Exception::class)
fun setEnv(newenv: Map<String, String>?) = try {
val env = theEnvironmentField.get(null) as MutableMap<String, String>
env.clear()
env.putAll(newenv!!)
val cienv = theCaseInsensitiveEnvironmentField.get(null) as MutableMap<String, String>
cienv.clear()
cienv.putAll(newenv)
} catch (e: NoSuchFieldException) {
val classes = Collections::class.java.declaredClasses
val env = System.getenv()
for (cl in classes) {
if ("java.util.Collections\$UnmodifiableMap" == cl.name) {
val field: Field = cl.getDeclaredField("m").apply { isAccessible = true }
val map = field.get(env) as MutableMap<String, String>
map.clear()
map.putAll(newenv!!)
}
}
}

View File

@ -2,19 +2,39 @@ package io.gitlab.jfronny.scripts
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.errors.IncorrectObjectTypeException
import org.eclipse.jgit.lib.AnyObjectId
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.PersonIdent
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.*
import org.eclipse.jgit.revwalk.RevCommit
import org.eclipse.jgit.revwalk.RevTag
import org.eclipse.jgit.revwalk.RevWalk
import org.eclipse.jgit.storage.file.FileBasedConfig
import org.eclipse.jgit.util.FS
import org.eclipse.jgit.util.SystemReader
import java.time.Instant
import java.time.ZoneOffset
import java.time.ZonedDateTime
fun initializeGit() {
val original = SystemReader.getInstance()
SystemReader.setInstance(object : SystemReader() {
override fun getHostname(): String = original.hostname
override fun getenv(key: String): String? = if (key == "PATH") null else original.getenv(key)
override fun getProperty(key: String?): String = original.getProperty(key)
override fun openUserConfig(parent: Config?, fs: FS?): FileBasedConfig = original.openUserConfig(parent, fs)
override fun openSystemConfig(parent: Config?, fs: FS?): FileBasedConfig = original.openSystemConfig(parent, fs)
override fun openJGitConfig(parent: Config?, fs: FS?): FileBasedConfig = original.openJGitConfig(parent, fs)
override fun getCurrentTime(): Long = original.currentTime
override fun getTimezone(`when`: Long): Int = original.getTimezone(`when`)
})
// Stuff
// val backup = HashMap(System.getenv())
// setEnv(mapOf())
// try {
org.eclipse.jgit.util.FS.DETECTED.gitSystemConfig
// } finally {
// setEnv(backup)
// }
}
fun Git.log(since: AnyObjectId? = null, until: AnyObjectId? = null): List<Commit> {
var command = this.log()
if (since != null) command = command.not(since)

View File

@ -9,6 +9,7 @@ val isRelease = project.hasProperty("release")
versionS = "0.0.0+nogit"
versionType = VersionType.ALPHA
if (File(projectDir, ".git").exists()) {
initializeGit()
Git.open(projectDir).use { git ->
versionS = "0.0.0+notag"
val tags: List<Tag> = git.getTags()
@ -48,7 +49,10 @@ if (File(projectDir, ".git").exists()) {
changelogHtml += "<ul>\n" + log.joinToString("") { " <li>${it.shortMessage}</li>\n" } + "</ul>"
}
}
} else changelog = "No changelog"
} else {
changelog = "No changelog"
changelogHtml = "<p>No changelog</p>"
}
if (!isRelease) {
versionS = "$nextRelease-SNAPSHOT"