Skip to content

Commit c9a24e1

Browse files
Add true configuration cache support for git commit hash (#2285)
Co-authored-by: firelight <147925818+fire-light42@users.noreply.github.com>
1 parent ca96aa6 commit c9a24e1

5 files changed

Lines changed: 84 additions & 30 deletions

File tree

app/build.gradle.kts

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,52 @@ plugins {
1313

1414
val javaTarget = JvmTarget.fromTarget(libs.versions.jvmTarget.get())
1515

16-
fun getGitCommitHash(): String {
17-
return try {
18-
val headFile = file("${project.rootDir}/.git/HEAD")
19-
20-
// Read the commit hash from .git/HEAD
21-
if (headFile.exists()) {
22-
val headContent = headFile.readText().trim()
23-
if (headContent.startsWith("ref:")) {
24-
val refPath = headContent.substring(5) // e.g., refs/heads/main
25-
val commitFile = file("${project.rootDir}/.git/$refPath")
26-
if (commitFile.exists()) commitFile.readText().trim() else ""
27-
} else headContent // If it's a detached HEAD (commit hash directly)
28-
} else {
29-
"" // If .git/HEAD doesn't exist
30-
}.take(7) // Return the short commit hash
31-
} catch (_: Throwable) {
32-
"" // Just return an empty string if any exception occurs
16+
abstract class GenerateGitHashTask : DefaultTask() {
17+
18+
@get:InputFile
19+
@get:PathSensitive(PathSensitivity.RELATIVE)
20+
abstract val headFile: RegularFileProperty
21+
22+
@get:InputDirectory
23+
@get:PathSensitive(PathSensitivity.RELATIVE)
24+
abstract val headsDir: DirectoryProperty
25+
26+
@get:OutputDirectory
27+
abstract val outputDir: DirectoryProperty
28+
29+
@TaskAction
30+
fun generate() {
31+
val head = headFile.get().asFile
32+
33+
val hash = try {
34+
if (head.exists()) {
35+
// Read the commit hash from .git/HEAD
36+
val headContent = head.readText().trim()
37+
if (headContent.startsWith("ref:")) {
38+
val refPath = headContent.substring(5) // e.g., refs/heads/main
39+
val commitFile = File(head.parentFile, refPath)
40+
if (commitFile.exists()) commitFile.readText().trim() else ""
41+
} else headContent // If it's a detached HEAD (commit hash directly)
42+
} else "" // If .git/HEAD doesn't exist
43+
} catch (_: Throwable) {
44+
"" // Just set to an empty string if any exception occurs
45+
}.take(7) // Get the short commit hash
46+
47+
val outFile = outputDir.file("git-hash.txt").get().asFile
48+
outFile.parentFile.mkdirs()
49+
outFile.writeText(hash)
3350
}
3451
}
3552

53+
val generateGitHash = tasks.register<GenerateGitHashTask>("generateGitHash") {
54+
val gitDir = layout.projectDirectory.dir("../.git")
55+
56+
headFile.set(gitDir.file("HEAD"))
57+
headsDir.set(gitDir.dir("refs/heads"))
58+
59+
outputDir.set(layout.buildDirectory.dir("generated/git"))
60+
}
61+
3662
android {
3763
@Suppress("UnstableApiUsage")
3864
testOptions {
@@ -47,6 +73,15 @@ android {
4773
includeInBundle = false
4874
}
4975

76+
androidComponents {
77+
onVariants { variant ->
78+
variant.sources.assets?.addGeneratedSourceDirectory(
79+
generateGitHash,
80+
GenerateGitHashTask::outputDir
81+
)
82+
}
83+
}
84+
5085
signingConfigs {
5186
// We just use SIGNING_KEY_ALIAS here since it won't change
5287
// so won't kill the configuration cache.
@@ -72,8 +107,6 @@ android {
72107
versionCode = 68
73108
versionName = "4.7.0"
74109

75-
resValue("string", "commit_hash", getGitCommitHash())
76-
77110
manifestPlaceholders["target_sdk_version"] = libs.versions.targetSdk.get()
78111

79112
// Reads local.properties
@@ -142,11 +175,11 @@ android {
142175
}
143176

144177
java {
145-
// Use Java 17 toolchain even if a higher JDK runs the build.
178+
// Use Java 17 toolchain even if a higher JDK runs the build.
146179
// We still use Java 8 for now which higher JDKs have deprecated.
147-
toolchain {
148-
languageVersion.set(JavaLanguageVersion.of(libs.versions.jdkToolchain.get()))
149-
}
180+
toolchain {
181+
languageVersion.set(JavaLanguageVersion.of(libs.versions.jdkToolchain.get()))
182+
}
150183
}
151184

152185
lint {
@@ -156,7 +189,6 @@ android {
156189

157190
buildFeatures {
158191
buildConfig = true
159-
resValues = true
160192
viewBinding = true
161193
}
162194

app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.lagradost.cloudstream3.ui.settings.Globals.TV
2727
import com.lagradost.cloudstream3.ui.settings.Globals.isLandscape
2828
import com.lagradost.cloudstream3.ui.settings.Globals.isLayout
2929
import com.lagradost.cloudstream3.utils.DataStoreHelper
30+
import com.lagradost.cloudstream3.utils.GitInfo.currentCommitHash
3031
import com.lagradost.cloudstream3.utils.ImageLoader.loadImage
3132
import com.lagradost.cloudstream3.utils.UIHelper.clipboardHelper
3233
import com.lagradost.cloudstream3.utils.UIHelper.fixSystemBarsPadding
@@ -247,16 +248,17 @@ class SettingsFragment : BaseFragment<MainSettingsBinding>(
247248
}
248249

249250
val appVersion = BuildConfig.VERSION_NAME
250-
val commitInfo = getString(R.string.commit_hash)
251+
val commitHash = activity?.currentCommitHash() ?: ""
251252
val buildTimestamp = SimpleDateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
252253
Locale.getDefault()
253254
).apply { timeZone = TimeZone.getTimeZone("UTC")
254255
}.format(Date(BuildConfig.BUILD_DATE)).replace("UTC", "")
255256

256257
binding.appVersion.text = appVersion
257258
binding.buildDate.text = buildTimestamp
259+
binding.commitHash.text = commitHash
258260
binding.appVersionInfo.setOnLongClickListener {
259-
clipboardHelper(txt(R.string.extension_version), "$appVersion $commitInfo $buildTimestamp")
261+
clipboardHelper(txt(R.string.extension_version), "$appVersion $commitHash $buildTimestamp")
260262
true
261263
}
262264
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.lagradost.cloudstream3.utils
2+
3+
import android.content.Context
4+
5+
/**
6+
* Simple helper to get the short commit hash from assets.
7+
* The hash is generated at build and stored as an asset
8+
* that can be accessed at runtime for Gradle
9+
* configuration cache support.
10+
*/
11+
object GitInfo {
12+
fun Context.currentCommitHash(): String = try {
13+
assets.open("git-hash.txt")
14+
.bufferedReader()
15+
.readText()
16+
.trim()
17+
} catch (_: Exception) {
18+
""
19+
}
20+
}

app/src/main/java/com/lagradost/cloudstream3/utils/InAppUpdater.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.lagradost.cloudstream3.services.PackageInstallerService
2424
import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus
2525
import com.lagradost.cloudstream3.utils.AppUtils.parseJson
2626
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
27+
import com.lagradost.cloudstream3.utils.GitInfo.currentCommitHash
2728
import kotlinx.coroutines.sync.Mutex
2829
import kotlinx.coroutines.sync.withLock
2930
import okio.BufferedSink
@@ -170,7 +171,7 @@ object InAppUpdater {
170171
Log.d(LOG_TAG, "Fetched GitHub tag: $updateCommitHash")
171172

172173
return Update(
173-
getString(R.string.commit_hash) != updateCommitHash,
174+
currentCommitHash() != updateCommitHash,
174175
foundAsset.browserDownloadUrl,
175176
updateCommitHash,
176177
found.body,

app/src/main/res/layout/main_settings.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@
134134
android:layout_width="wrap_content"
135135
android:layout_height="wrap_content"
136136
android:padding="10dp"
137-
android:text="@string/commit_hash"
138-
android:textColor="?attr/textColor" />
137+
android:textColor="?attr/textColor"
138+
tools:text="1234567" />
139139

140140
<TextView
141141
android:id="@+id/delimiter1"
@@ -152,7 +152,6 @@
152152
android:textColor="?attr/textColor"
153153
tools:text="21/03/2024 09:02 pm" />
154154
</LinearLayout>
155-
156155
</LinearLayout>
157156
</ScrollView>
158157
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)