Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b06b33ff57 |
No files matched your search
@@ -8,7 +8,6 @@ import sys
|
|||||||
import time
|
import time
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import asdict, dataclass, field
|
from dataclasses import asdict, dataclass, field
|
||||||
from functools import cache
|
|
||||||
from graphlib import CycleError, TopologicalSorter
|
from graphlib import CycleError, TopologicalSorter
|
||||||
|
|
||||||
from pkgmgr.actions.worktree.hooks import FINI_HOOK, hook_command, run_hook
|
from pkgmgr.actions.worktree.hooks import FINI_HOOK, hook_command, run_hook
|
||||||
@@ -38,7 +37,6 @@ Snapshot = tuple[
|
|||||||
dict[int, str],
|
dict[int, str],
|
||||||
dict[str, tuple[str | None, int | None, int | None, bool]],
|
dict[str, tuple[str | None, int | None, int | None, bool]],
|
||||||
Registrations,
|
Registrations,
|
||||||
Callable[[], dict[str, str]],
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# PROC_PID_INIT_INO: only the initial PID namespace sees every process of the host.
|
# PROC_PID_INIT_INO: only the initial PID namespace sees every process of the host.
|
||||||
@@ -667,7 +665,7 @@ def _build(
|
|||||||
tree: Worktree | None,
|
tree: Worktree | None,
|
||||||
snapshot: Snapshot,
|
snapshot: Snapshot,
|
||||||
) -> Item:
|
) -> Item:
|
||||||
local, remote, cwds, tracking, registrations, held = snapshot
|
local, remote, cwds, tracking, registrations = snapshot
|
||||||
upstream, ahead, behind, gone = tracking.get(
|
upstream, ahead, behind, gone = tracking.get(
|
||||||
branch or "", (None, None, None, False)
|
branch or "", (None, None, None, False)
|
||||||
)
|
)
|
||||||
@@ -761,7 +759,7 @@ def _build(
|
|||||||
and (time.time() - item.last_activity) / 3600 < repo.min_age
|
and (time.time() - item.last_activity) / 3600 < repo.min_age
|
||||||
):
|
):
|
||||||
item.skip = f"younger than {repo.min_age:g} h"
|
item.skip = f"younger than {repo.min_age:g} h"
|
||||||
item.actions = _actions(repo, item, tree, checkout, dirty, registrations, held)
|
item.actions = _actions(repo, item, tree, checkout, dirty, registrations)
|
||||||
if repo.unsafe:
|
if repo.unsafe:
|
||||||
item.actions = [a for a in item.actions if not a.startswith("remote-branch:")]
|
item.actions = [a for a in item.actions if not a.startswith("remote-branch:")]
|
||||||
return item
|
return item
|
||||||
@@ -774,10 +772,9 @@ def _actions(
|
|||||||
checkout: bool,
|
checkout: bool,
|
||||||
dirty: int,
|
dirty: int,
|
||||||
registrations: Registrations,
|
registrations: Registrations,
|
||||||
held: Callable[[], dict[str, str]],
|
|
||||||
) -> list[str]:
|
) -> list[str]:
|
||||||
if repo.scope == "local":
|
if repo.scope == "local":
|
||||||
return _local_actions(repo, item, tree, held)
|
return _local_actions(repo, item, tree)
|
||||||
tree_part: list[str] = []
|
tree_part: list[str] = []
|
||||||
if tree:
|
if tree:
|
||||||
if (
|
if (
|
||||||
@@ -816,12 +813,7 @@ def _actions(
|
|||||||
return actions
|
return actions
|
||||||
|
|
||||||
|
|
||||||
def _local_actions(
|
def _local_actions(repo: Repo, item: Item, tree: Worktree | None) -> list[str]:
|
||||||
repo: Repo,
|
|
||||||
item: Item,
|
|
||||||
tree: Worktree | None,
|
|
||||||
held: Callable[[], dict[str, str]],
|
|
||||||
) -> list[str]:
|
|
||||||
"""
|
"""
|
||||||
Settle the actions of an item planned with scope `local`.
|
Settle the actions of an item planned with scope `local`.
|
||||||
|
|
||||||
@@ -831,8 +823,6 @@ def _local_actions(
|
|||||||
item: the classified item; a merged-clean one whose tip is an
|
item: the classified item; a merged-clean one whose tip is an
|
||||||
ancestor of none of them gets that as its skip.
|
ancestor of none of them gets that as its skip.
|
||||||
tree: the worktree holding its branch, or None.
|
tree: the worktree holding its branch, or None.
|
||||||
held: the snapshot's _held_branches of the repository, read on the
|
|
||||||
first call and reused for every later item of that snapshot.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
`local-branch` for a merged-clean local branch that no worktree,
|
`local-branch` for a merged-clean local branch that no worktree,
|
||||||
@@ -842,9 +832,9 @@ def _local_actions(
|
|||||||
"""
|
"""
|
||||||
if item.state != "merged-clean" or not item.local_sha or tree is not None:
|
if item.state != "merged-clean" or not item.local_sha or tree is not None:
|
||||||
return []
|
return []
|
||||||
hold = held().get(item.branch)
|
held = _held_branches(repo.repo).get(item.branch)
|
||||||
if hold:
|
if held:
|
||||||
item.skip = item.skip or hold
|
item.skip = item.skip or held
|
||||||
return []
|
return []
|
||||||
targets = [repo.default, *repo.lines]
|
targets = [repo.default, *repo.lines]
|
||||||
if any(_is_ancestor(item.local_sha, ref, repo.repo) for ref in targets):
|
if any(_is_ancestor(item.local_sha, ref, repo.repo) for ref in targets):
|
||||||
@@ -864,7 +854,6 @@ def _snapshot(repo: Repo) -> tuple[list[Worktree], Snapshot]:
|
|||||||
cwds,
|
cwds,
|
||||||
tracking,
|
tracking,
|
||||||
_registrations(repo.repo),
|
_registrations(repo.repo),
|
||||||
cache(lambda: _held_branches(repo.repo)),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from pkgmgr.core.git import run
|
||||||
from pkgmgr.core.git.commands import (
|
from pkgmgr.core.git.commands import (
|
||||||
fetch,
|
fetch,
|
||||||
pull_ff_only,
|
pull_ff_only,
|
||||||
@@ -14,7 +15,18 @@ def ensure_clean_and_synced(*, preview: bool = False) -> None:
|
|||||||
Always run a pull BEFORE modifying anything.
|
Always run a pull BEFORE modifying anything.
|
||||||
Uses --ff-only to avoid creating merge commits automatically.
|
Uses --ff-only to avoid creating merge commits automatically.
|
||||||
If no upstream is configured, we skip.
|
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()
|
upstream = get_upstream_ref()
|
||||||
if not upstream:
|
if not upstream:
|
||||||
print("[INFO] No upstream configured for current branch. Skipping pull.")
|
print("[INFO] No upstream configured for current branch. Skipping pull.")
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from pkgmgr.cli.commands import (
|
|||||||
)
|
)
|
||||||
from pkgmgr.cli.context import CLIContext
|
from pkgmgr.cli.context import CLIContext
|
||||||
from pkgmgr.cli.proxy import maybe_handle_proxy
|
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.dir import get_repo_dir
|
||||||
from pkgmgr.core.repository.selected import get_selected_repos
|
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]]
|
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:
|
def dispatch_command(args, ctx: CLIContext) -> None:
|
||||||
if maybe_handle_proxy(args, ctx):
|
if maybe_handle_proxy(args, ctx):
|
||||||
return
|
return
|
||||||
@@ -143,6 +152,8 @@ def dispatch_command(args, ctx: CLIContext) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if args.command == "release":
|
if args.command == "release":
|
||||||
|
if not _has_explicit_selection(args):
|
||||||
|
selected = _at_cwd_checkout(selected)
|
||||||
handle_release(args, ctx, selected)
|
handle_release(args, ctx, selected)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -489,52 +489,6 @@ class TestBranchCleanupLocal(unittest.TestCase):
|
|||||||
heads["refs/heads/local-done"],
|
heads["refs/heads/local-done"],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_plan_reads_the_held_branches_once_per_repository(self) -> None:
|
|
||||||
self._set_head()
|
|
||||||
extra = ("more-a", "more-b", "more-c")
|
|
||||||
for name in extra:
|
|
||||||
git(self.repo, "branch", name, "done")
|
|
||||||
|
|
||||||
with patch.object(gc, "_held_branches", wraps=gc._held_branches) as held:
|
|
||||||
run = self._cleanup("local", apply=False)
|
|
||||||
|
|
||||||
self.assertEqual(held.call_count, 1)
|
|
||||||
self.assertEqual(
|
|
||||||
{name for name, i in run.items.items() if i["actions"]},
|
|
||||||
{"done", "local-done", *extra},
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_branch_held_after_the_plan_is_not_deleted(self) -> None:
|
|
||||||
self._set_head()
|
|
||||||
real = gc._rebuild
|
|
||||||
changes = [
|
|
||||||
lambda: git(
|
|
||||||
self.repo, "symbolic-ref", "refs/tags/pin", "refs/heads/local-done"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
def late(repo: gc.Repo, item: gc.Item) -> gc.Item:
|
|
||||||
if changes:
|
|
||||||
changes.pop()()
|
|
||||||
return real(repo, item)
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch.object(gc, "_held_branches", wraps=gc._held_branches) as held,
|
|
||||||
patch.object(gc, "_rebuild", side_effect=late),
|
|
||||||
):
|
|
||||||
run = self._cleanup("local", apply=True)
|
|
||||||
|
|
||||||
self.assertEqual(held.call_count, 3)
|
|
||||||
self.assertEqual(
|
|
||||||
self._results(run),
|
|
||||||
{
|
|
||||||
"done": "done",
|
|
||||||
"local-done": "skipped: changed since plan "
|
|
||||||
"(target of symbolic ref refs/tags/pin)",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
self.assertIn("refs/heads/local-done", self._refs(self.repo, "refs/heads"))
|
|
||||||
|
|
||||||
def test_delete_never_follows_a_symref_made_after_the_check(self) -> None:
|
def test_delete_never_follows_a_symref_made_after_the_check(self) -> None:
|
||||||
planned = git(self.repo, "rev-parse", "held-done")
|
planned = git(self.repo, "rev-parse", "held-done")
|
||||||
|
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -6,6 +6,7 @@ from unittest.mock import patch
|
|||||||
from pkgmgr.actions.release.git_ops import ensure_clean_and_synced
|
from pkgmgr.actions.release.git_ops import ensure_clean_and_synced
|
||||||
|
|
||||||
|
|
||||||
|
@patch("pkgmgr.actions.release.git_ops.run", return_value="")
|
||||||
class TestEnsureCleanAndSynced(unittest.TestCase):
|
class TestEnsureCleanAndSynced(unittest.TestCase):
|
||||||
@patch("pkgmgr.actions.release.git_ops.pull_ff_only")
|
@patch("pkgmgr.actions.release.git_ops.pull_ff_only")
|
||||||
@patch("pkgmgr.actions.release.git_ops.fetch")
|
@patch("pkgmgr.actions.release.git_ops.fetch")
|
||||||
@@ -15,6 +16,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase):
|
|||||||
mock_get_upstream_ref,
|
mock_get_upstream_ref,
|
||||||
mock_fetch,
|
mock_fetch,
|
||||||
mock_pull_ff_only,
|
mock_pull_ff_only,
|
||||||
|
_mock_run,
|
||||||
) -> None:
|
) -> None:
|
||||||
mock_get_upstream_ref.return_value = None
|
mock_get_upstream_ref.return_value = None
|
||||||
|
|
||||||
@@ -31,6 +33,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase):
|
|||||||
mock_get_upstream_ref,
|
mock_get_upstream_ref,
|
||||||
mock_fetch,
|
mock_fetch,
|
||||||
mock_pull_ff_only,
|
mock_pull_ff_only,
|
||||||
|
_mock_run,
|
||||||
) -> None:
|
) -> None:
|
||||||
mock_get_upstream_ref.return_value = "origin/main"
|
mock_get_upstream_ref.return_value = "origin/main"
|
||||||
|
|
||||||
@@ -53,6 +56,7 @@ class TestEnsureCleanAndSynced(unittest.TestCase):
|
|||||||
mock_get_upstream_ref,
|
mock_get_upstream_ref,
|
||||||
mock_fetch,
|
mock_fetch,
|
||||||
mock_pull_ff_only,
|
mock_pull_ff_only,
|
||||||
|
_mock_run,
|
||||||
) -> None:
|
) -> None:
|
||||||
mock_get_upstream_ref.return_value = "origin/main"
|
mock_get_upstream_ref.return_value = "origin/main"
|
||||||
|
|
||||||
@@ -66,3 +70,30 @@ class TestEnsureCleanAndSynced(unittest.TestCase):
|
|||||||
preview=False,
|
preview=False,
|
||||||
)
|
)
|
||||||
mock_pull_ff_only.assert_called_once_with(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()
|
||||||
Reference in new issue
Block a user