From 908eedb831fb9402f49ed554f8b3df57fba1b359 Mon Sep 17 00:00:00 2001 From: davotoula Date: Thu, 28 May 2026 11:22:17 +0200 Subject: [PATCH] fix(amethyst): set git cwd in versionName branch detection so worktrees work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ProcessBuilder("git", …) inherited the daemon's working directory, which in a git worktree (or when the Gradle daemon was started elsewhere) is not the project root. git printed "fatal: not a git repository"; with redirectErrorStream that landed in stdout, got cleaned to dashes, and truncated to exactly "fatal--not-a-git-rep" — visible as the version suffix in installed builds. Pass rootDir to ProcessBuilder.directory() so git always runs at the worktree root, and check the exit code so any future failure falls back to "unknown" instead of leaking stderr into the version name. Co-Authored-By: Claude Opus 4.7 (1M context) --- amethyst/build.gradle.kts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/amethyst/build.gradle.kts b/amethyst/build.gradle.kts index a38aa8c62e..5c99450c07 100644 --- a/amethyst/build.gradle.kts +++ b/amethyst/build.gradle.kts @@ -8,10 +8,11 @@ plugins { alias(libs.plugins.googleKsp) } -fun getCurrentBranch(): String = +fun getCurrentBranch(workingDir: java.io.File): String = try { val process = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD") + .directory(workingDir) .redirectErrorStream(true) .start() val branch = @@ -19,15 +20,18 @@ fun getCurrentBranch(): String = .bufferedReader() .use { it.readText() } .trim() - process.waitFor() - branch + val exitCode = process.waitFor() + if (exitCode != 0) "unknown" else branch } catch (e: Exception) { println("Could not determine git branch: ${e.message}") "unknown" } -fun generateVersionName(baseVersion: String): String { - val currentBranch = getCurrentBranch() +fun generateVersionName( + baseVersion: String, + workingDir: java.io.File, +): String { + val currentBranch = getCurrentBranch(workingDir) if (currentBranch == "main" || currentBranch == "master" || currentBranch == "unknown" || currentBranch == "HEAD") { return baseVersion @@ -73,7 +77,7 @@ android { .get() .toInt() versionCode = 447 - versionName = generateVersionName(libs.versions.app.get()) + versionName = generateVersionName(libs.versions.app.get(), rootDir) buildConfigField("String", "RELEASE_NOTES_ID", "\"8ec0d94550b5538115226c6858159b1115713c9c6ed942173bd4fd5d292d8ba6\"") testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"