From b06b33ff5720241245a036a99c84bcdd1fe59a88 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 27 Nov 2026 17:24:15 +0000 Subject: [PATCH 1/3] fix(release): release the cwd checkout and refuse tracked changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A release started from a nested clone (for example .worktrees/release-x) matched the configured main checkout by cwd prefix, so pkgmgr chdir'd into the main checkout and its commit -a swept that checkout's unfinished tracked edits into the release commit. Implicit selection for release now uses the cwd's git toplevel, an explicit identifier keeps the configured directory, and ensure_clean_and_synced refuses a tree with tracked changes before any sync or version bump. ๐ŸŽฏ fix 92 % ยท ๐Ÿ’ฅ break 3 % ยท ๐Ÿ’ธ tokens โ€“ ยท โฑ๏ธ velocity โ€“ ยท ๐Ÿ›ก๏ธ security 100 % ยท ๐Ÿ† score 89 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- src/pkgmgr/actions/release/git_ops.py | 12 ++ src/pkgmgr/cli/dispatch.py | 11 ++ .../integration/test_release_cwd_checkout.py | 105 ++++++++++++++++++ .../test_git_ops_ensure_clean_and_synced.py | 31 ++++++ 4 files changed, 159 insertions(+) create mode 100644 tests/integration/test_release_cwd_checkout.py diff --git a/src/pkgmgr/actions/release/git_ops.py b/src/pkgmgr/actions/release/git_ops.py index 4004801..f405cb4 100644 --- a/src/pkgmgr/actions/release/git_ops.py +++ b/src/pkgmgr/actions/release/git_ops.py @@ -1,5 +1,6 @@ from __future__ import annotations +from pkgmgr.core.git import run from pkgmgr.core.git.commands import ( fetch, pull_ff_only, @@ -14,7 +15,18 @@ def ensure_clean_and_synced(*, preview: bool = False) -> None: Always run a pull BEFORE modifying anything. Uses --ff-only to avoid creating merge commits automatically. If no upstream is configured, we skip. + + Raises: + RuntimeError: a tracked file is modified; the release commit uses + ``commit -a`` and would ship it. """ + dirty = run(["status", "--porcelain", "--untracked-files=no"]) + if dirty: + raise RuntimeError( + "Tracked changes in the release checkout; commit or park them " + f"first, the release commit would include them:\n{dirty}" + ) + upstream = get_upstream_ref() if not upstream: print("[INFO] No upstream configured for current branch. Skipping pull.") diff --git a/src/pkgmgr/cli/dispatch.py b/src/pkgmgr/cli/dispatch.py index 651ef80..cbbfc5f 100644 --- a/src/pkgmgr/cli/dispatch.py +++ b/src/pkgmgr/cli/dispatch.py @@ -23,6 +23,7 @@ from pkgmgr.cli.commands import ( ) from pkgmgr.cli.context import CLIContext from pkgmgr.cli.proxy import maybe_handle_proxy +from pkgmgr.core.git import GitRunError, run from pkgmgr.core.repository.dir import get_repo_dir from pkgmgr.core.repository.selected import get_selected_repos @@ -60,6 +61,14 @@ def _select_repo_for_current_directory(ctx: CLIContext) -> list[dict[str, Any]]: return [matches[0][1]] +def _at_cwd_checkout(selected: list[dict[str, Any]]) -> list[dict[str, Any]]: + try: + toplevel = run(["rev-parse", "--show-toplevel"]) + except GitRunError: + return selected + return [{**repo, "directory": toplevel} for repo in selected] + + def dispatch_command(args, ctx: CLIContext) -> None: if maybe_handle_proxy(args, ctx): return @@ -143,6 +152,8 @@ def dispatch_command(args, ctx: CLIContext) -> None: return if args.command == "release": + if not _has_explicit_selection(args): + selected = _at_cwd_checkout(selected) handle_release(args, ctx, selected) return diff --git a/tests/integration/test_release_cwd_checkout.py b/tests/integration/test_release_cwd_checkout.py new file mode 100644 index 0000000..25d2797 --- /dev/null +++ b/tests/integration/test_release_cwd_checkout.py @@ -0,0 +1,105 @@ +from __future__ import annotations + +import io +import os +import subprocess +import tempfile +import unittest +from contextlib import redirect_stdout +from pathlib import Path +from unittest.mock import patch + +from pkgmgr.actions.release.git_ops import ensure_clean_and_synced +from pkgmgr.cli.context import CLIContext +from pkgmgr.cli.dispatch import dispatch_command +from pkgmgr.cli.parser import create_parser + + +def git(cwd: str, *args: str) -> str: + return subprocess.run( + ["git", *args], cwd=cwd, check=True, capture_output=True, text=True + ).stdout.strip() + + +class TestReleaseCwdCheckout(unittest.TestCase): + def setUp(self) -> None: + self._tmp = tempfile.TemporaryDirectory() + self.addCleanup(self._tmp.cleanup) + root = Path(os.path.realpath(self._tmp.name)) + (root / "gitconfig").write_text( + "[user]\n\tname = t\n\temail = t@example.org\n" + "[commit]\n\tgpgsign = false\n" + "[init]\n\tdefaultBranch = main\n" + ) + env = patch.dict( + os.environ, + {"GIT_CONFIG_GLOBAL": str(root / "gitconfig"), "GIT_CONFIG_NOSYSTEM": "1"}, + ) + env.start() + self.addCleanup(env.stop) + cwd = os.getcwd() + self.addCleanup(os.chdir, cwd) + + self.main = str(root / "app") + git(str(root), "init", "-q", self.main) + Path(self.main, "app.py").write_text("v1\n") + git(self.main, "add", "app.py") + git(self.main, "commit", "-q", "-m", "init") + self.clone = os.path.join(self.main, ".worktrees", "release") + git(str(root), "clone", "-q", self.main, self.clone) + Path(self.main, "app.py").write_text("unfinished work\n") + + self.ctx = CLIContext( + config_merged={}, + repositories_base_dir=str(root / "repos"), + all_repositories=[ + { + "provider": "github.com", + "account": "t", + "repository": "app", + "directory": self.main, + } + ], + binaries_dir=str(root / "bin"), + user_config_path=str(root / "config.yaml"), + ) + + def _release_cwd(self, cwd: str, *argv: str) -> str: + seen: list[str] = [] + os.chdir(cwd) + args = create_parser("test").parse_args(["release", *argv, "--no-publish"]) + with ( + patch( + "pkgmgr.cli.commands.release.run_release", + side_effect=lambda **_kwargs: seen.append(os.getcwd()), + ), + redirect_stdout(io.StringIO()), + ): + dispatch_command(args, self.ctx) + self.assertEqual(len(seen), 1) + return seen[0] + + def test_release_from_a_nested_clone_runs_in_the_clone(self) -> None: + self.assertEqual(self._release_cwd(self.clone, "patch"), self.clone) + + def test_explicit_identifier_keeps_the_configured_checkout(self) -> None: + self.assertEqual(self._release_cwd(self.clone, "patch", "app"), self.main) + + def test_tracked_changes_refuse_the_release(self) -> None: + os.chdir(self.main) + + with self.assertRaises(RuntimeError) as cm: + ensure_clean_and_synced(preview=False) + + self.assertIn("app.py", str(cm.exception)) + self.assertEqual(Path(self.main, "app.py").read_text(), "unfinished work\n") + + def test_clean_clone_passes_the_check(self) -> None: + os.chdir(self.clone) + + with redirect_stdout(io.StringIO()): + ensure_clean_and_synced(preview=False) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/pkgmgr/actions/release/test_git_ops_ensure_clean_and_synced.py b/tests/unit/pkgmgr/actions/release/test_git_ops_ensure_clean_and_synced.py index e9bf75c..896a74c 100644 --- a/tests/unit/pkgmgr/actions/release/test_git_ops_ensure_clean_and_synced.py +++ b/tests/unit/pkgmgr/actions/release/test_git_ops_ensure_clean_and_synced.py @@ -6,6 +6,7 @@ from unittest.mock import patch from pkgmgr.actions.release.git_ops import ensure_clean_and_synced +@patch("pkgmgr.actions.release.git_ops.run", return_value="") class TestEnsureCleanAndSynced(unittest.TestCase): @patch("pkgmgr.actions.release.git_ops.pull_ff_only") @patch("pkgmgr.actions.release.git_ops.fetch") @@ -15,6 +16,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase): mock_get_upstream_ref, mock_fetch, mock_pull_ff_only, + _mock_run, ) -> None: mock_get_upstream_ref.return_value = None @@ -31,6 +33,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase): mock_get_upstream_ref, mock_fetch, mock_pull_ff_only, + _mock_run, ) -> None: mock_get_upstream_ref.return_value = "origin/main" @@ -53,6 +56,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase): mock_get_upstream_ref, mock_fetch, mock_pull_ff_only, + _mock_run, ) -> None: mock_get_upstream_ref.return_value = "origin/main" @@ -66,3 +70,30 @@ class TestEnsureCleanAndSynced(unittest.TestCase): preview=False, ) mock_pull_ff_only.assert_called_once_with(preview=False) + + @patch("pkgmgr.actions.release.git_ops.pull_ff_only") + @patch("pkgmgr.actions.release.git_ops.fetch") + @patch("pkgmgr.actions.release.git_ops.get_upstream_ref") + def test_tracked_changes_refuse_before_any_sync( + self, + mock_get_upstream_ref, + mock_fetch, + mock_pull_ff_only, + mock_run, + ) -> None: + mock_run.return_value = " M src/app.py" + + with self.assertRaises(RuntimeError) as cm: + ensure_clean_and_synced(preview=True) + + self.assertIn("src/app.py", str(cm.exception)) + mock_run.assert_called_once_with( + ["status", "--porcelain", "--untracked-files=no"] + ) + mock_get_upstream_ref.assert_not_called() + mock_fetch.assert_not_called() + mock_pull_ff_only.assert_not_called() + + +if __name__ == "__main__": + unittest.main() From 0917ceffdbdfd82df7018fd55d0c72c3c86cab6e Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 11:52:06 +0000 Subject: [PATCH 2/3] refactor(release): keep the cwd checkout out of dispatch and reuse the test module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The quality gate's differential check flagged PR 12: dispatch_command grew in length and complexity, tests/integration gained a 37th item and the new test module cloned test_push_mirror_leases' setup. The cwd-checkout step moves into release_at_cwd in the release command, has_explicit_selection moves next to get_selected_repos, and the four cwd tests move into test_release_publish_hook.py with an env-based git identity. Behaviour unchanged. ๐ŸŽฏ Fix: 90 % ๐Ÿ’ฅ Break: 3 % ๐Ÿ’ธ Tokens: โ€“ โฑ๏ธ Velocity: โ€“ ๐Ÿ›ก๏ธ Security: 100 % ๐Ÿ† Score: 87 ๐Ÿšฆ Gate: โœ… 90/5, tokens and velocity <= 100, security 100 % Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- src/pkgmgr/cli/commands/__init__.py | 3 +- src/pkgmgr/cli/commands/release.py | 33 ++++++ src/pkgmgr/cli/dispatch.py | 29 +---- src/pkgmgr/core/repository/selected.py | 18 +++ .../integration/test_release_cwd_checkout.py | 105 ------------------ .../integration/test_release_publish_hook.py | 92 +++++++++++++++ 6 files changed, 149 insertions(+), 131 deletions(-) delete mode 100644 tests/integration/test_release_cwd_checkout.py diff --git a/src/pkgmgr/cli/commands/__init__.py b/src/pkgmgr/cli/commands/__init__.py index ffb5e5d..61e210c 100644 --- a/src/pkgmgr/cli/commands/__init__.py +++ b/src/pkgmgr/cli/commands/__init__.py @@ -6,7 +6,7 @@ from .config import handle_config from .make import handle_make from .mirror import handle_mirror_command from .publish import handle_publish -from .release import handle_release +from .release import handle_release, release_at_cwd from .repos import handle_repos_command from .review import handle_review from .tools import handle_tools_command @@ -29,4 +29,5 @@ __all__ = [ "handle_tools_command", "handle_version", "handle_worktree", + "release_at_cwd", ] diff --git a/src/pkgmgr/cli/commands/release.py b/src/pkgmgr/cli/commands/release.py index 2f72d51..6b1d3c4 100644 --- a/src/pkgmgr/cli/commands/release.py +++ b/src/pkgmgr/cli/commands/release.py @@ -7,12 +7,45 @@ from typing import Any from pkgmgr.actions.publish import publish as run_publish from pkgmgr.actions.release import release as run_release from pkgmgr.cli.context import CLIContext +from pkgmgr.core.git import GitRunError, run from pkgmgr.core.repository.dir import get_repo_dir from pkgmgr.core.repository.identifier import get_repo_identifier +from pkgmgr.core.repository.selected import has_explicit_selection Repository = dict[str, Any] +def _at_cwd_checkout(args, selected: list[Repository]) -> list[Repository]: + """ + Args: + args: parsed CLI arguments. + selected: the repositories dispatch picked. + + Returns: + selected unchanged after an explicit selection or outside git; else each + repository pointed at the current directory's git toplevel, so a release + from a nested clone never commits in the configured main checkout. + """ + if has_explicit_selection(args): + return selected + try: + toplevel = run(["rev-parse", "--show-toplevel"]) + except GitRunError: + return selected + return [{**repo, "directory": toplevel} for repo in selected] + + +def release_at_cwd(args, ctx: CLIContext, selected: list[Repository]) -> None: + """ + Args: + args: parsed CLI arguments. + ctx: the CLI context. + selected: the repositories dispatch picked; without an explicit + selection each is released in the current directory's checkout. + """ + handle_release(args, ctx, _at_cwd_checkout(args, selected)) + + def handle_release( args, ctx: CLIContext, diff --git a/src/pkgmgr/cli/dispatch.py b/src/pkgmgr/cli/dispatch.py index cbbfc5f..5f3fcd2 100644 --- a/src/pkgmgr/cli/dispatch.py +++ b/src/pkgmgr/cli/dispatch.py @@ -14,28 +14,17 @@ from pkgmgr.cli.commands import ( handle_make, handle_mirror_command, handle_publish, - handle_release, handle_repos_command, handle_review, handle_tools_command, handle_version, handle_worktree, + release_at_cwd, ) from pkgmgr.cli.context import CLIContext from pkgmgr.cli.proxy import maybe_handle_proxy -from pkgmgr.core.git import GitRunError, run from pkgmgr.core.repository.dir import get_repo_dir -from pkgmgr.core.repository.selected import get_selected_repos - - -def _has_explicit_selection(args) -> bool: - return bool( - getattr(args, "all", False) - or getattr(args, "identifiers", []) - or getattr(args, "category", []) - or getattr(args, "tag", []) - or getattr(args, "string", "") - ) +from pkgmgr.core.repository.selected import get_selected_repos, has_explicit_selection def _select_repo_for_current_directory(ctx: CLIContext) -> list[dict[str, Any]]: @@ -61,14 +50,6 @@ def _select_repo_for_current_directory(ctx: CLIContext) -> list[dict[str, Any]]: return [matches[0][1]] -def _at_cwd_checkout(selected: list[dict[str, Any]]) -> list[dict[str, Any]]: - try: - toplevel = run(["rev-parse", "--show-toplevel"]) - except GitRunError: - return selected - return [{**repo, "directory": toplevel} for repo in selected] - - def dispatch_command(args, ctx: CLIContext) -> None: if maybe_handle_proxy(args, ctx): return @@ -109,7 +90,7 @@ def dispatch_command(args, ctx: CLIContext) -> None: if args.command in commands_with_selection: selected = ( get_selected_repos(args, ctx.all_repositories) - if _has_explicit_selection(args) + if has_explicit_selection(args) else _select_repo_for_current_directory(ctx) ) else: @@ -152,9 +133,7 @@ def dispatch_command(args, ctx: CLIContext) -> None: return if args.command == "release": - if not _has_explicit_selection(args): - selected = _at_cwd_checkout(selected) - handle_release(args, ctx, selected) + release_at_cwd(args, ctx, selected) return if args.command == "publish": diff --git a/src/pkgmgr/core/repository/selected.py b/src/pkgmgr/core/repository/selected.py index 704d294..20e5cbb 100644 --- a/src/pkgmgr/core/repository/selected.py +++ b/src/pkgmgr/core/repository/selected.py @@ -136,6 +136,24 @@ def _maybe_filter_ignored(args, repos: list[Repository]) -> list[Repository]: return filter_ignored(repos) +def has_explicit_selection(args) -> bool: + """ + Args: + args: parsed CLI arguments. + + Returns: + True when --all, an identifier, --category, --tag or --string picks + the repositories instead of the current directory. + """ + return bool( + getattr(args, "all", False) + or getattr(args, "identifiers", []) + or getattr(args, "category", []) + or getattr(args, "tag", []) + or getattr(args, "string", "") + ) + + def get_selected_repos(args, all_repositories: list[Repository]) -> list[Repository]: """ Compute the list of repositories selected by CLI arguments. diff --git a/tests/integration/test_release_cwd_checkout.py b/tests/integration/test_release_cwd_checkout.py deleted file mode 100644 index 25d2797..0000000 --- a/tests/integration/test_release_cwd_checkout.py +++ /dev/null @@ -1,105 +0,0 @@ -from __future__ import annotations - -import io -import os -import subprocess -import tempfile -import unittest -from contextlib import redirect_stdout -from pathlib import Path -from unittest.mock import patch - -from pkgmgr.actions.release.git_ops import ensure_clean_and_synced -from pkgmgr.cli.context import CLIContext -from pkgmgr.cli.dispatch import dispatch_command -from pkgmgr.cli.parser import create_parser - - -def git(cwd: str, *args: str) -> str: - return subprocess.run( - ["git", *args], cwd=cwd, check=True, capture_output=True, text=True - ).stdout.strip() - - -class TestReleaseCwdCheckout(unittest.TestCase): - def setUp(self) -> None: - self._tmp = tempfile.TemporaryDirectory() - self.addCleanup(self._tmp.cleanup) - root = Path(os.path.realpath(self._tmp.name)) - (root / "gitconfig").write_text( - "[user]\n\tname = t\n\temail = t@example.org\n" - "[commit]\n\tgpgsign = false\n" - "[init]\n\tdefaultBranch = main\n" - ) - env = patch.dict( - os.environ, - {"GIT_CONFIG_GLOBAL": str(root / "gitconfig"), "GIT_CONFIG_NOSYSTEM": "1"}, - ) - env.start() - self.addCleanup(env.stop) - cwd = os.getcwd() - self.addCleanup(os.chdir, cwd) - - self.main = str(root / "app") - git(str(root), "init", "-q", self.main) - Path(self.main, "app.py").write_text("v1\n") - git(self.main, "add", "app.py") - git(self.main, "commit", "-q", "-m", "init") - self.clone = os.path.join(self.main, ".worktrees", "release") - git(str(root), "clone", "-q", self.main, self.clone) - Path(self.main, "app.py").write_text("unfinished work\n") - - self.ctx = CLIContext( - config_merged={}, - repositories_base_dir=str(root / "repos"), - all_repositories=[ - { - "provider": "github.com", - "account": "t", - "repository": "app", - "directory": self.main, - } - ], - binaries_dir=str(root / "bin"), - user_config_path=str(root / "config.yaml"), - ) - - def _release_cwd(self, cwd: str, *argv: str) -> str: - seen: list[str] = [] - os.chdir(cwd) - args = create_parser("test").parse_args(["release", *argv, "--no-publish"]) - with ( - patch( - "pkgmgr.cli.commands.release.run_release", - side_effect=lambda **_kwargs: seen.append(os.getcwd()), - ), - redirect_stdout(io.StringIO()), - ): - dispatch_command(args, self.ctx) - self.assertEqual(len(seen), 1) - return seen[0] - - def test_release_from_a_nested_clone_runs_in_the_clone(self) -> None: - self.assertEqual(self._release_cwd(self.clone, "patch"), self.clone) - - def test_explicit_identifier_keeps_the_configured_checkout(self) -> None: - self.assertEqual(self._release_cwd(self.clone, "patch", "app"), self.main) - - def test_tracked_changes_refuse_the_release(self) -> None: - os.chdir(self.main) - - with self.assertRaises(RuntimeError) as cm: - ensure_clean_and_synced(preview=False) - - self.assertIn("app.py", str(cm.exception)) - self.assertEqual(Path(self.main, "app.py").read_text(), "unfinished work\n") - - def test_clean_clone_passes_the_check(self) -> None: - os.chdir(self.clone) - - with redirect_stdout(io.StringIO()): - ensure_clean_and_synced(preview=False) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/integration/test_release_publish_hook.py b/tests/integration/test_release_publish_hook.py index 9599aee..9073e06 100644 --- a/tests/integration/test_release_publish_hook.py +++ b/tests/integration/test_release_publish_hook.py @@ -1,10 +1,18 @@ from __future__ import annotations +import io +import os +import subprocess import tempfile import unittest +from contextlib import redirect_stdout +from pathlib import Path from types import SimpleNamespace from unittest.mock import patch +from pkgmgr.actions.release.git_ops import ensure_clean_and_synced +from pkgmgr.cli.context import CLIContext + class TestIntegrationReleasePublishHook(unittest.TestCase): def _ctx(self) -> SimpleNamespace: @@ -65,5 +73,89 @@ class TestIntegrationReleasePublishHook(unittest.TestCase): m_publish.assert_not_called() +class TestReleaseCwdCheckout(unittest.TestCase): + def _sh(self, cwd: str, *argv: str) -> None: + subprocess.run(["git", *argv], cwd=cwd, check=True, capture_output=True) + + def setUp(self) -> None: + self._tmp = tempfile.TemporaryDirectory() + self.addCleanup(self._tmp.cleanup) + base = os.path.realpath(self._tmp.name) + identity = { + f"GIT_{role}_{field}": value + for role in ("AUTHOR", "COMMITTER") + for field, value in (("NAME", "t"), ("EMAIL", "t@example.org")) + } + scrubbed = patch.dict( + os.environ, + {**identity, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"}, + ) + scrubbed.start() + self.addCleanup(scrubbed.stop) + self.addCleanup(os.chdir, os.getcwd()) + + self.main = os.path.join(base, "app") + self._sh(base, "init", "-q", "-b", "main", self.main) + Path(self.main, "app.py").write_text("v1\n") + self._sh(self.main, "add", "app.py") + self._sh(self.main, "commit", "-q", "--no-gpg-sign", "-m", "init") + self.clone = os.path.join(self.main, ".worktrees", "release") + self._sh(base, "clone", "-q", self.main, self.clone) + Path(self.main, "app.py").write_text("unfinished work\n") + self.ctx = CLIContext( + config_merged={}, + repositories_base_dir=os.path.join(base, "repos"), + all_repositories=[ + { + "provider": "github.com", + "account": "t", + "repository": "app", + "directory": self.main, + } + ], + binaries_dir=os.path.join(base, "bin"), + user_config_path=os.path.join(base, "config.yaml"), + ) + + def _released_in(self, cwd: str, *argv: str) -> str: + from pkgmgr.cli.dispatch import dispatch_command + from pkgmgr.cli.parser import create_parser + + seen: list[str] = [] + os.chdir(cwd) + args = create_parser("test").parse_args(["release", *argv, "--no-publish"]) + with ( + patch( + "pkgmgr.cli.commands.release.run_release", + side_effect=lambda **_kwargs: seen.append(os.getcwd()), + ), + redirect_stdout(io.StringIO()), + ): + dispatch_command(args, self.ctx) + self.assertEqual(len(seen), 1) + return seen[0] + + def test_release_from_a_nested_clone_runs_in_the_clone(self) -> None: + self.assertEqual(self._released_in(self.clone, "patch"), self.clone) + + def test_explicit_identifier_keeps_the_configured_checkout(self) -> None: + self.assertEqual(self._released_in(self.clone, "patch", "app"), self.main) + + def test_tracked_changes_refuse_the_release(self) -> None: + os.chdir(self.main) + + with self.assertRaises(RuntimeError) as cm: + ensure_clean_and_synced(preview=False) + + self.assertIn("app.py", str(cm.exception)) + self.assertEqual(Path(self.main, "app.py").read_text(), "unfinished work\n") + + def test_clean_clone_passes_the_check(self) -> None: + os.chdir(self.clone) + + with redirect_stdout(io.StringIO()): + ensure_clean_and_synced(preview=False) + + if __name__ == "__main__": unittest.main() From 28ff2f7a8e34502ad3d617499fbedce7893a20e5 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 28 Nov 2026 14:28:34 +0000 Subject: [PATCH 3/3] refactor(release): tighten the release-at-cwd code to Codacy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The docstrings of _at_cwd_checkout, release_at_cwd and has_explicit_selection end with a blank line after their last section (pydocstyle D413). TestReleaseCwdCheckout calls git through shutil.which and skips without git (Bandit B607). Kept on purpose, each contradicts another rule: D205/D212/D415 want a summary sentence (AGENTS allows docstrings only as parameter docs), D406/D407 numpy sections (AGENTS: Args: docstrings), B404/B603 on test subprocess calls (silencing needs # nosec, which quality clean.tool-pragma forbids). | ๐ŸŒฟ Branch | ๐Ÿ“Š Diff stat | ๐ŸŽฏ Fix | ๐Ÿ’ฅ Break | ๐Ÿ’ธ Tokens | โฑ๏ธ Velocity | ๐Ÿ›ก๏ธ Security | ๐Ÿ† Score | Gate | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | fix/release-cwd | 3 files +8/-1 | 90 % | 2 % | โ€“ | โ€“ | 100 % | 88 | โœ… 90/5, tokens and velocity <= 100, security 100 % | Refs: https://github.com/kevinveenbirkenbach/package-manager/pull/12 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- src/pkgmgr/cli/commands/release.py | 2 ++ src/pkgmgr/core/repository/selected.py | 1 + tests/integration/test_release_publish_hook.py | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pkgmgr/cli/commands/release.py b/src/pkgmgr/cli/commands/release.py index 6b1d3c4..8f107c2 100644 --- a/src/pkgmgr/cli/commands/release.py +++ b/src/pkgmgr/cli/commands/release.py @@ -25,6 +25,7 @@ def _at_cwd_checkout(args, selected: list[Repository]) -> list[Repository]: selected unchanged after an explicit selection or outside git; else each repository pointed at the current directory's git toplevel, so a release from a nested clone never commits in the configured main checkout. + """ if has_explicit_selection(args): return selected @@ -42,6 +43,7 @@ def release_at_cwd(args, ctx: CLIContext, selected: list[Repository]) -> None: ctx: the CLI context. selected: the repositories dispatch picked; without an explicit selection each is released in the current directory's checkout. + """ handle_release(args, ctx, _at_cwd_checkout(args, selected)) diff --git a/src/pkgmgr/core/repository/selected.py b/src/pkgmgr/core/repository/selected.py index 20e5cbb..f76422c 100644 --- a/src/pkgmgr/core/repository/selected.py +++ b/src/pkgmgr/core/repository/selected.py @@ -144,6 +144,7 @@ def has_explicit_selection(args) -> bool: Returns: True when --all, an identifier, --category, --tag or --string picks the repositories instead of the current directory. + """ return bool( getattr(args, "all", False) diff --git a/tests/integration/test_release_publish_hook.py b/tests/integration/test_release_publish_hook.py index 9073e06..562001c 100644 --- a/tests/integration/test_release_publish_hook.py +++ b/tests/integration/test_release_publish_hook.py @@ -2,6 +2,7 @@ from __future__ import annotations import io import os +import shutil import subprocess import tempfile import unittest @@ -13,6 +14,8 @@ from unittest.mock import patch from pkgmgr.actions.release.git_ops import ensure_clean_and_synced from pkgmgr.cli.context import CLIContext +GIT = shutil.which("git") + class TestIntegrationReleasePublishHook(unittest.TestCase): def _ctx(self) -> SimpleNamespace: @@ -73,9 +76,10 @@ class TestIntegrationReleasePublishHook(unittest.TestCase): m_publish.assert_not_called() +@unittest.skipUnless(GIT, "git builds the clones under test") class TestReleaseCwdCheckout(unittest.TestCase): def _sh(self, cwd: str, *argv: str) -> None: - subprocess.run(["git", *argv], cwd=cwd, check=True, capture_output=True) + subprocess.run([GIT, *argv], cwd=cwd, check=True, capture_output=True) def setUp(self) -> None: self._tmp = tempfile.TemporaryDirectory()