From 24500e9eed53c2b4f092a4398ef9ccf0bcfbce8a Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 18:19:06 +0000 Subject: [PATCH 1/8] docs: correct the credential statements in CLAUDE.md, CONTRIBUTING.md and the workflows State that a same-repo pull request gets org secrets and that Gitea 28 refuses the job token for org packages. Rename image.yml's workflow to "Images" (cosmetic, no step changed) and update stale comments. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .gitea/workflows/build.yml | 2 +- .gitea/workflows/image.yml | 16 +++++++++------- .gitea/workflows/lint.yml | 4 ++-- CLAUDE.md | 7 ++++--- CONTRIBUTING.md | 7 ++++--- .../credential-and-workflow-comments.yml | 2 ++ 6 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 changelogs/fragments/credential-and-workflow-comments.yml diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 762faff..fcc39e4 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -3,7 +3,7 @@ name: Build # Builds the Collection tarball and checks it. On a v* tag it also checks the release # invariants of ADR 0004 (annotated tag, tag equals v + galaxy.yml version). Nothing is # uploaded: the tarball is built in a temporary directory and discarded. Releases are -# consumed by git tag, so there is no publish step. No secrets are used. +# consumed by git tag, so there is no publish step. No org secret is used; the job token (secrets.GITHUB_TOKEN) clones the repository. on: # yamllint disable-line rule:truthy pull_request: branches: diff --git a/.gitea/workflows/image.yml b/.gitea/workflows/image.yml index 7d1a992..4ceee46 100644 --- a/.gitea/workflows/image.yml +++ b/.gitea/workflows/image.yml @@ -1,4 +1,4 @@ -name: Molecule image +name: Images # Builds the images under images/ with kaniko (osscontainertools fork, v1.28.6) and # pushes them to git.rkdr.net/mortis/ (ADR 0004, Image distribution): the molecule @@ -18,10 +18,11 @@ name: Molecule image # to that entrypoint, not substituted. The pinned kaniko executor is therefore # downloaded by layer digest and verified with sha256sum. # - Credentials: the job token is tried first (permissions: packages: write). Gitea 28 -# probably refuses it for an organisation-owned package (the Actions user is not an -# org member: services/context/package.go, TODO ActionUser permission check). Only if -# a write probe is refused are the org secrets PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN -# used. The log names the method that worked, never a value. +# refuses it for an organisation-owned package (HTTP 401 on a write probe, run 2237: +# the Actions user is not an org member, services/context/package.go, TODO ActionUser +# permission check). Only if a write probe is refused are the org secrets +# PACKAGE_PUSH_USER / PACKAGE_PUSH_TOKEN used. The log names the method that worked, +# never a value. # - Layers: every layer stays far below Cloudflare's 100 MB request-body limit (see the # Dockerfile). No kaniko cache: a tiny, rarely rebuilt image gains little, and cache # layers would be pushed through the same Cloudflare-limited path. @@ -107,8 +108,9 @@ jobs: # file difference under /usr, /lib, /bin, /sbin, /opt or /etc fails the job. # Names only, no sizes or modes, and /var/log, /run and dpkg's *-old files are # not compared: logs and dpkg state the install touched may end up in the first - # layer as modified files. That is cosmetic; after the first push, list the - # layers (crane export | tar t) and look for run/act, root/ and var/log/apt. + # layer as modified files. That is cosmetic. Not yet checked: listing the layers + # (crane export | tar t, looking for run/act, root/ and var/log/apt) needs a + # read:package token, which the Claude token lacks. # # The pinned kaniko v1.28.6 image has index digest # sha256:2fa20b0a4b40c7db72b479d57e55c2cf79f2178533b2d3bcbd129d6bd3c087ed and diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index c2f3e27..b3f1d9b 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -1,8 +1,8 @@ name: Lint # Runs on the Collection's own runner (label kamino-docker, scoped to the mortis org), -# never on the production runner. No secrets are used: the only credential is the -# job token, which is read-only for a fork pull request. No pull_request_target. +# never on the production runner. No org secret is used: the only credential is the +# job token (secrets.GITHUB_TOKEN), which is read-only for a fork pull request. No pull_request_target. # The tools come from the ci image (images/ci, built by image.yml): the pinned Python # tools of requirements-dev.txt and the hook environments of .pre-commit-config.yaml # are baked in. When a PR changes one of those files the checksum check below fails diff --git a/CLAUDE.md b/CLAUDE.md index ff5fb52..c964a80 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -33,7 +33,7 @@ protection, or create tags: those are the operator's. ## Workflows (`.gitea/workflows/`) -- Never `pull_request_target`. No secrets on `pull_request`. Pass ref, repo and name values to +- Never `pull_request_target`. A fork `pull_request` run gets no secrets and a read-only job token; a same-repo pull request does get the org Actions secrets, so nothing but `image.yml` may reference them. Pass ref, repo and name values to scripts through `env:`, never interpolate `${{ github.* }}` into a `run:` block. - Runner labels are `kamino-docker` and `kamino-molecule` only (`.gitea/actionlint.yaml`). Use the `github.*` contexts, which Gitea aliases; actionlint does not know `gitea.*`. @@ -55,5 +55,6 @@ protection, or create tags: those are the operator's. digest. - Each pushed layer must stay below 100 MB: Cloudflare rejects larger request bodies with HTTP 413. That is why the `ci` image deletes the Go toolchains from the hook cache. -- The push credential is the org Actions secrets `PACKAGE_PUSH_USER` and `PACKAGE_PUSH_TOKEN`. - Never print or use them anywhere else. +- `image.yml` tries the job token first with a write probe; Gitea 28 refuses it for the org's packages + (HTTP 401, run 2237), so the push uses the org Actions secrets `PACKAGE_PUSH_USER` and + `PACKAGE_PUSH_TOKEN`. Never print or use them anywhere else. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1bcbbf0..0a090e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,9 +28,10 @@ outside contributions: - Protect the `v*` tags so only maintainers can create them. A tag run uses the workflow files of the tagged commit and may receive organisation secrets. -- Protect `main`: pull request required, with the `Lint` and `Build` checks. -- The image workflow (`.gitea/workflows/image.yml`) pushes with the Actions job token - when Gitea allows it, otherwise with the organisation secrets +- Protect `main`: pull request required, with the `Lint` and `Build` checks. Molecule is not a required + check: its path filter would leave the check pending forever on a pull request that touches no role. +- The image workflow (`.gitea/workflows/image.yml`) tries the Actions job token first; + Gitea 28 refuses it for an organisation's packages, so it pushes with the organisation secrets `PACKAGE_PUSH_USER` and `PACKAGE_PUSH_TOKEN` (a bot user with write access to the organisation's packages and a token with only the `write:package` scope). Keep those the only credentials stored at organisation level. diff --git a/changelogs/fragments/credential-and-workflow-comments.yml b/changelogs/fragments/credential-and-workflow-comments.yml new file mode 100644 index 0000000..b858e5a --- /dev/null +++ b/changelogs/fragments/credential-and-workflow-comments.yml @@ -0,0 +1,2 @@ +trivial: + - Document the credential model in CLAUDE.md, CONTRIBUTING.md and the workflow headers, and rename the image workflow to Images. From 4ed03b508a18ee8bfa2fdaff7ecff4b7bd279615 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 18:20:47 +0000 Subject: [PATCH 2/8] fix(scripts): make dev-setup.sh check Python, core.hooksPath and the installed hooks Require Python 3.11+ (PYTHON selects the interpreter), rebuild a virtualenv made by an older Python, exit early when core.hooksPath is set, fail clearly when a hook was not installed, and resolve the hook path with --path-format=absolute. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- changelogs/fragments/dev-setup-robustness.yml | 4 +++ scripts/dev-setup.sh | 34 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/dev-setup-robustness.yml diff --git a/changelogs/fragments/dev-setup-robustness.yml b/changelogs/fragments/dev-setup-robustness.yml new file mode 100644 index 0000000..dac86bf --- /dev/null +++ b/changelogs/fragments/dev-setup-robustness.yml @@ -0,0 +1,4 @@ +trivial: + - >- + scripts/dev-setup.sh checks for Python 3.11 or newer (PYTHON selects one), rebuilds an older + virtualenv, and fails early when core.hooksPath is set or the hooks are missing. diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index 575abf7..08c0ab3 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -2,7 +2,7 @@ # Sets up a local checkout for development: a virtualenv with the pinned tools # (requirements-dev.txt, the versions CI runs) and git hooks that find them. # -# scripts/dev-setup.sh +# scripts/dev-setup.sh # PYTHON=python3.12 scripts/dev-setup.sh to pick one # # Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint, # ansible-lint): they run their tools by name, so the tools must be on PATH when git @@ -11,16 +11,44 @@ # again after changing requirements-dev.txt or the hook types. set -euo pipefail +# ansible-core 2.19 (requirements-dev.txt) needs Python 3.11+. The ci image uses the +# distribution's 3.12; any 3.11+ gives the same pins. +python="${PYTHON:-python3}" +py_ok='import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)' +if ! "$python" -c "$py_ok" 2>/dev/null; then + echo "error: $python is missing or older than 3.11 (ansible-core 2.19 needs 3.11+); set PYTHON=/path/to/python3.12" >&2 + exit 1 +fi + root="$(git rev-parse --show-toplevel)" cd "$root" -python3 -m venv .venv +# `pre-commit install` refuses to run when core.hooksPath is set, and git would not run +# .git/hooks anyway. Say so up front instead of failing half way. +hooks_path="$(git config --get core.hooksPath || true)" +if [ -n "$hooks_path" ]; then + echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2 + echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2 + echo " or manage the hooks in that directory yourself, then re-run this script." >&2 + exit 1 +fi + +# A virtualenv made by an older Python is rebuilt rather than reused. +if [ -x .venv/bin/python ] && ! .venv/bin/python -c "$py_ok" 2>/dev/null; then + rm -rf "$root/.venv" +fi +[ -x .venv/bin/python ] || "$python" -m venv .venv .venv/bin/pip install --quiet --disable-pip-version-check -r requirements-dev.txt .venv/bin/pre-commit install --install-hooks marker="# kamino-dev-setup: virtualenv first on PATH" +# default_install_hook_types in .pre-commit-config.yaml names these; keep in step. for hook in pre-commit commit-msg; do - file="$(git rev-parse --git-path "hooks/$hook")" + file="$(git rev-parse --path-format=absolute --git-path "hooks/$hook")" + if [ ! -f "$file" ]; then + echo "error: $file was not installed by 'pre-commit install'; check default_install_hook_types in .pre-commit-config.yaml." >&2 + exit 1 + fi if ! grep -qxF "$marker" "$file"; then tmp="$(mktemp)" { From 705b370e230375b91327f5d8f937548e79f59c2a Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 18:22:57 +0000 Subject: [PATCH 3/8] fix(ci): let a push to main finish its Lint and Build runs The concurrency group was the ref, so a second merge to main cancelled the run of the first. Key the group on the sha outside pull requests and cancel only on pull_request. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .gitea/workflows/build.yml | 6 ++++-- .gitea/workflows/lint.yml | 7 ++++--- changelogs/fragments/ci-concurrency-main-runs.yml | 2 ++ 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/ci-concurrency-main-runs.yml diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 762faff..c9d1ce7 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -18,9 +18,11 @@ on: # yamllint disable-line rule:truthy permissions: contents: read +# A newer push to the same pull request cancels the running run; a push to main or a tag +# never cancels another run. concurrency: - group: build-${{ github.ref }} - cancel-in-progress: true + group: build-${{ github.event_name == 'pull_request' && github.ref || github.sha }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: build: diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index c2f3e27..d5e3c3b 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -19,10 +19,11 @@ on: # yamllint disable-line rule:truthy permissions: contents: read -# A newer push to the same ref makes the running lint obsolete. +# A newer push to the same pull request makes the running run obsolete. A push to main or a +# tag is never cancelled: each merge commit keeps its own result. concurrency: - group: lint-${{ github.ref }} - cancel-in-progress: true + group: lint-${{ github.event_name == 'pull_request' && github.ref || github.sha }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: lint: diff --git a/changelogs/fragments/ci-concurrency-main-runs.yml b/changelogs/fragments/ci-concurrency-main-runs.yml new file mode 100644 index 0000000..9c037e5 --- /dev/null +++ b/changelogs/fragments/ci-concurrency-main-runs.yml @@ -0,0 +1,2 @@ +trivial: + - A later push to main no longer cancels the Lint and Build runs of an earlier one; pull request runs still cancel their predecessors. From 19faa49a4c484f562d5cd24925c398407c991cc3 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 18:48:07 +0000 Subject: [PATCH 4/8] fix(dev-setup): derive the repository from the script, rename PYTHON to KAMINO_PYTHON The script took its repository from the caller's working directory, so running it from another repository could create or remove that repository's .venv. It now resolves its own location (symlinks included) and uses git -C for every call. The interpreter variable is KAMINO_PYTHON, and the usage comment no longer claims it always selects the venv interpreter. A core.hooksPath that resolves to the repository's own hooks directory is lifted for the install and restored; any other value is still rejected. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- changelogs/fragments/dev-setup-robustness.yml | 2 +- scripts/dev-setup.sh | 46 ++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/changelogs/fragments/dev-setup-robustness.yml b/changelogs/fragments/dev-setup-robustness.yml index dac86bf..289916f 100644 --- a/changelogs/fragments/dev-setup-robustness.yml +++ b/changelogs/fragments/dev-setup-robustness.yml @@ -1,4 +1,4 @@ trivial: - >- - scripts/dev-setup.sh checks for Python 3.11 or newer (PYTHON selects one), rebuilds an older + scripts/dev-setup.sh checks for Python 3.11 or newer (KAMINO_PYTHON selects one), derives the repository from its own location, rebuilds an older virtualenv, and fails early when core.hooksPath is set or the hooks are missing. diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index 08c0ab3..e93eba4 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -2,7 +2,13 @@ # Sets up a local checkout for development: a virtualenv with the pinned tools # (requirements-dev.txt, the versions CI runs) and git hooks that find them. # -# scripts/dev-setup.sh # PYTHON=python3.12 scripts/dev-setup.sh to pick one +# scripts/dev-setup.sh # KAMINO_PYTHON=python3.12 scripts/dev-setup.sh to pick one +# +# KAMINO_PYTHON (default python3) selects the interpreter that is checked and that creates +# a new virtualenv. An existing virtualenv is kept when it already runs Python 3.11+, even +# if it was built by another interpreter; only an older one is rebuilt. +# +# The repository is the one this script lives in, whatever the caller's working directory. # # Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint, # ansible-lint): they run their tools by name, so the tools must be on PATH when git @@ -13,24 +19,42 @@ set -euo pipefail # ansible-core 2.19 (requirements-dev.txt) needs Python 3.11+. The ci image uses the # distribution's 3.12; any 3.11+ gives the same pins. -python="${PYTHON:-python3}" +python="${KAMINO_PYTHON:-python3}" py_ok='import sys; sys.exit(0 if sys.version_info >= (3, 11) else 1)' if ! "$python" -c "$py_ok" 2>/dev/null; then - echo "error: $python is missing or older than 3.11 (ansible-core 2.19 needs 3.11+); set PYTHON=/path/to/python3.12" >&2 + echo "error: $python is missing or older than 3.11 (ansible-core 2.19 needs 3.11+); set KAMINO_PYTHON=/path/to/python3.12" >&2 exit 1 fi -root="$(git rev-parse --show-toplevel)" +# The repository is derived from this script's own location (symlinks resolved), never +# from the caller's working directory: running it from another repository must not +# create or remove that repository's .venv. +script="$(readlink -f "${BASH_SOURCE[0]}")" +root="$(git -C "$(dirname "$script")/.." rev-parse --show-toplevel)" cd "$root" # `pre-commit install` refuses to run when core.hooksPath is set, and git would not run -# .git/hooks anyway. Say so up front instead of failing half way. -hooks_path="$(git config --get core.hooksPath || true)" +# .git/hooks anyway. A local setting that resolves to this repository's own hooks +# directory is harmless: it is lifted for the install and restored afterwards. Anything +# else (another directory, or a setting outside this repository's config) fails up front. +hooks_path="$(git -C "$root" config --get core.hooksPath || true)" +restore_hooks_path="" if [ -n "$hooks_path" ]; then - echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2 - echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2 - echo " or manage the hooks in that directory yourself, then re-run this script." >&2 - exit 1 + own_hooks="$(readlink -f "$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)/hooks")" + case "$hooks_path" in + /*) resolved="$(readlink -m "$hooks_path")" ;; + *) resolved="$(readlink -m "$root/$hooks_path")" ;; + esac + if [ "$resolved" = "$own_hooks" ] && git -C "$root" config --local --get core.hooksPath >/dev/null 2>&1; then + restore_hooks_path="$hooks_path" + git -C "$root" config --local --unset-all core.hooksPath + trap 'git -C "$root" config --local core.hooksPath "$restore_hooks_path"' EXIT + else + echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2 + echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2 + echo " or manage the hooks in that directory yourself, then re-run this script." >&2 + exit 1 + fi fi # A virtualenv made by an older Python is rebuilt rather than reused. @@ -44,7 +68,7 @@ fi marker="# kamino-dev-setup: virtualenv first on PATH" # default_install_hook_types in .pre-commit-config.yaml names these; keep in step. for hook in pre-commit commit-msg; do - file="$(git rev-parse --path-format=absolute --git-path "hooks/$hook")" + file="$(git -C "$root" rev-parse --path-format=absolute --git-path "hooks/$hook")" if [ ! -f "$file" ]; then echo "error: $file was not installed by 'pre-commit install'; check default_install_hook_types in .pre-commit-config.yaml." >&2 exit 1 From 3b19cfd65e1edc98e3ea55b74aabaa1696000f0b Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 18:52:57 +0000 Subject: [PATCH 5/8] fix(ci): give tag runs their own concurrency group A tag run and the main run of the same commit shared the group keyed by the commit SHA, so one could wait on the other. Tag runs are now keyed by their ref. Cancellation stays limited to pull requests. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .gitea/workflows/build.yml | 5 +++-- .gitea/workflows/lint.yml | 5 +++-- changelogs/fragments/ci-concurrency-main-runs.yml | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index c9d1ce7..b9ebd24 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -19,9 +19,10 @@ permissions: contents: read # A newer push to the same pull request cancels the running run; a push to main or a tag -# never cancels another run. +# never cancels another run. A tag run has its own group, so it never shares one with the +# main run of the same commit. concurrency: - group: build-${{ github.event_name == 'pull_request' && github.ref || github.sha }} + group: build-${{ github.event_name == 'pull_request' && github.ref || (github.ref_type == 'tag' && github.ref || github.sha) }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index d5e3c3b..3546e2f 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -20,9 +20,10 @@ permissions: contents: read # A newer push to the same pull request makes the running run obsolete. A push to main or a -# tag is never cancelled: each merge commit keeps its own result. +# tag is never cancelled: each merge commit keeps its own result, and a tag run has its own +# group, so it never shares one with the main run of the same commit. concurrency: - group: lint-${{ github.event_name == 'pull_request' && github.ref || github.sha }} + group: lint-${{ github.event_name == 'pull_request' && github.ref || (github.ref_type == 'tag' && github.ref || github.sha) }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: diff --git a/changelogs/fragments/ci-concurrency-main-runs.yml b/changelogs/fragments/ci-concurrency-main-runs.yml index 9c037e5..6fe8632 100644 --- a/changelogs/fragments/ci-concurrency-main-runs.yml +++ b/changelogs/fragments/ci-concurrency-main-runs.yml @@ -1,2 +1,4 @@ trivial: - - A later push to main no longer cancels the Lint and Build runs of an earlier one; pull request runs still cancel their predecessors. + - >- + A later push to main no longer cancels the Lint and Build runs of an earlier one; pull request + runs still cancel their predecessors, and a tag run has its own group. From 74a5a0739ba4eb884e09d217a5017624af5f0a92 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 19:05:20 +0000 Subject: [PATCH 6/8] fix(dev-setup): install the restore trap first and fail clearly on a global hooksPath Install the EXIT/INT/TERM traps before core.hooksPath is lifted, make the restore idempotent, and save and restore every local value of a multi-valued setting in order. When a global or system value still applies after the local one is lifted, print the script's own message and exit 1 before pre-commit runs. Note that readlink -f/-m are GNU. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- scripts/dev-setup.sh | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index e93eba4..0f45fe2 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -10,6 +10,9 @@ # # The repository is the one this script lives in, whatever the caller's working directory. # +# Requires GNU readlink (-f and -m): on macOS that is 12.3+ for -f, or coreutils (greadlink +# on PATH as readlink) for -m. +# # Some hooks of .pre-commit-config.yaml are `language: system` (REUSE, changelog lint, # ansible-lint): they run their tools by name, so the tools must be on PATH when git # runs the hook. `pre-commit install` does not arrange that, so this script puts the @@ -38,7 +41,21 @@ cd "$root" # directory is harmless: it is lifted for the install and restored afterwards. Anything # else (another directory, or a setting outside this repository's config) fails up front. hooks_path="$(git -C "$root" config --get core.hooksPath || true)" -restore_hooks_path="" +local_paths=() +restore_hooks_path() { + # Idempotent and harmless when the unset below failed: it only rewrites the setting when + # the local values differ from the saved ones. Never fails (it runs from the EXIT trap). + [ "${#local_paths[@]}" -gt 0 ] || return 0 + local now saved v + now="$(git -C "$root" config --local --get-all core.hooksPath 2>/dev/null || true)" + saved="$(printf '%s\n' "${local_paths[@]}")" + [ "$now" = "$saved" ] && return 0 + git -C "$root" config --local --unset-all core.hooksPath 2>/dev/null || true + for v in "${local_paths[@]}"; do + git -C "$root" config --local --add core.hooksPath "$v" || true + done + return 0 +} if [ -n "$hooks_path" ]; then own_hooks="$(readlink -f "$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)/hooks")" case "$hooks_path" in @@ -46,9 +63,19 @@ if [ -n "$hooks_path" ]; then *) resolved="$(readlink -m "$root/$hooks_path")" ;; esac if [ "$resolved" = "$own_hooks" ] && git -C "$root" config --local --get core.hooksPath >/dev/null 2>&1; then - restore_hooks_path="$hooks_path" + # Save every local value (the setting may be multi-valued), and install the traps + # BEFORE changing anything, so an interruption between the two cannot lose the setting. + mapfile -t local_paths < <(git -C "$root" config --local --get-all core.hooksPath) + trap restore_hooks_path EXIT + trap 'exit 130' INT + trap 'exit 143' TERM git -C "$root" config --local --unset-all core.hooksPath - trap 'git -C "$root" config --local core.hooksPath "$restore_hooks_path"' EXIT + if still="$(git -C "$root" config --get core.hooksPath)"; then + echo "error: core.hooksPath is still set to '$still' after lifting this clone's own setting, so a" >&2 + echo " global or system value applies and 'pre-commit install' would refuse to run." >&2 + echo " Unset that setting (git config --global --unset-all core.hooksPath), or run this script with it removed, then re-run." >&2 + exit 1 + fi else echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2 echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2 From 39fd0c385ac9b173ef7fa1628295ce73cdd1e9ea Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 19:18:28 +0000 Subject: [PATCH 7/8] fix(ci): drop the dead tag branch from the Lint concurrency group lint.yml has no tag trigger, so the ref_type == 'tag' branch of its concurrency group could never run. Keep it in build.yml, which has one, and correct the comment and changelog fragment accordingly. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .gitea/workflows/lint.yml | 8 ++++---- changelogs/fragments/ci-concurrency-main-runs.yml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index 3546e2f..944ee2e 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -19,11 +19,11 @@ on: # yamllint disable-line rule:truthy permissions: contents: read -# A newer push to the same pull request makes the running run obsolete. A push to main or a -# tag is never cancelled: each merge commit keeps its own result, and a tag run has its own -# group, so it never shares one with the main run of the same commit. +# A newer push to the same pull request makes the running run obsolete. A push to main is +# never cancelled: each merge commit keeps its own result, so its group is the commit SHA. +# (This workflow has no tag trigger, so no tag branch in the group.) concurrency: - group: lint-${{ github.event_name == 'pull_request' && github.ref || (github.ref_type == 'tag' && github.ref || github.sha) }} + group: lint-${{ github.event_name == 'pull_request' && github.ref || github.sha }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: diff --git a/changelogs/fragments/ci-concurrency-main-runs.yml b/changelogs/fragments/ci-concurrency-main-runs.yml index 6fe8632..9ac883d 100644 --- a/changelogs/fragments/ci-concurrency-main-runs.yml +++ b/changelogs/fragments/ci-concurrency-main-runs.yml @@ -1,4 +1,4 @@ trivial: - >- A later push to main no longer cancels the Lint and Build runs of an earlier one; pull request - runs still cancel their predecessors, and a tag run has its own group. + runs still cancel their predecessors, and in the Build workflow a tag run has its own group. From 72e98133945674675d144bafb89b62d78e261d94 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Sun, 25 Oct 2026 19:50:46 +0000 Subject: [PATCH 8/8] fix(dev-setup): quote the hook path, read core.hooksPath NUL-safely and lift an empty value The repository path is shell-quoted (printf %q) where it is written into the installed hooks, so a path with spaces, quotes or $(...) is data. The local core.hooksPath values are read with -z and compared and restored element by element, so a value with a newline survives; the loop avoids mapfile. Any local value, including an empty or trailing empty one, is lifted for the install and restored. tests/scripts/test_dev_setup.py covers it with stub tools in throwaway repositories, and lint.yml now runs tests/scripts. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm --- .gitea/workflows/lint.yml | 5 + changelogs/fragments/dev-setup-robustness.yml | 4 +- scripts/dev-setup.sh | 90 ++++--- tests/scripts/test_dev_setup.py | 249 ++++++++++++++++++ 4 files changed, 315 insertions(+), 33 deletions(-) create mode 100644 tests/scripts/test_dev_setup.py diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index c2f3e27..cb4a286 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -67,6 +67,11 @@ jobs: export PATH="/opt/venv/bin:$PATH" pre-commit run --all-files --show-diff-on-failure + - name: Test the repository scripts + run: | + export PATH="/opt/venv/bin:$PATH" + python3 -m unittest discover -s tests/scripts + # The gitleaks hook scans --staged only, which is empty in a clean checkout, # so scan the PR's commits explicitly with the binary pre-commit built. - name: Scan PR commits for secrets diff --git a/changelogs/fragments/dev-setup-robustness.yml b/changelogs/fragments/dev-setup-robustness.yml index 289916f..709ae0b 100644 --- a/changelogs/fragments/dev-setup-robustness.yml +++ b/changelogs/fragments/dev-setup-robustness.yml @@ -1,4 +1,6 @@ trivial: - >- scripts/dev-setup.sh checks for Python 3.11 or newer (KAMINO_PYTHON selects one), derives the repository from its own location, rebuilds an older - virtualenv, and fails early when core.hooksPath is set or the hooks are missing. + virtualenv, shell-quotes the repository path it writes into the installed hooks, lifts a repo-local core.hooksPath that points at this + repository's own hooks (including a multi-valued, empty or newline-containing one) for the install and restores it exactly, and fails early + only when core.hooksPath points elsewhere or is set outside the repository's config (a global value), or when the hooks are missing. diff --git a/scripts/dev-setup.sh b/scripts/dev-setup.sh index 0f45fe2..ad7ee17 100755 --- a/scripts/dev-setup.sh +++ b/scripts/dev-setup.sh @@ -37,51 +37,76 @@ root="$(git -C "$(dirname "$script")/.." rev-parse --show-toplevel)" cd "$root" # `pre-commit install` refuses to run when core.hooksPath is set, and git would not run -# .git/hooks anyway. A local setting that resolves to this repository's own hooks -# directory is harmless: it is lifted for the install and restored afterwards. Anything -# else (another directory, or a setting outside this repository's config) fails up front. -hooks_path="$(git -C "$root" config --get core.hooksPath || true)" +# .git/hooks anyway. A repo-local setting whose values all resolve to this repository's own +# hooks directory (or are empty) is harmless: it is lifted for the install and restored +# afterwards, byte for byte. Anything else (another directory, or a value outside this +# repository's config, such as a global one) fails up front. + +# Fills the global array cur_paths with every repo-local core.hooksPath value, read +# NUL-delimited so a value with a newline survives and an empty value is kept as an +# element. (No mapfile: it needs bash 4; the loop also runs on the 3.2 of macOS, untested.) +cur_paths=() +read_local_paths() { + local v + cur_paths=() + while IFS= read -r -d '' v; do + cur_paths+=("$v") + done < <(git -C "$root" config --local -z --get-all core.hooksPath 2>/dev/null || true) +} + local_paths=() restore_hooks_path() { # Idempotent and harmless when the unset below failed: it only rewrites the setting when # the local values differ from the saved ones. Never fails (it runs from the EXIT trap). [ "${#local_paths[@]}" -gt 0 ] || return 0 - local now saved v - now="$(git -C "$root" config --local --get-all core.hooksPath 2>/dev/null || true)" - saved="$(printf '%s\n' "${local_paths[@]}")" - [ "$now" = "$saved" ] && return 0 + local i v same=1 + read_local_paths + if [ "${#cur_paths[@]}" -ne "${#local_paths[@]}" ]; then + same=0 + else + i=0 + while [ "$i" -lt "${#local_paths[@]}" ]; do + [ "${cur_paths[$i]}" = "${local_paths[$i]}" ] || same=0 + i=$((i + 1)) + done + fi + [ "$same" -eq 1 ] && return 0 git -C "$root" config --local --unset-all core.hooksPath 2>/dev/null || true for v in "${local_paths[@]}"; do git -C "$root" config --local --add core.hooksPath "$v" || true done return 0 } -if [ -n "$hooks_path" ]; then + +read_local_paths +if [ "${#cur_paths[@]}" -gt 0 ]; then own_hooks="$(readlink -f "$(git -C "$root" rev-parse --path-format=absolute --git-common-dir)/hooks")" - case "$hooks_path" in - /*) resolved="$(readlink -m "$hooks_path")" ;; - *) resolved="$(readlink -m "$root/$hooks_path")" ;; - esac - if [ "$resolved" = "$own_hooks" ] && git -C "$root" config --local --get core.hooksPath >/dev/null 2>&1; then - # Save every local value (the setting may be multi-valued), and install the traps - # BEFORE changing anything, so an interruption between the two cannot lose the setting. - mapfile -t local_paths < <(git -C "$root" config --local --get-all core.hooksPath) - trap restore_hooks_path EXIT - trap 'exit 130' INT - trap 'exit 143' TERM - git -C "$root" config --local --unset-all core.hooksPath - if still="$(git -C "$root" config --get core.hooksPath)"; then - echo "error: core.hooksPath is still set to '$still' after lifting this clone's own setting, so a" >&2 - echo " global or system value applies and 'pre-commit install' would refuse to run." >&2 - echo " Unset that setting (git config --global --unset-all core.hooksPath), or run this script with it removed, then re-run." >&2 + for v in "${cur_paths[@]}"; do + [ -n "$v" ] || continue + case "$v" in + /*) resolved="$(readlink -m "$v")" ;; + *) resolved="$(readlink -m "$root/$v")" ;; + esac + if [ "$resolved" != "$own_hooks" ]; then + echo "error: core.hooksPath is set to '$v', so git does not run the hooks pre-commit installs." >&2 + echo " Unset it for this clone (git config --unset-all core.hooksPath)" >&2 + echo " or manage the hooks in that directory yourself, then re-run this script." >&2 exit 1 fi - else - echo "error: core.hooksPath is set to '$hooks_path', so git does not run the hooks pre-commit installs." >&2 - echo " Unset it for this clone (git config --unset core.hooksPath; add --global if it is set there)" >&2 - echo " or manage the hooks in that directory yourself, then re-run this script." >&2 - exit 1 - fi + done + # Save every local value (the setting may be multi-valued, or empty), and install the + # traps BEFORE changing anything, so an interruption between the two cannot lose it. + local_paths=("${cur_paths[@]}") + trap restore_hooks_path EXIT + trap 'exit 130' INT + trap 'exit 143' TERM + git -C "$root" config --local --unset-all core.hooksPath +fi +if still="$(git -C "$root" config --get core.hooksPath)" && [ -n "$still" ]; then + echo "error: core.hooksPath is still set to '$still' (outside this clone's own config: a global or" >&2 + echo " system value), so 'pre-commit install' would refuse to run." >&2 + echo " Unset that setting (git config --global --unset-all core.hooksPath), then re-run." >&2 + exit 1 fi # A virtualenv made by an older Python is rebuilt rather than reused. @@ -104,7 +129,8 @@ for hook in pre-commit commit-msg; do tmp="$(mktemp)" { head -n 1 "$file" - printf '%s\n' "$marker" "export PATH=\"$root/.venv/bin:\$PATH\"" + # %q shell-quotes the path: a repository path with spaces, quotes or $(...) is data. + printf '%s\n' "$marker" "export PATH=$(printf '%q' "$root/.venv/bin"):\"\$PATH\"" tail -n +2 "$file" } > "$tmp" cat "$tmp" > "$file" # keeps the file mode of the hook diff --git a/tests/scripts/test_dev_setup.py b/tests/scripts/test_dev_setup.py new file mode 100644 index 0000000..c861118 --- /dev/null +++ b/tests/scripts/test_dev_setup.py @@ -0,0 +1,249 @@ +# Copyright (c) 2026 Mortis +# SPDX-License-Identifier: GPL-3.0-or-later +"""Tests for scripts/dev-setup.sh against throwaway git repositories. + +Python, pip and pre-commit are stubs on a private directory, so nothing is installed +and no network is used. Git is isolated from the real configuration (GIT_CONFIG_GLOBAL +and HOME point into the temporary directory). Skipped when bash or git is missing. + +Run: python3 -I -m unittest discover -s tests/scripts +""" + +from __future__ import annotations + +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +SCRIPT = Path(__file__).resolve().parents[2] / "scripts" / "dev-setup.sh" + +PYTHON_STUB = """#!/bin/sh +# Stub interpreter: every -c check passes; `-m venv DIR` lays out DIR/bin with the stubs. +if [ "$1" = "-c" ]; then exit 0; fi +if [ "$1" = "-m" ] && [ "$2" = "venv" ]; then + mkdir -p "$3/bin" + cp "$STUB_DIR/python" "$3/bin/python" + cp "$STUB_DIR/pip" "$3/bin/pip" + cp "$STUB_DIR/pre-commit" "$3/bin/pre-commit" + chmod +x "$3/bin/python" "$3/bin/pip" "$3/bin/pre-commit" + exit 0 +fi +exit 2 +""" + +PIP_STUB = "#!/bin/sh\nexit 0\n" + +# Mimics `pre-commit install`: refuses when core.hooksPath has any value, otherwise writes +# the two hooks into the repository's hooks directory. The hook body prints PATH. +PRECOMMIT_STUB = """#!/bin/sh +if [ "${STUB_PRECOMMIT_FAIL:-}" = "1" ]; then echo "stub pre-commit failure" >&2; exit 1; fi +if git config --get-all core.hooksPath >/dev/null 2>&1; then + echo "Cannot install: core.hooksPath is set" >&2 + exit 1 +fi +for h in pre-commit commit-msg; do + f="$(git rev-parse --path-format=absolute --git-path "hooks/$h")" + mkdir -p "$(dirname "$f")" + printf '#!/usr/bin/env bash\\necho "hook-ran"\\necho "$PATH"\\n' > "$f" + chmod +x "$f" +done +exit 0 +""" + + +@unittest.skipUnless(shutil.which("bash") and shutil.which("git"), "bash and git are required") +class DevSetupTest(unittest.TestCase): + def setUp(self) -> None: + self.tmp = Path(tempfile.mkdtemp(prefix="kamino-devsetup-")) + self.addCleanup(shutil.rmtree, self.tmp, True) + self.stubs = self.tmp / "stubs" + self.stubs.mkdir() + for name, body in (("python", PYTHON_STUB), ("pip", PIP_STUB), ("pre-commit", PRECOMMIT_STUB)): + path = self.stubs / name + path.write_text(body) + path.chmod(0o755) + # The venv's own python only has to pass `-c`; reuse the same stub logic. + (self.stubs / "python").write_text(PYTHON_STUB) + self.global_cfg = self.tmp / "gitconfig" + self.global_cfg.write_text("") + self.home = self.tmp / "home" + self.home.mkdir() + self.env = { + "PATH": os.environ["PATH"], + "HOME": str(self.home), + "GIT_CONFIG_GLOBAL": str(self.global_cfg), + "GIT_CONFIG_NOSYSTEM": "1", + "STUB_DIR": str(self.stubs), + "KAMINO_PYTHON": str(self.stubs / "python"), + } + + def git(self, repo: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess: + return subprocess.run(["git", "-C", str(repo), *args], env=self.env, check=check, + capture_output=True) + + def make_repo(self, name: str = "repo") -> Path: + repo = self.tmp / name + (repo / "scripts").mkdir(parents=True) + shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh") + (repo / "requirements-dev.txt").write_text("") + self.git(repo, "init", "--quiet") + return repo + + def run_setup(self, repo: Path, cwd: Path | None = None, **extra: str) -> subprocess.CompletedProcess: + env = {**self.env, **extra} + return subprocess.run(["bash", str(repo / "scripts" / "dev-setup.sh")], env=env, + cwd=str(cwd or repo), capture_output=True, text=True, check=False) + + def hooks_z(self, repo: Path) -> bytes: + return self.git(repo, "config", "--local", "-z", "--get-all", "core.hooksPath", check=False).stdout + + def add_hooks_path(self, repo: Path, *values: str) -> None: + for value in values: + self.git(repo, "config", "--local", "--add", "core.hooksPath", value) + + def hook(self, repo: Path, name: str = "pre-commit") -> Path: + return repo / ".git" / "hooks" / name + + def test_normal_run(self) -> None: + repo = self.make_repo() + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + for name in ("pre-commit", "commit-msg"): + text = self.hook(repo, name).read_text() + self.assertIn("# kamino-dev-setup: virtualenv first on PATH", text) + self.assertEqual(self.hooks_z(repo), b"") + + def test_rerun_is_idempotent(self) -> None: + repo = self.make_repo() + self.assertEqual(self.run_setup(repo).returncode, 0) + first = self.hook(repo).read_text() + second_run = self.run_setup(repo) + self.assertEqual(second_run.returncode, 0, second_run.stderr) + self.assertEqual(self.hook(repo).read_text(), first) + self.assertEqual(first.count("kamino-dev-setup: virtualenv first"), 1) + + def test_own_absolute_hooks_path_is_lifted_and_restored(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, str(repo / ".git" / "hooks")) + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + self.assertTrue(self.hook(repo).exists()) + + def test_own_relative_hooks_path_is_lifted_and_restored(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, ".git/hooks") + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_foreign_hooks_path_fails_and_is_untouched(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, str(self.tmp / "elsewhere")) + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertNotEqual(result.returncode, 0) + self.assertIn("core.hooksPath is set to", result.stderr) + self.assertEqual(self.hooks_z(repo), before) + self.assertFalse(self.hook(repo).exists()) + + def test_global_hooks_path_fails_and_local_value_is_restored(self) -> None: + repo = self.make_repo() + self.global_cfg.write_text("[core]\n\thooksPath = /somewhere/global\n") + self.add_hooks_path(repo, ".git/hooks") + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertNotEqual(result.returncode, 0) + self.assertIn("global", result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_multi_valued_setting_is_restored(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, ".git/hooks", str(repo / ".git" / "hooks"), "./.git/hooks/") + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_value_with_newline_is_restored_exactly(self) -> None: + repo = self.make_repo() + # The newline sits in a component that ".." removes, so the value resolves to the + # repository's own hooks directory. + self.add_hooks_path(repo, ".git/hooks\n/../hooks") + before = self.hooks_z(repo) + self.assertIn(b"\n", before) + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_newline_value_is_restored_after_failure(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, ".git/hooks\n/../hooks", "") + before = self.hooks_z(repo) + result = self.run_setup(repo, STUB_PRECOMMIT_FAIL="1") + self.assertNotEqual(result.returncode, 0) + self.assertEqual(self.hooks_z(repo), before) + + def test_empty_value_is_lifted_and_restored(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, "") + before = self.hooks_z(repo) + self.assertEqual(before, b"\0") + # The stub refuses on ANY value, so success proves the empty value was lifted. + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_trailing_empty_value_is_lifted_and_restored(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, ".git/hooks", "") + before = self.hooks_z(repo) + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(self.hooks_z(repo), before) + + def test_setting_is_restored_when_pre_commit_fails(self) -> None: + repo = self.make_repo() + self.add_hooks_path(repo, ".git/hooks", str(repo / ".git" / "hooks")) + before = self.hooks_z(repo) + result = self.run_setup(repo, STUB_PRECOMMIT_FAIL="1") + self.assertNotEqual(result.returncode, 0) + self.assertEqual(self.hooks_z(repo), before) + + def test_run_from_another_repository_leaves_it_untouched(self) -> None: + repo = self.make_repo("target") + other = self.tmp / "other" + other.mkdir() + self.git(other, "init", "--quiet") + result = self.run_setup(repo, cwd=other) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse((other / ".venv").exists()) + self.assertEqual(os.listdir(other / ".git" / "hooks"), + [n for n in os.listdir(other / ".git" / "hooks") if n.endswith(".sample")]) + self.assertTrue((repo / ".venv").is_dir()) + self.assertTrue(self.hook(repo).exists()) + + def test_crafted_repository_path_is_not_executed(self) -> None: + repo = self.make_repo('a b $(touch PWNED) "q" `touch PWNED2` $HOME') + result = self.run_setup(repo) + self.assertEqual(result.returncode, 0, result.stderr) + workdir = self.tmp / "hookcwd" + workdir.mkdir() + hook = subprocess.run([str(self.hook(repo))], env={**self.env}, cwd=str(workdir), + capture_output=True, text=True, check=False) + self.assertEqual(hook.returncode, 0, hook.stderr) + lines = hook.stdout.splitlines() + self.assertEqual(lines[0], "hook-ran") + self.assertTrue(lines[1].startswith(f"{repo}/.venv/bin:"), lines[1]) + for place in (workdir, repo, self.tmp, Path.cwd()): + self.assertFalse((place / "PWNED").exists(), place) + self.assertFalse((place / "PWNED2").exists(), place) + + +if __name__ == "__main__": + unittest.main()