diff --git a/pgdevkit/testdb/naming.py b/pgdevkit/testdb/naming.py index a954b8c..44ca574 100644 --- a/pgdevkit/testdb/naming.py +++ b/pgdevkit/testdb/naming.py @@ -1,6 +1,7 @@ from __future__ import annotations import hashlib +import os import re import subprocess from pathlib import Path @@ -25,7 +26,15 @@ def slugify(value: str) -> str: def current_branch(cwd: Path | None = None) -> str: - """Return the branch checked out in the git worktree rooted at cwd.""" + """Return the branch checked out in the git worktree rooted at cwd. + + PGDEVKIT_TESTDB_BRANCH overrides this when set -- lets concurrent CI + checkouts (which all otherwise resolve to detached-HEAD "HEAD") get + distinct database names on a shared test-DB container. + """ + override = os.environ.get("PGDEVKIT_TESTDB_BRANCH") + if override: + return override result = subprocess.run( ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd, diff --git a/pyproject.toml b/pyproject.toml index 0949bec..7217cb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ packages = ["pgdevkit"] [project] name = "pgdevkit" -version = "0.7.1" +version = "0.7.2" description = "A helper for developing with Postgres" readme = "README.md" requires-python = ">=3.14" diff --git a/tests/testdb/test_naming.py b/tests/testdb/test_naming.py index 6a71ebd..66cd380 100644 --- a/tests/testdb/test_naming.py +++ b/tests/testdb/test_naming.py @@ -53,6 +53,14 @@ def test_current_branch_reads_the_checked_out_branch(tmp_path: Path): assert current_branch(tmp_path) == "my-feature" +def test_current_branch_env_override_skips_git_entirely(tmp_path: Path, monkeypatch): + # Not a git repo at all -- the override must short-circuit before the + # `git rev-parse` subprocess call, which would otherwise raise. + monkeypatch.setenv("PGDEVKIT_TESTDB_BRANCH", "ci-job-42") + + assert current_branch(tmp_path) == "ci-job-42" + + def _init_repo(repo: Path, initial_branch: str) -> None: repo.mkdir() subprocess.run(["git", "init", "-q"], cwd=repo, check=True) diff --git a/uv.lock b/uv.lock index cb04842..0fe71f7 100644 --- a/uv.lock +++ b/uv.lock @@ -313,7 +313,7 @@ wheels = [ [[package]] name = "pgdevkit" -version = "0.7.1" +version = "0.7.2" source = { editable = "." } dependencies = [ { name = "docker" },