Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b18deede8 | ||
|
|
1707cc0192 | ||
|
|
d9a8746635 | ||
|
|
5151b36250 | ||
|
|
757903fdd5 | ||
|
|
c09e44af50 | ||
|
|
791761a239 | ||
|
|
3936125633 | ||
|
|
0d88c69c51 | ||
|
|
b0f9395c68 | ||
|
|
814322f8f5 |
No files matched your search
@@ -71,3 +71,9 @@ jobs:
|
||||
contents: read
|
||||
security-events: write
|
||||
uses: ./.github/workflows/lint-docker.yml
|
||||
|
||||
quality:
|
||||
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main'
|
||||
permissions:
|
||||
contents: read
|
||||
uses: infinito-nexus/quality/.github/workflows/quality.yml@ed91a38fa19c2734d2cf07a0ade77bbb63f3096f
|
||||
@@ -8,19 +8,7 @@ permissions:
|
||||
|
||||
jobs:
|
||||
lint-python:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v7
|
||||
with:
|
||||
python-version: "3.12"
|
||||
|
||||
- name: Install ruff
|
||||
run: pip install ruff
|
||||
|
||||
- name: Run ruff
|
||||
run: |
|
||||
ruff check src tests
|
||||
uses: infinito-nexus/quality/.github/workflows/tool.yml@ed91a38fa19c2734d2cf07a0ade77bbb63f3096f
|
||||
with:
|
||||
tool: ruff
|
||||
args: check src tests
|
||||
@@ -1,5 +1,16 @@
|
||||
# Changelog
|
||||
|
||||
## [2.5.0] - 2026-11-13
|
||||
|
||||
Added:
|
||||
|
||||
- Branch: *cleanup --local* deletes merged branches only where git would
|
||||
- Review: expand and collapse are icon buttons with labels
|
||||
|
||||
Fixed:
|
||||
|
||||
- GC: merged local-only branches are removed without a forge token
|
||||
|
||||
## [2.4.0] - 2026-11-07
|
||||
|
||||
Added:
|
||||
|
||||
@@ -80,6 +80,7 @@ versioning features it can drive **end-to-end workflows**:
|
||||
| `pkgmgr worktree flow clear [PATH] [--json]` | Removes the merge flow |
|
||||
| `pkgmgr worktree cleanup [--apply]` | Removes clean worktrees whose commits are merged or pushed: pinned `worktree-fini` hook, compose stack, checkout; the branch stays |
|
||||
| `pkgmgr branch cleanup [--apply]` | Removes merged branches that no worktree holds, locally and on the remotes |
|
||||
| `pkgmgr branch cleanup --local [--apply] [--json] [--merged-into REF]...` | Deletes only local branches whose tip is an ancestor of the remote-tracking default or of a `--merged-into` REF, each with `git update-ref --no-deref -d` against the planned sha and then its `branch.<name>` section in the repository's own config file (`git config --file <git common dir>/config`; a section it cannot remove is noted on the done item), also when the branch has an upstream or remote copy. Every git call of `--local` runs without any inherited `GIT_*` variable but `GIT_CEILING_DIRECTORIES`, which only narrows repository discovery, so a directory below a ceiling is never resolved to the repository above it; only the config reads of `deleteMerged` and of the upstreams run twice, both with the system config: once with the caller's `GIT_CONFIG_GLOBAL`, `GIT_CONFIG_SYSTEM`, `GIT_CONFIG_PARAMETERS` and `GIT_CONFIG_COUNT` with its `GIT_CONFIG_KEY_<n>` and `GIT_CONFIG_VALUE_<n>`, once without any of them, without `XDG_CONFIG_HOME` and with `HOME` set to the passwd home, and a branch either read keeps stays, so the environment can add keeps but never remove one. A `--merged-into` REF must name exactly one branch or remote-tracking ref, as `git rev-parse --symbolic-full-name` resolves it, in every scanned repository git can read; `X~0`, `X^0`, `X@{0}`, a sha, a range or an unknown ref stops the run. The default comes from `origin`, or from the only remote when there is no `origin`: the ref `refs/remotes/<remote>/HEAD`, else `main`, else `master` resolves to, every symref step inside `refs/remotes/<remote>/`, so never a local branch or `HEAD`; without one, for example with several remotes and no `origin`, the repository is refused, also under `--merged-into`; so is one whose `refs`, `packed-refs`, `reftable`, `logs` or `logs/refs` is a symlink (git-new-workdir), one with a symlink anywhere below `refs/`, `reftable/` or `logs/refs/` (for example `refs/heads/team` linked to another clone's heads), one whose git dir is neither its git common dir nor directly under `<common>/worktrees/` (a registration moved elsewhere), or one whose git output is not UTF-8. The repository is the one the current directory belongs to, also in a submodule or with a separate git dir; one whose main worktree belongs to another repository (`core.worktree`), whose git dir is neither its git common dir nor directly under `<common>/worktrees/`, or whose path is not UTF-8 is refused with exit 2. Skips a symbolic branch, every branch a symbolic ref anywhere under `refs/` (also a tag or remote-tracking one) or under the main worktree's or any registration's `refs/worktree/` points to, a branch that is itself a `--merged-into` target, a branch a worktree holds or that the HEAD of the main repository or of any `worktrees/<id>` registration names (also one `git worktree list` omits or whose id is no valid ref name, read through `git --git-dir=<dir> symbolic-ref HEAD`, so also under reftable), one a rebase or bisect in any worktree works on, one merged only by patch or PR, and, as `git branch --delete-merged` keeps them, one whose `branch.<name>.deleteMerged` is false in the config of any worktree (also its `config.worktree` or an `includeIf "onbranch:..."` file) or in the system config file, and the local upstream, as git's `%(upstream)` resolves it in any worktree, of any branch that stays, also of one that `--branch` or `--exclude` leaves out, each shown as kept with its reason; these holds are read through the git common dir, whichever worktree is scanned, and a registration, symbolic ref, HEAD or state file they cannot read refuses the repository; when a repository vanishes, moves or stops being readable, or its default or a `--merged-into` target stops resolving mid-run, its later items are skipped with that reason and the plan is still printed. A repository git or the filesystem fails on while planning (for example a dangling `.git` file under `--all`) is refused with that reason and the run goes on. The plan, refused repositories included, is printed (as JSON with `--json`), and the run exits 1 when an item failed, else 2 when a repository was refused, else 0. It stops with a message on stderr, no plan and exit 2 in exactly these cases: without `--all`, the current directory is not inside a git repository, its git dir is neither its git common dir nor directly under `<common>/worktrees/`, its main worktree belongs to another repository (`core.worktree`), or its path or git output is not UTF-8; `--apply` outside the host PID namespace; a `--merged-into` REF refused as above; a `--branch` that matches no item (also one that exists only on a remote). Meant for working clones and their worktrees, not for a bare server repository. Not mirrored from `git branch --delete-merged`: git also keeps a branch whose push would update its own upstream; `--local` does not. A branch is deleted before its planned local upstream; right before each delete its holds and then itLine truncated
|
||||
| `pkgmgr branch tree [--format json\|yaml] [--output FILE]` | Writes every managed repository's branches and worktrees with their cleanup state, without fetching |
|
||||
| `pkgmgr gc [--all] [--apply]` | Both cleanups in one pass, for the current repository or every managed one |
|
||||
| `pkgmgr gc --report [--all] [--root DIR] [--threshold-mb MB] [--docker] [--json]` | Read-only disk inventory: worktree sizes, changes a cleanup would keep, `--root` children, core dumps, docker usage, free space per filesystem |
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
rec {
|
||||
pkgmgr = pyPkgs.buildPythonApplication {
|
||||
pname = "package-manager";
|
||||
version = "2.4.0";
|
||||
version = "2.5.0";
|
||||
|
||||
# Use the git repo as source
|
||||
src = ./.;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Maintainer: Kevin Veen-Birkenbach <info@veen.world>
|
||||
|
||||
pkgname=package-manager
|
||||
pkgver=2.4.0
|
||||
pkgver=2.5.0
|
||||
pkgrel=1
|
||||
pkgdesc="Local-flake wrapper for Kevin's package-manager (Nix-based)."
|
||||
arch=('any')
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
package-manager (2.5.0-1) unstable; urgency=medium
|
||||
|
||||
Added:
|
||||
|
||||
- Branch: *cleanup --local* deletes merged branches only where git would
|
||||
- Review: expand and collapse are icon buttons with labels
|
||||
|
||||
Fixed:
|
||||
|
||||
- GC: merged local-only branches are removed without a forge token
|
||||
|
||||
-- Kevin Veen-Birkenbach <kevin@veen.world> Fri, 13 Nov 2026 22:53:12 +0000
|
||||
|
||||
package-manager (2.4.0-1) unstable; urgency=medium
|
||||
|
||||
Added:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Name: package-manager
|
||||
Version: 2.4.0
|
||||
Version: 2.5.0
|
||||
Release: 1%{?dist}
|
||||
Summary: Wrapper that runs Kevin's package-manager via Nix flake
|
||||
|
||||
@@ -74,6 +74,16 @@ echo ">>> package-manager removed. Nix itself was not removed."
|
||||
/usr/lib/package-manager/
|
||||
|
||||
%changelog
|
||||
* Fri Nov 13 2026 Kevin Veen-Birkenbach <kevin@veen.world> - 2.5.0-1
|
||||
- Added:
|
||||
|
||||
- - Branch: *cleanup --local* deletes merged branches only where git would
|
||||
- - Review: expand and collapse are icon buttons with labels
|
||||
|
||||
- Fixed:
|
||||
|
||||
- - GC: merged local-only branches are removed without a forge token
|
||||
|
||||
* Sat Nov 07 2026 Kevin Veen-Birkenbach <kevin@veen.world> - 2.4.0-1
|
||||
- Added:
|
||||
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "kpmx"
|
||||
version = "2.4.0"
|
||||
version = "2.5.0"
|
||||
description = "Kevin's package-manager tool (pkgmgr)"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.9"
|
||||
@@ -52,4 +52,4 @@ include = ["pkgmgr*"]
|
||||
|
||||
[tool.setuptools.package-data]
|
||||
"pkgmgr.config" = ["*.yml", "*.yaml"]
|
||||
"pkgmgr.actions.review" = ["*.j2"]
|
||||
"pkgmgr.actions.review" = ["*.j2", "*.LICENSE"]
|
||||
+897
-27
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import glob
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from pkgmgr.actions.mirror.io import read_mirrors_file
|
||||
from pkgmgr.actions.mirror.types import Repository
|
||||
@@ -12,20 +12,62 @@ from pkgmgr.core.version.semver import SemVer
|
||||
|
||||
from .git_tags import head_semver_tags
|
||||
from .pypi_url import parse_pypi_project_url
|
||||
from .types import PyPITarget
|
||||
|
||||
UPLOAD_URLS = {
|
||||
"pypi.org": "https://upload.pypi.org/legacy/",
|
||||
"test.pypi.org": "https://test.pypi.org/legacy/",
|
||||
}
|
||||
|
||||
|
||||
def _require_tool(module: str) -> None:
|
||||
def _child_env(repo_dir: str) -> dict[str, str]:
|
||||
"""
|
||||
Args:
|
||||
repo_dir: absolute path of the repository being published.
|
||||
|
||||
Returns:
|
||||
The environment for build and twine without pkgmgr's own variables,
|
||||
so no PKGMGR_* token reaches a build backend or an upload, and
|
||||
without PYTHONPATH entries inside repo_dir, so no repository file
|
||||
shadows build or twine.
|
||||
"""
|
||||
env = {k: v for k, v in os.environ.items() if not k.startswith("PKGMGR_")}
|
||||
if "PYTHONPATH" in env:
|
||||
root = os.path.realpath(repo_dir)
|
||||
kept = [
|
||||
entry
|
||||
for entry in env["PYTHONPATH"].split(os.pathsep)
|
||||
if entry and os.path.commonpath([root, os.path.realpath(entry)]) != root
|
||||
]
|
||||
if kept:
|
||||
env["PYTHONPATH"] = os.pathsep.join(kept)
|
||||
else:
|
||||
del env["PYTHONPATH"]
|
||||
return env
|
||||
|
||||
|
||||
def _require_tool(module: str, cwd: str, env: dict[str, str]) -> None:
|
||||
"""
|
||||
Args:
|
||||
module: the Python module run with -m.
|
||||
cwd: an empty private directory, so no file of the repository
|
||||
shadows the module on sys.path.
|
||||
env: the child environment from _child_env.
|
||||
"""
|
||||
try:
|
||||
subprocess.run(
|
||||
["python", "-m", module, "--help"],
|
||||
[sys.executable, "-m", module, "--help"],
|
||||
cwd=cwd,
|
||||
env=env,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=True,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise RuntimeError(
|
||||
f"Required Python module '{module}' is not available. "
|
||||
f"Install it via: pip install {module}"
|
||||
f"Required Python module '{module}' is not available to "
|
||||
f"{sys.executable}. Install it via: "
|
||||
f"{sys.executable} -m pip install {module}"
|
||||
) from exc
|
||||
|
||||
|
||||
@@ -66,22 +108,38 @@ def publish(
|
||||
print("[PREVIEW] Would build and upload to PyPI.")
|
||||
return
|
||||
|
||||
_require_tool("build")
|
||||
_require_tool("twine")
|
||||
with tempfile.TemporaryDirectory() as out:
|
||||
_upload(target, os.path.abspath(repo_dir), out, interactive, allow_prompt)
|
||||
|
||||
dist_dir = os.path.join(repo_dir, "dist")
|
||||
if os.path.isdir(dist_dir):
|
||||
shutil.rmtree(dist_dir, ignore_errors=True)
|
||||
print("[INFO] Publish completed.")
|
||||
|
||||
|
||||
def _upload(
|
||||
target: PyPITarget, repo_dir: str, out: str, interactive: bool, allow_prompt: bool
|
||||
) -> None:
|
||||
"""
|
||||
Args:
|
||||
target: the PyPI target parsed from the mirror URL.
|
||||
repo_dir: absolute path of the repository to build.
|
||||
out: an empty private directory that receives the artifacts and is
|
||||
the working directory of every child process.
|
||||
interactive: allow interactive token resolution.
|
||||
allow_prompt: allow prompting for the token.
|
||||
"""
|
||||
env = _child_env(repo_dir)
|
||||
_require_tool("build", out, env)
|
||||
_require_tool("twine", out, env)
|
||||
|
||||
subprocess.run(
|
||||
["python", "-m", "build"],
|
||||
cwd=repo_dir,
|
||||
[sys.executable, "-m", "build", "--outdir", out, repo_dir],
|
||||
cwd=out,
|
||||
env=env,
|
||||
check=True,
|
||||
)
|
||||
|
||||
artifacts = sorted(glob.glob(os.path.join(dist_dir, "*")))
|
||||
artifacts = sorted(os.path.join(out, name) for name in os.listdir(out))
|
||||
if not artifacts:
|
||||
raise RuntimeError("No build artifacts found in dist/.")
|
||||
raise RuntimeError("No build artifacts found.")
|
||||
|
||||
resolver = TokenResolver()
|
||||
|
||||
@@ -98,15 +156,18 @@ def publish(
|
||||
),
|
||||
).token
|
||||
|
||||
env = dict(os.environ)
|
||||
env["TWINE_USERNAME"] = "__token__"
|
||||
env["TWINE_PASSWORD"] = token
|
||||
|
||||
subprocess.run(
|
||||
["python", "-m", "twine", "upload", *artifacts],
|
||||
cwd=repo_dir,
|
||||
env=env,
|
||||
[
|
||||
sys.executable,
|
||||
"-m",
|
||||
"twine",
|
||||
"upload",
|
||||
"--non-interactive",
|
||||
"--repository-url",
|
||||
UPLOAD_URLS[target.host],
|
||||
*artifacts,
|
||||
],
|
||||
cwd=out,
|
||||
env={**env, "TWINE_USERNAME": "__token__", "TWINE_PASSWORD": token},
|
||||
check=True,
|
||||
)
|
||||
|
||||
print("[INFO] Publish completed.")
|
||||
@@ -8,6 +8,36 @@ try:
|
||||
except ImportError:
|
||||
import tomli as tomllib # type: ignore
|
||||
|
||||
from pkgmgr.core.version.source import dynamic_version_source, version_literal
|
||||
|
||||
|
||||
def update_module_version(
|
||||
path: str, name: str, new_version: str, preview: bool
|
||||
) -> None:
|
||||
"""
|
||||
Args:
|
||||
path: the module file that holds the version literal.
|
||||
name: the attribute assigned the version, e.g. __version__.
|
||||
new_version: the version to write.
|
||||
preview: print the change instead of writing it.
|
||||
|
||||
Raises:
|
||||
RuntimeError: the file holds no `name = "..."` literal.
|
||||
"""
|
||||
with open(path, encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
new_content, count = version_literal(name).subn(
|
||||
lambda mm: f"{mm.group(1)}{new_version}{mm.group(3)}", content, count=1
|
||||
)
|
||||
if count == 0:
|
||||
raise RuntimeError(f"Missing {name} literal in {path}")
|
||||
if preview:
|
||||
print(f"[PREVIEW] Would update {name} in {path} to {new_version}")
|
||||
return
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(new_content)
|
||||
print(f"Updated {name} in {path} to {new_version}")
|
||||
|
||||
|
||||
def update_pyproject_version(
|
||||
pyproject_path: str, new_version: str, preview: bool = False
|
||||
@@ -23,7 +53,12 @@ def update_pyproject_version(
|
||||
print(f"[WARN] Could not read pyproject.toml: {exc}")
|
||||
return
|
||||
|
||||
project = tomllib.loads(content).get("project")
|
||||
data = tomllib.loads(content)
|
||||
project = data.get("project")
|
||||
source = dynamic_version_source(pyproject_path, data)
|
||||
if source is not None:
|
||||
update_module_version(*source, new_version, preview)
|
||||
return
|
||||
if project is None or "version" in project.get("dynamic", []):
|
||||
print("[INFO] pyproject.toml declares no static version, skipping.")
|
||||
return
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2019-2024 The Bootstrap Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -1,5 +1,9 @@
|
||||
{% set status_letters = {"added": "A", "deleted": "D", "modified": "M", "renamed": "R", "copied": "C", "untracked": "U"} %}
|
||||
{% set signs = {"add": "+", "del": "-", "ctx": "", "meta": ""} %}
|
||||
{% set icon_paths = {
|
||||
"arrows-expand": "M1 8a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13A.5.5 0 0 1 1 8M7.646.146a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 1.707V5.5a.5.5 0 0 1-1 0V1.707L6.354 2.854a.5.5 0 1 1-.708-.708zM8 10a.5.5 0 0 1 .5.5v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 0 1 .708-.708L7.5 14.293V10.5A.5.5 0 0 1 8 10",
|
||||
"arrows-collapse": "M1 8a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13A.5.5 0 0 1 1 8m7-8a.5.5 0 0 1 .5.5v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 1 1 .708-.708L7.5 4.293V.5A.5.5 0 0 1 8 0m-.5 11.707-1.146 1.147a.5.5 0 0 1-.708-.708l2-2a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 11.707V15.5a.5.5 0 0 1-1 0z",
|
||||
} %}
|
||||
{% set style %}
|
||||
:root {
|
||||
color-scheme: light dark;
|
||||
@@ -93,6 +97,7 @@ input[type="search"] { width: 260px; max-width: 100%; padding: 5px 10px; border:
|
||||
input[type="search"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
|
||||
button { padding: 5px 12px; border: 1px solid var(--border); border-radius: 6px; background: var(--subtle); color: var(--text); font: 500 13px var(--sans); cursor: pointer; }
|
||||
button:hover { border-color: var(--muted); }
|
||||
button.icon { display: inline-flex; align-items: center; justify-content: center; padding: 6px 8px; }
|
||||
main { padding-top: 24px; padding-bottom: 80px; }
|
||||
.group { margin-bottom: 36px; }
|
||||
h2 { display: flex; flex-wrap: wrap; align-items: baseline; gap: 12px; margin: 0 0 12px; font-size: 17px; font-weight: 600; }
|
||||
@@ -187,6 +192,7 @@ details.commit > summary { padding: 10px 14px; }
|
||||
})();
|
||||
{% endset %}
|
||||
{% macro stat(added, removed) %}<span class="stat"><span class="add">+{{ added }}</span><span class="del">−{{ removed }}</span></span>{% endmacro %}
|
||||
{% macro icon(name) %}<svg class="bi bi-{{ name }}" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true" focusable="false"><path fill-rule="evenodd" d="{{ icon_paths[name] }}"/></svg>{% endmacro %}
|
||||
{% macro plural(count, word) %}{{ count }} {{ word }}{{ "" if count == 1 else "s" }}{% endmacro %}
|
||||
{% macro file_block(file, open) %}
|
||||
<details class="file" id="{{ file.anchor }}" data-path="{{ file.search }}"{% if open %} open{% endif %}>
|
||||
@@ -282,8 +288,8 @@ details.commit > summary { padding: 10px 14px; }
|
||||
<div class="spacer"></div>
|
||||
<input id="filter" type="search" placeholder="Filter files by path ( / )" autocomplete="off" spellcheck="false" aria-label="Filter files by path">
|
||||
<span id="filter-count" class="muted"></span>
|
||||
<button type="button" id="expand-all">Expand all</button>
|
||||
<button type="button" id="collapse-all">Collapse all</button>
|
||||
<button type="button" id="expand-all" class="icon" title="Expand all" aria-label="Expand all">{{ icon("arrows-expand") }}</button>
|
||||
<button type="button" id="collapse-all" class="icon" title="Collapse all" aria-label="Collapse all">{{ icon("arrows-collapse") }}</button>
|
||||
</div>
|
||||
</nav>
|
||||
<main>
|
||||
|
||||
@@ -11,7 +11,7 @@ from pkgmgr.actions.branch import (
|
||||
open_branch,
|
||||
tree,
|
||||
)
|
||||
from pkgmgr.actions.branch.gc import main_repo_root
|
||||
from pkgmgr.actions.branch.gc import local_repo_root, main_repo_root
|
||||
from pkgmgr.cli.context import CLIContext
|
||||
from pkgmgr.core.git.errors import GitBaseError
|
||||
from pkgmgr.core.repository.dir import get_repo_dir
|
||||
@@ -60,14 +60,23 @@ def refresh_tree(roots: list[str]) -> None:
|
||||
print(f"WARN tree file not rewritten for {root}: {exc}", file=sys.stderr)
|
||||
|
||||
|
||||
def scan_repo_roots(args, ctx: CLIContext) -> list[str]:
|
||||
def scan_repo_roots(args, ctx: CLIContext, local: bool = False) -> list[str]:
|
||||
"""
|
||||
Resolve the repositories a scan covers: the current one, or every
|
||||
pkgmgr-managed one with --all. Exits with 2 outside a repository.
|
||||
|
||||
Args:
|
||||
args: parsed arguments; `all` selects every managed repository.
|
||||
ctx: CLI context.
|
||||
local: resolve the current repository as scope `local` does, also in
|
||||
a submodule or with a separate git dir, refusing a mismatch.
|
||||
|
||||
Returns:
|
||||
The main worktree roots to scan.
|
||||
"""
|
||||
if not args.all:
|
||||
try:
|
||||
return [main_repo_root(".")]
|
||||
return [(local_repo_root if local else main_repo_root)(".")]
|
||||
except GitBaseError as exc:
|
||||
print(f"Not inside a git repository: {exc}", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
@@ -84,7 +93,7 @@ def scan_options(args) -> dict:
|
||||
|
||||
|
||||
def run_cleanup(args, ctx: CLIContext, scope: str) -> None:
|
||||
roots = scan_repo_roots(args, ctx)
|
||||
roots = scan_repo_roots(args, ctx, local=scope == "local")
|
||||
code = cleanup(
|
||||
roots,
|
||||
scope=scope,
|
||||
@@ -94,7 +103,7 @@ def run_cleanup(args, ctx: CLIContext, scope: str) -> None:
|
||||
hooks=pinned_hooks(ctx),
|
||||
**scan_options(args),
|
||||
)
|
||||
if args.apply:
|
||||
if args.apply and scope != "local":
|
||||
refresh_tree(roots)
|
||||
sys.exit(code)
|
||||
|
||||
@@ -107,8 +116,9 @@ def handle_branch(args, ctx: CLIContext) -> None:
|
||||
- pkgmgr branch open [<name>] [--base <branch>]
|
||||
- pkgmgr branch close [<name>] [--base <branch>] [--force|-f]
|
||||
- pkgmgr branch drop [<name>] [--base <branch>] [--force|-f]
|
||||
- pkgmgr branch cleanup [--apply] [--json] [--all] [--branch N]... [--exclude N]...
|
||||
[--merged-into REF]... [--sizes] [--min-age HOURS]
|
||||
- pkgmgr branch cleanup [--apply] [--json] [--local] [--all] [--branch N]...
|
||||
[--exclude N]... [--merged-into REF]... [--sizes]
|
||||
[--min-age HOURS]
|
||||
- pkgmgr branch purge [--all] [--branch N]... [--exclude N]...
|
||||
[--merged-into REF]... [--sizes] [--min-age HOURS]
|
||||
- pkgmgr branch tree [--format json|yaml] [--output FILE]
|
||||
@@ -124,7 +134,7 @@ def handle_branch(args, ctx: CLIContext) -> None:
|
||||
)
|
||||
|
||||
if args.subcommand == "cleanup":
|
||||
run_cleanup(args, ctx, "branch")
|
||||
run_cleanup(args, ctx, "local" if args.local else "branch")
|
||||
|
||||
if args.subcommand == "purge":
|
||||
roots = scan_repo_roots(args, ctx)
|
||||
|
||||
@@ -105,6 +105,41 @@ def add_branch_subparsers(
|
||||
),
|
||||
)
|
||||
add_apply_options(branch_cleanup)
|
||||
branch_cleanup.add_argument(
|
||||
"--local",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Delete only local branches whose tip is an ancestor of origin's "
|
||||
"remote-tracking default (else the only remote's) or a --merged-into "
|
||||
"REF, against the planned sha; refuse a repository without such a "
|
||||
"default, one git fails on, one whose holds cannot be read, one "
|
||||
"whose git dir is neither its common dir nor directly under "
|
||||
"<common>/worktrees/ and one with a symlink in or below refs, "
|
||||
"packed-refs, reftable or logs/refs, and still print the plan; skip "
|
||||
"a symbolic branch, a branch any symbolic ref of any worktree names, "
|
||||
"a branch a worktree, worktree registration, rebase or bisect holds, "
|
||||
"a --merged-into target, a branch with branch.<name>.deleteMerged "
|
||||
"false in any worktree's or the system config and the local "
|
||||
"upstream of a branch that stays, also of one --branch or --exclude "
|
||||
"leaves out; "
|
||||
"run git without inherited GIT_* variables but "
|
||||
"GIT_CEILING_DIRECTORIES; the keep config reads run once with the "
|
||||
"caller's GIT_CONFIG_* variables and once with the default system "
|
||||
"and passwd-home config, and either read's keep counts; read no "
|
||||
"forge token; "
|
||||
"never fetch, push, delete a remote branch, run docker or hooks, or "
|
||||
"refresh the branch tree file; exit 1 when an item failed, else 2 "
|
||||
"when a repository was refused, else 0; exit 2 without a plan when, "
|
||||
"without --all, the current directory is in no git repository, its "
|
||||
"git dir is neither its common dir nor directly under "
|
||||
"<common>/worktrees/, its main worktree belongs to another "
|
||||
"repository or its path or git output is not UTF-8, on --apply "
|
||||
"outside the host PID namespace, on a --merged-into REF that is not "
|
||||
"exactly one branch or remote-tracking ref (X~0, X^0, X@{0}, a sha), "
|
||||
"and on a --branch "
|
||||
"that matches nothing; meant for working clones"
|
||||
),
|
||||
)
|
||||
|
||||
branch_purge = branch_subparsers.add_parser(
|
||||
"purge",
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextvars
|
||||
import os
|
||||
import pwd
|
||||
import re
|
||||
import subprocess
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
|
||||
from .errors import GitNotRepositoryError, GitRunError
|
||||
|
||||
@@ -52,10 +57,20 @@ SCAN_UNSET = (
|
||||
"GIT_PAGER",
|
||||
"PAGER",
|
||||
)
|
||||
STRICT_KEEP = ("GIT_CEILING_DIRECTORIES",)
|
||||
CONFIG_READ_KEEP = re.compile(
|
||||
r"GIT_CONFIG_(GLOBAL|SYSTEM|PARAMETERS|COUNT|KEY_\d+|VALUE_\d+)"
|
||||
)
|
||||
CALLER_CONFIG = "caller"
|
||||
DEFAULT_CONFIG = "default"
|
||||
CONFIG_VIEWS = (CALLER_CONFIG, DEFAULT_CONFIG)
|
||||
SCAN_TIMEOUT = 60
|
||||
# ref and remote names come from repositories an agent may write: one named
|
||||
# like --output=<path> would otherwise make rev-list create that file
|
||||
END_OF_OPTIONS = "--end-of-options"
|
||||
_STRICT_ENV: contextvars.ContextVar[bool] = contextvars.ContextVar(
|
||||
"pkgmgr_git_strict_env", default=False
|
||||
)
|
||||
|
||||
|
||||
def _is_not_repo_error(stderr: str) -> bool:
|
||||
@@ -63,9 +78,61 @@ def _is_not_repo_error(stderr: str) -> bool:
|
||||
return "not a git repository" in msg
|
||||
|
||||
|
||||
def _scan_env() -> dict[str, str]:
|
||||
env = {key: value for key, value in os.environ.items() if key not in SCAN_UNSET}
|
||||
return {**env, **SCAN_ENV}
|
||||
@contextmanager
|
||||
def strict_env() -> Iterator[None]:
|
||||
token = _STRICT_ENV.set(True)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
_STRICT_ENV.reset(token)
|
||||
|
||||
|
||||
def _strict_keeps(key: str, caller: bool) -> bool:
|
||||
return key in STRICT_KEEP or (
|
||||
caller and CONFIG_READ_KEEP.fullmatch(key) is not None
|
||||
)
|
||||
|
||||
|
||||
def _passwd_home() -> str | None:
|
||||
try:
|
||||
return pwd.getpwuid(os.getuid()).pw_dir
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
||||
def _scan_env(strict: bool = False, view: str | None = None) -> dict[str, str]:
|
||||
"""
|
||||
Build the environment of a scan or strict git call.
|
||||
|
||||
Args:
|
||||
strict: drop every inherited `GIT_*` variable but STRICT_KEEP.
|
||||
view: None, CALLER_CONFIG or DEFAULT_CONFIG, as run takes it.
|
||||
|
||||
Returns:
|
||||
The inherited environment without SCAN_UNSET, plus SCAN_ENV; for a
|
||||
config view with the system config; for DEFAULT_CONFIG also without
|
||||
any `GIT_*` but STRICT_KEEP and XDG_CONFIG_HOME, HOME set to the
|
||||
passwd home (unset without a passwd entry).
|
||||
"""
|
||||
caller = view == CALLER_CONFIG
|
||||
scrub = strict or view == DEFAULT_CONFIG
|
||||
env = {
|
||||
key: value
|
||||
for key, value in os.environ.items()
|
||||
if key not in SCAN_UNSET
|
||||
and not (scrub and key.startswith("GIT_") and not _strict_keeps(key, caller))
|
||||
}
|
||||
env = {**env, **SCAN_ENV}
|
||||
if view is None:
|
||||
return env
|
||||
del env["GIT_CONFIG_NOSYSTEM"]
|
||||
if view == DEFAULT_CONFIG:
|
||||
env.pop("XDG_CONFIG_HOME", None)
|
||||
env.pop("HOME", None)
|
||||
home = _passwd_home()
|
||||
if home:
|
||||
env["HOME"] = home
|
||||
return env
|
||||
|
||||
|
||||
def run(
|
||||
@@ -75,6 +142,7 @@ def run(
|
||||
preview: bool = False,
|
||||
scan: bool = False,
|
||||
input_text: str | None = None,
|
||||
config_view: str | None = None,
|
||||
) -> str:
|
||||
"""
|
||||
Run a Git command and return its stdout as a stripped string.
|
||||
@@ -85,9 +153,17 @@ def run(
|
||||
preview: print the command instead of running it.
|
||||
scan: read a repository an agent may have written: no fsmonitor, hook,
|
||||
pager, lazy fetch, replace object, system config or prompt, stdin
|
||||
closed, a timeout.
|
||||
closed, a timeout. Inside strict_env() every call, scan or not,
|
||||
also runs without any inherited `GIT_*` variable but
|
||||
`GIT_CEILING_DIRECTORIES`, which only narrows discovery.
|
||||
input_text: text fed to stdin, e.g. for `cat-file --batch-check`;
|
||||
replaces the closed stdin of scan mode.
|
||||
config_view: for a pure config read, the config it sees, both with
|
||||
the system config: CALLER_CONFIG also keeps, inside strict_env(),
|
||||
`GIT_CONFIG_GLOBAL`, `GIT_CONFIG_SYSTEM`, `GIT_CONFIG_PARAMETERS`,
|
||||
`GIT_CONFIG_COUNT` and its `GIT_CONFIG_KEY_<n>` and
|
||||
`GIT_CONFIG_VALUE_<n>`; DEFAULT_CONFIG reads the default system
|
||||
and passwd-home global config whatever the environment says.
|
||||
|
||||
Raises GitRunError (or a subclass) if execution fails or times out.
|
||||
"""
|
||||
@@ -98,10 +174,15 @@ def run(
|
||||
print(f"[PREVIEW] Would run in {cwd!r}: {cmd_str}")
|
||||
return ""
|
||||
|
||||
strict = _STRICT_ENV.get()
|
||||
hardened: dict = (
|
||||
{"env": _scan_env(), "stdin": subprocess.DEVNULL, "timeout": SCAN_TIMEOUT}
|
||||
{
|
||||
"env": _scan_env(strict, config_view),
|
||||
"stdin": subprocess.DEVNULL,
|
||||
"timeout": SCAN_TIMEOUT,
|
||||
}
|
||||
if scan
|
||||
else {}
|
||||
else ({"env": _scan_env(True, config_view)} if strict else {})
|
||||
)
|
||||
if input_text is not None:
|
||||
hardened.pop("stdin", None)
|
||||
|
||||
@@ -9,11 +9,59 @@ import yaml
|
||||
from pkgmgr.core.repository.paths import resolve_repo_paths
|
||||
|
||||
|
||||
def version_literal(name: str) -> re.Pattern[str]:
|
||||
"""
|
||||
Args:
|
||||
name: the module attribute holding the version, e.g. __version__.
|
||||
|
||||
Returns:
|
||||
A pattern whose groups are the assignment up to the opening quote, the
|
||||
version and the closing quote of `name = "X.Y.Z"`.
|
||||
"""
|
||||
return re.compile(rf"""(?m)^(\s*{re.escape(name)}\s*=\s*["'])([^"']+)(["'])""")
|
||||
|
||||
|
||||
def dynamic_version_source(pyproject_path: str, data: dict) -> tuple[str, str] | None:
|
||||
"""
|
||||
Args:
|
||||
pyproject_path: the pyproject.toml file.
|
||||
data: its parsed content.
|
||||
|
||||
Returns:
|
||||
(module file, attribute name) that setuptools reads the version from
|
||||
when [project].dynamic lists version and [tool.setuptools.dynamic]
|
||||
sets version = {attr = "pkg.module.NAME"}, with
|
||||
[tool.setuptools].package-dir applied; None otherwise.
|
||||
"""
|
||||
project = data.get("project") or {}
|
||||
if "version" not in project.get("dynamic", []):
|
||||
return None
|
||||
setuptools = (data.get("tool") or {}).get("setuptools") or {}
|
||||
attr = ((setuptools.get("dynamic") or {}).get("version") or {}).get("attr")
|
||||
if not isinstance(attr, str) or "." not in attr:
|
||||
return None
|
||||
module, _, name = attr.rpartition(".")
|
||||
parts = module.split(".")
|
||||
dirs = setuptools.get("package-dir") or {}
|
||||
if parts[0] in dirs:
|
||||
base = os.path.join(dirs[parts[0]], *parts[1:])
|
||||
else:
|
||||
base = os.path.join(dirs.get("", ""), *parts)
|
||||
root = os.path.dirname(os.path.abspath(pyproject_path))
|
||||
for candidate in (f"{base}.py", os.path.join(base, "__init__.py")):
|
||||
path = os.path.join(root, candidate)
|
||||
if os.path.isfile(path):
|
||||
return path, name
|
||||
return None
|
||||
|
||||
|
||||
def read_pyproject_version(repo_dir: str) -> str | None:
|
||||
"""
|
||||
Read the version from pyproject.toml in repo_dir, if present.
|
||||
|
||||
Expects a PEP 621-style [project] table with a 'version' field.
|
||||
Expects a PEP 621-style [project] table with a 'version' field, or a
|
||||
dynamic version that setuptools reads from a module attribute
|
||||
(dynamic_version_source()).
|
||||
"""
|
||||
paths = resolve_repo_paths(repo_dir)
|
||||
path = paths.pyproject_toml
|
||||
@@ -30,7 +78,14 @@ def read_pyproject_version(repo_dir: str) -> str | None:
|
||||
data = tomllib.load(f)
|
||||
project = data.get("project") or {}
|
||||
version = project.get("version")
|
||||
return str(version).strip() if version else None
|
||||
if version:
|
||||
return str(version).strip()
|
||||
source = dynamic_version_source(path, data)
|
||||
if source is None:
|
||||
return None
|
||||
with open(source[0], encoding="utf-8") as f:
|
||||
match = version_literal(source[1]).search(f.read())
|
||||
return match.group(2).strip() if match else None
|
||||
except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError):
|
||||
return None
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -6,17 +6,18 @@ import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from pkgmgr.actions.branch import gc
|
||||
from pkgmgr.actions.branch import gc, gc_forge
|
||||
from pkgmgr.actions.branch.gc_forge import ForgeFacts
|
||||
from pkgmgr.cli.commands.branch import handle_branch
|
||||
from pkgmgr.cli.parser import create_parser
|
||||
from pkgmgr.core.credentials.types import NoCredentialsError
|
||||
|
||||
GIT_ENV = {
|
||||
**os.environ,
|
||||
GITHUB_URL = "https://github.com/example/demo.git"
|
||||
AUTHOR = {
|
||||
"GIT_AUTHOR_NAME": "t",
|
||||
"GIT_AUTHOR_EMAIL": "t@example.org",
|
||||
"GIT_COMMITTER_NAME": "t",
|
||||
@@ -28,7 +29,7 @@ def git(cwd: str, *args: str) -> str:
|
||||
return subprocess.run(
|
||||
["git", "-c", "commit.gpgsign=false", *args],
|
||||
cwd=cwd,
|
||||
env=GIT_ENV,
|
||||
env={**os.environ, **AUTHOR},
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
@@ -46,7 +47,16 @@ def docker_available() -> bool:
|
||||
class GcFixture:
|
||||
def setUp(self) -> None:
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self._tmp.cleanup)
|
||||
self.root = os.path.realpath(self._tmp.name)
|
||||
self.addCleanup(os.chdir, os.getcwd())
|
||||
os.chdir(self.root)
|
||||
home = os.path.join(self.root, "home")
|
||||
os.makedirs(home)
|
||||
scratch = {k: v for k, v in os.environ.items() if not k.startswith("GIT_")}
|
||||
patcher = patch.dict(os.environ, {**scratch, "HOME": home}, clear=True)
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
self.origin = os.path.join(self.root, "origin.git")
|
||||
self.repo = os.path.join(self.root, "repo")
|
||||
git(self.root, "init", "--bare", "-b", "main", self.origin)
|
||||
@@ -81,9 +91,6 @@ class GcFixture:
|
||||
merged_heads={self.squashed_sha: 7}, open_heads={"wip": 3}
|
||||
)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self._tmp.cleanup()
|
||||
|
||||
def _commit(self, name: str) -> None:
|
||||
with open(os.path.join(self.repo, f"{name}.txt"), "w") as fh:
|
||||
fh.write(name)
|
||||
@@ -241,6 +248,80 @@ class TestBranchGc(GcFixture, unittest.TestCase):
|
||||
self.assertEqual(items["merged"].skip, "forge unavailable")
|
||||
self.assertIsNone(items["squashed"].merged_by)
|
||||
|
||||
def _github_without_token(self) -> None:
|
||||
git(self.repo, "branch", "local-done", "merged")
|
||||
git(self.repo, "checkout", "-b", "local-wip", "main")
|
||||
self._commit("local-wip")
|
||||
git(self.repo, "checkout", "main")
|
||||
git(self.repo, "branch", "--track", "tracked", "origin/merged")
|
||||
git(self.repo, "push", "origin", "merged:refs/heads/remote-done")
|
||||
git(self.repo, "remote", "set-url", "origin", GITHUB_URL)
|
||||
|
||||
def _cleanup_without_token(self, apply: bool) -> tuple[int, dict]:
|
||||
out = io.StringIO()
|
||||
with (
|
||||
patch.dict(os.environ, {"GIT_ALLOW_PROTOCOL": "file"}),
|
||||
patch.object(gc, "host_pid_namespace", return_value=True),
|
||||
patch.object(gc, "list_compose_containers", return_value=[]),
|
||||
patch.object(gc, "_process_cwds", return_value={}),
|
||||
patch.object(gc_forge, "TokenResolver") as resolver,
|
||||
patch.object(gc_forge, "HttpClient") as http,
|
||||
redirect_stdout(out),
|
||||
redirect_stderr(io.StringIO()),
|
||||
):
|
||||
resolver.return_value.get_token.side_effect = NoCredentialsError("none")
|
||||
code = gc.cleanup(
|
||||
[self.repo],
|
||||
scope="branch",
|
||||
apply=apply,
|
||||
as_json=True,
|
||||
include=[],
|
||||
exclude=[],
|
||||
merged_into=[],
|
||||
sizes=False,
|
||||
min_age=None,
|
||||
hooks={},
|
||||
)
|
||||
resolver.return_value.get_token.assert_called_once()
|
||||
http.assert_not_called()
|
||||
return code, json.loads(out.getvalue())["repos"][0]
|
||||
|
||||
def test_local_only_merged_branch_needs_no_forge(self) -> None:
|
||||
self._github_without_token()
|
||||
code, plan = self._cleanup_without_token(apply=False)
|
||||
items = {i["branch"]: i for i in plan["items"] if i["branch"]}
|
||||
|
||||
self.assertEqual(code, 0)
|
||||
self.assertIn("forge unavailable: no GitHub token (none)", plan["warnings"])
|
||||
self.assertIsNone(items["local-done"]["skip"])
|
||||
self.assertEqual(items["local-done"]["merged_by"], "ancestor")
|
||||
self.assertEqual(items["local-done"]["actions"], ["local-branch"])
|
||||
self.assertEqual(items["remote-done"]["state"], "merged-clean")
|
||||
self.assertEqual(items["tracked"]["state"], "merged-clean")
|
||||
self.assertEqual(items["local-wip"]["state"], "unmerged")
|
||||
for name in ("merged", "remote-done", "tracked", "local-wip", "squashed"):
|
||||
with self.subTest(branch=name):
|
||||
self.assertEqual(items[name]["skip"], "forge unavailable")
|
||||
|
||||
def test_apply_without_token_deletes_only_the_local_only_merged_branch(
|
||||
self,
|
||||
) -> None:
|
||||
self._github_without_token()
|
||||
before = set(git(self.repo, "for-each-ref", "--format=%(refname)").split())
|
||||
code, plan = self._cleanup_without_token(apply=True)
|
||||
after = set(git(self.repo, "for-each-ref", "--format=%(refname)").split())
|
||||
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(
|
||||
[(i["branch"], i["result"]) for i in plan["items"] if i["result"]],
|
||||
[("local-done", "done")],
|
||||
)
|
||||
self.assertEqual(before - after, {"refs/heads/local-done"})
|
||||
self.assertEqual(after - before, set())
|
||||
self.assertEqual(
|
||||
git(self.origin, "branch", "--list", "remote-done"), "remote-done"
|
||||
)
|
||||
|
||||
def test_stale_metadata_is_listed_not_pruned(self) -> None:
|
||||
stub = os.path.join(self.repo, ".git", "worktrees", "stub")
|
||||
os.makedirs(stub)
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -160,6 +161,27 @@ class TestReview(unittest.TestCase):
|
||||
self.assertNotIn("big 11</td>", html)
|
||||
self.assertIn("20 more lines not shown", html)
|
||||
|
||||
def test_toolbar_buttons_are_labelled_icons(self) -> None:
|
||||
html = self._render("page.html", None, [])
|
||||
|
||||
for key, label in (("expand", "Expand all"), ("collapse", "Collapse all")):
|
||||
with self.subTest(button=key):
|
||||
button = re.search(
|
||||
rf'<button type="button" id="{key}-all"([^>]*)>(.*?)</button>',
|
||||
html,
|
||||
re.DOTALL,
|
||||
)
|
||||
self.assertIsNotNone(button)
|
||||
attrs, inner = button.groups()
|
||||
self.assertIn(f'title="{label}"', attrs)
|
||||
self.assertIn(f'aria-label="{label}"', attrs)
|
||||
self.assertRegex(
|
||||
inner, rf'^<svg class="bi bi-arrows-{key}"[^>]* aria-hidden="true"'
|
||||
)
|
||||
self.assertEqual(re.sub(r"<[^>]+>", "", inner).strip(), "")
|
||||
self.assertNotIn("<link", html)
|
||||
self.assertNotIn("<img", html)
|
||||
|
||||
def test_errors_exit_two(self) -> None:
|
||||
plain = os.path.join(self.root, "plain")
|
||||
os.makedirs(plain)
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import ExitStack
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pkgmgr.actions.publish.workflow import publish
|
||||
|
||||
|
||||
class TestPublishWorkflowChildren(unittest.TestCase):
|
||||
def publish_with(self, mirror: str, environ: dict) -> tuple[list, str]:
|
||||
"""
|
||||
Args:
|
||||
mirror: the PyPI project URL of the mirrors file.
|
||||
environ: extra environment variables of the pkgmgr process;
|
||||
"{repo}" in a value becomes the repository directory.
|
||||
|
||||
Returns:
|
||||
The recorded subprocess.run calls and the repository directory,
|
||||
whose dist is a symlink to a foreign directory with an old
|
||||
artifact.
|
||||
"""
|
||||
repo_dir = tempfile.mkdtemp()
|
||||
foreign = tempfile.mkdtemp()
|
||||
self.addCleanup(shutil.rmtree, repo_dir)
|
||||
self.addCleanup(shutil.rmtree, foreign)
|
||||
with open(os.path.join(foreign, "example-0.9.0.tar.gz"), "w"):
|
||||
pass
|
||||
os.symlink(foreign, os.path.join(repo_dir, "dist"))
|
||||
|
||||
def run(argv, *_args, **kwargs):
|
||||
if argv[2] == "build" and "--help" not in argv:
|
||||
if "--outdir" in argv:
|
||||
out = argv[argv.index("--outdir") + 1]
|
||||
else:
|
||||
out = os.path.join(kwargs["cwd"], "dist")
|
||||
with open(os.path.join(out, "example-1.0.0.tar.gz"), "w"):
|
||||
pass
|
||||
return kwargs
|
||||
|
||||
module = "pkgmgr.actions.publish.workflow"
|
||||
with ExitStack() as stack:
|
||||
stack.enter_context(
|
||||
patch(f"{module}.read_mirrors_file", return_value={"pypi": mirror})
|
||||
)
|
||||
stack.enter_context(
|
||||
patch(f"{module}.head_semver_tags", return_value=["v1.0.0"])
|
||||
)
|
||||
resolver = stack.enter_context(patch(f"{module}.TokenResolver"))
|
||||
mock_run = stack.enter_context(
|
||||
patch(f"{module}.subprocess.run", side_effect=run)
|
||||
)
|
||||
stack.enter_context(
|
||||
patch.dict(
|
||||
os.environ,
|
||||
{
|
||||
name: value.format(repo=repo_dir)
|
||||
for name, value in environ.items()
|
||||
},
|
||||
)
|
||||
)
|
||||
resolver.return_value.get_token.return_value = MagicMock(token="t")
|
||||
publish(repo={}, repo_dir=repo_dir, interactive=False, allow_prompt=False)
|
||||
return mock_run.call_args_list, repo_dir
|
||||
|
||||
def test_every_child_runs_under_the_current_interpreter(self):
|
||||
calls, _ = self.publish_with("https://pypi.org/project/example/", {})
|
||||
argvs = [call.args[0] for call in calls]
|
||||
self.assertEqual(
|
||||
[argv[2] for argv in argvs], ["build", "twine", "build", "twine"]
|
||||
)
|
||||
self.assertEqual({argv[0] for argv in argvs}, {sys.executable})
|
||||
|
||||
def test_children_never_run_in_the_repository(self):
|
||||
calls, repo_dir = self.publish_with("https://pypi.org/project/example/", {})
|
||||
cwds = {call.kwargs["cwd"] for call in calls}
|
||||
self.assertEqual(len(cwds), 1)
|
||||
self.assertNotEqual(os.path.realpath(cwds.pop()), os.path.realpath(repo_dir))
|
||||
|
||||
def test_only_the_fresh_build_is_uploaded(self):
|
||||
calls, repo_dir = self.publish_with("https://pypi.org/project/example/", {})
|
||||
upload = calls[-1].args[0]
|
||||
self.assertEqual(
|
||||
[os.path.basename(path) for path in upload if path.endswith(".tar.gz")],
|
||||
["example-1.0.0.tar.gz"],
|
||||
)
|
||||
self.assertEqual(calls[2].args[0][-1], repo_dir)
|
||||
|
||||
def test_no_pkgmgr_variable_reaches_a_child(self):
|
||||
calls, _ = self.publish_with(
|
||||
"https://pypi.org/project/example/",
|
||||
{"PKGMGR_PYPI_TOKEN": "secret", "PKGMGR_GITHUB_TOKEN": "secret"},
|
||||
)
|
||||
for call in calls:
|
||||
self.assertFalse(
|
||||
[name for name in call.kwargs["env"] if name.startswith("PKGMGR_")]
|
||||
)
|
||||
self.assertEqual(calls[-1].kwargs["env"]["TWINE_PASSWORD"], "t")
|
||||
|
||||
def test_no_repository_path_stays_on_the_children_pythonpath(self):
|
||||
calls, _ = self.publish_with(
|
||||
"https://pypi.org/project/example/",
|
||||
{"PYTHONPATH": os.pathsep.join(["{repo}", "{repo}/src", "/opt/keep"])},
|
||||
)
|
||||
self.assertEqual(
|
||||
{call.kwargs["env"]["PYTHONPATH"] for call in calls}, {"/opt/keep"}
|
||||
)
|
||||
|
||||
def test_the_upload_goes_to_the_host_of_the_mirror(self):
|
||||
for mirror, url in (
|
||||
("https://pypi.org/project/example/", "https://upload.pypi.org/legacy/"),
|
||||
("https://test.pypi.org/project/example/", "https://test.pypi.org/legacy/"),
|
||||
):
|
||||
with self.subTest(mirror=mirror):
|
||||
calls, _ = self.publish_with(mirror, {})
|
||||
upload = calls[-1].args[0]
|
||||
self.assertIn("--non-interactive", upload)
|
||||
self.assertEqual(upload[upload.index("--repository-url") + 1], url)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -140,6 +140,75 @@ class TestUpdatePyprojectVersion(unittest.TestCase):
|
||||
|
||||
self.assertEqual(content, original)
|
||||
|
||||
def write_dynamic_attr_project(self, tmpdir: str, module: str) -> str:
|
||||
with open(os.path.join(tmpdir, "pyproject.toml"), "w", encoding="utf-8") as f:
|
||||
f.write(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
[project]
|
||||
name = "quality"
|
||||
dynamic = ["version"]
|
||||
|
||||
[tool.setuptools]
|
||||
package-dir = {"" = "src"}
|
||||
|
||||
[tool.setuptools.dynamic]
|
||||
version = {attr = "quality.__version__"}
|
||||
"""
|
||||
).lstrip()
|
||||
)
|
||||
os.makedirs(os.path.join(tmpdir, "src", "quality"))
|
||||
path = os.path.join(tmpdir, "src", "quality", "__init__.py")
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(module)
|
||||
return path
|
||||
|
||||
def test_a_dynamic_attr_version_is_bumped_in_its_module_and_read_back(self) -> None:
|
||||
from pkgmgr.core.version.source import read_pyproject_version
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
module = self.write_dynamic_attr_project(
|
||||
tmpdir, '"""Quality."""\n\n__version__ = "0.1.0"\n'
|
||||
)
|
||||
with open(os.path.join(tmpdir, "pyproject.toml"), encoding="utf-8") as f:
|
||||
before = f.read()
|
||||
self.assertEqual(read_pyproject_version(tmpdir), "0.1.0")
|
||||
|
||||
update_pyproject_version(
|
||||
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=False
|
||||
)
|
||||
|
||||
with open(module, encoding="utf-8") as f:
|
||||
self.assertEqual(f.read(), '"""Quality."""\n\n__version__ = "0.2.0"\n')
|
||||
with open(os.path.join(tmpdir, "pyproject.toml"), encoding="utf-8") as f:
|
||||
self.assertEqual(f.read(), before)
|
||||
self.assertEqual(read_pyproject_version(tmpdir), "0.2.0")
|
||||
|
||||
def test_a_dynamic_attr_version_preview_writes_nothing(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
module = self.write_dynamic_attr_project(tmpdir, "__version__ = '0.1.0'\n")
|
||||
|
||||
update_pyproject_version(
|
||||
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=True
|
||||
)
|
||||
|
||||
with open(module, encoding="utf-8") as f:
|
||||
self.assertEqual(f.read(), "__version__ = '0.1.0'\n")
|
||||
|
||||
def test_a_dynamic_attr_module_without_the_literal_raises(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
self.write_dynamic_attr_project(
|
||||
tmpdir,
|
||||
"from importlib.metadata import version\n__version__ = version('quality')\n",
|
||||
)
|
||||
|
||||
with self.assertRaises(RuntimeError) as cm:
|
||||
update_pyproject_version(
|
||||
os.path.join(tmpdir, "pyproject.toml"), "0.2.0", preview=False
|
||||
)
|
||||
|
||||
self.assertIn("Missing __version__ literal", str(cm.exception))
|
||||
|
||||
def test_update_pyproject_version_missing_file_is_skipped(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
path = os.path.join(tmpdir, "pyproject.toml")
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import pwd
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from pkgmgr.core.git.errors import GitRunError
|
||||
from pkgmgr.core.git.run import run
|
||||
from pkgmgr.core.git.run import CALLER_CONFIG, DEFAULT_CONFIG, run, strict_env
|
||||
|
||||
|
||||
class TestGitRun(unittest.TestCase):
|
||||
@@ -55,6 +59,98 @@ class TestGitRun(unittest.TestCase):
|
||||
self.assertNotIn("stdin", kwargs)
|
||||
self.assertIn("timeout", kwargs)
|
||||
|
||||
OVERRIDES = (
|
||||
"GIT_DIR",
|
||||
"GIT_WORK_TREE",
|
||||
"GIT_INDEX_FILE",
|
||||
"GIT_COMMON_DIR",
|
||||
"GIT_CONFIG",
|
||||
"GIT_CEILING_DIRECTORIES",
|
||||
"GIT_CONFIG_GLOBAL",
|
||||
"GIT_CONFIG_SYSTEM",
|
||||
"GIT_CONFIG_PARAMETERS",
|
||||
"GIT_CONFIG_COUNT",
|
||||
"GIT_CONFIG_KEY_0",
|
||||
"GIT_CONFIG_VALUE_0",
|
||||
"GIT_CONFIG_KEY_X",
|
||||
)
|
||||
CONFIG_READ = (
|
||||
"GIT_CEILING_DIRECTORIES",
|
||||
"GIT_CONFIG_COUNT",
|
||||
"GIT_CONFIG_GLOBAL",
|
||||
"GIT_CONFIG_KEY_0",
|
||||
"GIT_CONFIG_PARAMETERS",
|
||||
"GIT_CONFIG_SYSTEM",
|
||||
"GIT_CONFIG_VALUE_0",
|
||||
)
|
||||
|
||||
def _env(
|
||||
self, scan: bool, strict: bool, view: str | None = None
|
||||
) -> dict[str, str] | None:
|
||||
completed = MagicMock()
|
||||
completed.stdout = ""
|
||||
planted = {
|
||||
**dict.fromkeys(self.OVERRIDES, "/elsewhere"),
|
||||
"HOME": "/elsewhere",
|
||||
"XDG_CONFIG_HOME": "/elsewhere",
|
||||
}
|
||||
with (
|
||||
patch.dict("os.environ", planted),
|
||||
patch(
|
||||
"pkgmgr.core.git.run.subprocess.run", return_value=completed
|
||||
) as mock_run,
|
||||
):
|
||||
if strict:
|
||||
with strict_env():
|
||||
run(["status"], scan=scan, config_view=view)
|
||||
else:
|
||||
run(["status"], scan=scan, config_view=view)
|
||||
return mock_run.call_args.kwargs.get("env")
|
||||
|
||||
def _kept(self, env: dict[str, str]) -> list[str]:
|
||||
return sorted(name for name in self.OVERRIDES if name in env)
|
||||
|
||||
def test_scan_drops_repository_overrides_from_the_environment(self) -> None:
|
||||
unset = ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE")
|
||||
for view in (None, CALLER_CONFIG):
|
||||
with self.subTest(view=view):
|
||||
env = self._env(scan=True, strict=False, view=view)
|
||||
|
||||
self.assertEqual(
|
||||
self._kept(env),
|
||||
sorted(name for name in self.OVERRIDES if name not in unset),
|
||||
)
|
||||
self.assertEqual(env.get("GIT_CONFIG_NOSYSTEM"), None if view else "1")
|
||||
self.assertIsNone(self._env(scan=False, strict=False, view=view))
|
||||
|
||||
def test_strict_env_keeps_only_the_ceiling(self) -> None:
|
||||
for scan in (True, False):
|
||||
with self.subTest(scan=scan):
|
||||
env = self._env(scan=scan, strict=True)
|
||||
|
||||
self.assertEqual(self._kept(env), ["GIT_CEILING_DIRECTORIES"])
|
||||
self.assertEqual(env.get("GIT_CONFIG_NOSYSTEM"), "1")
|
||||
|
||||
def test_strict_caller_config_also_keeps_the_config_variables(self) -> None:
|
||||
for scan in (True, False):
|
||||
with self.subTest(scan=scan):
|
||||
env = self._env(scan=scan, strict=True, view=CALLER_CONFIG)
|
||||
|
||||
self.assertEqual(self._kept(env), list(self.CONFIG_READ))
|
||||
self.assertIsNone(env.get("GIT_CONFIG_NOSYSTEM"))
|
||||
self.assertEqual(env.get("HOME"), "/elsewhere")
|
||||
|
||||
def test_default_config_ignores_the_environment(self) -> None:
|
||||
home = pwd.getpwuid(os.getuid()).pw_dir
|
||||
for strict in (True, False):
|
||||
with self.subTest(strict=strict):
|
||||
env = self._env(scan=True, strict=strict, view=DEFAULT_CONFIG)
|
||||
|
||||
self.assertEqual(self._kept(env), ["GIT_CEILING_DIRECTORIES"])
|
||||
self.assertIsNone(env.get("GIT_CONFIG_NOSYSTEM"))
|
||||
self.assertEqual(env.get("HOME"), home)
|
||||
self.assertFalse("XDG_CONFIG_HOME" in env)
|
||||
|
||||
def test_failure_raises_giterror_with_details(self) -> None:
|
||||
# Build a CalledProcessError with stdout/stderr populated
|
||||
import subprocess as sp
|
||||
|
||||
Reference in new issue
Block a user