11import com.codingfeline.buildkonfig.compiler.FieldSpec
22import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
3+ import java.io.File
34import java.time.ZoneId
45import java.time.ZonedDateTime
56import java.time.format.DateTimeFormatter
@@ -62,15 +63,60 @@ kotlin {
6263 }
6364}
6465
65- val gitHashProvider: Provider <String > = providers.exec {
66- commandLine(" git" , " rev-parse" , " HEAD" )
67- }.standardOutput.asText.map { it.trim() }
66+ fun resolveGitDir (rootDir : File ): File ? {
67+ val dotGit = rootDir.resolve(" .git" )
68+ if (dotGit.isDirectory) {
69+ return dotGit
70+ }
71+ if (! dotGit.isFile) {
72+ return null
73+ }
74+ val pointer = dotGit.readText().trim()
75+ val prefix = " gitdir:"
76+ if (! pointer.startsWith(prefix)) {
77+ return null
78+ }
79+ return rootDir.resolve(pointer.removePrefix(prefix).trim()).normalize().takeIf { it.exists() }
80+ }
81+
82+ fun resolveGitHash (rootDir : File ): String? {
83+ val gitDir = resolveGitDir(rootDir) ? : return null
84+ val headFile = gitDir.resolve(" HEAD" )
85+ if (! headFile.isFile) {
86+ return null
87+ }
88+ val head = headFile.readText().trim()
89+ if (! head.startsWith(" ref: " )) {
90+ return head.takeIf { it.matches(Regex (" ^[0-9a-fA-F]{40}$" )) }
91+ }
6892
69- val gitShortHashProvider: Provider <String > = gitHashProvider.map { it.substring(0 , 7 ) }
93+ val ref = head.removePrefix(" ref: " ).trim()
94+ val refFile = gitDir.resolve(ref)
95+ if (refFile.isFile) {
96+ return refFile.readText().trim()
97+ }
7098
71- val coreLibGitHashProvider: Provider <String > = providers.exec {
72- commandLine(" git" , " -C" , project(" :acidify-core" ).projectDir.absolutePath, " rev-parse" , " HEAD" )
73- }.standardOutput.asText.map { it.trim() }
99+ val packedRefsFile = gitDir.resolve(" packed-refs" )
100+ if (! packedRefsFile.isFile) {
101+ return null
102+ }
103+ return packedRefsFile.useLines { lines ->
104+ lines
105+ .filterNot { it.startsWith(" #" ) || it.startsWith(" ^" ) }
106+ .firstOrNull { it.endsWith(" $ref " ) }
107+ ?.substringBefore(' ' )
108+ ?.trim()
109+ }
110+ }
111+
112+ val gitHashProvider: Provider <String > = providers.gradleProperty(" yogurtGitHash" )
113+ .orElse(providers.environmentVariable(" YOGURT_GIT_HASH" ))
114+ .orElse(providers.environmentVariable(" GITHUB_SHA" ))
115+ .orElse(providers.provider { resolveGitHash(rootProject.rootDir) ? : " unknown" })
116+
117+ val gitShortHashProvider: Provider <String > = gitHashProvider.map { hash ->
118+ if (hash.length <= 7 ) hash else hash.substring(0 , 7 )
119+ }
74120
75121val buildTimeProvider: Provider <String > = providers.provider {
76122 ZonedDateTime .now(ZoneId .of(" Asia/Shanghai" ))
@@ -87,7 +133,7 @@ buildkonfig {
87133 FieldSpec .Type .STRING ,
88134 " coreVersion" ,
89135 project(" :acidify-core" ).let {
90- " ${it.name} ${it.version} +${coreLibGitHashProvider .get().substring( 0 , 7 )} "
136+ " ${it.name} ${it.version} +${gitShortHashProvider .get()} "
91137 }
92138 )
93139 buildConfigField(
0 commit comments