Added worktree-layered build config: resolves .gittally.yml from the worktree for per-branch build settings, with precedence worktree > .git > project; pinned secrets, server-side keys, and sandbox policy to .git.

This commit is contained in:
Michael Hoennig
2026-07-09 09:20:34 +02:00
parent 095e6fa44f
commit fb6d4501b9
7 changed files with 209 additions and 17 deletions
@@ -19,8 +19,33 @@ class ConfigLoader {
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
fun load(workingDir: Path = Paths.get(".")): GitTallyConfig {
val raw = loadRaw(workingDir)
fun load(workingDir: Path = Paths.get(".")): GitTallyConfig = toConfig(loadRaw(workingDir))
/**
* Config for building a branch in [worktreeDir]: the worktree's `.gittally.yml`
* (the committed config of the branch being built) overrides the primary/`.git`
* config, giving the precedence worktree > `.git` > project. So a branch controls
* its own build settings (`buildCommand`, `cleanCommand`, `artifactDirs`,
* `docker.image`/`env`, …).
*
* The [pinned][stripPinned] keys are the exception: secrets (`git`), `gitea`/`server`
* settings, and the docker sandbox policy (`docker.enabled`/`docker.network`) always
* come from `.git`/primary — a branch must never be able to disable its own container,
* change its network mode, or reach the credentials. They are stripped from the
* worktree layer before it is merged, so a worktree cannot set them at all.
*
* With no worktree `.gittally.yml` this is identical to [load].
*/
fun loadForWorktree(
workingDir: Path,
worktreeDir: Path,
): GitTallyConfig {
val primary = loadRaw(workingDir)
val worktree = stripPinned(loadFile(worktreeDir.resolve(".gittally.yml").toFile()))
return toConfig(deepMerge(primary, worktree))
}
private fun toConfig(raw: Map<String, Any?>): GitTallyConfig {
val config =
if (raw.isEmpty()) {
GitTallyConfig()
@@ -30,6 +55,32 @@ class ConfigLoader {
return defaultPublicBaseUrl(config)
}
/**
* Removes the keys a build worktree must never override: the secret/server-side
* top-level sections and the per-branch docker sandbox policy. See [loadForWorktree].
*/
@Suppress("UNCHECKED_CAST")
private fun stripPinned(worktree: Map<String, Any?>): Map<String, Any?> {
if (worktree.isEmpty()) {
return worktree
}
val result = worktree.toMutableMap()
PINNED_TOP_LEVEL_KEYS.forEach { result.remove(it) }
val branches = result["branches"] as? Map<String, Any?>
if (branches != null) {
result["branches"] =
branches.mapValues { (_, value) ->
val branch = value as? Map<String, Any?> ?: return@mapValues value
val docker = branch["docker"] as? Map<String, Any?> ?: return@mapValues branch
val strippedDocker = docker.toMutableMap().apply { PINNED_DOCKER_KEYS.forEach { remove(it) } }
branch.toMutableMap().apply {
if (strippedDocker.isEmpty()) remove("docker") else put("docker", strippedDocker)
}
}
}
return result
}
/** Legacy default: an empty `server.publicBaseUrl` becomes `https://<nginx.serverName>/`. */
private fun defaultPublicBaseUrl(config: GitTallyConfig): GitTallyConfig {
if (config.server.publicBaseUrl.isNotBlank() ||
@@ -88,4 +139,12 @@ class ConfigLoader {
}
return result
}
companion object {
/** Top-level sections a build worktree must never override: secrets and server-side settings. */
private val PINNED_TOP_LEVEL_KEYS = setOf("git", "gitea", "server")
/** Per-branch `docker` keys the worktree must never override: the sandbox policy. */
private val PINNED_DOCKER_KEYS = setOf("enabled", "network")
}
}