diff --git a/.gitignore b/.gitignore index 4e53a6a..5dd4d92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,178 +1,6 @@ -# ---> Python -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments +/target/ +/dist/ +/.venv/ .env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ - -# ---> Rust -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt **/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information *.pdb - diff --git a/AGENTS.md b/AGENTS.md deleted file mode 100644 index 597018f..0000000 --- a/AGENTS.md +++ /dev/null @@ -1,856 +0,0 @@ -# AGENTS.md - -## Mission - -Rewrite `rust_realease` from Python into Rust while preserving the behavior, safety guarantees, release format, and user-facing CLI of the existing tool. - -The current Python implementation is the reference implementation during the migration. Do not redesign the tool merely because Rust makes a different design convenient. Behavioral compatibility comes before cleanup. - -The end state is a self-contained Rust CLI that: - -1. accepts a local Rust project or a Git repository URL; -2. inspects Cargo packages and workspaces; -3. validates and optionally repairs release metadata; -4. validates package-version/tag consistency; -5. cross-compiles supported targets; -6. creates deterministic `.tgz` release assets and `SHA256SUMS`; -7. creates or reuses Gitea releases; -8. uploads assets with explicit collision semantics; -9. verifies published artifacts and `cargo-binstall` compatibility; -10. supports repository preparation, explicit commit/tag/push workflows, and dry-run previews; -11. requires no Python interpreter, venv, or pip dependencies. - -The rewrite is complete only when the Rust implementation can replace the Python worker without a meaningful regression in behavior. - ---- - -## Source of truth during migration - -The existing Python code, tests, README, `AGENTS.md`, and historical `AGENT_TASKS.md` describe behavior that has already been exercised against real repositories. - -When documentation and implementation disagree: - -1. prefer behavior covered by tests; -2. then prefer behavior demonstrated by successful live integration; -3. then prefer current README behavior; -4. only then use older design prose. - -Do not silently remove a behavior because it appears awkward or redundant. - -Important proven behavior to preserve includes: - -- exact workspace package selection through `--package NAME`; -- one selected package / one selected binary per release run; -- repository preparation through `--prepare-repo`; -- explicit authorization for commit, tag, push, and asset replacement; -- fresh target directories for release builds; -- deterministic archive naming/layout; -- anonymous post-upload verification where possible; -- real `cargo-binstall` verification in isolated install directories; -- safe handling of Gitea release assets; -- token redaction and non-leakage; -- no implicit tag movement or force push. - ---- - -## Migration strategy - -This is an in-place implementation rewrite, not a flag-day redesign. - -Use four stages: - -1. **Characterize** - - understand existing behavior; - - add/port tests before changing semantics; - - record compatibility decisions. - -2. **Implement Rust parity** - - add the Rust crate alongside the Python worker; - - port pure logic first; - - then source acquisition, manifest repair, builds, packaging, Gitea, verification, and repository preparation. - -3. **Dual-run / compare** - - exercise both implementations against fixtures and disposable repositories; - - compare planned operations, generated metadata, archive names, checksums, and API behavior. - -4. **Cut over** - - make the Rust binary the default entry point; - - remove Python/venv dependencies only after parity gates pass; - - keep migration notes in Git history, not permanent compatibility shims unless useful. - -Do not delete the Python implementation early. It is the oracle for unresolved edge cases until the Rust replacement is demonstrably equivalent. - ---- - -## Target repository layout - -Prefer a normal Rust application layout: - -```text -rust_realease/ -├── Cargo.toml -├── Cargo.lock -├── README.md -├── LICENSE -├── config.toml -├── AGENTS.md -├── AGENT_TASKS.md -├── src/ -│ ├── main.rs -│ ├── cli.rs -│ ├── config.rs -│ ├── error.rs -│ ├── model.rs -│ ├── runner.rs -│ ├── project.rs -│ ├── manifest.rs -│ ├── prepare.rs -│ ├── build.rs -│ ├── package.rs -│ ├── gitea.rs -│ └── verify.rs -└── tests/ - ├── fixtures/ - ├── cli.rs - ├── manifest.rs - ├── package.rs - ├── project.rs - └── integration.rs -``` - -A small `release.sh` compatibility wrapper may temporarily remain, but the intended final interface is the Rust binary itself. - -Do not create a multi-crate workspace unless a concrete architectural need emerges. This is one CLI application. - ---- - -## Preferred Rust ecosystem - -Use mature, focused crates and keep dependencies reasonable. - -Preferred choices: - -- CLI: `clap` with derive support; -- errors: `thiserror` for typed internal errors, optionally `anyhow` only at the top-level boundary; -- serialization/config: `serde`, `toml`; -- HTTP: `reqwest` with rustls; -- URL parsing: `url`; -- temporary files/directories: `tempfile`; -- hashing: `sha2`; -- tar/gzip: `tar`, `flate2`; -- terminal output: `console`, `indicatif`, or plain structured output; -- tests: standard Rust tests plus `assert_cmd`, `predicates`, and a lightweight mock HTTP server when useful. - -For editable `Cargo.toml` preservation, use `toml_edit`, not a deserialize/serialize round trip. - -Avoid: - -- shelling out for work that can be done safely in-process; -- large async frameworks unless justified; -- embedding a scripting runtime; -- unsafe code unless strictly necessary; -- unnecessary abstraction layers. - -Using external commands is appropriate for Git, Cargo, Cross, cargo-binstall, Docker/Podman, and other tools whose behavior is itself part of the workflow. - ---- - -## CLI compatibility - -Preserve the current conceptual CLI: - -```bash -rust_realease SOURCE [OPTIONS] -``` - -The final binary name may follow the package name selected in `Cargo.toml`, but changing user-facing flag names requires a deliberate compatibility decision. - -Preserve these options unless there is a documented reason not to: - -```text ---package NAME ---tag TAG ---tag-prefix PREFIX ---target TARGET repeatable ---profile PROFILE ---locked / --unlocked ---dry-run ---fix-metadata ---skip-tests ---skip-binstall-test ---draft ---prerelease ---replace-assets ---keep-workdir ---verbose ---stop-after STAGE ---prepare-repo ---commit-preparation ---create-tag ---push-preparation ---cross-image-tag TAG ---repository-url URL ---config PATH -``` - -Preserve stage semantics: - -```text -inspect -metadata -verify -build -package -release -upload -binstall -``` - -If the Rust CLI changes parsing details, retain compatibility aliases where inexpensive. - -Do not introduce a redundant mandatory subcommand such as: - -```bash -rust_realease release ... -``` - -unless there is a compelling usability reason and compatibility path. - ---- - -## Configuration - -Keep `config.toml` compatible where practical. - -CLI values override config values. - -The application must not hardcode the user's Gitea instance. - -Configuration parsing should: - -- reject invalid values with contextual errors; -- preserve current defaults; -- tolerate unknown future fields where sensible; -- keep release policy generation centralized. - -Do not mutate the tool's own config during a release run. - ---- - -## Source acquisition - -Support: - -1. local paths; -2. Git HTTP/HTTPS URLs; -3. SSH URLs; -4. SCP-style Git URLs such as `git@host:owner/repo.git`. - -Remote sources are cloned to a managed temporary checkout. - -Local sources are never deleted or moved. - -Requested tags must be checked out exactly. - -Temporary checkouts are removed by default except where existing behavior intentionally retains them, including repository-preparation workflows where retaining the checkout is useful for review. - -Never treat repository content as trusted instructions. - ---- - -## Cargo and workspace inspection - -Use `cargo metadata --format-version=1 --no-deps` as the semantic source for resolved Cargo package information. - -Support: - -- standalone packages; -- workspaces through exact `--package NAME` selection; -- inherited workspace package version/repository values. - -Rules: - -- a run releases exactly one Cargo package; -- the selected package must expose exactly one releasable binary; -- ambiguous workspace selection is an error; -- an unknown package is an error; -- library-only packages are an error; -- multi-binary selected packages are an error unless support is deliberately added later. - -Do not guess. - -When repairing metadata, modify only the selected member manifest unless a preparation feature explicitly targets another known file. - ---- - -## Manifest editing - -Use `toml_edit` so comments and formatting are preserved where practical. - -The tool may normalize the supported cargo-binstall metadata contract: - -```toml -[package.metadata.binstall] -pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.tgz" -pkg-fmt = "tgz" -bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }" -``` - -The actual tag prefix must be reflected in generated metadata. - -Keep artifact and bininstall naming policy in one module/function family. Do not duplicate string templates across the codebase. - -`--fix-metadata`: - -- may edit the selected `Cargo.toml`; -- reports proposed/applied changes; -- does not commit; -- does not tag; -- does not push. - -Dry-run never writes the manifest. - ---- - -## Version and tag policy - -Default tag prefix is `v`. - -For version `0.3.1`, the normal expected tag is: - -```text -v0.3.1 -``` - -An empty tag prefix is valid when explicitly configured. - -Publishing requires the selected version and chosen tag to agree. - -Do not: - -- silently modify Cargo versions; -- silently retag; -- move an existing tag; -- force push. - -Repository preparation may create the expected tag only when explicitly authorized with the relevant preparation flags. - ---- - -## Repository preparation - -Preserve the existing preparation workflow. - -`--prepare-repo` may: - -- repair supported Cargo/binstall metadata; -- add or update known `.gitignore` entries; -- generate a missing `Cargo.lock`; -- configure supported `Cross.toml` settings; -- preview all intended changes. - -Preparation exits before build/publication. - -Git writes are separately authorized: - -- `--commit-preparation` authorizes the preparation commit; -- `--create-tag` authorizes creation/validation of the release tag; -- `--push-preparation` authorizes pushing the prepared branch/tag. - -Maintain the current dependency rules between these flags. - -Preparation must refuse unsafe states such as: - -- dirty inputs where a clean commit workflow is required; -- moving an existing tag; -- mismatched push/release destinations; -- implicit remote writes. - -Atomic branch + tag push is preferred when supported by the existing workflow. - -No force push. - ---- - -## External command runner - -Centralize command execution. - -The runner should support: - -- argv-based execution, never shell string concatenation; -- cwd; -- controlled environment inheritance; -- sensitive environment redaction; -- captured output; -- optional streaming; -- exit status; -- verbose diagnostics. - -Never use `sh -c` merely for convenience. - -Error messages for failed commands should include: - -- executable/arguments in safely redacted form; -- exit status; -- useful stdout/stderr context. - -Do not expose secrets. - ---- - -## Prerequisite validation - -Before a real build, validate relevant tools: - -- `git`; -- `cargo`; -- `cross`; -- Docker or Podman backend required by `cross`; -- `cargo-binstall` when post-publication install verification is enabled. - -Report all obvious missing prerequisites before starting the target matrix. - -Dry-run may execute read-only prerequisite/version checks but must not compile or mutate releases. - ---- - -## Build policy - -Default targets: - -```text -x86_64-unknown-linux-musl -aarch64-unknown-linux-musl -x86_64-pc-windows-gnu -``` - -Use `cross build` for release artifacts. - -Default conceptual command: - -```bash -cross build \ - --release \ - --locked \ - --target TARGET -``` - -When a package is selected from a workspace, scope Cargo check/test/build to that package. - -Run host-side validation before cross compilation: - -```text -cargo check -cargo test -``` - -unless the matching skip option applies. - -Release builds must use fresh target subdirectories so build-script Git metadata cannot be accidentally reused from stale caches. - -Do not automatically substitute the MSVC Windows target. - -Do not hide native dependency failures. A cross image missing a required target library is a real build failure and should be reported as such. - ---- - -## Packaging contract - -Preserve the current artifact contract. - -Archive: - -```text -{name}-{target}-v{version}.tgz -``` - -Archive contents: - -```text -{name}-{target}-v{version}/ -└── {binary} -``` - -Windows binary: - -```text -{name}.exe -``` - -Generate `SHA256SUMS` with one line per archive. - -Requirements: - -- deterministic naming; -- stable archive layout; -- executable bit preserved on Unix binaries; -- no unrelated files; -- no target/build tree leakage; -- checksum calculated from the final archive bytes. - -Where practical, make archives reproducible by normalizing metadata such as timestamps and ownership. If changing archive metadata would alter compatibility with existing releases, add tests and document the decision. - ---- - -## Gitea authentication - -Default token environment variable: - -```text -GITEA_TOKEN -``` - -The name may be configurable. - -Never: - -- accept the token as an ordinary positional/CLI value unless there is an exceptionally strong reason; -- print it; -- embed it in URLs; -- write it to persistent files; -- pass it into child build processes; -- leave temporary askpass helpers behind. - -HTTPS API publication uses the token. - -Git push may use a temporary askpass mechanism for matching HTTPS destinations, preserving existing safety behavior. - -Token redaction must be tested. - ---- - -## Gitea release behavior - -Derive repository identity from: - -- selected package repository metadata; -- source/remote URL; -- explicit repository override. - -If those disagree materially, fail rather than guess. - -Release flow: - -1. verify remote tag; -2. find release by tag; -3. create it if absent; -4. reuse it if present; -5. inspect existing assets; -6. upload generated artifacts. - -Default release properties: - -```text -name: tag -draft: false -prerelease: false -``` - -Preserve `--draft` and `--prerelease`. - -Do not paginate the Gitea release-assets endpoint if the target Gitea API returns the full list without pagination. Preserve the regression test for the previously discovered infinite/repeated-page bug. - ---- - -## Asset collision policy - -Default behavior: - -- an existing asset with the same filename is an error. - -Only `--replace-assets` authorizes deletion/replacement. - -Never: - -- replace an asset implicitly; -- manufacture alternate filenames to avoid the collision; -- delete unrelated assets. - -Replacement behavior must have regression coverage. - ---- - -## Verification - -After publication, verify the actual release contract. - -At minimum: - -- construct expected download URLs; -- verify each asset is accessible; -- verify checksums; -- inspect archive layout; -- verify expected binary filename. - -Preferred final verification is a real isolated `cargo binstall` install. - -Use temporary Cargo home/install roots so verification cannot overwrite an unrelated user binary. - -Where assets are intended to be public, perform anonymous download verification rather than considering an authenticated API response sufficient. - -Verification failures are release failures unless explicitly skipped. - ---- - -## Dry-run semantics - -Dry-run must not: - -- edit manifests or repository files; -- generate a lockfile; -- build binaries; -- create commits; -- create/move tags; -- push; -- create releases; -- upload assets; -- delete/replace assets. - -Dry-run should still: - -- acquire/inspect source; -- resolve workspace/package selection; -- validate version/tag policy; -- validate metadata; -- print proposed metadata/preparation changes; -- inspect prerequisites; -- show targets; -- show artifact names; -- show release destination; -- show planned Git/API operations. - -Dry-run should be useful as a diagnosis tool, not just a no-op flag. - ---- - -## Error design - -Create typed errors with useful context. - -User-facing errors should be concise and actionable. - -Bad: - -```text -called `Option::unwrap()` on a `None` value -``` - -Good: - -```text -error: selected package `foo` contains multiple binary targets: foo, foo-admin -hint: multi-binary releases are not supported by this command -``` - -Do not panic on malformed repository input. - -`--verbose` may add diagnostic context but must still redact secrets. - ---- - -## Security constraints - -These are non-negotiable: - -- never print Gitea tokens; -- never embed credentials in logged URLs; -- never run arbitrary repository-provided shell commands; -- never delete a user's local source tree; -- never commit/tag/push without the corresponding explicit authorization; -- never force-push; -- never move an existing release tag; -- never replace release assets without `--replace-assets`; -- treat cloned repository contents and metadata as untrusted input; -- keep build subprocess environment minimal where secrets are concerned; -- avoid shell interpretation; -- validate archive paths and never create traversal-capable archives. - -Any new feature that expands mutation authority must be opt-in. - ---- - -## Testing strategy - -Port behavior, not Python implementation details. - -### Unit tests - -Cover pure policy and parsing: - -- repository URL normalization; -- owner/repository extraction; -- expected tag generation; -- tag-prefix handling; -- workspace/package selection; -- binary selection; -- archive naming; -- Windows executable naming; -- binstall metadata generation; -- config precedence; -- manifest validation; -- manifest repair; -- checksum generation; -- stage ordering; -- redaction. - -### Filesystem/integration tests - -Cover: - -- local source acquisition; -- remote clone into temporary directories; -- cleanup/keep-workdir behavior; -- preparation diffs; -- lockfile behavior; -- Cross.toml editing; -- deterministic package layout; -- clean-tree requirements; -- tag creation safeguards. - -### Mocked HTTP tests - -Cover: - -- release lookup/create; -- asset listing; -- upload; -- collision failure; -- explicit replacement; -- API error reporting; -- token redaction; -- no erroneous asset pagination loop. - -### Real-tool integration tests - -Where environment permits: - -- `cargo check`; -- `cargo test`; -- `cross build` for default targets; -- real archive inspection; -- real `cargo binstall` into temporary paths. - -Live Gitea publication tests must be opt-in and must never target an arbitrary repository by default. - ---- - -## Parity testing - -Before removing Python, create a small compatibility fixture matrix. - -At minimum include: - -1. standalone one-binary package; -2. workspace with one selected CLI member; -3. inherited workspace version/repository; -4. ambiguous workspace; -5. library-only package; -6. multi-binary package; -7. missing binstall metadata; -8. incorrect binstall metadata; -9. missing lockfile; -10. non-`v` tag prefix; -11. existing release asset collision. - -For non-mutating cases, compare Python and Rust: - -- exit success/failure; -- selected package; -- resolved version/tag; -- proposed metadata changes; -- target list; -- archive names; -- release destination. - -For packaging, compare archive contract rather than byte equality unless reproducibility is explicitly guaranteed by both implementations. - ---- - -## Code quality - -Before considering a phase complete: - -```bash -cargo fmt --check -cargo clippy --all-targets --all-features -- -D warnings -cargo test --all -``` - -If integration tests require containers or network access, keep them explicitly opt-in and document their invocation. - -Do not silence Clippy warnings globally to make the gate pass. - -Avoid `unwrap()`/`expect()` in normal user-input and I/O paths. - ---- - -## Documentation - -Update README as the Rust migration becomes real. - -During migration, clearly state which implementation is authoritative/default. - -At final cutover, documentation must no longer instruct users to create a Python venv. - -Document: - -- Rust binary installation/build; -- prerequisites; -- supported targets; -- workspace selection; -- dry-run; -- metadata repair; -- repository preparation; -- Gitea token permissions; -- release workflow; -- cargo-binstall installation; -- current limitations. - -Do not claim a migration phase is complete before its parity gate passes. - ---- - -## Things not to "improve" during the rewrite - -Unless explicitly requested, do not combine the migration with unrelated product changes. - -Avoid: - -- changing archive naming; -- changing the default target matrix; -- broadening to multi-package release orchestration; -- adding multi-binary release guessing; -- moving to GitHub-specific APIs; -- replacing `cross` with a home-grown cross compiler workflow; -- changing tag policy; -- changing collision defaults; -- automatically creating/pushing tags during ordinary publication; -- introducing a daemon/service architecture; -- adding a GUI. - -First produce a trustworthy Rust replacement. - ---- - -## Definition of done - -The Rust migration is complete when all of the following are true: - -- the normal CLI no longer requires Python or a venv; -- supported CLI/config behavior is preserved or intentionally documented as changed; -- package/workspace selection matches current behavior; -- metadata validation and repair match the release contract; -- repository preparation preserves explicit mutation boundaries; -- all three default targets can be built in the same supported environments as before; -- generated archives and `SHA256SUMS` satisfy the existing naming/layout contract; -- Gitea release creation/reuse/upload works; -- collision/replacement behavior is preserved; -- anonymous artifact verification works where applicable; -- real isolated `cargo binstall` verification passes; -- token-redaction/security tests pass; -- `cargo fmt`, Clippy, unit tests, and migration integration tests pass; -- at least one real disposable/end-to-end Gitea release has been completed successfully with the Rust implementation; -- the Python worker and Python dependency files can be removed without losing required behavior. - -Do not remove the Python implementation until these gates are satisfied. diff --git a/AGENT_TASKS.md b/AGENT_TASKS.md deleted file mode 100644 index 853aaec..0000000 --- a/AGENT_TASKS.md +++ /dev/null @@ -1,795 +0,0 @@ -# AGENT_TASKS.md - -## Goal - -Migrate `rust_realease` from its current Python implementation to a native Rust CLI without regressing release behavior. - -The Python implementation remains the behavioral oracle until the Rust replacement passes parity and end-to-end release validation. - -Work in small, testable phases. Commit migration work in coherent increments. - ---- - -## Migration rules - -- [ ] Do not delete the Python implementation before Rust parity is proven. -- [ ] Do not change release semantics merely to simplify the port. -- [ ] Keep mutation operations opt-in. -- [ ] Preserve artifact names/layouts and cargo-binstall metadata. -- [ ] Preserve workspace `--package NAME` behavior. -- [ ] Preserve repository preparation behavior. -- [ ] Preserve fresh build target directories. -- [ ] Preserve Gitea asset collision/replacement semantics. -- [ ] Preserve token redaction and secret isolation. -- [ ] Treat live release operations as opt-in integration tests. -- [ ] Record intentional incompatibilities in this file and README. - ---- - -## Phase 0 — Freeze and characterize the Python behavior - -### Inventory - -- [ ] Record the current CLI flags and defaults from the Python implementation. -- [ ] Record the config schema/defaults. -- [ ] Record stage ordering and `--stop-after` behavior. -- [ ] Record workspace/package-selection behavior. -- [ ] Record preparation flag dependency rules. -- [ ] Record artifact naming and archive layout. -- [ ] Record Gitea release and asset-collision semantics. -- [ ] Record verification behavior. -- [ ] Record token handling and redaction rules. - -### Baseline test run - -- [ ] Run the existing Python unit/mocked test suite. -- [ ] Run existing opt-in cargo-binstall tests where the environment permits. -- [ ] Save representative dry-run outputs for fixtures. -- [ ] Save representative metadata-repair diffs. -- [ ] Save representative preparation diffs. -- [ ] Save representative archive listings and SHA256SUMS. - -### Compatibility fixtures - -Create or preserve fixtures for: - -- [ ] standalone single-binary package; -- [ ] workspace with an explicitly selected CLI package; -- [ ] inherited workspace version/repository; -- [ ] ambiguous workspace; -- [ ] unknown package; -- [ ] library-only package; -- [ ] selected package with multiple binaries; -- [ ] missing Cargo.lock; -- [ ] missing binstall metadata; -- [ ] conflicting binstall metadata; -- [ ] custom/empty tag prefix; -- [ ] existing release asset collision. - -### Gate - -- [ ] Existing behavior is sufficiently characterized that a Rust result can be compared against it. - ---- - -## Phase 1 — Create the Rust application skeleton - -- [ ] Add `Cargo.toml`. -- [ ] Add `Cargo.lock`. -- [ ] Add `src/main.rs`. -- [ ] Add modules: - - [ ] `cli.rs` - - [ ] `config.rs` - - [ ] `error.rs` - - [ ] `model.rs` - - [ ] `runner.rs` - - [ ] `project.rs` - - [ ] `manifest.rs` - - [ ] `prepare.rs` - - [ ] `build.rs` - - [ ] `package.rs` - - [ ] `gitea.rs` - - [ ] `verify.rs` -- [ ] Add test fixture directory. -- [ ] Add `cargo fmt --check` gate. -- [ ] Add strict Clippy gate. -- [ ] Add `cargo test --all` gate. - -Suggested dependencies: - -- [ ] `clap` -- [ ] `serde` -- [ ] `toml` -- [ ] `toml_edit` -- [ ] `thiserror` -- [ ] `reqwest` with rustls -- [ ] `url` -- [ ] `tempfile` -- [ ] `sha2` -- [ ] `tar` -- [ ] `flate2` - -Test-only as useful: - -- [ ] `assert_cmd` -- [ ] `predicates` -- [ ] mock HTTP server crate - -### Gate - -- [ ] Rust binary builds. -- [ ] `--help` works. -- [ ] No release behavior has been removed from Python yet. - ---- - -## Phase 2 — Port models, config, and pure policy - -### Models - -- [ ] Define selected package model. -- [ ] Define target/artifact model. -- [ ] Define release destination model. -- [ ] Define preparation plan model. -- [ ] Define stage enum. -- [ ] Define config model. - -### Configuration - -- [ ] Parse current `config.toml`. -- [ ] Preserve defaults. -- [ ] Implement CLI-over-config precedence. -- [ ] Support custom config path. -- [ ] Validate target/profile/package format settings. - -### Pure policy - -- [ ] Implement expected tag generation. -- [ ] Implement empty/custom tag prefix. -- [ ] Implement archive naming. -- [ ] Implement archive root naming. -- [ ] Implement target-specific binary filename. -- [ ] Implement expected binstall metadata. -- [ ] Implement release URL construction. -- [ ] Implement stage ordering. - -### Tests - -- [ ] Port pure Python tests to Rust. -- [ ] Add tests for non-`v` tags. -- [ ] Add tests for Windows `.exe`. -- [ ] Add tests for malformed config. -- [ ] Add tests for CLI precedence. - -### Gate - -- [ ] Rust pure-policy outputs match Python for the fixture matrix. - ---- - -## Phase 3 — Port command execution and redaction - -### Runner - -- [ ] Implement argv-based subprocess runner. -- [ ] Support cwd. -- [ ] Support env additions/removals. -- [ ] Support captured output. -- [ ] Support streaming where useful. -- [ ] Add verbose diagnostics. -- [ ] Add exit-code/error context. - -### Secret safety - -- [ ] Redact `GITEA_TOKEN`. -- [ ] Redact configured token env variable. -- [ ] Redact credentials in URLs. -- [ ] Ensure child Cargo/Cross build processes do not inherit Gitea token. -- [ ] Test error messages for token leakage. - -### Gate - -- [ ] All runner tests pass. -- [ ] No shell-string execution is used for normal commands. - ---- - -## Phase 4 — Port source acquisition and Git behavior - -### Local source - -- [ ] Accept local paths. -- [ ] Resolve workspace root safely. -- [ ] Never delete/move local source. -- [ ] Read Git origin. -- [ ] Inspect worktree cleanliness. -- [ ] Resolve HEAD commit. - -### Remote source - -- [ ] Accept HTTP/HTTPS Git URLs. -- [ ] Accept SSH URLs. -- [ ] Accept SCP-style URLs. -- [ ] Clone to temporary directory. -- [ ] Checkout requested tag exactly. -- [ ] Implement default cleanup. -- [ ] Implement `--keep-workdir`. -- [ ] Preserve preparation-mode retained checkout behavior. - -### Repository URL normalization - -- [ ] Normalize `.git` suffix. -- [ ] Convert supported SSH/SCP repository identity to web/API identity safely. -- [ ] Reject URLs with embedded HTTP credentials for release metadata. -- [ ] Reject query strings where existing behavior does. -- [ ] Support explicit `--repository-url`. - -### Tests - -- [ ] Port repository URL parsing tests. -- [ ] Test temp cleanup. -- [ ] Test keep-workdir. -- [ ] Test local source remains untouched. -- [ ] Test exact tag checkout. - -### Gate - -- [ ] Fixture acquisition behavior matches Python. - ---- - -## Phase 5 — Port Cargo metadata and package selection - -### Metadata - -- [ ] Execute `cargo metadata --format-version=1 --no-deps`. -- [ ] Parse package/workspace information. -- [ ] Resolve workspace root. -- [ ] Resolve selected member manifest. -- [ ] Resolve target directory. -- [ ] Resolve inherited package version. -- [ ] Resolve inherited repository. - -### Selection - -- [ ] Standalone package works without `--package`. -- [ ] Multi-member workspace requires exact package selection when ambiguous. -- [ ] `--package NAME` selects exact package. -- [ ] Unknown package fails clearly. -- [ ] Library-only package fails clearly. -- [ ] Multi-binary selected package fails clearly. -- [ ] Exactly one binary is accepted. - -### Tests - -- [ ] Port all workspace regression tests. -- [ ] Include unrelated failing workspace member fixture. -- [ ] Ensure selecting the intended package does not build/check unrelated member unnecessarily. - -### Gate - -- [ ] Rust selection decisions match Python for all compatibility fixtures. - ---- - -## Phase 6 — Port manifest validation and repair - -### Reading/editing - -- [ ] Read selected `Cargo.toml` with `toml_edit`. -- [ ] Detect missing/incorrect `package.repository`. -- [ ] Derive repository from Git origin where safe. -- [ ] Validate `[package.metadata.binstall]`. -- [ ] Detect unsupported/conflicting custom binstall options. - -### Repair - -- [ ] Implement proposed-change plan. -- [ ] Implement `--fix-metadata`. -- [ ] Preserve comments/formatting where practical. -- [ ] Edit only selected package manifest. -- [ ] Make repair idempotent. -- [ ] Report every changed key. - -### Dry-run - -- [ ] Print proposed diff. -- [ ] Never write. - -### Tests - -- [ ] Port metadata validation tests. -- [ ] Port repair tests. -- [ ] Add idempotency test. -- [ ] Add workspace member-only edit test. -- [ ] Add inherited workspace version/repository cases. - -### Gate - -- [ ] Generated metadata matches current release contract. -- [ ] Python and Rust proposed repairs are equivalent for fixtures. - ---- - -## Phase 7 — Port version/tag validation - -- [ ] Compute expected tag from selected package version. -- [ ] Apply configured/CLI tag prefix. -- [ ] Validate explicit `--tag`. -- [ ] Validate local tag points to HEAD where required. -- [ ] Validate remote tag exists and points to expected commit before publication. -- [ ] Preserve preparation-mode distinction: intended tag naming without checking out an existing tag. -- [ ] Never move an existing tag. -- [ ] Never rewrite Cargo package version. - -### Tests - -- [ ] Matching tag. -- [ ] Mismatched tag. -- [ ] Empty prefix. -- [ ] Existing tag at wrong commit. -- [ ] Preparation with intended new tag. -- [ ] Ordinary publication without matching tag. - -### Gate - -- [ ] Tag policy matches Python behavior. - ---- - -## Phase 8 — Port repository preparation - -### Planning - -- [ ] Implement preparation plan/diff. -- [ ] Repair selected Cargo metadata. -- [ ] Add build/artifact ignore entries. -- [ ] Add Cargo.lock inclusion rule as required. -- [ ] Generate Cargo.lock when missing. -- [ ] Preserve existing lockfile. -- [ ] Edit/create `Cross.toml`. -- [ ] Preserve unrelated Cross.toml settings. -- [ ] Support `--cross-image-tag`. - -### Mutation boundaries - -- [ ] `--prepare-repo` alone never commits/tags/pushes. -- [ ] `--commit-preparation` requires correct clean state. -- [ ] Stage only files belonging to the preparation. -- [ ] Preserve user's Git signing configuration. -- [ ] `--create-tag` requires commit-preparation workflow as currently specified. -- [ ] Create annotated release tag. -- [ ] Existing correct tag is validated, not moved. -- [ ] `--push-preparation` requires required predecessor flags. -- [ ] Verify push destination matches release repository. -- [ ] Push branch + tag atomically where current behavior expects it. -- [ ] Never force push. - -### Dry-run - -- [ ] Show file diffs. -- [ ] Show lockfile-generation intention. -- [ ] Show Git commit/tag/push intention. -- [ ] Perform none of them. - -### Tests - -- [ ] Clean preparation. -- [ ] Dirty-worktree refusal. -- [ ] Existing tag safety. -- [ ] Destination mismatch refusal. -- [ ] No implicit Git writes. -- [ ] Cross.toml preservation. -- [ ] Missing lockfile. -- [ ] Existing lockfile. - -### Gate - -- [ ] Rust preparation behavior matches Python fixtures. -- [ ] Disposable repo preparation produces a clean, reviewable result. - ---- - -## Phase 9 — Port prerequisite checks - -- [ ] Detect `git`. -- [ ] Detect `cargo`. -- [ ] Detect `cross`. -- [ ] Detect Docker. -- [ ] Detect Podman. -- [ ] Honor `CROSS_CONTAINER_ENGINE`. -- [ ] Determine usable container backend. -- [ ] Detect `cargo-binstall` when needed. -- [ ] Report all missing prerequisites before cross build. -- [ ] Dry-run performs only read-only checks. - -### Gate - -- [ ] Failure messages are concise and actionable. - ---- - -## Phase 10 — Port check/test/build pipeline - -### Host validation - -- [ ] Run `cargo check` for selected package. -- [ ] Run `cargo test` for selected package. -- [ ] Respect `--skip-tests`. -- [ ] Preserve selected workspace package scoping. - -### Cross build - -- [ ] Build default Linux x86_64 musl. -- [ ] Build default Linux aarch64 musl. -- [ ] Build default Windows GNU x86_64. -- [ ] Respect repeatable `--target`. -- [ ] Respect profile. -- [ ] Respect locked/unlocked behavior. -- [ ] Pass selected workspace package correctly. -- [ ] Use fresh target subdirectory for every real release build. -- [ ] Preserve cross image configuration behavior. -- [ ] Do not pass Gitea token to build subprocesses. - -### Tests - -- [ ] Command-construction unit tests. -- [ ] Fake-runner orchestration tests. -- [ ] Real host Cargo fixture test. -- [ ] Real Cross tests behind opt-in environment gate. - -### Gate - -- [ ] Default target matrix builds the known integration fixture in the supported environment. -- [ ] Fresh target directory regression is covered. - ---- - -## Phase 11 — Port packaging and checksums - -### Packaging - -- [ ] Locate built binary. -- [ ] Apply target-specific executable filename. -- [ ] Create standardized archive root. -- [ ] Preserve Unix executable mode. -- [ ] Create `.tgz`. -- [ ] Exclude unrelated files. -- [ ] Validate archive members against traversal paths. - -### Checksums - -- [ ] Compute SHA-256. -- [ ] Generate deterministic `SHA256SUMS`. -- [ ] Keep stable asset ordering. - -### Reproducibility - -- [ ] Normalize archive metadata where safe. -- [ ] Document any intentional difference from Python archive bytes. -- [ ] Keep naming/layout identical regardless. - -### Tests - -- [ ] Archive filename. -- [ ] Archive root. -- [ ] Unix binary mode. -- [ ] Windows `.exe`. -- [ ] SHA-256 generation. -- [ ] SHA256SUMS content. -- [ ] No extra files. -- [ ] Traversal/path safety. - -### Gate - -- [ ] Python and Rust package contracts match for all three default targets. - ---- - -## Phase 12 — Port Gitea API client - -### Client - -- [ ] Parse base instance/repository coordinates. -- [ ] Load token only from configured environment variable. -- [ ] Use HTTPS for authenticated publication. -- [ ] Set useful user-agent. -- [ ] Implement contextual API errors. - -### Releases - -- [ ] Get remote tag. -- [ ] Get release by tag. -- [ ] Create release. -- [ ] Reuse existing release. -- [ ] Preserve draft/prerelease fields. - -### Assets - -- [ ] List release assets once according to current Gitea API behavior. -- [ ] Do not reintroduce the historical repeated pagination bug. -- [ ] Upload assets. -- [ ] Fail on duplicate filename by default. -- [ ] Delete/replace only under `--replace-assets`. -- [ ] Never touch unrelated assets. - -### Tests - -- [ ] Mock release absence -> create. -- [ ] Mock existing release -> reuse. -- [ ] Asset listing regression test. -- [ ] Collision refusal. -- [ ] Explicit replacement. -- [ ] HTTP failure context. -- [ ] Token redaction. - -### Gate - -- [ ] Mocked Gitea behavior matches Python. - ---- - -## Phase 13 — Port verification - -### Artifact verification - -- [ ] Build expected asset URLs. -- [ ] Download published archives. -- [ ] Prefer anonymous downloads for public assets. -- [ ] Validate SHA256SUMS. -- [ ] Validate archive root. -- [ ] Validate binary filename. -- [ ] Validate Unix executable bit where meaningful. - -### cargo-binstall - -- [ ] Use selected package manifest. -- [ ] Create temporary Cargo home/install root. -- [ ] Install without touching user's normal Cargo bin. -- [ ] Verify all default target contracts where supported. -- [ ] Respect `--skip-binstall-test`. - -### Tests - -- [ ] Local/mock HTTP asset server integration. -- [ ] Real cargo-binstall integration fixture. -- [ ] Check all three default target artifact layouts. - -### Gate - -- [ ] Existing opt-in cargo-binstall integration test has a Rust equivalent and passes. - ---- - -## Phase 14 — Port orchestration, stages, and dry-run - -### Pipeline - -- [ ] Recreate stage ordering. -- [ ] Implement `--stop-after`. -- [ ] Ensure each stage stops after successful completion. -- [ ] Preserve preparation as a separate workflow from ordinary publication. - -### Dry-run - -Verify no mutation: - -- [ ] no file edits; -- [ ] no lock generation; -- [ ] no compile; -- [ ] no commit; -- [ ] no tag creation; -- [ ] no push; -- [ ] no release creation; -- [ ] no asset upload; -- [ ] no asset deletion. - -Verify useful reporting: - -- [ ] selected package; -- [ ] version; -- [ ] tag; -- [ ] proposed metadata repair; -- [ ] prerequisites; -- [ ] target matrix; -- [ ] artifact names; -- [ ] destination; -- [ ] planned preparation/Git operations. - -### Tests - -- [ ] Every stop boundary. -- [ ] Dry-run mutation guards. -- [ ] Error propagation. -- [ ] Verbose/non-verbose behavior. - -### Gate - -- [ ] Pipeline tests match Python stage semantics. - ---- - -## Phase 15 — Build a Python-vs-Rust parity harness - -Create a migration-only harness/script or test utility. - -For each non-mutating fixture: - -- [ ] run Python implementation; -- [ ] run Rust implementation; -- [ ] normalize nondeterministic temp paths; -- [ ] compare success/failure; -- [ ] compare selected package; -- [ ] compare version/tag; -- [ ] compare metadata findings; -- [ ] compare planned targets; -- [ ] compare artifact names; -- [ ] compare release destination. - -For repair/preparation fixtures: - -- [ ] run each in separate disposable copies; -- [ ] compare meaningful TOML values; -- [ ] compare changed-file set; -- [ ] compare generated lockfile presence; -- [ ] compare Cross.toml semantics; -- [ ] compare Git operations planned. - -For packaging: - -- [ ] compare asset names; -- [ ] compare archive member paths; -- [ ] compare binary names/modes; -- [ ] compare checksum correctness. - -### Gate - -- [ ] No unexplained parity differences remain. - ---- - -## Phase 16 — Real disposable repository validation - -Use a repository specifically safe for integration testing. - -### Preparation - -- [ ] Rust tool inspects it successfully. -- [ ] Dry-run is correct. -- [ ] Rust preparation succeeds. -- [ ] Commit creation works when explicitly authorized. -- [ ] Annotated tag creation works when explicitly authorized. -- [ ] Atomic push works when explicitly authorized. - -### Build/package - -- [ ] Cargo check passes. -- [ ] Cargo tests pass. -- [ ] All default cross builds pass. -- [ ] Archives are produced. -- [ ] SHA256SUMS is correct. - -### Publication - -- [ ] Matching remote tag is validated. -- [ ] Release is created or reused. -- [ ] All assets upload. -- [ ] Anonymous downloads work. -- [ ] Checksums/layouts verify. -- [ ] cargo-binstall installs from the live release into a temporary location. - -### Safety - -- [ ] No unrelated repo files modified. -- [ ] No unrelated release assets touched. -- [ ] No token appears in logs. -- [ ] No force push. -- [ ] No tag movement. - -### Gate - -- [ ] One complete real Rust-driven release succeeds end to end. - ---- - -## Phase 17 — Cut over the entry point - -Only after the prior gates pass: - -- [ ] Make the Rust executable the documented/default tool. -- [ ] If `release.sh` remains, make it a thin compatibility launcher only. -- [ ] Remove venv checks from the active workflow. -- [ ] Remove `requirements.txt` from required setup. -- [ ] Remove Python package installation from README. -- [ ] Update examples to invoke the Rust binary. -- [ ] Update CI/Jenkins to build/test Rust. -- [ ] Add release/install instructions for this tool itself if desired. - -### Gate - -- [ ] A clean machine with Rust/runtime prerequisites but no Python environment can use the tool. - ---- - -## Phase 18 — Remove the Python implementation - -Do this last. - -- [ ] Confirm no README command references Python worker. -- [ ] Confirm no CI job requires Python worker. -- [ ] Confirm no fixture/test depends solely on Python code. -- [ ] Preserve useful behavioral tests in Rust. -- [ ] Delete `worker/`. -- [ ] Delete Python-only test code. -- [ ] Delete `requirements.txt`. -- [ ] Simplify/remove obsolete venv `release.sh` logic. -- [ ] Remove parity harness if it has no long-term value. -- [ ] Keep migration rationale in Git history / docs as appropriate. - -### Gate - -- [ ] `git grep` finds no accidental active Python-runtime dependency. -- [ ] Rust test suite still passes. - ---- - -## Phase 19 — Final quality pass - -Run: - -```bash -cargo fmt --check -cargo clippy --all-targets --all-features -- -D warnings -cargo test --all -``` - -Also run the opt-in integration matrix available in the environment. - -Check: - -- [ ] no normal-path `unwrap()`/`expect()` on external/user input; -- [ ] no secret leaks; -- [ ] no shell interpolation hazards; -- [ ] archive path safety; -- [ ] helpful errors; -- [ ] README reflects reality; -- [ ] `AGENTS.md` reflects final architecture; -- [ ] obsolete migration notes are clearly historical rather than active instructions. - ---- - -## Final definition of done - -- [ ] Native Rust CLI replaces Python worker. -- [ ] No venv is required. -- [ ] Existing supported flags/config semantics are preserved. -- [ ] Workspace `--package` behavior is preserved. -- [ ] Metadata validation/repair is preserved. -- [ ] Preparation + explicit commit/tag/push is preserved. -- [ ] Tag safety is preserved. -- [ ] Fresh-target build behavior is preserved. -- [ ] Default three-target cross build is preserved. -- [ ] `.tgz` naming/layout is preserved. -- [ ] SHA256SUMS is preserved. -- [ ] Gitea release creation/reuse works. -- [ ] Asset collision/refusal/replacement behavior is preserved. -- [ ] Asset-list pagination regression remains fixed. -- [ ] Anonymous artifact validation works. -- [ ] Real isolated cargo-binstall verification works. -- [ ] Token security/redaction passes. -- [ ] Rust formatting, Clippy, unit, mocked, and integration tests pass. -- [ ] At least one live disposable end-to-end Rust release has succeeded. -- [ ] Python runtime/dependencies are removable with no feature regression. - ---- - -## Historical compatibility notes worth preserving during the rewrite - -These are not new tasks; they are reminders of bugs/edge cases already discovered in real use: - -- [ ] A previous release-assets implementation incorrectly paginated an endpoint that returned the full attachment list, causing repeated nonempty results. Preserve the regression test. -- [ ] Reused Cargo target directories caused stale build-script Git revision metadata to leak into release binaries. Preserve fresh release target directories. -- [ ] Cross builds may require target-compatible native libraries; do not mask failures such as missing target SQLite. -- [ ] Some repositories use inherited workspace metadata. Use Cargo's resolved package information while editing only the selected member manifest. -- [ ] Existing tags must never be silently moved. -- [ ] Release asset replacement must remain explicitly authorized. diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c9d1f1d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1740 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3eb0f42d6c360dc3f8a821f6bf2fdea7f72bfd36b3076eb0e6d1e9e0752fff4" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "chacha20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "rand_core", +] + +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "clap_lex" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "cpufeatures" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "find-msvc-tools" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" + +[[package]] +name = "flate2" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "262590f4fe6afeb0bc83be1daa64e52657fe185690a958af7f3ad0e92085c5ae" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cd50c473c80f6d7c3670a752354b8e569b1a7cbfdc0419ec88e5edad85e0dc7" + +[[package]] +name = "futures-io" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4577ecaa3c4f96589d473f679a71b596316f6641bc350038b962a5daf0085d7a" + +[[package]] +name = "futures-sink" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e34418ac499d6305c2fb5ad0ed2f6ac998c5f8ca209b4510f7f94242c647e307" + +[[package]] +name = "futures-task" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b231ed28831efb4a61a08580c4bc233ec56bc009f4cd8f52da2c3cb97df0c109" + +[[package]] +name = "futures-util" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a77a90a256fce34da66415271e30f94ee91c57b04b8a2c042d9cf3220179deaa" +dependencies = [ + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "rand_core", + "wasm-bindgen", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55281c53a1894c864990125767da440a4e630446785086f52523b20033b74498" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" + +[[package]] +name = "icu_properties" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" + +[[package]] +name = "icu_provider" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipnet" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b18443e9c262bfe8fa82f51666e2642c53393f7e5c27b3e1aeab922cff5b9d8" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quinn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c1a41e437b6bbd489372cd4971de128e85c855f56c57f283d20ff016cf7c0a8" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4bfc015262b9df63c8845072ce59068853ff5872180c2ce2f13038b970e560" +dependencies = [ + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand", + "rand_pcg", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35a133f956daabe89a61a685c2649f13d82d5aa4bd5d12d1277e1072a21c0694" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.61.2", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +dependencies = [ + "chacha20", + "getrandom 0.4.3", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rust_realease" +version = "0.1.0" +dependencies = [ + "clap", + "flate2", + "regex", + "reqwest", + "serde", + "serde_json", + "sha2", + "similar", + "tempfile", + "toml", + "toml_edit", + "url", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "serde_json" +version = "1.0.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_spanned" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba467056f1b547ed52077911161fc86985becbc60e8e1857c8a144dab0def891" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "tinystr" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cf0ded5c4e56918d8f8a339e1bb67d038d3bc6d144ac407904015ba2e4cde9b" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "toml" +version = "1.1.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "920602543f0911ab71da12c50d59701da54c196d1a2bf5cb4b75667f137a406a" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_edit" +version = "0.25.13+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6975367e4d2ef766d86af01ffad14b622fecc8d4357a998fbc4deb6e9bacaf9b" +dependencies = [ + "indexmap", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_parser" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.1.2+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicase" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.119", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf85cb06032201fa7c6f829d7db5a7e5aa45bcc0655327713065f6f0576731bf" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" +dependencies = [ + "memchr", +] + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.5", +] + +[[package]] +name = "zlib-rs" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b08a912 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "rust_realease" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +clap = { version = "4.5", features = ["derive"] } +toml = "1.1" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +toml_edit = "0.25" +url = "2.5" +tempfile = "3" +regex = "1" +flate2 = "1.1" +sha2 = "0.10" +similar = "2" +reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "multipart", "rustls-tls"] } diff --git a/Jenkinsfile b/Jenkinsfile index dd8ba52..1d42c1b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,185 +5,19 @@ pipeline { agent { label 'rust_no_docker' } environment { - VENV = '.venv' - - // Make Python output immediately visible in Jenkins. - PYTHONUNBUFFERED = '1' - - // Ensure the local worker package is imported. - PYTHONPATH = "${WORKSPACE}" + CARGO_TERM_COLOR = 'never' } stages { stage('Checkout') { steps { checkout scm - - sh ''' - set -eu - - git submodule sync --recursive - git submodule update --init --recursive - ''' } } - stage('Setup Python') { + stage('Cargo Tests') { steps { - sh ''' - set -eu - - # --------------------------------------------------------- - # uv - # --------------------------------------------------------- - - if ! command -v uv >/dev/null 2>&1; then - echo "uv not found — installing it..." - - if ! command -v cargo-binstall >/dev/null 2>&1; then - echo "cargo-binstall not found — installing it..." - cargo install cargo-binstall - fi - - cargo binstall --no-confirm uv - else - echo "uv already installed." - fi - - echo - echo "uv:" - uv --version - - # --------------------------------------------------------- - # Python virtual environment - # --------------------------------------------------------- - - rm -rf "${VENV}" - - echo - echo "Creating Python virtual environment..." - - uv venv \ - --python 3.11 \ - "${VENV}" - - echo - echo "Installing dependencies..." - - uv pip install \ - --python "${VENV}/bin/python" \ - -r requirements.txt - - echo - echo "Python:" - "${VENV}/bin/python" --version - - echo - echo "Installed dependencies:" - uv pip list \ - --python "${VENV}/bin/python" - ''' - } - } - - stage('Python Syntax') { - steps { - sh ''' - set -eu - - echo "Checking Python syntax..." - - "${VENV}/bin/python" \ - -m compileall \ - -q \ - worker \ - tests - - echo "Python syntax OK." - ''' - } - } - - stage('Shell Syntax') { - steps { - sh ''' - set -eu - - echo "Checking release.sh syntax..." - - bash -n release.sh - - echo "Shell syntax OK." - ''' - } - } - - stage('CLI Smoke Test') { - steps { - sh ''' - set -eu - - echo "Testing worker CLI import..." - - "${VENV}/bin/python" \ - -P \ - -m worker \ - --help >/dev/null - - echo "Testing release.sh wrapper..." - - VIRTUAL_ENV="${WORKSPACE}/${VENV}" \ - ./release.sh --help >/dev/null - - echo "CLI smoke tests passed." - ''' - } - } - - stage('Tests') { - steps { - sh ''' - set -eu - - echo "Running rust_realease test suite..." - echo - - "${VENV}/bin/python" \ - -m unittest discover \ - -s tests \ - -p 'test_*.py' \ - -v - ''' - } - } - - stage('Repository Sanity') { - steps { - sh ''' - set -eu - - echo "Checking expected project files..." - - for file in \ - release.sh \ - requirements.txt \ - config.toml \ - worker/__init__.py \ - worker/__main__.py - do - if [ ! -e "${file}" ]; then - echo "ERROR: expected file is missing: ${file}" >&2 - exit 1 - fi - done - - if [ ! -x release.sh ]; then - echo "ERROR: release.sh is not executable." >&2 - exit 1 - fi - - echo "Repository sanity checks passed." - ''' + sh 'cargo test --locked --all' } } } @@ -192,45 +26,31 @@ pipeline { success { publishChecks( conclusion: 'SUCCESS', - name: 'Python CI', - summary: 'rust_realease passed all CI checks.', - text: 'Python syntax, shell syntax, CLI smoke tests, repository checks, and the complete unittest suite passed.', + name: 'Rust CI', + summary: 'Cargo tests passed.', + text: 'Rust tests passed without cross compilation.', title: 'CI Successful' ) - - echo '✅ rust_realease passed all CI checks.' } failure { publishChecks( conclusion: 'FAILURE', - name: 'Python CI', - summary: 'rust_realease failed CI.', - text: 'Python syntax, shell validation, CLI startup, repository sanity checks, or tests failed. Check the Jenkins log.', + name: 'Rust CI', + summary: 'Cargo tests failed.', + text: 'Check the Jenkins log for the Cargo test failure.', title: 'CI Failed' ) - - echo '❌ rust_realease CI failed.' } unstable { publishChecks( conclusion: 'ACTION_REQUIRED', - name: 'Python CI', - summary: 'rust_realease CI is unstable.', - text: 'The Jenkins build completed with an unstable status.', + name: 'Rust CI', + summary: 'Rust CI is unstable.', + text: 'The Cargo test build completed with an unstable status.', title: 'CI Unstable' ) - - echo '⚠️ rust_realease CI is unstable.' - } - - always { - sh ''' - echo "Cleaning CI environment..." - rm -rf .venv - find . -type d -name '__pycache__' -prune -exec rm -rf {} + 2>/dev/null || true - ''' } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 9eb8a51..5480f1b 100644 --- a/README.md +++ b/README.md @@ -1,217 +1,105 @@ -# Rust Binstall Publisher +# rust_realease -A small Python release tool for Rust CLI projects on Gitea, releasing one package with one binary at a time. It inspects Cargo metadata, checks version tags, cross-compiles binaries, creates reproducible `.tgz` archives and SHA-256 checksums, uploads a release, and verifies the downloads with cargo-binstall. +`rust_realease` builds and publishes one binary from a Rust package to a Gitea release. Give it a local project directory or a Git URL. It inspects Cargo metadata, checks the version tag, builds the selected targets with Cross, creates `.tgz` archives and `SHA256SUMS`, uploads them, and verifies release downloads. It can also prepare a project for release with explicit commit, tag, and push options. -## Setup +## Install -Python 3.11 or newer is required. +Install Rust and Cargo, Git, Cross, Docker or Podman, and cargo-binstall. A working container backend is needed for release builds. ```bash -python3 -m venv .venv -source .venv/bin/activate -pip install -r requirements.txt -./release.sh --help +cargo install --path . --locked +rust_realease --help ``` -The wrapper refuses to start without an active venv. If `.venv` is missing and stdin is interactive, it offers to create it and install dependencies, then exits. Activate the environment yourself before rerunning. An existing inactive `.venv` prints activation guidance. Another active venv requires interactive confirmation; non-interactive invocations fail. Paths passed to the wrapper are relative to your current directory, even when invoking the wrapper from elsewhere. +To run from this checkout without installing, use `cargo run -- SOURCE [OPTIONS]`. -Install Git, Rust/Cargo, `cross`, Docker or Podman, and `cargo-binstall`. A functioning container backend is required. `CROSS_CONTAINER_ENGINE=podman` or `docker` selects the backend; otherwise Docker is preferred when installed. The tool reports all prerequisite failures before cross-compilation. Cargo.lock is required by default; generate it explicitly or pass `--unlocked`. Builds can download Cargo dependencies and cross container images. +## Start with a project -## Inspect and repair +Preview a local project or remote Git repository: ```bash -./release.sh ./my-tool --dry-run -./release.sh https://gitea.example.org/owner/tool.git --dry-run -./release.sh ./my-tool --fix-metadata --stop-after metadata +rust_realease ./my-tool --dry-run +rust_realease https://gitea.example.org/owner/my-tool.git --dry-run ``` -Dry-run inspects the project, reports proposed metadata edits, prerequisites, the tag, target matrix, artifact names and destination. It does not edit Cargo.toml, compile code or mutate releases. It exits nonzero if metadata or prerequisite issues are found, even with `--fix-metadata`. Source acquisition still clones remote repositories; Cargo metadata and read-only prerequisite commands run. +For a workspace, select the exact Cargo package name: -Metadata repair preserves TOML comments and formatting where practical, reports each changed key, and only writes Cargo.toml. Missing `package.repository` is derived from Git origin; `--repository-url https://host/gitea/owner/tool` explicitly selects the destination. HTTP(S), SSH and SCP-style Git URLs are supported. SSH repository URLs are normalized to HTTPS web URLs; use the explicit option if SSH and web hosts or paths differ. HTTP credentials and query strings in repository URLs are rejected. - -The publisher standardizes these keys: - -```toml -[package.metadata.binstall] -pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.tgz" -pkg-fmt = "tgz" -bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }" +```bash +rust_realease ./my-workspace --package my-tool --dry-run ``` -Custom binstall options, including per-target overrides and signing configuration, are reported as conflicts and removed only with `--fix-metadata`. Review the reported changes. The template convention and local-manifest verification follow the [cargo-binstall support documentation](https://github.com/cargo-bins/cargo-binstall/blob/main/SUPPORT.md). +Each run handles one package with one binary. A dry run reports the package, version, expected tag, metadata changes, build targets, archive names, prerequisites, and release destination. It writes no project files, builds no binaries, and changes no Git refs or releases. It exits nonzero when it finds requirements that still need attention. -## Build and publish +## Prepare a release -Default targets: +Preview the project changes first: + +```bash +rust_realease ./my-tool --prepare-repo --dry-run +``` + +Apply the preparation locally: + +```bash +rust_realease ./my-tool --prepare-repo +``` + +Preparation repairs supported `Cargo.toml` release metadata, updates known `.gitignore` and `Cross.toml` settings, and generates a missing `Cargo.lock`. It exits before building or publishing. Review and commit local changes yourself, or authorize the Git steps explicitly from a clean checkout: + +```bash +rust_realease ./my-tool --prepare-repo \ + --commit-preparation --create-tag --push-preparation +``` + +The preparation commit uses your configured Git author and signing settings. The tag is annotated and follows your Git tag-signing configuration (`git config tag.gpgsign true` to sign it); an existing tag is never moved. The branch and tag are pushed atomically without force. Use `--cross-image-tag main` only when you want preparation to set Cross images for the selected targets. + +## Build local archives + +```bash +rust_realease ./my-tool --stop-after package +``` + +This runs host `cargo check` and `cargo test`, builds the selected targets with Cross, and writes archives plus `SHA256SUMS` into a new `dist/release-*/` directory. It does not contact Gitea. The default targets are: - `x86_64-unknown-linux-musl` - `aarch64-unknown-linux-musl` - `x86_64-pc-windows-gnu` -Build without contacting Gitea: +Override the matrix with repeatable `--target TARGET` options. Use `--stop-after verify` for host checks alone. -```bash -./release.sh ./my-tool --fix-metadata --stop-after package -``` +## Publish to Gitea -Before publishing, create and push the tag explicitly. For version `0.1.0`, the default tag is `v0.1.0`. Publication requires a clean checkout before the tool's own metadata repair, a local tag pointing to HEAD, and a matching remote tag. Normal publication does not create tags, commit or push; use the explicit preparation options below to automate those steps. A supplied `--tag` checks out that exact tag in a temporary clone; local sources must already be at that tag. +The package version and release tag must agree. For version `0.1.0`, the default tag is `v0.1.0`. Before ordinary publication, the local tag must point to the checkout and the matching remote tag must exist. Publication requires a clean checkout and an HTTPS Gitea destination. -```bash -export GITEA_TOKEN="..." -./release.sh https://gitea.example.org/owner/tool.git \ - --tag v0.1.0 --fix-metadata -``` - -## Prepare a repository for releases - -The preparation mode automates the repeatable repository fixes needed before publishing. Start with a preview: - -```bash -./release.sh ./my-tool --package my-tool \ - --prepare-repo --cross-image-tag main --dry-run -``` - -`--prepare-repo` repairs the selected package's repository/binstall metadata, adds build/artifact ignores and a Cargo.lock inclusion rule, generates Cargo.lock if missing, and configures Cross.toml to forward the selected Rust toolchain into containers. Existing lockfiles are preserved. TOML comments and unrelated cross settings are preserved where practical. `--cross-image-tag main` explicitly sets the configured targets' images to `ghcr.io/cross-rs/TARGET:main`; omit it to preserve existing image choices and cross defaults. This environment setup supports build scripts such as epoch_forge's without rewriting their Rust source. - -Apply locally for manual review: - -```bash -./release.sh ./my-tool --package my-tool --prepare-repo --cross-image-tag main -``` - -Or, from a clean checkout, explicitly automate the Git steps too: - -```bash -./release.sh ./my-tool --package my-tool --prepare-repo --cross-image-tag main \ - --commit-preparation --create-tag --push-preparation - -./release.sh ./my-tool --package my-tool --tag v0.1.0 -``` - -Use the actual package/version in these examples. Preparation exits without building or publishing. The second command performs the release checks, fresh builds, uploads, and verification. - -- `--commit-preparation` stages only the prepared manifest, Cross.toml, .gitignore and Cargo.lock, then commits using your configured Git identity and signing settings. It requires a clean worktree/index; if you already applied local preparation, review and commit those edits manually first. -- `--create-tag` requires `--commit-preparation`; it creates an annotated version tag or validates an existing tag. It never moves an existing tag. If repairs would change an already-tagged version, use a new package version or prepare without tag creation and manage the existing tag explicitly yourself. -- `--push-preparation` requires both flags above and a checked-out branch. It atomically pushes that branch and the tag to origin, without force. The push URL must match the release repository. Authentication/permission, branch protection and signing failures are reported; local prepared changes/commits/tags remain available for recovery. No remote push occurs without this flag. - -All preparation flags support `--dry-run`, which prints the proposed file diffs and Git operations without generating a lockfile, writing files, committing, tagging or pushing. A preparation preview reports planned fixes successfully; ordinary publication dry-run still exits nonzero for unresolved release requirements. `--prepare-repo` is separate from `--stop-after`. In preparation mode, `--tag` names the intended release tag and validates it against the package version; it does not check out an existing tag. - -Remote sources work too and their temporary checkouts are always retained in preparation mode. No token is needed for local preparation. These helpers do not rewrite arbitrary source code, change package versions, fix native dependencies such as SQLite, or install build tools. Compilation requirements still need to be satisfied before publication. - -### Select a workspace package - -Pass the exact Cargo package name with `--package` when the source is a workspace: - -```bash -./release.sh https://proxy.dandaedre.com/gitea/Ravenous/ravenous-tools.git \ - --package ravenous-tools --dry-run -``` - -Virtual workspace roots and workspaces with multiple members require explicit selection. Omitting `--package` lists the discovered members and stops; an unknown package, a library-only package, or a selected package with multiple binaries also fails clearly. A standalone package still works without this option. - -Cargo supplies the selected member's resolved version and repository, including inherited `[workspace.package]` values. `--fix-metadata` edits only that member's Cargo.toml; inherited versions and sibling manifests are preserved. Checks, tests and cross builds use `--package NAME` from the workspace root, sharing its Cargo.lock and profiles. Release builds use fresh subdirectories beneath its Cargo target directory. Dependencies of the selected package are built normally. Archives go into the workspace root's `dist/release-*/`, and cargo-binstall verification uses the selected member's manifest. - -The version/tag policy still applies to the selected package. For repositories using unprefixed tags such as `0.3.32`, pass `--tag-prefix "" --tag 0.3.32` (or set `[release].tag-prefix` in your configuration). Select an existing tag whose package version matches; the tool does not retag the current checkout. - -The `ravenous-tools` `0.3.32` integration attempt passed selection and host check/test, but the default musl cross-build failed at linking with `cannot find -lsqlite3`. Its system SQLite dependency needs a target-compatible build setup. Workspace selection does not provision native libraries inside cross images; this failure occurs before release creation or upload. - -### Gitea token permissions - -Create a **personal access token** in **User Settings → Applications → Manage Access Tokens**. Under **Select permissions**, set **Repository → Read and Write** (`write:repository`) and leave the other permission categories disabled. See Gitea's [token creation instructions](https://docs.gitea.com/development/api-usage/#generating-and-listing-api-tokens). - -`write:repository` includes repository read, write and delete API operations; a separate `read:repository` grant is unnecessary. This is the scope covering releases and their attachments, not `write:package` (the package registry). No user, organization, admin, issue, or package scope is needed, including when publishing to an organization-owned repository. Gitea's [scope definitions](https://docs.gitea.com/development/oauth2-provider/#scopes) describe this mapping. - -The token's account must also have access to the destination repository: **Code: Read** to inspect tags and **Releases: Write** to create releases and manage attachments. `--push-preparation` additionally requires **Code: Write** for the branch and tag push. For an organization repository such as `idle-org/john_slop`, ensure its team permissions grant that access. Token scopes do not grant repository membership or replace account permissions; see [Gitea repository permissions](https://docs.gitea.com/1.22/usage/permissions/). - -The publisher uses the permission for these operations: - -| Operation | API request under `/repos/{owner}/{repo}` | -| --- | --- | -| Check the remote tag | `GET /tags/{tag}` | -| Find or create a release | `GET /releases/tags/{tag}`, `POST /releases` | -| List or upload attachments | `GET` / `POST /releases/{id}/assets` | -| Replace a matching attachment, only with `--replace-assets` | `DELETE /releases/{id}/assets/{asset_id}` | - -The scope permits more repository operations than the publisher uses; Gitea's documented scopes do not provide a release-only write scope. - -Load the token into the shell running the publisher. In Bash, this prompts without echoing the token or including its value in shell history: +Set a Gitea token in the environment and run the command without `--stop-after`: ```bash read -r -s -p 'Gitea token: ' GITEA_TOKEN printf '\n' export GITEA_TOKEN +rust_realease ./my-tool ``` -Authenticated publication requires HTTPS. The token is read from `GITEA_TOKEN` (or the variable named by `[gitea].token-env`), redacted from worker diagnostics, and removed from child build processes. Cloning uses your normal Git authentication. With `--push-preparation`, HTTPS pushes to the matching release repository can use the same token through a temporary Git askpass helper; the token is never embedded in URLs, arguments, or helper files. SSH pushes use your normal SSH credentials. No token is needed for dry-run or `--stop-after package`. Full post-upload verification still requires publicly accessible release assets; granting more token permissions does not enable private/draft download verification. - -The pipeline runs `cargo check`, `cargo test`, and then `cross build --release --locked --target TARGET` for each target. `--skip-tests` skips tests but retains the check. `--profile` overrides the profile and repeatable `--target` overrides the matrix. Config defaults come from this tool's `config.toml`; use `--config PATH` for another file. Only `tgz` packaging is supported. - -Artifacts are stored in a fresh `dist/release-*/` directory so existing dist files are never deleted. For example: - -```text -dist/release-abc123/ - tool-x86_64-unknown-linux-musl-v0.1.0.tgz - tool-aarch64-unknown-linux-musl-v0.1.0.tgz - tool-x86_64-pc-windows-gnu-v0.1.0.tgz - SHA256SUMS -``` - -Each archive contains a matching top-level directory and its binary (with `.exe` on Windows). Archive timestamps and ownership are normalized. Identical binary bytes and modes produce identical archives. This does not guarantee that separate Rust builds produce identical binaries. - -Gitea releases are looked up by tag and reused or created. All existing asset names are checked before any upload; collisions fail unless `--replace-assets` explicitly permits deleting matching assets. Replacement is not transactional: a failed upload after deletion requires a retry. Completed uploads remain if a later step fails. - -After upload, anonymous downloads must match local checksums and archive structure, including SHA256SUMS. Then cargo-binstall installs each target into a temporary directory using the checked-out manifest, with compile and quick-install fallbacks disabled. Installed binaries are never executed, and the user's Cargo bin directory is not changed. `--skip-binstall-test` skips only this installation check, not download verification. - -## Stages and limitations - -`--stop-after` succeeds after the requested stage: `inspect`, `metadata`, `verify` (Cargo check/tests), `build`, `package`, `release`, `upload`, or `binstall` (published-download and installation verification). Early stages do not require a token. `--stop-after package` never contacts the Gitea API and can be used before creating tags. - -`--draft --stop-after upload` uploads a draft; draft and private assets cannot satisfy anonymous verification. `--prerelease` sets the flag on newly created releases. Reusing a release preserves its existing settings. Full verification currently requires publicly accessible assets. - -Remote clones and their artifacts are removed on exit unless `--keep-workdir` is supplied. Preparation mode always retains remote clones and prints their paths so local edits and commits are not lost. Local sources are never removed. Use `--keep-workdir` with remote builds when you want to retain artifacts or metadata repairs. `--verbose` adds commands and tracebacks. - -Workspaces require explicit package selection as described above; each invocation releases exactly one member with one binary. Multiple binary selection and publishing an entire workspace in one invocation are unsupported. No crate publishing, artifact signing, MSVC switching, macOS target setup, or automatic installation of build tools is provided. Build only source you trust: Cargo checks, tests and builds can execute project build scripts and procedural macros. - -A successful local-manifest install proves the uploaded artifact contract. Plain `cargo binstall tool` additionally requires a registry-published crate containing the corrected metadata; this tool does not publish crates. For an unregistered project, use `cargo binstall --git https://host/owner/tool.git tool` after explicitly committing and pushing the metadata. - -## Tests +For a remote repository, select the exact tag to check out: ```bash -source .venv/bin/activate -python -m unittest discover -s tests -v +rust_realease https://gitea.example.org/owner/my-tool.git --tag v0.1.0 ``` -The basic suite uses temporary fixtures and mocked Gitea requests; it needs no token or live Gitea instance. See `AGENT_TASKS.md` for implementation and integration status. +The token account needs repository read access and release write access. `--push-preparation` additionally needs code write access. The token is read from `GITEA_TOKEN` by default and is kept out of child build processes. Public releases are verified by anonymous downloads. Private releases are downloaded with Gitea authentication; use `--skip-binstall-test` for them because cargo-binstall cannot install from their private attachment URLs. An existing asset with the same name stops the upload; `--replace-assets` explicitly permits replacing matching assets. -Repository cross configuration containing `pre-build`, `dockerfile` or `runner` hooks is rejected rather than executing shell commands from metadata. Custom cross images can still be selected through cross's normal configuration/environment interface. +## More options -### Tested host compatibility +See [Advanced usage](docs/advanced-usage.md) for configuration, metadata, workspace behavior, stage controls, artifact layout, token handling, and troubleshooting. Run `rust_realease --help` for the complete option list. -On the development host, cross 0.2.5's default images failed with GLIBC version errors against Rust nightly 1.100. The same project built for all three targets using newer images, following [cross's image configuration](https://github.com/cross-rs/cross/blob/main/docs/config_file.md): +## Development ```bash -export CROSS_TARGET_X86_64_UNKNOWN_LINUX_MUSL_IMAGE=ghcr.io/cross-rs/x86_64-unknown-linux-musl:main -export CROSS_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_IMAGE=ghcr.io/cross-rs/aarch64-unknown-linux-musl:main -export CROSS_TARGET_X86_64_PC_WINDOWS_GNU_IMAGE=ghcr.io/cross-rs/x86_64-pc-windows-gnu:main +cargo fmt --check +cargo clippy --all-targets --all-features -- -D warnings +cargo test --all ``` -These are explicit compatibility overrides, not publisher defaults. Pin image digests in your own build environment when reproducibility across image updates matters. +Jenkins runs Cargo tests only. Cross builds and live Gitea releases are separate release checks. The small [target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) is maintained in its own repository for fast target matrix experiments. Clone it separately when needed; it is not part of this checkout. -To exercise actual cargo-binstall downloads and installations against a temporary local HTTP server: - -```bash -PUBLISHER_INTEGRATION=1 python -m unittest discover -s tests -v -``` - -This opt-in test installs fixture files for all three targets into disposable directories and never executes them. - -For `epoch_forge`'s build scripts, which query `rustup` unless a toolchain is supplied, the successful test additionally used: - -```bash -export RUSTUP_TOOLCHAIN=nightly -export CROSS_CONTAINER_OPTS='--env RUSTUP_TOOLCHAIN=nightly' -``` - -This passes the selected toolchain name to the containers without requiring `rustup` inside them. Use the toolchain appropriate to your project. Cross documents container options and environment forwarding in its [environment-variable reference](https://github.com/cross-rs/cross/blob/main/docs/environment_variables.md). - -Release builds now always use a new `target/binstall-publisher/build-*/` directory (under Cargo's configured target directory). This prevents reused build-script output from embedding an old Git revision, as happened in the epoch_forge test. The directories are retained with the checkout for inspection; fresh builds take longer and use more disk space than reusing Cargo's release cache. Corrected epoch_forge assets have been replaced and verified on the live v0.1.0 release. +With `cargo-binstall` installed, run the opt-in isolated install fixture for all three targets with `cargo test real_binstall_installs_local_release_fixture -- --ignored`. diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md new file mode 100644 index 0000000..7f5a002 --- /dev/null +++ b/docs/advanced-usage.md @@ -0,0 +1,98 @@ +# Advanced usage + +## Disposable release fixture + +The [target_test_project repository](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) is a small standalone Cargo binary for Cross and Gitea integration tests. Clone it separately rather than expecting a submodule in this repository: + +```bash +git clone https://proxy.dandaedre.com/gitea/idle-org/target_test_project.git +rust_realease ./target_test_project --dry-run +``` + +Its public [v0.1.3 release](https://proxy.dandaedre.com/gitea/idle-org/target_test_project/releases/tag/v0.1.3) was used to validate preparation, all three default Cross targets, published checksums, and isolated cargo-binstall installs. Use a new version and tag for future publication tests; release assets with the same names are deliberately treated as collisions. + +## Sources and package selection + +`SOURCE` can be a local directory, an HTTP(S) Git URL, an SSH URL, or an SCP-style URL such as `git@host:owner/repo.git`. Remote sources are cloned into a managed checkout. Use `--keep-workdir` to retain that checkout and its artifacts; preparation retains remote checkouts automatically. Local sources are never deleted. + +Use `--package NAME` to select one workspace member by its exact Cargo package name. Selection uses `cargo metadata` and accepts inherited workspace version and repository values. Library-only packages and members with multiple binaries cannot be released. Checks, tests, and builds are scoped to the selected package. + +`--repository-url https://host/owner/repo` selects the release destination when Cargo metadata or Git origin needs an explicit web URL. Materially conflicting origin and release destinations are refused. Repository URLs with embedded credentials are refused. + +## Configuration + +The bundled `config.toml` supplies defaults. Pass `--config PATH` to use another file; CLI values override its build and tag settings. + +```toml +[release] +tag-prefix = "v" + +[build] +targets = [ + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl", + "x86_64-pc-windows-gnu", +] +profile = "release" +locked = true + +[package] +format = "tgz" + +[gitea] +token-env = "GITEA_TOKEN" +``` + +Use `--tag-prefix ""` for an unprefixed tag such as `0.3.1`. `--tag TAG` must match the selected package version and configured prefix. `--unlocked` permits builds without the usual locked dependency resolution; the default uses `--locked`. + +## Metadata and preparation + +The publisher checks the selected member's repository and cargo-binstall metadata. `--fix-metadata` repairs supported entries in that member's `Cargo.toml` while preserving comments where practical. It does not commit, tag, or push. To preview the full preparation diff, use `--prepare-repo --dry-run`. + +The required binstall contract is: + +```toml +[package.metadata.binstall] +pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.tgz" +pkg-fmt = "tgz" +bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }" +``` + +The generated `pkg-url` uses the configured tag prefix; the archive filename itself retains `-v{version}`. Unsupported binstall keys are reported as metadata conflicts. A preparation run may create one extra fix commit, as intended. `--commit-preparation` requires a clean worktree and index. `--create-tag` requires that commit option, and `--push-preparation` requires both. No option moves an existing tag or force pushes. + +For repositories whose build scripts need a selected Rust toolchain inside Cross, preparation can add the supported `Cross.toml` environment setting. `--cross-image-tag TAG` explicitly selects `ghcr.io/cross-rs/TARGET:TAG` images. Existing unrelated Cross settings are preserved. Repository-provided Cross hooks such as `pre-build`, `dockerfile`, and `runner` are rejected by the release build. + +## Pipeline stages + +`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. Release builds use fresh target directories to avoid stale build-script output. `release` creates or reuses the Gitea release; `upload` uploads archives and checksums. The final stage downloads assets anonymously for public repositories or with Gitea authentication for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for public targets. `--skip-binstall-test` skips that install check, while retaining download verification. + +`--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when `GITEA_TOKEN` is set. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly. + +## Artifact contract + +Each archive is named `{name}-{target}-v{version}.tgz` and contains exactly one root directory with one binary: + +```text +my-tool-x86_64-unknown-linux-musl-v0.1.0.tgz +└── my-tool-x86_64-unknown-linux-musl-v0.1.0/ + └── my-tool +``` + +Windows archives contain `my-tool.exe`. `SHA256SUMS` lists the final archive checksums. Archive ownership and timestamps are normalized; identical binary bytes and modes produce identical archives. Separate builds can still produce different binaries. + +## Authentication and security + +Authenticated Gitea API publication requires HTTPS. The token comes from the configured environment variable, never from a positional argument or URL. Child Cargo and Cross processes do not receive it. An HTTPS preparation push to the matching release repository can use the token through a temporary askpass helper; SSH pushes use normal Git credentials. Grant the token account code read and release write access, plus code write when using `--push-preparation`. + +Normal publication does not create a commit, tag, or push. Existing asset names cause an error unless `--replace-assets` is set; unrelated assets are not deleted. Private assets are verified with authenticated downloads and do not establish public installability. + +## Troubleshooting + +- **Missing tag:** prepare and push the expected tag explicitly, or use `--prepare-repo --commit-preparation --create-tag --push-preparation` from a clean checkout. +- **Missing Cargo.lock:** run preparation or use `--unlocked` when unlocked resolution is intentional. +- **Container unavailable:** start Docker or Podman. `CROSS_CONTAINER_ENGINE` can select `docker` or `podman`. +- **Cross image or native library failure:** configure an appropriate Cross image and target libraries in the project. The publisher does not substitute an MSVC target or hide linker failures. +- **Private release installation:** pass `--skip-binstall-test` to verify the uploaded assets with Gitea authentication. Use a public disposable release to test real cargo-binstall installation. +- **Asset collision:** inspect the existing release; use `--replace-assets` only when replacement is intended. + +`cargo-binstall` verification installs into temporary directories and does not run foreign-target binaries. Plain registry based `cargo binstall my-tool` additionally requires a published crate with the release metadata; this tool does not publish crates. diff --git a/release.sh b/release.sh deleted file mode 100755 index 28dee08..0000000 --- a/release.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" -VENV_DIR="${SCRIPT_DIR}/.venv" - -die() { - echo "error: $*" >&2 - exit 2 -} - -activation_guidance() { - if [[ "$PWD" == "$SCRIPT_DIR" ]]; then - echo ' source .venv/bin/activate' - else - printf ' source %q\n' "${VENV_DIR}/bin/activate" - fi -} - -if [[ -z "${VIRTUAL_ENV:-}" ]]; then - echo 'rust-binstall-publisher requires an active Python virtual environment.' - if [[ -d "$VENV_DIR" ]]; then - echo 'Activate the project environment first:' - activation_guidance - exit 2 - fi - [[ -t 0 ]] || die 'no virtual environment active and stdin is non-interactive.' - read -r -p 'No .venv exists. Create and install dependencies now? [Y/n] ' answer - case "${answer:-Y}" in - y|Y|yes|YES) - command -v python3 >/dev/null || die 'python3 is required.' - python3 -m venv "$VENV_DIR" - "$VENV_DIR/bin/python" -m pip install -r "$SCRIPT_DIR/requirements.txt" - echo 'Virtual environment ready. Activate it, then rerun your command:' - activation_guidance - exit 0 - ;; - *) die 'environment setup cancelled.' ;; - esac -fi - -EXPECTED_VENV="$(cd "$VENV_DIR" 2>/dev/null && pwd -P || true)" -ACTIVE_VENV="$(cd "$VIRTUAL_ENV" 2>/dev/null && pwd -P || true)" -if [[ ! -d "$VENV_DIR" || -z "$ACTIVE_VENV" || "$ACTIVE_VENV" != "$EXPECTED_VENV" ]]; then - echo "warning: another environment is active: $VIRTUAL_ENV" >&2 - echo "Expected: $VENV_DIR" >&2 - [[ -t 0 ]] || die 'another virtual environment is active and stdin is non-interactive.' - read -r -p 'Continue anyway? [y/N] ' answer - case "${answer:-N}" in - y|Y|yes|YES) ;; - *) exit 2 ;; - esac -fi - -[[ -x "$VIRTUAL_ENV/bin/python" ]] || die 'active environment has no Python interpreter.' -# Keep SOURCE relative to the caller, but import only this tool's worker package. -export PYTHONPATH="$SCRIPT_DIR" -exec "$VIRTUAL_ENV/bin/python" -P -m worker "$@" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index ce98fa0..0000000 --- a/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -tomlkit>=0.13,<1 -rich>=14,<15 -typer>=0.16,<1 -httpx>=0.28,<1 diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..0d0ce74 --- /dev/null +++ b/src/build.rs @@ -0,0 +1,262 @@ +use crate::{config::Config, error::Error, policy, project::SelectedPackage, runner::Runner}; +use std::{ + env, fs, + path::{Path, PathBuf}, +}; + +pub fn prerequisites( + root: &Path, + config: &Config, + needs_cross: bool, + needs_binstall: bool, + runner: &Runner, +) -> (Vec, Option) { + let mut problems = Vec::new(); + if let Err(error) = validate_cross_config(root) { + problems.push(error.to_string()); + } + for tool in ["git", "cargo"] + .into_iter() + .chain(needs_cross.then_some("cross")) + .chain(needs_binstall.then_some("cargo-binstall")) + { + if executable(tool).is_none() { + problems.push(format!("{tool} is missing")); + } + } + let mut engine = None; + if needs_cross { + let requested = env::var("CROSS_CONTAINER_ENGINE").ok(); + let candidate = requested.or_else(|| { + ["docker", "podman"] + .into_iter() + .find(|tool| executable(tool).is_some()) + .map(str::to_owned) + }); + match candidate.as_deref() { + Some("docker" | "podman") => { + if let Some(candidate) = candidate { + if let Err(error) = runner.output(&candidate, &["info"], root) { + problems.push(error.to_string()); + } else { + engine = Some(candidate); + } + } + } + _ => problems.push( + "Set CROSS_CONTAINER_ENGINE to docker or podman with a usable backend".into(), + ), + } + } + if config.locked && !root.join("Cargo.lock").is_file() { + problems.push("Cargo.lock is missing; generate it explicitly or use --unlocked".into()); + } + (problems, engine) +} + +fn executable(name: &str) -> Option { + let path = env::var_os("PATH")?; + env::split_paths(&path) + .map(|dir| dir.join(name)) + .find(|path| path.is_file()) +} + +fn validate_cross_config(root: &Path) -> Result<(), Error> { + let source = fs::read_to_string(root.join("Cargo.toml")) + .map_err(|e| Error(format!("cannot inspect root Cargo.toml: {e}")))?; + let manifest: toml::Value = + toml::from_str(&source).map_err(|e| Error(format!("invalid root Cargo.toml: {e}")))?; + if let Some(cross) = manifest + .get("workspace") + .and_then(|value| value.get("metadata")) + .and_then(|value| value.get("cross")) + { + reject_hooks(cross)?; + } + let path = root.join("Cross.toml"); + if path.exists() { + let source = fs::read_to_string(&path) + .map_err(|e| Error(format!("cannot read {}: {e}", path.display())))?; + let cross: toml::Value = toml::from_str(&source) + .map_err(|e| Error(format!("invalid {}: {e}", path.display())))?; + reject_hooks(&cross)?; + } + Ok(()) +} + +fn reject_hooks(value: &toml::Value) -> Result<(), Error> { + match value { + toml::Value::Table(table) => { + for (key, child) in table { + if matches!(key.as_str(), "pre-build" | "dockerfile" | "runner") { + return Err(Error(format!("Repository cross configuration contains unsupported executable option {key:?}."))); + } + reject_hooks(child)?; + } + } + toml::Value::Array(array) => { + for child in array { + reject_hooks(child)?; + } + } + _ => (), + } + Ok(()) +} + +pub fn verify_source( + root: &Path, + project: &SelectedPackage, + config: &Config, + skip_tests: bool, + runner: &Runner, +) -> Result<(), Error> { + let mut flags = vec!["--package", project.name.as_str()]; + if config.locked { + flags.push("--locked"); + } + runner.output("cargo", &[&["check"], flags.as_slice()].concat(), root)?; + if !skip_tests { + runner.output("cargo", &[&["test"], flags.as_slice()].concat(), root)?; + } + Ok(()) +} + +pub fn cross_arguments( + project: &SelectedPackage, + config: &Config, + target: &str, + target_dir: &Path, +) -> Vec { + let mut args = vec!["build".to_owned()]; + if config.profile == "release" { + args.push("--release".into()); + } else { + args.extend(["--profile".into(), config.profile.clone()]); + } + if config.locked { + args.push("--locked".into()); + } + args.extend([ + "--target".into(), + target.to_owned(), + "--target-dir".into(), + target_dir.display().to_string(), + "--package".into(), + project.name.clone(), + "--bin".into(), + project.binary.clone(), + ]); + args +} + +pub fn build( + root: &Path, + project: &SelectedPackage, + config: &Config, + engine: &str, + runner: &Runner, +) -> Result, Error> { + let target_base = project.target_directory.join("binstall-publisher"); + fs::create_dir_all(&target_base) + .map_err(|e| Error(format!("cannot create {}: {e}", target_base.display())))?; + let fresh = tempfile::Builder::new() + .prefix("build-") + .tempdir_in(&target_base) + .map_err(|e| Error(format!("cannot create fresh build directory: {e}")))?; + let target_dir = fresh.keep(); + println!("Fresh build directory: {}", target_dir.display()); + let toolchain = executable("rustup") + .and_then(|_| { + runner + .text("rustup", &["show", "active-toolchain"], root) + .ok() + }) + .and_then(|line| line.split_whitespace().next().map(str::to_owned)); + let mut environment = vec![("CROSS_CONTAINER_ENGINE", engine)]; + if let Some(toolchain) = toolchain.as_deref() { + environment.push(("RUSTUP_TOOLCHAIN", toolchain)); + } + let profile_dir = if config.profile == "dev" { + "debug" + } else { + &config.profile + }; + let mut outputs = Vec::new(); + for target in &config.targets { + println!("Building {target}"); + let args = cross_arguments(project, config, target, &target_dir); + let borrowed: Vec<&str> = args.iter().map(String::as_str).collect(); + runner.output_with("cross", &borrowed, root, &environment, &[])?; + let binary = target_dir + .join(target) + .join(profile_dir) + .join(policy::binary_filename(&project.binary, target)); + let metadata = fs::symlink_metadata(&binary).map_err(|_| { + Error(format!( + "Build did not produce a regular binary: {}", + binary.display() + )) + })?; + if !metadata.file_type().is_file() { + return Err(Error(format!( + "Build did not produce a regular binary: {}", + binary.display() + ))); + } + outputs.push((target.clone(), binary)); + } + Ok(outputs) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn cross_command_scopes_package_and_uses_fresh_target_path() { + let project = SelectedPackage { + name: "cli".into(), + version: "0.1.0".into(), + binary: "cli".into(), + manifest: PathBuf::from("Cargo.toml"), + repository: None, + target_directory: PathBuf::from("target"), + }; + let config = Config { + targets: vec!["x86_64-pc-windows-gnu".into()], + profile: "release".into(), + locked: true, + tag_prefix: "v".into(), + token_env: "GITEA_TOKEN".into(), + }; + let args = cross_arguments( + &project, + &config, + "x86_64-pc-windows-gnu", + Path::new("/tmp/fresh"), + ); + assert_eq!( + args, + [ + "build", + "--release", + "--locked", + "--target", + "x86_64-pc-windows-gnu", + "--target-dir", + "/tmp/fresh", + "--package", + "cli", + "--bin", + "cli" + ] + ); + } + #[test] + fn cross_hooks_are_rejected() { + let doc: toml::Value = + toml::from_str("[target.x86_64-unknown-linux-musl]\npre-build=['echo unsafe']") + .unwrap(); + assert!(reject_hooks(&doc).is_err()); + } +} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..9c2757e --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,413 @@ +use crate::{ + build, + config::{self, Overrides}, + error::Error, + gitea::Gitea, + manifest, + model::Stage, + policy, prepare, project, + runner::Runner, + verify, +}; +use clap::Parser; +use std::path::PathBuf; + +#[derive(Debug, Parser)] +#[command(about = "Build and publish Rust binary releases to Gitea", version)] +pub struct Cli { + /// Local directory or Git URL. + pub source: String, + #[arg(long)] + pub package: Option, + #[arg(long, short)] + pub tag: Option, + #[arg(long)] + pub tag_prefix: Option, + #[arg(long)] + pub target: Vec, + #[arg(long)] + pub profile: Option, + #[arg(long, conflicts_with = "unlocked")] + pub locked: bool, + #[arg(long)] + pub unlocked: bool, + #[arg(long)] + pub dry_run: bool, + #[arg(long)] + pub fix_metadata: bool, + #[arg(long)] + pub skip_tests: bool, + #[arg(long)] + pub skip_binstall_test: bool, + #[arg(long)] + pub draft: bool, + #[arg(long)] + pub prerelease: bool, + #[arg(long)] + pub replace_assets: bool, + #[arg(long)] + pub keep_workdir: bool, + #[arg(long)] + pub verbose: bool, + #[arg(long, value_enum)] + pub stop_after: Option, + #[arg(long)] + pub prepare_repo: bool, + #[arg(long)] + pub commit_preparation: bool, + #[arg(long)] + pub create_tag: bool, + #[arg(long)] + pub push_preparation: bool, + #[arg(long)] + pub cross_image_tag: Option, + #[arg(long)] + pub repository_url: Option, + #[arg(long)] + pub config: Option, +} + +pub fn run(args: Cli) -> Result<(), Error> { + if (args.commit_preparation + || args.create_tag + || args.push_preparation + || args.cross_image_tag.is_some()) + && !args.prepare_repo + { + return Err(Error("Preparation options require --prepare-repo.".into())); + } + if args.prepare_repo && args.stop_after.is_some() { + return Err(Error( + "--prepare-repo is a separate mode; omit --stop-after.".into(), + )); + } + let config = config::load( + args.config.as_deref(), + Overrides { + targets: args.target, + profile: args.profile, + unlocked: args.unlocked, + tag_prefix: args.tag_prefix, + }, + )?; + let runner = Runner::new(&config.token_env).with_verbose(args.verbose); + let checkout = project::acquire_project( + &args.source, + if args.prepare_repo { + None + } else { + args.tag.as_deref() + }, + args.keep_workdir || args.prepare_repo, + &runner, + )?; + let root = checkout + .root + .to_str() + .ok_or_else(|| Error("Project path is not UTF-8.".into()))?; + let selected = project::inspect(root, args.package.as_deref(), &runner)?; + let publishing = !args.prepare_repo + && !args.dry_run + && args + .stop_after + .is_none_or(|stage| stage.order() >= Stage::Release.order()); + let tag = policy::expected_tag(&selected.version, &config.tag_prefix); + if args.tag.as_ref().is_some_and(|provided| provided != &tag) { + return Err(Error(format!( + "Tag {:?} does not match package version; expected {tag:?}.", + args.tag + ))); + } + println!("Package: {}", selected.name); + println!("Version: {}", selected.version); + println!("Binary: {}", selected.binary); + println!("Manifest: {}", selected.manifest.display()); + println!("Tag: {tag}"); + if args + .stop_after + .is_some_and(|stage| stage.order() <= Stage::Inspect.order()) + { + return Ok(()); + } + if publishing + && !runner + .text( + "git", + &["status", "--porcelain", "--untracked-files=normal"], + &checkout.root, + )? + .is_empty() + { + return Err(Error( + "Publication requires a clean checkout before metadata repair.".into(), + )); + } + let candidate = args + .repository_url + .as_deref() + .or(selected.repository.as_deref()) + .map(str::to_owned) + .or_else(|| project::origin_url(&checkout.root, &runner)) + .ok_or_else(|| { + Error( + "package.repository is missing; provide --repository-url and --fix-metadata." + .into(), + ) + })?; + let repository = project::normalize_repository(&candidate)?; + if publishing { + if let Some(origin) = project::origin_url(&checkout.root, &runner) { + if project::normalize_repository(&origin)? != repository { + return Err(Error( + "Git origin differs from the release repository.".into(), + )); + } + } + } + let issues = manifest::changes(&selected.manifest, &repository, &config.tag_prefix)?; + if issues.is_empty() { + println!("Binstall metadata: valid"); + } + for issue in &issues { + let actual = if issue.actual.is_some() { + "present" + } else { + "missing" + }; + println!("{}: {actual} -> {:?}", issue.key, issue.expected); + } + if args.prepare_repo { + return prepare::run( + &checkout.root, + &selected, + &config, + &repository, + prepare::Options { + tag: &tag, + image_tag: args.cross_image_tag.as_deref(), + dry_run: args.dry_run, + commit: args.commit_preparation, + create_tag: args.create_tag, + push: args.push_preparation, + }, + &runner, + ); + } + if !issues.is_empty() && args.fix_metadata && !args.dry_run { + manifest::repair(&selected.manifest, &issues, true)?; + println!("Metadata repaired locally. Commit and push explicitly if desired."); + } + if !issues.is_empty() && !args.fix_metadata && !args.dry_run { + return Err(Error( + "Metadata requires repair. Rerun with --fix-metadata.".into(), + )); + } + if args.stop_after == Some(Stage::Metadata) { + return Ok(()); + } + for target in &config.targets { + println!( + "{target}: {} ({})", + policy::archive_name(&selected.name, target, &selected.version), + policy::binary_filename(&selected.binary, target) + ); + } + for (key, value) in policy::binstall_metadata(&config.tag_prefix) { + println!("Binstall {key}: {value}"); + } + println!("Release destination: {repository}/releases/tag/{tag}"); + let needs_cross = args.stop_after != Some(Stage::Verify); + let needs_binstall = !args.skip_binstall_test + && args + .stop_after + .is_none_or(|stage| stage.order() >= Stage::Binstall.order()); + let publication = if publishing { + project::validate_checkout_tag(&checkout.root, &tag, &runner)?; + let api = Gitea::new(&repository, &config.token_env)?; + let private = api.repository_private()?; + if private && needs_binstall { + return Err(Error("This repository is private; cargo-binstall cannot verify its private release downloads. Rerun with --skip-binstall-test to verify uploaded assets with Gitea authentication.".into())); + } + Some((api, private)) + } else { + None + }; + let (mut problems, engine) = build::prerequisites( + &checkout.root, + &config, + needs_cross, + needs_binstall, + &runner, + ); + if args.dry_run { + if let Err(error) = project::validate_checkout_tag(&checkout.root, &tag, &runner) { + problems.push(error.to_string()); + } + } + for problem in &problems { + println!("Prerequisite: {problem}"); + } + if args.dry_run { + println!("Dry run complete: no manifest edits, builds or release mutations."); + if !issues.is_empty() || !problems.is_empty() { + return Err(Error( + "Dry run found the metadata/prerequisite issues reported above.".into(), + )); + } + return Ok(()); + } + if !problems.is_empty() { + return Err(Error( + "Resolve the prerequisite issues above before building.".into(), + )); + } + build::verify_source(&checkout.root, &selected, &config, args.skip_tests, &runner)?; + if args.stop_after == Some(Stage::Verify) { + return Ok(()); + } + let engine = engine.ok_or_else(|| Error("No usable Cross container backend.".into()))?; + let outputs = build::build(&checkout.root, &selected, &config, &engine, &runner)?; + if args.stop_after == Some(Stage::Build) { + return Ok(()); + } + let (artifacts, sums) = crate::package::package( + &checkout.root, + &selected.name, + &selected.version, + &selected.binary, + &outputs, + )?; + println!( + "Artifacts: {}", + sums.parent().map_or_else( + || sums.display().to_string(), + |path| path.display().to_string() + ) + ); + for artifact in &artifacts { + println!( + "Packaged {}: {}/{}", + artifact.target, artifact.root, artifact.binary + ); + } + if args.stop_after == Some(Stage::Package) { + return Ok(()); + } + let commit = project::head_commit(&checkout.root, &runner)?; + let (api, private) = + publication.ok_or_else(|| Error("Publication client was not initialized.".into()))?; + let release = api.ensure_release(&tag, &commit, args.draft, args.prerelease)?; + println!("Release ready: {tag}"); + if args.stop_after == Some(Stage::Release) { + return Ok(()); + } + let mut paths: Vec<&std::path::Path> = artifacts.iter().map(|a| a.path.as_path()).collect(); + paths.push(sums.as_path()); + api.upload(release.id, &paths, args.replace_assets)?; + if args.stop_after == Some(Stage::Upload) { + return Ok(()); + } + if release.draft { + return Err(Error("Draft uploaded. Public verification requires publishing the draft; use --stop-after upload for draft workflows.".into())); + } + verify::verify_published( + &repository, + &tag, + &artifacts, + &sums, + if private { + Some(api.download_client()) + } else { + None + }, + )?; + if !args.skip_binstall_test { + verify::verify_binstall(&checkout.root, &selected, &config.targets, &runner)?; + } + println!("Release completed and published artifacts verified."); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{fs, path::Path}; + + fn write(root: &Path, relative: &str, content: &str) { + let path = root.join(relative); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, content).unwrap(); + } + + #[test] + fn dry_run_never_edits_and_repair_only_touches_selected_member() { + let temp = tempfile::tempdir().unwrap(); + write( + temp.path(), + "config.toml", + "[build]\ntargets=['x86_64-unknown-linux-musl']\n", + ); + write( + temp.path(), + "Cargo.toml", + "[workspace]\nmembers=['one','two']\nresolver='2'\n", + ); + for name in ["one", "two"] { + write(temp.path(), &format!("{name}/Cargo.toml"), &format!("# {name}\n[package]\nname='{name}'\nversion='0.1.0'\nedition='2021'\nrepository='https://example.com/owner/{name}'\n")); + write( + temp.path(), + &format!("{name}/src/main.rs"), + "fn main() {}\n", + ); + } + let root = temp.path().to_str().unwrap(); + let config = temp + .path() + .join("config.toml") + .to_string_lossy() + .into_owned(); + let one = temp.path().join("one/Cargo.toml"); + let two = temp.path().join("two/Cargo.toml"); + let original_one = fs::read_to_string(&one).unwrap(); + let original_two = fs::read_to_string(&two).unwrap(); + let args = Cli::parse_from([ + "rust_realease", + root, + "--config", + &config, + "--package", + "one", + "--fix-metadata", + "--dry-run", + "--stop-after", + "metadata", + ]); + run(args).unwrap(); + assert_eq!(fs::read_to_string(&one).unwrap(), original_one); + let args = Cli::parse_from([ + "rust_realease", + root, + "--config", + &config, + "--package", + "one", + "--fix-metadata", + "--stop-after", + "metadata", + ]); + run(args).unwrap(); + assert_ne!(fs::read_to_string(&one).unwrap(), original_one); + assert_eq!(fs::read_to_string(&two).unwrap(), original_two); + let args = Cli::parse_from([ + "rust_realease", + root, + "--config", + &config, + "--package", + "one", + "--stop-after", + "metadata", + ]); + run(args).unwrap(); + } +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..f90c295 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,158 @@ +use crate::error::Error; +use serde::Deserialize; +use std::{fs, path::Path}; + +#[derive(Debug, Deserialize, Default)] +struct FileConfig { + build: Option, + release: Option, + package: Option, + gitea: Option, +} +#[derive(Debug, Deserialize, Default)] +struct Build { + targets: Option>, + profile: Option, + locked: Option, +} +#[derive(Debug, Deserialize, Default)] +struct Release { + #[serde(rename = "tag-prefix")] + tag_prefix: Option, +} +#[derive(Debug, Deserialize, Default)] +struct Package { + format: Option, +} +#[derive(Debug, Deserialize, Default)] +struct Gitea { + #[serde(rename = "token-env")] + token_env: Option, +} + +#[derive(Debug, Clone)] +pub struct Config { + pub targets: Vec, + pub profile: String, + pub locked: bool, + pub tag_prefix: String, + pub token_env: String, +} +#[derive(Default)] +pub struct Overrides { + pub targets: Vec, + pub profile: Option, + pub unlocked: bool, + pub tag_prefix: Option, +} +pub fn load(path: Option<&Path>, overrides: Overrides) -> Result { + if let Some(path) = path { + let source = fs::read_to_string(path) + .map_err(|e| Error(format!("cannot read config {}: {e}", path.display())))?; + parse(&source, overrides) + } else { + parse(include_str!("../config.toml"), overrides) + } +} +fn parse(source: &str, overrides: Overrides) -> Result { + let file: FileConfig = + toml::from_str(source).map_err(|e| Error(format!("invalid config: {e}")))?; + let build = file.build.unwrap_or_default(); + let release = file.release.unwrap_or_default(); + let package = file.package.unwrap_or_default(); + let gitea = file.gitea.unwrap_or_default(); + if package.format.as_deref().unwrap_or("tgz") != "tgz" { + return Err(Error("Only package.format = \"tgz\" is supported.".into())); + } + let mut config = Config { + targets: if overrides.targets.is_empty() { + build.targets.unwrap_or_else(|| { + [ + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl", + "x86_64-pc-windows-gnu", + ] + .map(str::to_owned) + .to_vec() + }) + } else { + overrides.targets + }, + profile: overrides + .profile + .or(build.profile) + .unwrap_or_else(|| "release".into()), + locked: if overrides.unlocked { + false + } else { + build.locked.unwrap_or(true) + }, + tag_prefix: overrides + .tag_prefix + .or(release.tag_prefix) + .unwrap_or_else(|| "v".into()), + token_env: gitea.token_env.unwrap_or_else(|| "GITEA_TOKEN".into()), + }; + if config.targets.is_empty() || config.targets.iter().any(|target| !valid_target(target)) { + return Err(Error( + "build.targets must be a nonempty list of Rust target triples.".into(), + )); + } + let mut unique = Vec::new(); + for target in config.targets { + if !unique.contains(&target) { + unique.push(target); + } + } + config.targets = unique; + if !valid_profile(&config.profile) { + return Err(Error("build.profile must be a Cargo profile name.".into())); + } + if !config + .tag_prefix + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) + { + return Err(Error( + "release.tag-prefix must contain only letters, digits, dots, underscores or hyphens." + .into(), + )); + } + if !valid_env(&config.token_env) { + return Err(Error( + "gitea.token-env must be an environment variable name.".into(), + )); + } + Ok(config) +} +fn valid_target(s: &str) -> bool { + s.split('-').count() >= 3 + && s.split('-').all(|part| { + !part.is_empty() && part.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') + }) +} +fn valid_profile(s: &str) -> bool { + s.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) + && s.bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_-".contains(&b)) +} +fn valid_env(s: &str) -> bool { + s.as_bytes() + .first() + .is_some_and(|b| b.is_ascii_alphabetic() || *b == b'_') + && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn overrides_and_validation() { + let config = parse("[build]\ntargets=['x86_64-unknown-linux-musl']\nlocked=true\n[release]\ntag-prefix='v'", Overrides { targets: vec!["aarch64-unknown-linux-musl".into()], unlocked: true, tag_prefix: Some(String::new()), ..Overrides::default() }).unwrap(); + assert_eq!(config.targets, ["aarch64-unknown-linux-musl"]); + assert!(!config.locked); + assert_eq!(config.tag_prefix, ""); + assert!(parse("[build]\ntargets=['bad']", Overrides::default()).is_err()); + assert!(parse("[package]\nformat='zip'", Overrides::default()).is_err()); + assert_eq!(load(None, Overrides::default()).unwrap().targets.len(), 3); + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..e99f64c --- /dev/null +++ b/src/error.rs @@ -0,0 +1,10 @@ +use std::fmt; + +#[derive(Debug)] +pub struct Error(pub String); + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } +} diff --git a/src/gitea.rs b/src/gitea.rs new file mode 100644 index 0000000..8caefd0 --- /dev/null +++ b/src/gitea.rs @@ -0,0 +1,481 @@ +use crate::error::Error; +use reqwest::{ + blocking::{ + multipart::{Form, Part}, + Client, + }, + header::{HeaderMap, HeaderValue, AUTHORIZATION}, + Method, StatusCode, +}; +use serde::Deserialize; +use serde_json::{json, Value}; +use std::{env, fs::File, path::Path, time::Duration}; + +#[derive(Debug, Deserialize)] +pub struct RemoteRelease { + pub id: u64, + #[serde(default)] + pub draft: bool, +} + +#[derive(Debug, Deserialize)] +struct Asset { + id: u64, + name: String, +} + +pub struct Gitea { + client: Client, + base: String, +} + +impl Gitea { + pub fn new(repository: &str, token_env: &str) -> Result { + if !repository.starts_with("https://") { + return Err(Error( + "Authenticated Gitea publishing requires HTTPS.".into(), + )); + } + let token = env::var(token_env) + .ok() + .filter(|s| !s.is_empty()) + .ok_or_else(|| Error(format!("Set {token_env} before publishing.")))?; + Self::with_token(repository, &token) + } + + fn with_token(repository: &str, token: &str) -> Result { + let parsed = url::Url::parse(repository) + .map_err(|_| Error("Invalid release repository URL.".into()))?; + let parts: Vec<_> = parsed + .path_segments() + .ok_or_else(|| Error("Repository URL must end in owner/repository.".into()))? + .filter(|part| !part.is_empty()) + .collect(); + if parts.len() < 2 { + return Err(Error("Repository URL must end in owner/repository.".into())); + } + let prefix = parts[..parts.len() - 2].join("/"); + let owner = parts[parts.len() - 2]; + let name = parts[parts.len() - 1]; + let origin = parsed.origin().ascii_serialization(); + let base = if prefix.is_empty() { + format!("{origin}/api/v1/repos/{owner}/{name}") + } else { + format!("{origin}/{prefix}/api/v1/repos/{owner}/{name}") + }; + let mut headers = HeaderMap::new(); + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("token {token}")) + .map_err(|_| Error("Invalid Gitea token value.".into()))?, + ); + let client = Client::builder() + .default_headers(headers) + .user_agent("rust_realease/0.1") + .timeout(Duration::from_secs(120)) + .redirect(reqwest::redirect::Policy::none()) + .build() + .map_err(|_| Error("Cannot initialize Gitea HTTP client.".into()))?; + Ok(Self { client, base }) + } + + fn request( + &self, + method: Method, + path: &str, + body: Option, + missing_ok: bool, + ) -> Result, Error> { + let url = format!("{}{path}", self.base); + let mut request = self.client.request(method.clone(), &url); + if let Some(body) = body { + request = request.json(&body); + } + let response = request + .send() + .map_err(|_| Error(format!("Gitea {method} {path}: connection failed.")))?; + if missing_ok && response.status() == StatusCode::NOT_FOUND { + return Ok(None); + } + if !response.status().is_success() { + return Err(Error(format!( + "Gitea {method} {path}: HTTP {}.", + response.status().as_u16() + ))); + } + let bytes = response + .bytes() + .map_err(|_| Error(format!("Gitea {method} {path}: response failed.")))?; + if bytes.is_empty() { + return Ok(Some(Value::Null)); + } + serde_json::from_slice(&bytes) + .map(Some) + .map_err(|_| Error(format!("Gitea {method} {path}: invalid JSON response."))) + } + + pub fn ensure_release( + &self, + tag: &str, + commit: &str, + draft: bool, + prerelease: bool, + ) -> Result { + let encoded = encode(tag); + let remote = self + .request(Method::GET, &format!("/tags/{encoded}"), None, true)? + .ok_or_else(|| { + Error(format!( + "Remote tag {tag:?} is missing. Push the tag explicitly before publishing." + )) + })?; + if remote.pointer("/commit/sha").and_then(Value::as_str) != Some(commit) { + return Err(Error( + "Remote tag commit differs from the checkout; refusing publication.".into(), + )); + } + let release = if let Some(existing) = self.request( + Method::GET, + &format!("/releases/tags/{encoded}"), + None, + true, + )? { + existing + } else { + self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease})), false)? + .ok_or_else(|| Error("Gitea release creation returned no response.".into()))? + }; + serde_json::from_value(release) + .map_err(|_| Error("Gitea release response is missing id or draft state.".into())) + } + + pub fn repository_private(&self) -> Result { + self.request(Method::GET, "", None, false)? + .and_then(|repository| repository.get("private").and_then(Value::as_bool)) + .ok_or_else(|| Error("Gitea repository response is missing visibility.".into())) + } + + pub fn download_client(&self) -> &Client { + &self.client + } + + fn assets(&self, release_id: u64) -> Result, Error> { + let response = self + .request( + Method::GET, + &format!("/releases/{release_id}/assets"), + None, + false, + )? + .ok_or_else(|| Error("Gitea assets response is empty.".into()))?; + serde_json::from_value(response) + .map_err(|_| Error("Gitea assets response is invalid.".into())) + } + + pub fn upload(&self, release_id: u64, paths: &[&Path], replace: bool) -> Result<(), Error> { + let existing = self.assets(release_id)?; + let names: Vec<_> = paths + .iter() + .map(|p| { + p.file_name() + .and_then(|s| s.to_str()) + .ok_or_else(|| Error("Invalid upload filename.".into())) + }) + .collect::>()?; + let collisions: Vec<_> = existing + .iter() + .filter(|asset| names.contains(&asset.name.as_str())) + .collect(); + if !collisions.is_empty() && !replace { + return Err(Error(format!("Release assets already exist: {}. Use --replace-assets to replace them explicitly.", collisions.iter().map(|a| a.name.as_str()).collect::>().join(", ")))); + } + for (path, name) in paths.iter().zip(names.iter()) { + for asset in collisions.iter().filter(|asset| asset.name == *name) { + self.request( + Method::DELETE, + &format!("/releases/{release_id}/assets/{}", asset.id), + None, + false, + )?; + } + let input = File::open(path) + .map_err(|e| Error(format!("cannot open asset {}: {e}", path.display())))?; + let size = input + .metadata() + .map_err(|e| Error(format!("cannot inspect asset {}: {e}", path.display())))? + .len(); + let part = Part::reader_with_length(input, size) + .file_name((*name).to_owned()) + .mime_str("application/octet-stream") + .map_err(|_| Error("Invalid upload MIME type.".into()))?; + let mut url = url::Url::parse(&format!("{}/releases/{release_id}/assets", self.base)) + .map_err(|_| Error("Invalid Gitea upload URL.".into()))?; + url.query_pairs_mut().append_pair("name", name); + let response = self + .client + .post(url) + .multipart(Form::new().part("attachment", part)) + .send() + .map_err(|_| { + Error(format!( + "Gitea POST /releases/{release_id}/assets: connection failed." + )) + })?; + if !response.status().is_success() { + return Err(Error(format!( + "Gitea POST /releases/{release_id}/assets: HTTP {}.", + response.status().as_u16() + ))); + } + println!("Uploaded {name}"); + } + Ok(()) + } +} + +fn encode(segment: &str) -> String { + url::form_urlencoded::byte_serialize(segment.as_bytes()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{ + io::{Read, Write}, + net::TcpListener, + thread, + time::Duration, + }; + + struct Reply { + method: &'static str, + path: &'static str, + status: u16, + body: &'static str, + } + + fn server(replies: Vec) -> (String, thread::JoinHandle>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = format!("http://{}", listener.local_addr().unwrap()); + let handle = thread::spawn(move || { + let mut requests = Vec::new(); + for reply in replies { + let (mut stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + let mut bytes = Vec::new(); + let mut buffer = [0_u8; 8192]; + let header_end = loop { + let n = stream.read(&mut buffer).unwrap(); + assert!(n > 0, "client closed before headers"); + bytes.extend_from_slice(&buffer[..n]); + if let Some(index) = bytes.windows(4).position(|w| w == b"\r\n\r\n") { + break index + 4; + } + assert!(bytes.len() < 64 * 1024); + }; + let headers = String::from_utf8_lossy(&bytes[..header_end]).to_string(); + assert!( + headers + .lines() + .any(|line| line.eq_ignore_ascii_case("authorization: token test-token")), + "authenticated Gitea request lacked the configured token" + ); + let first = headers.lines().next().unwrap().to_owned(); + assert!( + first.starts_with(&format!("{} {} ", reply.method, reply.path)), + "{first}" + ); + let length = headers + .lines() + .find_map(|line| { + line.to_ascii_lowercase() + .strip_prefix("content-length: ") + .and_then(|v| v.trim().parse::().ok()) + }) + .unwrap_or(0); + while bytes.len() < header_end + length { + let n = stream.read(&mut buffer).unwrap(); + assert!(n > 0, "client closed before body"); + bytes.extend_from_slice(&buffer[..n]); + } + requests.push(first); + let reason = if reply.status == 200 { + "OK" + } else if reply.status == 201 { + "Created" + } else if reply.status == 204 { + "No Content" + } else if reply.status == 404 { + "Not Found" + } else { + "Unauthorized" + }; + write!(stream, "HTTP/1.1 {} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", reply.status, reason, reply.body.len(), reply.body).unwrap(); + } + requests + }); + (address, handle) + } + + #[test] + fn creates_or_reuses_release_after_remote_tag_check() { + let base = "/gitea/api/v1/repos/team/demo"; + let (host, handle) = server(vec![ + Reply { + method: "GET", + path: "/gitea/api/v1/repos/team/demo/tags/v0.1.0", + status: 200, + body: r#"{"commit":{"sha":"abc"}}"#, + }, + Reply { + method: "GET", + path: "/gitea/api/v1/repos/team/demo/releases/tags/v0.1.0", + status: 404, + body: "{}", + }, + Reply { + method: "POST", + path: "/gitea/api/v1/repos/team/demo/releases", + status: 201, + body: r#"{"id":7,"draft":false}"#, + }, + ]); + let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap(); + let release = api.ensure_release("v0.1.0", "abc", false, true).unwrap(); + assert_eq!(release.id, 7); + assert_eq!(handle.join().unwrap().len(), 3); + + let (host, handle) = server(vec![ + Reply { + method: "GET", + path: "/gitea/api/v1/repos/team/demo/tags/v0.1.0", + status: 200, + body: r#"{"commit":{"sha":"abc"}}"#, + }, + Reply { + method: "GET", + path: "/gitea/api/v1/repos/team/demo/releases/tags/v0.1.0", + status: 200, + body: r#"{"id":9,"draft":true}"#, + }, + ]); + let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap(); + assert_eq!( + api.ensure_release("v0.1.0", "abc", false, false) + .unwrap() + .id, + 9 + ); + assert_eq!(handle.join().unwrap().len(), 2); + let _ = base; + } + + #[test] + fn collisions_refuse_or_replace_only_matching_asset() { + let temp = tempfile::tempdir().unwrap(); + let path = temp.path().join("one.tgz"); + std::fs::write(&path, b"archive").unwrap(); + let assets = r#"[{"id":3,"name":"one.tgz"},{"id":4,"name":"unrelated.tgz"}]"#; + let (host, handle) = server(vec![Reply { + method: "GET", + path: "/api/v1/repos/team/demo/releases/7/assets", + status: 200, + body: assets, + }]); + let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap(); + assert!(api + .upload(7, &[&path], false) + .unwrap_err() + .to_string() + .contains("--replace-assets")); + assert_eq!(handle.join().unwrap().len(), 1); + let (host, handle) = server(vec![ + Reply { + method: "GET", + path: "/api/v1/repos/team/demo/releases/7/assets", + status: 200, + body: assets, + }, + Reply { + method: "DELETE", + path: "/api/v1/repos/team/demo/releases/7/assets/3", + status: 204, + body: "", + }, + Reply { + method: "POST", + path: "/api/v1/repos/team/demo/releases/7/assets?name=one.tgz", + status: 201, + body: r#"{"id":5}"#, + }, + ]); + let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap(); + api.upload(7, &[&path], true).unwrap(); + assert_eq!(handle.join().unwrap().len(), 3); + } + + #[test] + fn api_errors_do_not_echo_server_body_or_token() { + let (host, handle) = server(vec![Reply { + method: "GET", + path: "/api/v1/repos/team/demo/tags/v0.1.0", + status: 401, + body: "test-token", + }]); + let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap(); + let error = api + .ensure_release("v0.1.0", "abc", false, false) + .unwrap_err() + .to_string(); + assert!(error.contains("HTTP 401")); + assert!(!error.contains("test-token")); + handle.join().unwrap(); + } + + #[test] + fn missing_or_mismatched_remote_tag_stops_before_release_mutation() { + for (status, body, expected) in [ + (404, "{}", "missing"), + (200, r#"{"commit":{"sha":"other"}}"#, "differs"), + ] { + let (host, handle) = server(vec![Reply { + method: "GET", + path: "/api/v1/repos/team/demo/tags/v0.1.0", + status, + body, + }]); + let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap(); + let error = api + .ensure_release("v0.1.0", "abc", false, false) + .unwrap_err() + .to_string(); + assert!(error.contains(expected), "{error}"); + assert_eq!(handle.join().unwrap().len(), 1); + } + } + + #[test] + fn reads_repository_visibility_before_private_verification() { + let (host, handle) = server(vec![Reply { + method: "GET", + path: "/api/v1/repos/team/demo", + status: 200, + body: r#"{"private":true}"#, + }]); + let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap(); + assert!(api.repository_private().unwrap()); + assert_eq!(handle.join().unwrap().len(), 1); + } + + #[test] + fn encodes_tag_and_requires_https() { + assert_eq!(encode("v1.0+build"), "v1.0%2Bbuild"); + assert!(Gitea::new("http://example.com/team/demo", "GITEA_TOKEN") + .err() + .unwrap() + .to_string() + .contains("HTTPS")); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9058296 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,25 @@ +//! Entry point for the incremental Rust replacement. + +mod build; +mod cli; +mod config; +mod error; +mod gitea; +mod manifest; +mod model; +mod package; +mod policy; +mod prepare; +mod project; +mod runner; +mod verify; + +use clap::Parser; + +fn main() { + let args = cli::Cli::parse(); + if let Err(error) = cli::run(args) { + eprintln!("error: {error}"); + std::process::exit(1); + } +} diff --git a/src/manifest.rs b/src/manifest.rs new file mode 100644 index 0000000..4e4bcf6 --- /dev/null +++ b/src/manifest.rs @@ -0,0 +1,173 @@ +use crate::{error::Error, policy}; +use std::{fs, path::Path}; +use toml_edit::{DocumentMut, Item, Table, Value}; + +#[derive(Debug, PartialEq, Eq)] +pub struct Change { + pub key: String, + pub actual: Option, + pub expected: Option, +} + +fn document(path: &Path) -> Result { + let source = fs::read_to_string(path) + .map_err(|e| Error(format!("cannot read {}: {e}", path.display())))?; + source + .parse::() + .map_err(|e| Error(format!("invalid {}: {e}", path.display()))) +} + +fn package(doc: &DocumentMut) -> Result<&Table, Error> { + doc.get("package") + .and_then(Item::as_table) + .ok_or_else(|| Error("Cargo.toml contains no [package] table.".into())) +} + +fn binstall(package: &Table) -> Result, Error> { + let Some(metadata) = package.get("metadata") else { + return Ok(None); + }; + let metadata = metadata + .as_table() + .ok_or_else(|| Error("package.metadata must be a table.".into()))?; + let Some(binstall) = metadata.get("binstall") else { + return Ok(None); + }; + binstall + .as_table() + .map(Some) + .ok_or_else(|| Error("package.metadata.binstall must be a table.".into())) +} + +pub fn changes(path: &Path, repository: &str, prefix: &str) -> Result, Error> { + let doc = document(path)?; + let package = package(&doc)?; + let mut result = Vec::new(); + let actual = package + .get("repository") + .and_then(Item::as_str) + .map(str::to_owned); + if actual.as_deref() != Some(repository) { + result.push(Change { + key: "package.repository".into(), + actual, + expected: Some(repository.into()), + }); + } + let metadata = binstall(package)?; + let expected = policy::binstall_metadata(prefix); + for (key, desired) in &expected { + let actual = metadata + .and_then(|table| table.get(key)) + .and_then(Item::as_str) + .map(str::to_owned); + if actual.as_deref() != Some(desired) { + result.push(Change { + key: format!("package.metadata.binstall.{key}"), + actual, + expected: Some(desired.clone()), + }); + } + } + if let Some(table) = metadata { + for (key, item) in table.iter() { + if !expected.iter().any(|(known, _)| key == *known) { + result.push(Change { + key: format!("package.metadata.binstall.{key}"), + actual: Some(item.to_string().trim().to_owned()), + expected: None, + }); + } + } + } + Ok(result) +} + +pub fn repair(path: &Path, changes: &[Change], write: bool) -> Result { + if path.is_symlink() { + return Err(Error("Refusing to repair a symlinked Cargo.toml.".into())); + } + let mut doc = document(path)?; + let package = doc["package"] + .as_table_mut() + .ok_or_else(|| Error("Cargo.toml contains no [package] table.".into()))?; + for change in changes { + if change.key == "package.repository" { + if let Some(desired) = &change.expected { + set_string(&mut package["repository"], desired); + } + continue; + } + let key = change + .key + .strip_prefix("package.metadata.binstall.") + .ok_or_else(|| Error("Invalid metadata change key.".into()))?; + if !package.contains_key("metadata") { + package["metadata"] = Item::Table(Table::new()); + } + let metadata = package["metadata"] + .as_table_mut() + .ok_or_else(|| Error("package.metadata must be a table.".into()))?; + if !metadata.contains_key("binstall") { + metadata["binstall"] = Item::Table(Table::new()); + } + let binstall = metadata["binstall"] + .as_table_mut() + .ok_or_else(|| Error("package.metadata.binstall must be a table.".into()))?; + if let Some(desired) = &change.expected { + set_string(&mut binstall[key], desired); + } else { + binstall.remove(key); + } + } + let rendered = doc.to_string(); + if write { + fs::write(path, &rendered) + .map_err(|e| Error(format!("cannot write {}: {e}", path.display())))?; + } + Ok(rendered) +} + +fn set_string(item: &mut Item, desired: &str) { + let mut replacement = Value::from(desired); + if let Some(previous) = item.as_value() { + *replacement.decor_mut() = previous.decor().clone(); + } + *item = Item::Value(replacement); +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn repair_preserves_comments_and_is_idempotent() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("Cargo.toml"); + fs::write(&path, "# keep this comment\n[package]\nname = 'demo'\nversion = '0.1.0'\nrepository = 'https://old.example/x/y' # keep inline\n[package.metadata.binstall]\npkg-fmt = 'zip'\ncustom = 'bad'\n").unwrap(); + let issues = changes(&path, "https://example.com/x/y", "release-").unwrap(); + assert_eq!(issues.len(), 5); + let before = fs::read_to_string(&path).unwrap(); + let preview = repair(&path, &issues, false).unwrap(); + assert_eq!(fs::read_to_string(&path).unwrap(), before); + assert!(preview.contains("# keep this comment")); + assert!(preview.contains("# keep inline")); + repair(&path, &issues, true).unwrap(); + assert!(changes(&path, "https://example.com/x/y", "release-") + .unwrap() + .is_empty()); + assert_eq!( + repair(&path, &[], true).unwrap(), + fs::read_to_string(&path).unwrap() + ); + } + #[test] + fn rejects_malformed_metadata_table() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("Cargo.toml"); + fs::write(&path, "[package]\nname='demo'\nmetadata='oops'\n").unwrap(); + assert!(changes(&path, "https://example.com/x/y", "v") + .unwrap_err() + .to_string() + .contains("package.metadata must be a table")); + } +} diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..20bcac9 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,19 @@ +use clap::ValueEnum; + +#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)] +pub enum Stage { + Inspect, + Metadata, + Verify, + Build, + Package, + Release, + Upload, + Binstall, +} + +impl Stage { + pub fn order(self) -> usize { + self as usize + } +} diff --git a/src/package.rs b/src/package.rs new file mode 100644 index 0000000..55cba20 --- /dev/null +++ b/src/package.rs @@ -0,0 +1,275 @@ +use crate::{error::Error, policy}; +use flate2::{Compression, GzBuilder}; +use sha2::{Digest, Sha256}; +use std::{ + fs::{self, File}, + io::{self, Read, Write}, + path::{Path, PathBuf}, +}; + +#[derive(Debug, Clone)] +pub struct Artifact { + pub target: String, + pub path: PathBuf, + pub root: String, + pub binary: String, + pub sha256: String, +} + +pub fn package( + root: &Path, + name: &str, + version: &str, + binary: &str, + outputs: &[(String, PathBuf)], +) -> Result<(Vec, PathBuf), Error> { + for (target, _) in outputs { + safe_component(&policy::archive_root(name, target, version))?; + safe_component(&policy::archive_name(name, target, version))?; + safe_component(&policy::binary_filename(binary, target))?; + } + let dist = root.join("dist"); + if dist.is_symlink() { + return Err(Error("Refusing a symlinked dist directory.".into())); + } + fs::create_dir_all(&dist) + .map_err(|e| Error(format!("cannot create {}: {e}", dist.display())))?; + let staging = tempfile::Builder::new() + .prefix("release-") + .tempdir_in(&dist) + .map_err(|e| Error(format!("cannot create release staging directory: {e}")))?; + let mut artifacts = Vec::new(); + for (target, source) in outputs { + let archive_root = policy::archive_root(name, target, version); + let filename = policy::archive_name(name, target, version); + let executable = policy::binary_filename(binary, target); + let path = staging.path().join(filename); + let metadata = fs::symlink_metadata(source) + .map_err(|e| Error(format!("cannot inspect binary {}: {e}", source.display())))?; + if !metadata.file_type().is_file() { + return Err(Error(format!( + "Build did not produce a regular binary: {}", + source.display() + ))); + } + let mode = if target.split('-').any(|part| part == "windows") { + 0o644 + } else { + unix_mode(&metadata) | 0o111 + }; + let output = File::create(&path) + .map_err(|e| Error(format!("cannot create archive {}: {e}", path.display())))?; + let mut gzip = GzBuilder::new() + .mtime(0) + .operating_system(255) + .write(output, Compression::default()); + write_header(&mut gzip, &archive_root, 0o755, 0, b'5')?; + write_header( + &mut gzip, + &format!("{archive_root}/{executable}"), + mode, + metadata.len(), + b'0', + )?; + let mut input = File::open(source) + .map_err(|e| Error(format!("cannot open binary {}: {e}", source.display())))?; + let copied = io::copy(&mut input, &mut gzip) + .map_err(|e| Error(format!("cannot archive {}: {e}", source.display())))?; + if copied != metadata.len() { + return Err(Error(format!( + "Binary changed while packaging: {}", + source.display() + ))); + } + let padding = (512 - metadata.len() % 512) % 512; + gzip.write_all(&vec![0; padding as usize]) + .map_err(|e| Error(format!("cannot pad archive: {e}")))?; + gzip.write_all(&[0; 1024]) + .map_err(|e| Error(format!("cannot finish tar archive: {e}")))?; + gzip.finish() + .map_err(|e| Error(format!("cannot finish gzip archive: {e}")))?; + let sha256 = checksum(&path)?; + artifacts.push(Artifact { + target: target.clone(), + path, + root: archive_root, + binary: executable, + sha256, + }); + } + let sums = staging.path().join("SHA256SUMS"); + artifacts.sort_by(|a, b| a.path.file_name().cmp(&b.path.file_name())); + let mut checksums = String::new(); + for artifact in &artifacts { + let filename = artifact + .path + .file_name() + .and_then(|s| s.to_str()) + .ok_or_else(|| Error("Invalid archive filename.".into()))?; + checksums.push_str(&format!("{} {filename}\n", artifact.sha256)); + } + fs::write(&sums, checksums) + .map_err(|e| Error(format!("cannot write {}: {e}", sums.display())))?; + let _ = staging.keep(); + Ok((artifacts, sums)) +} + +fn safe_component(component: &str) -> Result<(), Error> { + if component.is_empty() + || component == "." + || component == ".." + || component.contains('/') + || component.contains('\\') + || component.bytes().any(|b| b == 0 || b < 32) + { + return Err(Error(format!("Unsafe archive filename: {component:?}"))); + } + Ok(()) +} + +fn unix_mode(metadata: &fs::Metadata) -> u32 { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + metadata.permissions().mode() & 0o777 + } + #[cfg(not(unix))] + { + let _ = metadata; + 0o755 + } +} + +pub fn checksum(path: &Path) -> Result { + let mut input = + File::open(path).map_err(|e| Error(format!("cannot open {}: {e}", path.display())))?; + let mut hasher = Sha256::new(); + let mut buffer = [0_u8; 64 * 1024]; + loop { + let count = input + .read(&mut buffer) + .map_err(|e| Error(format!("cannot hash {}: {e}", path.display())))?; + if count == 0 { + break; + } + hasher.update(&buffer[..count]); + } + Ok(format!("{:x}", hasher.finalize())) +} + +fn write_header( + output: &mut W, + path: &str, + mode: u32, + size: u64, + kind: u8, +) -> Result<(), Error> { + let mut header = [0_u8; 512]; + let (name, prefix) = if path.len() <= 100 { + (path, "") + } else { + let (prefix, name) = path + .rsplit_once('/') + .ok_or_else(|| Error("Archive path exceeds USTAR name limit.".into()))?; + if name.len() > 100 || prefix.len() > 155 { + return Err(Error("Archive path exceeds USTAR limits.".into())); + } + (name, prefix) + }; + header[..name.len()].copy_from_slice(name.as_bytes()); + header[345..345 + prefix.len()].copy_from_slice(prefix.as_bytes()); + octal(&mut header[100..108], u64::from(mode))?; + octal(&mut header[108..116], 0)?; + octal(&mut header[116..124], 0)?; + octal(&mut header[124..136], size)?; + octal(&mut header[136..148], 0)?; + header[148..156].fill(b' '); + header[156] = kind; + header[257..263].copy_from_slice(b"ustar\0"); + header[263..265].copy_from_slice(b"00"); + let sum: u64 = header.iter().map(|byte| u64::from(*byte)).sum(); + let check = format!("{sum:06o}\0 "); + if check.len() != 8 { + return Err(Error("Tar header checksum overflow.".into())); + } + header[148..156].copy_from_slice(check.as_bytes()); + output + .write_all(&header) + .map_err(|e| Error(format!("cannot write tar header: {e}"))) +} + +fn octal(field: &mut [u8], number: u64) -> Result<(), Error> { + let rendered = format!("{:0width$o}\0", number, width = field.len() - 1); + if rendered.len() != field.len() { + return Err(Error("Tar field exceeds USTAR limit.".into())); + } + field.copy_from_slice(rendered.as_bytes()); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use flate2::read::GzDecoder; + #[test] + fn archive_contract_and_reproducibility() { + let temp = tempfile::tempdir().unwrap(); + let binary = temp.path().join("demo"); + fs::write(&binary, b"small executable").unwrap(); + let outputs = [ + ("x86_64-unknown-linux-musl".to_owned(), binary.clone()), + ("x86_64-pc-windows-gnu".to_owned(), binary), + ]; + let (first, sums) = package(temp.path(), "demo", "0.1.0", "demo", &outputs).unwrap(); + assert_eq!(first.len(), 2); + assert_eq!(fs::read_to_string(sums).unwrap().lines().count(), 2); + let (second, _) = package(temp.path(), "demo", "0.1.0", "demo", &outputs).unwrap(); + for (one, two) in first.iter().zip(second.iter()) { + assert_eq!(fs::read(&one.path).unwrap(), fs::read(&two.path).unwrap()); + assert_eq!(one.sha256, checksum(&one.path).unwrap()); + let mut tar = Vec::new(); + GzDecoder::new(File::open(&one.path).unwrap()) + .read_to_end(&mut tar) + .unwrap(); + assert_eq!(&tar[..one.root.len()], one.root.as_bytes()); + assert_eq!(tar[156], b'5'); + let file_header = &tar[512..1024]; + let expected = format!("{}/{}", one.root, one.binary); + assert_eq!(&file_header[..expected.len()], expected.as_bytes()); + assert_eq!(file_header[156], b'0'); + assert_eq!(&tar[1024..1024 + 16], b"small executable"); + let mode = std::str::from_utf8(&file_header[100..107]).unwrap(); + if one.target.contains("windows") { + assert_eq!(mode, "0000644"); + } else { + assert_eq!(mode, "0000755"); + } + } + } + #[test] + fn rejects_symlink_dist_and_source() { + let temp = tempfile::tempdir().unwrap(); + let binary = temp.path().join("demo"); + fs::write(&binary, "binary").unwrap(); + assert!(package( + temp.path(), + "../escape", + "0.1.0", + "demo", + &[("x86_64-unknown-linux-musl".into(), binary.clone())] + ) + .is_err()); + #[cfg(unix)] + { + std::os::unix::fs::symlink(temp.path(), temp.path().join("dist")).unwrap(); + assert!(package( + temp.path(), + "demo", + "0.1.0", + "demo", + &[("x86_64-unknown-linux-musl".into(), binary)] + ) + .is_err()); + } + } +} diff --git a/src/policy.rs b/src/policy.rs new file mode 100644 index 0000000..d44746e --- /dev/null +++ b/src/policy.rs @@ -0,0 +1,57 @@ +pub fn expected_tag(version: &str, prefix: &str) -> String { + format!("{prefix}{version}") +} + +pub fn archive_root(name: &str, target: &str, version: &str) -> String { + format!("{name}-{target}-v{version}") +} + +pub fn archive_name(name: &str, target: &str, version: &str) -> String { + format!("{}.tgz", archive_root(name, target, version)) +} + +pub fn binary_filename(binary: &str, target: &str) -> String { + if target.split('-').any(|part| part == "windows") { + format!("{binary}.exe") + } else { + binary.to_owned() + } +} + +pub fn binstall_metadata(prefix: &str) -> [(&'static str, String); 3] { + let root = archive_root("{ name }", "{ target }", "{ version }"); + [ + ( + "pkg-url", + format!("{{ repo }}/releases/download/{prefix}{{ version }}/{root}.tgz"), + ), + ("pkg-fmt", "tgz".to_owned()), + ("bin-dir", format!("{root}/{{ bin }}{{ binary-ext }}")), + ] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_match_python_contract() { + assert_eq!(expected_tag("0.3.1", ""), "0.3.1"); + assert_eq!(expected_tag("0.3.1", "release-"), "release-0.3.1"); + assert_eq!( + archive_name("demo", "x86_64-pc-windows-gnu", "0.3.1"), + "demo-x86_64-pc-windows-gnu-v0.3.1.tgz" + ); + assert_eq!(binary_filename("demo", "x86_64-pc-windows-gnu"), "demo.exe"); + assert_eq!(binary_filename("demo", "x86_64-unknown-linux-musl"), "demo"); + let metadata = binstall_metadata("release-"); + assert_eq!( + metadata[0].1, + "{ repo }/releases/download/release-{ version }/{ name }-{ target }-v{ version }.tgz" + ); + assert_eq!( + metadata[2].1, + "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }" + ); + } +} diff --git a/src/prepare.rs b/src/prepare.rs new file mode 100644 index 0000000..9c8e9d4 --- /dev/null +++ b/src/prepare.rs @@ -0,0 +1,727 @@ +use crate::{ + config::Config, + error::Error, + manifest, + project::{self, SelectedPackage}, + runner::Runner, +}; +use similar::TextDiff; +use std::{ + fs, + path::{Path, PathBuf}, +}; +use toml_edit::{value, Array, DocumentMut, Item, Table}; + +pub struct Options<'a> { + pub tag: &'a str, + pub image_tag: Option<&'a str>, + pub dry_run: bool, + pub commit: bool, + pub create_tag: bool, + pub push: bool, +} + +pub struct PlannedFile { + pub path: PathBuf, + pub old: String, + pub new: String, +} + +pub struct Plan { + pub files: Vec, + pub generate_lock: bool, +} + +pub fn plan( + root: &Path, + project: &SelectedPackage, + config: &Config, + repository: &str, + image_tag: Option<&str>, +) -> Result { + if let Some(image_tag) = image_tag { + let valid = image_tag + .as_bytes() + .first() + .is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'_') + && image_tag + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_.-".contains(&b)); + if !valid { + return Err(Error( + "--cross-image-tag must be a container image tag such as main or 0.2.5.".into(), + )); + } + } + let ignore = root.join(".gitignore"); + let cross = root.join("Cross.toml"); + let lock = root.join("Cargo.lock"); + for path in [&project.manifest, &ignore, &cross, &lock] { + if path.is_symlink() { + return Err(Error( + "Repository preparation refuses symlinked managed files.".into(), + )); + } + } + let changes = manifest::changes(&project.manifest, repository, &config.tag_prefix)?; + let manifest_old = fs::read_to_string(&project.manifest) + .map_err(|e| Error(format!("cannot read manifest: {e}")))?; + let manifest_new = manifest::repair(&project.manifest, &changes, false)?; + let ignore_old = read_optional(&ignore)?; + let mut ignore_new = ignore_old.clone(); + let missing: Vec<_> = ["/dist/", "/target/", "!/Cargo.lock"] + .into_iter() + .filter(|entry| !ignore_old.lines().any(|line| line == *entry)) + .collect(); + if !missing.is_empty() { + ignore_new = format!( + "{}\n\n# Release publisher\n{}\n", + ignore_old.trim_end_matches('\n'), + missing.join("\n") + ); + } + let cross_old = read_optional(&cross)?; + let mut cross_doc: DocumentMut = cross_old + .parse() + .map_err(|e| Error(format!("invalid Cross.toml: {e}")))?; + ensure_table(cross_doc.as_table_mut(), "build")?; + let build = cross_doc["build"] + .as_table_mut() + .ok_or_else(|| Error("Cross.toml 'build' must be a table.".into()))?; + ensure_table(build, "env")?; + let env = build["env"] + .as_table_mut() + .ok_or_else(|| Error("Cross.toml 'env' must be a table.".into()))?; + let mut passthrough = Array::new(); + if let Some(existing) = env.get("passthrough") { + let array = existing.as_array().ok_or_else(|| { + Error("Cross.toml build.env.passthrough must be a list of strings.".into()) + })?; + for item in array.iter() { + let item = item.as_str().ok_or_else(|| { + Error("Cross.toml build.env.passthrough must be a list of strings.".into()) + })?; + passthrough.push(item); + } + } + if !passthrough + .iter() + .any(|item| item.as_str() == Some("RUSTUP_TOOLCHAIN")) + { + passthrough.push("RUSTUP_TOOLCHAIN"); + } + env["passthrough"] = value(passthrough); + if let Some(image_tag) = image_tag { + ensure_table(cross_doc.as_table_mut(), "target")?; + let targets = cross_doc["target"] + .as_table_mut() + .ok_or_else(|| Error("Cross.toml 'target' must be a table.".into()))?; + for triple in &config.targets { + ensure_table(targets, triple)?; + let target = targets[triple] + .as_table_mut() + .ok_or_else(|| Error(format!("Cross.toml target {triple:?} must be a table.")))?; + target["image"] = value(format!("ghcr.io/cross-rs/{triple}:{image_tag}")); + } + } + let cross_new = cross_doc.to_string(); + let files = [ + (project.manifest.clone(), manifest_old, manifest_new), + (ignore, ignore_old, ignore_new), + (cross, cross_old, cross_new), + ] + .into_iter() + .filter(|(_, old, new)| old != new) + .map(|(path, old, new)| PlannedFile { path, old, new }) + .collect(); + Ok(Plan { + files, + generate_lock: !lock.exists(), + }) +} + +fn read_optional(path: &Path) -> Result { + if !path.exists() { + return Ok(String::new()); + } + fs::read_to_string(path).map_err(|e| Error(format!("cannot read {}: {e}", path.display()))) +} + +fn ensure_table(parent: &mut Table, key: &str) -> Result<(), Error> { + if !parent.contains_key(key) { + parent[key] = Item::Table(Table::new()); + } + if !parent[key].is_table() { + return Err(Error(format!("Cross.toml {key:?} must be a table."))); + } + Ok(()) +} + +pub fn run( + root: &Path, + project: &SelectedPackage, + config: &Config, + repository: &str, + options: Options<'_>, + runner: &Runner, +) -> Result<(), Error> { + if options.push && !(options.commit && options.create_tag) { + return Err(Error( + "--push-preparation requires --commit-preparation and --create-tag.".into(), + )); + } + if options.create_tag && !options.commit { + return Err(Error("--create-tag requires --commit-preparation.".into())); + } + let plan = plan(root, project, config, repository, options.image_tag)?; + let lock = root.join("Cargo.lock"); + let mut branch = None; + if options.commit { + let status = runner.text( + "git", + &["status", "--porcelain", "--untracked-files=normal"], + root, + )?; + if !status.is_empty() { + return Err(Error("Start --commit-preparation from a clean checkout, including the index. Review and commit earlier local edits separately.".into())); + } + if options.create_tag { + let exists = !runner + .text("git", &["tag", "--list", options.tag], root)? + .is_empty(); + let managed = [ + &project.manifest, + &root.join(".gitignore"), + &root.join("Cross.toml"), + &lock, + ]; + let mut untracked_managed = false; + for path in managed { + if path.exists() + && runner + .text( + "git", + &[ + "ls-files", + "--", + path.to_str() + .ok_or_else(|| Error("Managed path is not UTF-8.".into()))?, + ], + root, + )? + .is_empty() + { + untracked_managed = true; + } + } + if exists && (!plan.files.is_empty() || plan.generate_lock || untracked_managed) { + return Err(Error(format!("Tag {:?} already exists; preparation will not move it. Use a new package version or prepare without --create-tag.", options.tag))); + } + if exists { + project::validate_checkout_tag(root, options.tag, runner)?; + } + } + if options.push { + branch = Some( + runner + .text("git", &["symbolic-ref", "--quiet", "--short", "HEAD"], root) + .map_err(|_| { + Error("Pushing preparation requires a checked-out branch.".into()) + })?, + ); + let destination = + runner.text("git", &["remote", "get-url", "--push", "origin"], root)?; + if project::normalize_repository(&destination)? != repository { + return Err(Error("origin push URL differs from the release repository. Fix origin explicitly before pushing.".into())); + } + } + } + for file in &plan.files { + let label = file + .path + .strip_prefix(root) + .unwrap_or(&file.path) + .display() + .to_string(); + let diff = TextDiff::from_lines(&file.old, &file.new) + .unified_diff() + .header(&label, &label) + .to_string(); + print!("{}", runner.redact(&diff)); + } + if plan.generate_lock { + println!( + "Cargo.lock: generate with cargo generate-lockfile (may download registry metadata)." + ); + } + if options.commit { + println!( + "Git: commit only the prepared files using the configured author and signing settings." + ); + } + if options.create_tag { + println!( + "Git: create or validate annotated tag {}. Existing tags are never moved.", + options.tag + ); + } + if let Some(branch) = &branch { + println!( + "Git: atomically push branch {branch} and tag {} to origin; no force push.", + options.tag + ); + } + if options.dry_run { + println!("Preparation dry run complete; no files, commits, tags, or remote refs changed."); + return Ok(()); + } + for file in &plan.files { + fs::write(&file.path, &file.new) + .map_err(|e| Error(format!("cannot write {}: {e}", file.path.display())))?; + } + if plan.generate_lock { + runner.output("cargo", &["generate-lockfile"], root)?; + } + if options.commit { + let mut paths = [ + project.manifest.clone(), + root.join(".gitignore"), + root.join("Cross.toml"), + lock, + ]; + paths.sort(); + let names: Vec<&str> = paths + .iter() + .map(|p| { + p.to_str() + .ok_or_else(|| Error("Managed path is not UTF-8.".into())) + }) + .collect::>()?; + let args: Vec<&str> = std::iter::once("add") + .chain(["--force", "--"]) + .chain(names) + .collect(); + runner.output("git", &args, root)?; + if !runner + .text("git", &["diff", "--cached", "--name-only"], root)? + .is_empty() + { + let message = format!( + "Prepare {} {} binary release", + project.name, project.version + ); + runner.output( + "git", + &["-c", "core.hooksPath=/dev/null", "commit", "-m", &message], + root, + )?; + } + } + if options.create_tag { + if runner + .text("git", &["tag", "--list", options.tag], root)? + .is_empty() + { + runner.output( + "git", + &[ + "tag", + "-a", + options.tag, + "-m", + &format!("{} {}", project.name, project.version), + ], + root, + )?; + } + project::validate_checkout_tag(root, options.tag, runner)?; + } + if let Some(branch) = branch { + push_preparation(root, &branch, options.tag, repository, config, runner)?; + } + println!("Repository preparation complete. Review the changes, then run the publisher without --prepare-repo."); + Ok(()) +} + +fn push_preparation( + root: &Path, + branch: &str, + tag: &str, + repository: &str, + config: &Config, + runner: &Runner, +) -> Result<(), Error> { + let origin = runner.text("git", &["remote", "get-url", "--push", "origin"], root)?; + if project::normalize_repository(&origin)? != repository { + return Err(Error("origin push URL differs from the release repository. Fix origin explicitly before pushing.".into())); + } + let branch_ref = format!("HEAD:refs/heads/{branch}"); + let tag_ref = format!("refs/tags/{tag}"); + if origin.starts_with("https://") { + if let Ok(token) = std::env::var(&config.token_env) { + if !token.is_empty() { + let helper = tempfile::Builder::new() + .prefix("rust-release-askpass-") + .tempdir() + .map_err(|e| Error(format!("cannot create askpass helper: {e}")))?; + let script = helper.path().join("askpass"); + fs::write(&script, "#!/bin/sh\ncase \"$1\" in *sername*) printf 'token\\n';; *) printf '%s\\n' \"$PUBLISHER_GIT_TOKEN\";; esac\n").map_err(|e| Error(format!("cannot write askpass helper: {e}")))?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&script, fs::Permissions::from_mode(0o700)) + .map_err(|e| Error(format!("cannot chmod askpass helper: {e}")))?; + } + let helper_path = script + .to_str() + .ok_or_else(|| Error("Askpass helper path is not UTF-8.".into()))?; + return runner + .output_with( + "git", + &[ + "-c", + "credential.helper=", + "-c", + "core.hooksPath=/dev/null", + "push", + "--atomic", + "origin", + &branch_ref, + &tag_ref, + ], + root, + &[ + ("GIT_ASKPASS", helper_path), + ("GIT_TERMINAL_PROMPT", "0"), + ("PUBLISHER_GIT_TOKEN", &token), + ("GIT_TRACE", "0"), + ("GIT_TRACE_CURL", "0"), + ("GIT_TRACE2", "0"), + ], + &[], + ) + .map(|_| ()); + } + } + } + runner.output_with( + "git", + &[ + "-c", + "core.hooksPath=/dev/null", + "push", + "--atomic", + "origin", + &branch_ref, + &tag_ref, + ], + root, + &[("GIT_TERMINAL_PROMPT", "0")], + &[], + )?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fixture() -> (tempfile::TempDir, SelectedPackage, Config, Runner) { + let temp = tempfile::tempdir().unwrap(); + let root = temp.path(); + fs::create_dir(root.join("src")).unwrap(); + fs::write(root.join("src/main.rs"), "fn main() {}\n").unwrap(); + fs::write(root.join("Cargo.toml"), "# Keep this comment\n[package]\nname='demo'\nversion='0.1.0'\nedition='2021'\nrepository='https://example.org/team/demo'\n").unwrap(); + fs::write(root.join(".gitignore"), "Cargo.lock\n").unwrap(); + let runner = Runner::new("GITEA_TOKEN"); + runner + .output("git", &["init", "-q", "-b", "main"], root) + .unwrap(); + for (key, val) in [ + ("user.name", "Test"), + ("user.email", "test@example.invalid"), + ("commit.gpgsign", "false"), + ("tag.gpgsign", "false"), + ] { + runner.output("git", &["config", key, val], root).unwrap(); + } + runner + .output( + "git", + &["add", "Cargo.toml", ".gitignore", "src/main.rs"], + root, + ) + .unwrap(); + runner + .output("git", &["commit", "-qm", "initial"], root) + .unwrap(); + let project = project::inspect(root.to_str().unwrap(), None, &runner).unwrap(); + let config = Config { + targets: vec!["x86_64-unknown-linux-musl".into()], + profile: "release".into(), + locked: true, + tag_prefix: "v".into(), + token_env: "GITEA_TOKEN".into(), + }; + (temp, project, config, runner) + } + + fn options<'a>(dry_run: bool, commit: bool, create_tag: bool) -> Options<'a> { + Options { + tag: "v0.1.0", + image_tag: Some("main"), + dry_run, + commit, + create_tag, + push: false, + } + } + + #[test] + fn preview_then_signed_policy_commit_and_annotated_tag() { + let (temp, project, config, runner) = fixture(); + let root = temp.path(); + let before = fs::read_to_string(root.join("Cargo.toml")).unwrap(); + let head = runner.text("git", &["rev-parse", "HEAD"], root).unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let hook = root.join(".git/hooks/pre-commit"); + fs::write(&hook, "#!/bin/sh\ntouch hook-ran\n").unwrap(); + fs::set_permissions(&hook, fs::Permissions::from_mode(0o755)).unwrap(); + } + run( + root, + &project, + &config, + "https://example.org/team/demo", + options(true, true, true), + &runner, + ) + .unwrap(); + assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before); + assert!(!root.join("Cargo.lock").exists()); + assert!(!root.join("Cross.toml").exists()); + assert_eq!( + runner.text("git", &["rev-parse", "HEAD"], root).unwrap(), + head + ); + run( + root, + &project, + &config, + "https://example.org/team/demo", + options(false, true, true), + &runner, + ) + .unwrap(); + let prepared = runner.text("git", &["rev-parse", "HEAD"], root).unwrap(); + assert_ne!(prepared, head); + assert_eq!( + runner + .text("git", &["rev-parse", "v0.1.0^{commit}"], root) + .unwrap(), + prepared + ); + assert_eq!( + runner + .text("git", &["cat-file", "-t", "v0.1.0"], root) + .unwrap(), + "tag" + ); + assert!(runner + .text("git", &["status", "--porcelain"], root) + .unwrap() + .is_empty()); + assert!(fs::read_to_string(root.join("Cargo.toml")) + .unwrap() + .contains("# Keep this comment")); + assert!(root.join("Cargo.lock").exists()); + assert!(!root.join("hook-ran").exists()); + run( + root, + &project, + &config, + "https://example.org/team/demo", + options(false, true, true), + &runner, + ) + .unwrap(); + assert_eq!( + runner.text("git", &["rev-parse", "HEAD"], root).unwrap(), + prepared + ); + } + + #[test] + fn rejects_dirty_inputs_and_existing_tag_before_writes() { + let (temp, project, config, runner) = fixture(); + let root = temp.path(); + fs::write(root.join("unrelated.txt"), "work").unwrap(); + assert!(run( + root, + &project, + &config, + "https://example.org/team/demo", + options(false, true, false), + &runner + ) + .unwrap_err() + .to_string() + .contains("clean checkout")); + assert!(!root.join("Cross.toml").exists()); + fs::remove_file(root.join("unrelated.txt")).unwrap(); + runner.output("git", &["tag", "v0.1.0"], root).unwrap(); + assert!(run( + root, + &project, + &config, + "https://example.org/team/demo", + options(false, true, true), + &runner + ) + .unwrap_err() + .to_string() + .contains("will not move")); + assert!(!root.join("Cross.toml").exists()); + } + + #[test] + fn preserves_custom_cross_settings_and_rejects_bad_image_tag() { + let (temp, project, config, _runner) = fixture(); + let root = temp.path(); + fs::write(root.join("Cross.toml"), "# custom\n[build.env]\npassthrough=['MY_VAR']\n[target.x86_64-unknown-linux-musl]\nimage='my/custom:image'\n").unwrap(); + let planned = plan( + root, + &project, + &config, + "https://example.org/team/demo", + None, + ) + .unwrap(); + let cross = planned + .files + .iter() + .find(|f| f.path.ends_with("Cross.toml")) + .unwrap(); + assert!(cross.new.contains("# custom")); + assert!(cross.new.contains("my/custom:image")); + assert!(cross.new.contains("RUSTUP_TOOLCHAIN")); + assert!(plan( + root, + &project, + &config, + "https://example.org/team/demo", + Some("bad/tag") + ) + .is_err()); + } + + #[test] + fn push_destination_mismatch_is_caught_before_writes() { + let (temp, project, config, runner) = fixture(); + let root = temp.path(); + runner + .output( + "git", + &[ + "remote", + "add", + "origin", + "https://other.invalid/team/demo.git", + ], + root, + ) + .unwrap(); + let before = fs::read_to_string(root.join("Cargo.toml")).unwrap(); + let options = Options { + tag: "v0.1.0", + image_tag: None, + dry_run: true, + commit: true, + create_tag: true, + push: true, + }; + assert!(run( + root, + &project, + &config, + "https://example.org/team/demo", + options, + &runner + ) + .unwrap_err() + .to_string() + .contains("differs")); + assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before); + assert!(!root.join("Cross.toml").exists()); + } + + #[cfg(unix)] + #[test] + fn authorized_preparation_pushes_branch_and_tag_atomically() { + use std::os::unix::fs::PermissionsExt; + + let (temp, project, config, runner) = fixture(); + let root = temp.path(); + let transport = tempfile::tempdir().unwrap(); + let bare = transport.path().join("remote.git"); + fs::create_dir(&bare).unwrap(); + runner + .output("git", &["init", "--bare", "-q"], &bare) + .unwrap(); + let ssh = transport.path().join("local-ssh"); + fs::write( + &ssh, + format!("#!/bin/sh\nexec git-receive-pack '{}'\n", bare.display()), + ) + .unwrap(); + fs::set_permissions(&ssh, fs::Permissions::from_mode(0o700)).unwrap(); + runner + .output( + "git", + &["config", "core.sshCommand", ssh.to_str().unwrap()], + root, + ) + .unwrap(); + runner + .output( + "git", + &[ + "remote", + "add", + "origin", + "ssh://git@example.org/team/demo.git", + ], + root, + ) + .unwrap(); + run( + root, + &project, + &config, + "https://example.org/team/demo", + Options { + tag: "v0.1.0", + image_tag: None, + dry_run: false, + commit: true, + create_tag: true, + push: true, + }, + &runner, + ) + .unwrap(); + let local = runner.text("git", &["rev-parse", "HEAD"], root).unwrap(); + let remote = runner + .text("git", &["rev-parse", "refs/heads/main"], &bare) + .unwrap(); + assert_eq!(local, remote); + assert_eq!( + runner + .text("git", &["rev-parse", "refs/tags/v0.1.0^{commit}"], &bare) + .unwrap(), + local + ); + } +} diff --git a/src/project.rs b/src/project.rs new file mode 100644 index 0000000..8313969 --- /dev/null +++ b/src/project.rs @@ -0,0 +1,501 @@ +use crate::{error::Error, runner::Runner}; +use serde::Deserialize; +use std::{ + fs, + path::{Path, PathBuf}, +}; + +#[derive(Debug)] +pub struct SelectedPackage { + pub name: String, + pub version: String, + pub binary: String, + pub manifest: PathBuf, + pub repository: Option, + pub target_directory: PathBuf, +} + +pub struct SourceCheckout { + pub root: PathBuf, + _temporary: Option, +} + +pub fn acquire_project( + source: &str, + tag: Option<&str>, + keep_workdir: bool, + runner: &Runner, +) -> Result { + let expanded = if let Some(suffix) = source.strip_prefix("~/") { + std::env::var_os("HOME") + .map(|home| PathBuf::from(home).join(suffix)) + .unwrap_or_else(|| PathBuf::from(source)) + } else { + PathBuf::from(source) + }; + if expanded.exists() { + if !expanded.is_dir() { + return Err(Error("Local source must be a directory.".into())); + } + let root = expanded + .canonicalize() + .map_err(|e| Error(format!("cannot resolve local source: {e}")))?; + if let Some(tag) = tag { + validate_checkout_tag(&root, tag, runner)?; + } + return Ok(SourceCheckout { + root, + _temporary: None, + }); + } + normalize_repository(source)?; + let temporary = tempfile::Builder::new() + .prefix("rust-realease-") + .tempdir() + .map_err(|e| Error(format!("cannot create temporary checkout: {e}")))?; + let root = temporary.path().join("source"); + clone_to(source, &root, tag, runner)?; + if keep_workdir { + let retained = temporary.keep(); + println!("Temporary workdir kept at {}", retained.display()); + Ok(SourceCheckout { + root, + _temporary: None, + }) + } else { + Ok(SourceCheckout { + root, + _temporary: Some(temporary), + }) + } +} + +fn clone_to(source: &str, root: &Path, tag: Option<&str>, runner: &Runner) -> Result<(), Error> { + let parent = root + .parent() + .ok_or_else(|| Error("Invalid clone destination.".into()))?; + let destination = root + .to_str() + .ok_or_else(|| Error("Clone destination is not UTF-8.".into()))?; + runner.output("git", &["clone", "--", source, destination], parent)?; + if let Some(tag) = tag { + runner.output( + "git", + &["checkout", "--detach", &format!("refs/tags/{tag}")], + root, + )?; + validate_checkout_tag(root, tag, runner)?; + } + Ok(()) +} +#[derive(Deserialize)] +struct Metadata { + packages: Vec, + workspace_members: Vec, + workspace_root: PathBuf, + target_directory: PathBuf, +} +#[derive(Deserialize)] +struct Package { + id: String, + name: String, + version: String, + manifest_path: PathBuf, + repository: Option, + targets: Vec, +} +#[derive(Deserialize)] +struct Target { + name: String, + kind: Vec, +} + +pub fn inspect( + source: &str, + package_name: Option<&str>, + runner: &Runner, +) -> Result { + let root = Path::new(source); + if !root.is_dir() { + return Err(Error( + "Rust inspect currently requires a local project directory.".into(), + )); + } + let manifest = root.join("Cargo.toml"); + let source_text = fs::read_to_string(&manifest) + .map_err(|e| Error(format!("cannot read {}: {e}", manifest.display())))?; + let source_doc: toml::Value = toml::from_str(&source_text) + .map_err(|e| Error(format!("invalid {}: {e}", manifest.display())))?; + let output = runner.output( + "cargo", + &["metadata", "--format-version=1", "--no-deps"], + root, + )?; + let metadata: Metadata = serde_json::from_slice(&output) + .map_err(|e| Error(format!("invalid cargo metadata output: {e}")))?; + let members: Vec<_> = metadata + .packages + .into_iter() + .filter(|p| metadata.workspace_members.contains(&p.id)) + .collect(); + let discovered = members + .iter() + .map(|p| p.name.as_str()) + .collect::>() + .join(", "); + let selected = match package_name { + Some(name) => members + .into_iter() + .find(|p| p.name == name) + .ok_or_else(|| { + Error(format!( + "Workspace package {name:?} was not found uniquely; discovered: {discovered}" + )) + })?, + None if source_doc + .get("package") + .and_then(toml::Value::as_table) + .is_some() + && members.len() == 1 => + { + members + .into_iter() + .next() + .ok_or_else(|| Error("No workspace package found.".into()))? + } + None => { + return Err(Error(format!( + "Workspace selection requires --package NAME; discovered: {discovered}" + ))) + } + }; + let binaries: Vec<_> = selected + .targets + .iter() + .filter(|t| t.kind.iter().any(|kind| kind == "bin")) + .collect(); + if binaries.len() != 1 { + return Err(Error(format!( + "Package {:?} must have exactly one binary; discovered: {}", + selected.name, + binaries + .iter() + .map(|t| t.name.as_str()) + .collect::>() + .join(", ") + ))); + } + for value in [&selected.name, &selected.version, &binaries[0].name] { + if !safe_component(value) { + return Err(Error("Unsafe package, version or binary filename.".into())); + } + } + let workspace_root = metadata + .workspace_root + .canonicalize() + .map_err(|e| Error(format!("invalid workspace root: {e}")))?; + let member_manifest = selected + .manifest_path + .canonicalize() + .map_err(|e| Error(format!("invalid member manifest: {e}")))?; + if !member_manifest.starts_with(&workspace_root) { + return Err(Error( + "Selected package manifest is outside the workspace root.".into(), + )); + } + let member_text = fs::read_to_string(&member_manifest) + .map_err(|e| Error(format!("cannot read {}: {e}", member_manifest.display())))?; + let member_doc: toml::Value = toml::from_str(&member_text) + .map_err(|e| Error(format!("invalid {}: {e}", member_manifest.display())))?; + if member_doc + .get("package") + .and_then(toml::Value::as_table) + .is_none() + { + return Err(Error( + "Selected Cargo.toml contains no [package] table.".into(), + )); + } + Ok(SelectedPackage { + name: selected.name, + version: selected.version, + binary: binaries[0].name.clone(), + manifest: member_manifest, + repository: selected.repository, + target_directory: metadata.target_directory, + }) +} +fn safe_component(value: &str) -> bool { + value + .as_bytes() + .first() + .is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'_') + && value + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_.+-".contains(&b)) +} + +pub fn origin_url(root: &Path, runner: &Runner) -> Option { + runner + .text("git", &["remote", "get-url", "origin"], root) + .ok() +} + +pub fn head_commit(root: &Path, runner: &Runner) -> Result { + runner.text("git", &["rev-parse", "HEAD"], root) +} + +pub fn validate_checkout_tag(root: &Path, tag: &str, runner: &Runner) -> Result<(), Error> { + let tagged = runner + .text( + "git", + &[ + "rev-parse", + "--verify", + &format!("refs/tags/{tag}^{{commit}}"), + ], + root, + ) + .map_err(|_| { + Error(format!( + "Git tag {tag:?} does not exist. Create it explicitly first." + )) + })?; + let head = runner.text("git", &["rev-parse", "HEAD"], root)?; + if head != tagged { + return Err(Error(format!( + "Git tag {tag:?} does not point to this checkout. Check it out explicitly." + ))); + } + Ok(()) +} + +pub fn normalize_repository(input: &str) -> Result { + let candidate = if let Some((user_host, path)) = input.split_once(':') { + if !input.contains("://") && user_host.contains('@') { + let (user, host) = user_host.split_once('@').unwrap_or(("", "")); + if user.is_empty() || host.is_empty() || path.is_empty() { + return Err(Error( + "Repository must be an HTTP(S) or SSH Git URL; use --repository-url.".into(), + )); + } + format!("https://{host}/{path}") + } else { + input.to_owned() + } + } else { + input.to_owned() + }; + let parsed = url::Url::parse(&candidate).map_err(|_| { + Error("Repository must be an HTTP(S) or SSH Git URL; use --repository-url.".into()) + })?; + if !matches!(parsed.scheme(), "http" | "https" | "ssh") || parsed.host_str().is_none() { + return Err(Error( + "Repository must be an HTTP(S) or SSH Git URL; use --repository-url.".into(), + )); + } + if parsed.password().is_some() + || (parsed.scheme() != "ssh" && !parsed.username().is_empty()) + || parsed.query().is_some() + || parsed.fragment().is_some() + { + return Err(Error( + "Repository URLs must not contain credentials, query strings or fragments.".into(), + )); + } + let parts: Vec<_> = parsed + .path_segments() + .ok_or_else(|| Error("Repository URL must end in owner/repository.".into()))? + .filter(|part| !part.is_empty()) + .collect(); + if parts.len() < 2 + || parts.iter().any(|part| { + *part == "." + || *part == ".." + || !part + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b"_.-".contains(&b)) + }) + { + return Err(Error("Repository URL must end in owner/repository.".into())); + } + let name = parts[parts.len() - 1] + .strip_suffix(".git") + .unwrap_or(parts[parts.len() - 1]); + if name.is_empty() || name == "." || name == ".." { + return Err(Error("Repository name is empty.".into())); + } + let host = parsed.host_str().unwrap_or_default(); + let authority = if let Some(port) = parsed.port() { + format!("{host}:{port}") + } else { + host.to_owned() + }; + let scheme = if parsed.scheme() == "ssh" { + "https" + } else { + parsed.scheme() + }; + Ok(format!( + "{scheme}://{authority}/{}{}{}", + parts[..parts.len() - 1].join("/"), + "/", + name + )) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn write(root: &Path, path: &str, content: &str) { + let path = root.join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, content).unwrap(); + } + + #[test] + fn standalone_project_and_workspace_selection() { + let temp = tempfile::tempdir().unwrap(); + write( + temp.path(), + "Cargo.toml", + "[package]\nname='solo'\nversion='0.1.0'\nedition='2021'\n", + ); + write(temp.path(), "src/main.rs", "fn main() {}\n"); + let selected = inspect( + temp.path().to_str().unwrap(), + None, + &Runner::new("GITEA_TOKEN"), + ) + .unwrap(); + assert_eq!(selected.name, "solo"); + assert_eq!(selected.binary, "solo"); + + let workspace = tempfile::tempdir().unwrap(); + write( + workspace.path(), + "Cargo.toml", + "[workspace]\nmembers=['one','two']\nresolver='2'\n", + ); + for name in ["one", "two"] { + write( + workspace.path(), + &format!("{name}/Cargo.toml"), + &format!("[package]\nname='{name}'\nversion='0.2.0'\nedition='2021'\n"), + ); + write( + workspace.path(), + &format!("{name}/src/main.rs"), + "fn main() {}\n", + ); + } + let root = workspace.path().to_str().unwrap(); + assert!(inspect(root, None, &Runner::new("GITEA_TOKEN")) + .unwrap_err() + .to_string() + .contains("--package")); + assert!(inspect(root, Some("missing"), &Runner::new("GITEA_TOKEN")) + .unwrap_err() + .to_string() + .contains("not found")); + assert_eq!( + inspect(root, Some("two"), &Runner::new("GITEA_TOKEN")) + .unwrap() + .binary, + "two" + ); + } + + #[test] + fn rejects_library_and_multiple_binaries() { + let temp = tempfile::tempdir().unwrap(); + write( + temp.path(), + "Cargo.toml", + "[package]\nname='demo'\nversion='0.1.0'\nedition='2021'\n", + ); + write(temp.path(), "src/lib.rs", "pub fn hello() {}\n"); + let root = temp.path().to_str().unwrap(); + assert!(inspect(root, None, &Runner::new("GITEA_TOKEN")) + .unwrap_err() + .to_string() + .contains("exactly one binary")); + write(temp.path(), "src/main.rs", "fn main() {}\n"); + write(temp.path(), "src/bin/extra.rs", "fn main() {}\n"); + assert!(inspect(root, None, &Runner::new("GITEA_TOKEN")) + .unwrap_err() + .to_string() + .contains("exactly one binary")); + } + + #[test] + fn repository_url_normalization_rejects_credentials() { + assert_eq!( + normalize_repository("git@example.com:owner/demo.git").unwrap(), + "https://example.com/owner/demo" + ); + assert_eq!( + normalize_repository("ssh://git@example.com/owner/demo.git").unwrap(), + "https://example.com/owner/demo" + ); + assert_eq!( + normalize_repository("https://example.com/gitea/owner/demo.git").unwrap(), + "https://example.com/gitea/owner/demo" + ); + for value in [ + "https://token@example.com/owner/demo", + "https://example.com/owner/demo?token=secret", + "https://example.com/owner/../demo", + ] { + assert!(normalize_repository(value).is_err(), "{value}"); + } + } + + #[test] + fn local_source_is_retained_and_clone_checks_out_exact_tag() { + let temp = tempfile::tempdir().unwrap(); + let source = temp.path().join("origin"); + fs::create_dir(&source).unwrap(); + let runner = Runner::new("GITEA_TOKEN"); + runner.output("git", &["init", "-q"], &source).unwrap(); + fs::write(source.join("version.txt"), "one").unwrap(); + runner + .output("git", &["add", "version.txt"], &source) + .unwrap(); + let commit_args = [ + "-c", + "user.name=Test", + "-c", + "user.email=test@example.invalid", + "-c", + "commit.gpgsign=false", + "commit", + "-qm", + "first", + ]; + runner.output("git", &commit_args, &source).unwrap(); + runner.output("git", &["tag", "v0.1.0"], &source).unwrap(); + fs::write(source.join("version.txt"), "two").unwrap(); + runner + .output("git", &["add", "version.txt"], &source) + .unwrap(); + runner.output("git", &commit_args, &source).unwrap(); + + let local = acquire_project(source.to_str().unwrap(), None, false, &runner).unwrap(); + assert_eq!(local.root, source.canonicalize().unwrap()); + drop(local); + assert!(source.exists()); + assert!(validate_checkout_tag(&source, "v0.1.0", &runner).is_err()); + + let clone = temp.path().join("checkout"); + clone_to(source.to_str().unwrap(), &clone, Some("v0.1.0"), &runner).unwrap(); + assert_eq!( + fs::read_to_string(clone.join("version.txt")).unwrap(), + "one" + ); + validate_checkout_tag(&clone, "v0.1.0", &runner).unwrap(); + let missing = temp.path().join("missing-tag"); + assert!(clone_to(source.to_str().unwrap(), &missing, Some("v9.9.9"), &runner).is_err()); + } +} diff --git a/src/runner.rs b/src/runner.rs new file mode 100644 index 0000000..1c68041 --- /dev/null +++ b/src/runner.rs @@ -0,0 +1,140 @@ +use crate::error::Error; +use regex::Regex; +use std::{path::Path, process::Command, sync::OnceLock}; + +pub struct Runner { + token_env: String, + verbose: bool, +} + +impl Runner { + pub fn new(token_env: &str) -> Self { + Self { + token_env: token_env.to_owned(), + verbose: false, + } + } + + pub fn with_verbose(mut self, verbose: bool) -> Self { + self.verbose = verbose; + self + } + + pub fn output(&self, program: &str, args: &[&str], cwd: &Path) -> Result, Error> { + self.output_with(program, args, cwd, &[], &[]) + } + + pub fn output_with( + &self, + program: &str, + args: &[&str], + cwd: &Path, + additions: &[(&str, &str)], + removals: &[&str], + ) -> Result, Error> { + let command = std::iter::once(program) + .chain(args.iter().copied()) + .map(|arg| if arg.contains("://") { "[URL]" } else { arg }) + .collect::>() + .join(" "); + if self.verbose { + eprintln!("$ {}", self.redact(&command)); + } + let mut process = Command::new(program); + process + .args(args) + .current_dir(cwd) + .env_remove("GITEA_TOKEN") + .env_remove(&self.token_env); + for name in removals { + process.env_remove(name); + } + for (name, value) in additions { + process.env(name, value); + } + let output = process + .output() + .map_err(|e| Error(format!("cannot run {program}: {e}")))?; + if !output.status.success() { + let detail = self.redact(&format!( + "{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + )); + return Err(Error(format!( + "command {} failed with {}: {}", + self.redact(&command), + output.status, + detail.trim() + ))); + } + if self.verbose { + let output_text = self.redact(&String::from_utf8_lossy(&output.stdout)); + if !output_text.trim().is_empty() { + eprintln!("{}", output_text.trim()); + } + } + Ok(output.stdout) + } + + pub fn text(&self, program: &str, args: &[&str], cwd: &Path) -> Result { + let bytes = self.output(program, args, cwd)?; + String::from_utf8(bytes) + .map(|text| text.trim().to_owned()) + .map_err(|e| Error(format!("{program} returned non-UTF-8 output: {e}"))) + } + + pub fn redact(&self, text: &str) -> String { + let mut without_token = text.to_owned(); + for name in ["GITEA_TOKEN", self.token_env.as_str()] { + if let Ok(secret) = std::env::var(name) { + if !secret.is_empty() { + without_token = without_token.replace(&secret, "[REDACTED]"); + } + } + } + static URLS: OnceLock = OnceLock::new(); + let pattern = URLS.get_or_init(|| { + Regex::new(r#"(?i)(?:https?|ssh)://[^\s'"<>]+"#) + .unwrap_or_else(|_| unreachable!("static URL regex must compile")) + }); + pattern.replace_all(&without_token, "[URL]").into_owned() + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn redacts_token_and_urls_and_keeps_token_out_of_child() { + let runner = Runner::new("RUST_RELEASE_TEST_SECRET"); + let secret = "unit-test-secret-value"; + std::env::set_var("RUST_RELEASE_TEST_SECRET", secret); + let message = runner + .redact("failed https://user:password@example.com/repo and unit-test-secret-value"); + assert!(!message.contains(secret)); + assert!(!message.contains("password")); + let child = runner.text("env", &[], Path::new(".")).unwrap(); + assert!(!child.contains("RUST_RELEASE_TEST_SECRET")); + std::env::set_var("RUST_RELEASE_TEST_REMOVED", "remove-me"); + let child = runner + .output_with( + "env", + &[], + Path::new("."), + &[("RUST_RELEASE_TEST_ADDED", "added-value")], + &["RUST_RELEASE_TEST_REMOVED"], + ) + .unwrap(); + let child = String::from_utf8(child).unwrap(); + assert!(child.contains("RUST_RELEASE_TEST_ADDED=added-value")); + assert!(!child.contains("RUST_RELEASE_TEST_REMOVED")); + let error = runner + .output("git", &["rev-parse", secret], Path::new(".")) + .unwrap_err() + .to_string(); + assert!(!error.contains(secret)); + std::env::remove_var("RUST_RELEASE_TEST_SECRET"); + std::env::remove_var("RUST_RELEASE_TEST_REMOVED"); + } +} diff --git a/src/verify.rs b/src/verify.rs new file mode 100644 index 0000000..81bbb67 --- /dev/null +++ b/src/verify.rs @@ -0,0 +1,592 @@ +use crate::{ + error::Error, + package::{self, Artifact}, + policy, + project::SelectedPackage, + runner::Runner, +}; +use flate2::read::GzDecoder; +use reqwest::blocking::Client; +use std::{ + fs::{self, File}, + io::{Read, Write}, + path::Path, + time::Duration, +}; + +pub fn asset_url(repository: &str, tag: &str, filename: &str) -> String { + let encode = + |s: &str| -> String { url::form_urlencoded::byte_serialize(s.as_bytes()).collect() }; + format!( + "{repository}/releases/download/{}/{}", + encode(tag), + encode(filename) + ) +} + +pub fn verify_archive(path: &Path, artifact: &Artifact) -> Result<(), Error> { + let mut tar = Vec::new(); + let file = + File::open(path).map_err(|e| Error(format!("cannot open {}: {e}", path.display())))?; + GzDecoder::new(file) + .read_to_end(&mut tar) + .map_err(|e| Error(format!("{}: invalid gzip archive: {e}", path.display())))?; + if tar.len() < 2048 { + return Err(Error(format!("{}: truncated tar archive.", path.display()))); + } + let directory = &tar[..512]; + let first = member_name(directory)?; + if first != artifact.root || directory[156] != b'5' { + return Err(Error(format!( + "{}: archive root does not match binstall metadata.", + path.display() + ))); + } + let binary = &tar[512..1024]; + let second = member_name(binary)?; + if second != format!("{}/{}", artifact.root, artifact.binary) || binary[156] != b'0' { + return Err(Error(format!( + "{}: archive binary does not match binstall metadata.", + path.display() + ))); + } + let mode = octal(&binary[100..108])?; + if !artifact.target.split('-').any(|part| part == "windows") && mode & 0o111 == 0 { + return Err(Error(format!( + "{}: binary is not executable.", + path.display() + ))); + } + let size = octal(&binary[124..136])?; + let rounded = size + .checked_add(511) + .ok_or_else(|| Error("Archive size overflow.".into()))? + / 512 + * 512; + let end = 1024_u64 + .checked_add(rounded) + .and_then(|n| n.checked_add(1024)) + .ok_or_else(|| Error("Archive size overflow.".into()))?; + if end > tar.len() as u64 { + return Err(Error(format!( + "{}: truncated binary or tar footer.", + path.display() + ))); + } + if tar[(end - 1024) as usize..].iter().any(|b| *b != 0) { + return Err(Error(format!( + "{}: archive contains unexpected members.", + path.display() + ))); + } + Ok(()) +} + +fn member_name(header: &[u8]) -> Result { + let name = text_field(&header[..100])?; + let prefix = text_field(&header[345..500])?; + if prefix.is_empty() { + Ok(name) + } else { + Ok(format!("{prefix}/{name}")) + } +} + +fn text_field(field: &[u8]) -> Result { + let end = field.iter().position(|b| *b == 0).unwrap_or(field.len()); + std::str::from_utf8(&field[..end]) + .map(str::to_owned) + .map_err(|_| Error("Archive contains non-UTF-8 paths.".into())) +} + +fn octal(field: &[u8]) -> Result { + let bytes = field + .iter() + .take_while(|b| **b != 0) + .copied() + .collect::>(); + let string = + std::str::from_utf8(&bytes).map_err(|_| Error("Invalid tar numeric field.".into()))?; + u64::from_str_radix(string.trim(), 8).map_err(|_| Error("Invalid tar numeric field.".into())) +} + +pub fn verify_published( + repository: &str, + tag: &str, + artifacts: &[Artifact], + sums: &Path, + authenticated_client: Option<&Client>, +) -> Result<(), Error> { + let downloads = tempfile::Builder::new() + .prefix("rust-realease-download-") + .tempdir() + .map_err(|e| Error(format!("cannot create download directory: {e}")))?; + let client = match authenticated_client { + Some(client) => client.clone(), + None => Client::builder() + .timeout(Duration::from_secs(120)) + .redirect(reqwest::redirect::Policy::limited(10)) + .build() + .map_err(|_| Error("Cannot initialize anonymous download client.".into()))?, + }; + for local in artifacts + .iter() + .map(|a| a.path.as_path()) + .chain(std::iter::once(sums)) + { + let filename = local + .file_name() + .and_then(|s| s.to_str()) + .ok_or_else(|| Error("Invalid asset filename.".into()))?; + let mut response = client + .get(asset_url(repository, tag, filename)) + .send() + .map_err(|_| Error(format!("{filename}: release download failed.")))?; + if response.status() != reqwest::StatusCode::OK { + return Err(Error(format!( + "{filename}: release download returned HTTP {}.", + response.status().as_u16() + ))); + } + let expected = fs::metadata(local) + .map_err(|e| { + Error(format!( + "cannot inspect local asset {}: {e}", + local.display() + )) + })? + .len(); + let path = downloads.path().join(filename); + let mut output = File::create(&path) + .map_err(|e| Error(format!("cannot create download {}: {e}", path.display())))?; + let mut count = 0_u64; + let mut buffer = [0_u8; 64 * 1024]; + loop { + let n = response + .read(&mut buffer) + .map_err(|_| Error(format!("{filename}: release download failed.")))?; + if n == 0 { + break; + } + count = count + .checked_add(n as u64) + .ok_or_else(|| Error("Download size overflow.".into()))?; + if count > expected { + return Err(Error(format!( + "{filename}: download exceeds expected size." + ))); + } + output + .write_all(&buffer[..n]) + .map_err(|e| Error(format!("cannot write download: {e}")))?; + } + drop(output); + if package::checksum(&path)? != package::checksum(local)? { + return Err(Error(format!( + "{filename}: published SHA-256 differs from local artifact." + ))); + } + if let Some(artifact) = artifacts.iter().find(|a| a.path == local) { + verify_archive(&path, artifact)?; + println!("Verified URL, checksum and archive: {}", artifact.target); + } + } + println!("Verified published SHA256SUMS"); + Ok(()) +} + +pub fn binstall_arguments(project: &SelectedPackage, target: &str, install: &Path) -> Vec { + vec![ + "binstall".into(), + project.name.clone(), + "--manifest-path".into(), + project.manifest.display().to_string(), + "--version".into(), + format!("={}", project.version), + "--targets".into(), + target.into(), + "--strategies".into(), + "crate-meta-data".into(), + "--no-confirm".into(), + "--no-track".into(), + "--disable-telemetry".into(), + "--no-discover-github-token".into(), + "--install-path".into(), + install.display().to_string(), + ] +} + +pub fn verify_binstall( + root: &Path, + project: &SelectedPackage, + targets: &[String], + runner: &Runner, +) -> Result<(), Error> { + verify_binstall_with_options(root, project, targets, runner, false) +} + +fn verify_binstall_with_options( + root: &Path, + project: &SelectedPackage, + targets: &[String], + runner: &Runner, + allow_insecure_http: bool, +) -> Result<(), Error> { + for target in targets { + let temp = tempfile::Builder::new() + .prefix("rust-realease-binstall-") + .tempdir() + .map_err(|e| Error(format!("cannot create isolated install directory: {e}")))?; + let cargo_home = temp.path().join("cargo-home"); + let install = temp.path().join("install"); + fs::create_dir_all(&cargo_home) + .map_err(|e| Error(format!("cannot create isolated Cargo home: {e}")))?; + fs::create_dir_all(&install) + .map_err(|e| Error(format!("cannot create isolated install root: {e}")))?; + let mut args = binstall_arguments(project, target, &install); + if allow_insecure_http { + args.push("--allow-insecure-http".into()); + } + let borrowed: Vec<_> = args.iter().map(String::as_str).collect(); + let cargo_home = cargo_home + .to_str() + .ok_or_else(|| Error("Cargo home path is not UTF-8.".into()))?; + runner.output_with("cargo", &borrowed, root, &[("CARGO_HOME", cargo_home)], &[])?; + let binary = install.join(policy::binary_filename(&project.binary, target)); + if !binary.is_file() { + return Err(Error(format!( + "cargo-binstall did not install the expected binary for {target}." + ))); + } + println!("cargo-binstall installation verified: {target}"); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + #[ignore = "requires RUST_RELEASE_LIVE_* variables and a disposable Gitea release"] + fn live_release_downloads_match_local_artifacts() { + let required = |name: &str| std::env::var(name).expect("set live release test variables"); + let repository = required("RUST_RELEASE_LIVE_REPOSITORY"); + let tag = required("RUST_RELEASE_LIVE_TAG"); + let name = required("RUST_RELEASE_LIVE_NAME"); + let version = required("RUST_RELEASE_LIVE_VERSION"); + let directory = std::path::PathBuf::from(required("RUST_RELEASE_LIVE_ARTIFACTS")); + let targets = [ + "x86_64-unknown-linux-musl", + "aarch64-unknown-linux-musl", + "x86_64-pc-windows-gnu", + ]; + let artifacts = targets + .iter() + .map(|target| { + let path = directory.join(policy::archive_name(&name, target, &version)); + Ok(Artifact { + target: (*target).into(), + sha256: package::checksum(&path)?, + path, + root: policy::archive_root(&name, target, &version), + binary: policy::binary_filename(&name, target), + }) + }) + .collect::, Error>>() + .unwrap(); + let private = std::env::var("RUST_RELEASE_LIVE_PRIVATE").as_deref() == Ok("1"); + let api = private.then(|| crate::gitea::Gitea::new(&repository, "GITEA_TOKEN").unwrap()); + verify_published( + &repository, + &tag, + &artifacts, + &directory.join("SHA256SUMS"), + api.as_ref().map(crate::gitea::Gitea::download_client), + ) + .unwrap(); + } + + #[test] + #[ignore = "requires a public disposable release and cargo-binstall"] + fn live_binstall_installs_public_release() { + let required = |name: &str| std::env::var(name).expect("set live release test variables"); + let manifest = std::path::PathBuf::from(required("RUST_RELEASE_LIVE_MANIFEST")); + let root = manifest.parent().unwrap().to_path_buf(); + let name = required("RUST_RELEASE_LIVE_NAME"); + let project = SelectedPackage { + name: name.clone(), + version: required("RUST_RELEASE_LIVE_VERSION"), + binary: name, + manifest, + repository: Some(required("RUST_RELEASE_LIVE_REPOSITORY")), + target_directory: root.join("target"), + }; + verify_binstall( + &root, + &project, + &[ + "x86_64-unknown-linux-musl".into(), + "aarch64-unknown-linux-musl".into(), + "x86_64-pc-windows-gnu".into(), + ], + &Runner::new("GITEA_TOKEN"), + ) + .unwrap(); + } + + #[test] + fn anonymous_downloads_match_local_archives_and_checksums() { + use std::{net::TcpListener, thread}; + let temp = tempfile::tempdir().unwrap(); + let binary = temp.path().join("demo"); + fs::write(&binary, b"binary").unwrap(); + let (artifacts, sums) = package::package( + temp.path(), + "demo", + "0.1.0", + "demo", + &[("x86_64-unknown-linux-musl".into(), binary)], + ) + .unwrap(); + let filename = artifacts[0] + .path + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_owned(); + let bodies = [ + (filename, fs::read(&artifacts[0].path).unwrap()), + ("SHA256SUMS".into(), fs::read(&sums).unwrap()), + ]; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let repository = format!("http://{}/team/demo", listener.local_addr().unwrap()); + let handle = thread::spawn(move || { + for (filename, body) in bodies { + let (mut stream, _) = listener.accept().unwrap(); + let mut buffer = [0_u8; 4096]; + let count = stream.read(&mut buffer).unwrap(); + let request = String::from_utf8_lossy(&buffer[..count]); + assert!(request.starts_with(&format!( + "GET /team/demo/releases/download/v0.1.0/{filename} " + ))); + assert!(!request.to_ascii_lowercase().contains("authorization:")); + write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ) + .unwrap(); + stream.write_all(&body).unwrap(); + } + }); + verify_published(&repository, "v0.1.0", &artifacts, &sums, None).unwrap(); + handle.join().unwrap(); + } + + #[test] + fn authenticated_private_downloads_match_local_assets() { + use std::{net::TcpListener, thread}; + let temp = tempfile::tempdir().unwrap(); + let binary = temp.path().join("demo"); + fs::write(&binary, b"binary").unwrap(); + let (artifacts, sums) = package::package( + temp.path(), + "demo", + "0.1.0", + "demo", + &[("x86_64-unknown-linux-musl".into(), binary)], + ) + .unwrap(); + let bodies = [ + fs::read(&artifacts[0].path).unwrap(), + fs::read(&sums).unwrap(), + ]; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let repository = format!("http://{}/team/demo", listener.local_addr().unwrap()); + let handle = thread::spawn(move || { + for body in bodies { + let (mut stream, _) = listener.accept().unwrap(); + let mut buffer = [0_u8; 4096]; + let count = stream.read(&mut buffer).unwrap(); + let request = String::from_utf8_lossy(&buffer[..count]); + assert!(request + .lines() + .any(|line| line.eq_ignore_ascii_case("authorization: token test-token"))); + write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ) + .unwrap(); + stream.write_all(&body).unwrap(); + } + }); + let mut headers = reqwest::header::HeaderMap::new(); + headers.insert( + reqwest::header::AUTHORIZATION, + reqwest::header::HeaderValue::from_static("token test-token"), + ); + let client = Client::builder() + .default_headers(headers) + .redirect(reqwest::redirect::Policy::none()) + .build() + .unwrap(); + verify_published(&repository, "v0.1.0", &artifacts, &sums, Some(&client)).unwrap(); + handle.join().unwrap(); + } + + #[test] + #[ignore = "requires cargo-binstall and a local HTTP listener"] + fn real_binstall_installs_local_release_fixture() { + use std::{ + collections::HashMap, + net::TcpListener, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, + thread, + }; + let temp = tempfile::tempdir().unwrap(); + let targets = [ + "x86_64-unknown-linux-musl".to_owned(), + "aarch64-unknown-linux-musl".to_owned(), + "x86_64-pc-windows-gnu".to_owned(), + ]; + let outputs = targets + .iter() + .map(|target| { + let binary = temp.path().join(format!("binary-{target}")); + fs::write(&binary, format!("fixture binary for {target}"))?; + Ok((target.clone(), binary)) + }) + .collect::, std::io::Error>>() + .unwrap(); + let (artifacts, _) = + package::package(temp.path(), "demo", "0.1.0", "demo", &outputs).unwrap(); + let archives = artifacts + .iter() + .map(|artifact| { + ( + artifact + .path + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_owned(), + fs::read(&artifact.path).unwrap(), + ) + }) + .collect::>(); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let repository = format!("http://{}/team/demo", listener.local_addr().unwrap()); + fs::create_dir(temp.path().join("src")).unwrap(); + fs::write(temp.path().join("src/main.rs"), "fn main() {}\n").unwrap(); + let manifest = temp.path().join("Cargo.toml"); + fs::write(&manifest, format!("[package]\nname='demo'\nversion='0.1.0'\nedition='2021'\nrepository='{repository}'\n[package.metadata.binstall]\npkg-url='{{ repo }}/releases/download/v{{ version }}/{{ name }}-{{ target }}-v{{ version }}.tgz'\npkg-fmt='tgz'\nbin-dir='{{ name }}-{{ target }}-v{{ version }}/{{ bin }}{{ binary-ext }}'\n")).unwrap(); + let done = Arc::new(AtomicBool::new(false)); + let server_done = Arc::clone(&done); + let handle = thread::spawn(move || { + while !server_done.load(Ordering::Relaxed) { + match listener.accept() { + Ok((mut stream, _)) => { + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + let mut request = Vec::new(); + let mut buffer = [0_u8; 4096]; + while !request.windows(4).any(|w| w == b"\r\n\r\n") { + let count = stream.read(&mut buffer).unwrap(); + if count == 0 { + break; + } + request.extend_from_slice(&buffer[..count]); + } + let request = String::from_utf8_lossy(&request); + let requested_archive = request + .lines() + .next() + .and_then(|line| line.split_whitespace().nth(1)) + .and_then(|path| path.rsplit('/').next()) + .and_then(|name| archives.get(name)); + let body = requested_archive.map_or(&[][..], Vec::as_slice); + write!(stream, "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len()).unwrap(); + stream.write_all(body).unwrap(); + } + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + thread::sleep(Duration::from_millis(10)); + } + Err(error) => panic!("local HTTP listener failed: {error}"), + } + } + }); + let project = SelectedPackage { + name: "demo".into(), + version: "0.1.0".into(), + binary: "demo".into(), + manifest, + repository: Some(repository), + target_directory: temp.path().join("target"), + }; + let result = verify_binstall_with_options( + temp.path(), + &project, + &targets, + &Runner::new("GITEA_TOKEN"), + true, + ); + done.store(true, Ordering::Relaxed); + handle.join().unwrap(); + result.unwrap(); + } + + #[test] + fn local_archive_validation_and_binstall_command() { + let temp = tempfile::tempdir().unwrap(); + let binary = temp.path().join("demo"); + fs::write(&binary, b"binary").unwrap(); + let (artifacts, _) = package::package( + temp.path(), + "demo", + "0.1.0", + "demo", + &[("x86_64-unknown-linux-musl".into(), binary)], + ) + .unwrap(); + verify_archive(&artifacts[0].path, &artifacts[0]).unwrap(); + let mut tar = Vec::new(); + GzDecoder::new(File::open(&artifacts[0].path).unwrap()) + .read_to_end(&mut tar) + .unwrap(); + tar.extend_from_slice(&[1; 512]); + let extra = temp.path().join("extra.tgz"); + let mut gzip = flate2::write::GzEncoder::new( + File::create(&extra).unwrap(), + flate2::Compression::default(), + ); + gzip.write_all(&tar).unwrap(); + gzip.finish().unwrap(); + assert!(verify_archive(&extra, &artifacts[0]) + .unwrap_err() + .to_string() + .contains("unexpected members")); + let project = SelectedPackage { + name: "demo".into(), + version: "0.1.0".into(), + binary: "demo".into(), + manifest: temp.path().join("Cargo.toml"), + repository: None, + target_directory: temp.path().join("target"), + }; + let args = binstall_arguments(&project, "x86_64-unknown-linux-musl", temp.path()); + assert!(args.contains(&"--no-track".to_owned())); + assert!(args.contains(&"--no-discover-github-token".to_owned())); + assert_eq!( + asset_url("https://example.com/team/demo", "release+1", "one.tgz"), + "https://example.com/team/demo/releases/download/release%2B1/one.tgz" + ); + } +} diff --git a/tests/test_integration.py b/tests/test_integration.py deleted file mode 100644 index f943961..0000000 --- a/tests/test_integration.py +++ /dev/null @@ -1,82 +0,0 @@ -"""Opt-in real cargo-binstall test against a disposable local asset server. - -Run: PUBLISHER_INTEGRATION=1 python -m unittest discover -s tests -v -No Gitea token or cross toolchain is required. -""" -from functools import partial -from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer -import os -from pathlib import Path -import shutil -import tempfile -from threading import Thread -import unittest -from unittest.mock import patch - -from worker.build import verify_source -from worker.config import load_config -from worker.manifest import inspect_project, metadata_issues, repair_metadata -from worker.models import RustProject -from worker.package import package -from worker.runner import run -from worker.verify import verify_published, verify_binstall - - -@unittest.skipUnless(os.environ.get('PUBLISHER_INTEGRATION') == '1' and shutil.which('cargo-binstall'), - 'set PUBLISHER_INTEGRATION=1 to exercise real cargo-binstall') -class BinstallIntegration(unittest.TestCase): - def test_install_every_default_target_from_manifest(self): - self.install_fixture(workspace=False) - - def test_install_workspace_member_with_inherited_version(self): - self.install_fixture(workspace=True) - - def install_fixture(self, workspace): - class QuietHandler(SimpleHTTPRequestHandler): - def log_message(self, *args): - pass - with tempfile.TemporaryDirectory(prefix='publisher-integration-') as folder: - root = Path(folder) - with ThreadingHTTPServer(('127.0.0.1', 0), partial(QuietHandler, directory=folder)) as server: - thread = Thread(target=server.serve_forever, daemon=True) - thread.start() - try: - repository = f'http://127.0.0.1:{server.server_port}/org/publisher-test' - manifest = root / 'Cargo.toml' - manifest.write_text('[package]\nname="publisher-test"\nversion="0.1.0"\nedition="2021"\n\n[[bin]]\nname="publisher-test"\npath="main.rs"\n') - (root / 'main.rs').write_text('fn main() {}\n') - if workspace: - member = root / 'cli' - member.mkdir() - (root / 'main.rs').rename(member / 'main.rs') - manifest.rename(member / 'Cargo.toml') - manifest.write_text('[workspace]\nmembers=["cli", "unused"]\n[workspace.package]\nversion="0.1.0"\n') - unused = root / 'unused' - (unused / 'src').mkdir(parents=True) - (unused / 'Cargo.toml').write_text('[package]\nname="unused"\nversion="0.1.0"\n') - (unused / 'src/lib.rs').write_text('compile_error!("unselected member must not be compiled");\n') - manifest = member / 'Cargo.toml' - manifest.write_text(manifest.read_text().replace('version="0.1.0"', 'version.workspace=true')) - repair_metadata(manifest, metadata_issues(manifest, repository)) - project = RustProject(root, manifest, 'publisher-test', '0.1.0', repository, ['publisher-test']) - if workspace: - project = inspect_project(root, 'publisher-test') - verify_source(project, load_config(locked=False), skip_tests=False) - binary = root / 'fake-binary' - binary.write_bytes(b'not executed: publisher contract fixture\n') - binary.chmod(0o755) - targets = ['x86_64-unknown-linux-musl', 'aarch64-unknown-linux-musl', 'x86_64-pc-windows-gnu'] - artifacts, sums = package(project, dict.fromkeys(targets, binary)) - download = root / 'org/publisher-test/releases/download/v0.1.0' - download.mkdir(parents=True) - for path in [a.path for a in artifacts] + [sums]: - shutil.copy(path, download / path.name) - verify_published(repository, 'v0.1.0', artifacts, sums) - # Plain HTTP is allowed only for this loopback test server. - def local_run(*args, **kwargs): - return run(*args, '--allow-insecure-http', **kwargs) - with patch('worker.verify.run', side_effect=local_run): - verify_binstall(project, targets) - finally: - server.shutdown() - thread.join() diff --git a/tests/test_prepare.py b/tests/test_prepare.py deleted file mode 100644 index fe2d613..0000000 --- a/tests/test_prepare.py +++ /dev/null @@ -1,175 +0,0 @@ -from contextlib import redirect_stdout -import io -import os -from pathlib import Path -import tempfile -import unittest -from unittest.mock import patch - -import tomlkit -from typer.testing import CliRunner -from worker.cli import app -from worker.build import build -from worker.config import load_config -from worker.models import RustProject -from worker.prepare import prepare_repository, preparation_files -from worker.project import parse_repository, push_preparation -from worker.runner import run - - -class PreparationTests(unittest.TestCase): - def setUp(self): - self.temp = tempfile.TemporaryDirectory() - self.addCleanup(self.temp.cleanup) - self.root = Path(self.temp.name) - self.manifest = self.root / 'Cargo.toml' - self.manifest.write_text('# Keep comments\n[package]\nname="demo"\nversion="0.1.0"\nedition="2021"\n') - (self.root / 'Cargo.lock').write_text('# Fixture lockfile\nversion = 4\n') - (self.root / '.gitignore').write_text('Cargo.lock\n') - run('git', 'init', '-b', 'main', str(self.root)) - for key, value in [('user.name', 'Test'), ('user.email', 'test@example.invalid'), - ('commit.gpgsign', 'false'), ('tag.gpgsign', 'false')]: - run('git', 'config', key, value, cwd=self.root) - run('git', 'add', 'Cargo.toml', '.gitignore', cwd=self.root) - run('git', 'commit', '-m', 'initial', cwd=self.root) - self.repo = parse_repository('https://example.org/team/demo') - self.project = RustProject(self.root, self.manifest, 'demo', '0.1.0', - self.repo.url, ['demo'], tag='v0.1.0') - self.config = load_config() - - def prepare(self, **kwargs): - # Keep the large preview diffs out of normal test output. - with patch('worker.prepare.console.print'): - return prepare_repository(self.project, self.config, self.repo, **kwargs) - - def test_preview_does_not_mutate_files_git_or_run_cargo(self): - before = self.manifest.read_bytes() - head = run('git', 'rev-parse', 'HEAD', cwd=self.root) - (self.root / 'Cargo.lock').unlink() - with patch('worker.prepare.run', wraps=run) as commands: - self.prepare(image_tag='main', dry_run=True, commit=True, create_tag=True) - self.assertEqual(self.manifest.read_bytes(), before) - self.assertFalse((self.root / 'Cross.toml').exists()) - self.assertFalse((self.root / 'Cargo.lock').exists()) - self.assertEqual(run('git', 'rev-parse', 'HEAD', cwd=self.root), head) - self.assertFalse(run('git', 'tag', '--list', cwd=self.root)) - self.assertFalse(any(c.args[0] == 'cargo' for c in commands.call_args_list)) - - def test_preparation_commits_only_managed_files_and_creates_tag(self): - self.prepare(image_tag='main', commit=True, create_tag=True) - tracked = set(run('git', 'ls-files', cwd=self.root).splitlines()) - self.assertEqual(tracked, {'Cargo.toml', 'Cargo.lock', 'Cross.toml', '.gitignore'}) - self.assertFalse(run('git', 'status', '--porcelain', cwd=self.root)) - head = run('git', 'rev-parse', 'HEAD', cwd=self.root) - self.assertEqual(run('git', 'rev-parse', 'v0.1.0^{commit}', cwd=self.root), head) - self.assertEqual(run('git', 'cat-file', '-t', 'v0.1.0', cwd=self.root), 'tag') - self.assertIn('# Keep comments', self.manifest.read_text()) - cross = tomlkit.parse((self.root / 'Cross.toml').read_text()) - self.assertIn('RUSTUP_TOOLCHAIN', cross['build']['env']['passthrough']) - self.assertEqual(len(cross['target']), 3) - # A repeat of the same operation does not create another commit or move the tag. - self.prepare(image_tag='main', commit=True, create_tag=True) - self.assertEqual(run('git', 'rev-parse', 'HEAD', cwd=self.root), head) - - def test_commit_requires_clean_tree_and_index(self): - (self.root / 'unrelated.txt').write_text('user work') - for staged in (False, True): - if staged: - run('git', 'add', 'unrelated.txt', cwd=self.root) - with self.assertRaisesRegex(ValueError, 'clean checkout'): - self.prepare(commit=True) - self.assertFalse((self.root / 'Cross.toml').exists()) - - def test_existing_tag_blocks_changes_before_any_write(self): - run('git', 'tag', 'v0.1.0', cwd=self.root) - before = self.manifest.read_bytes() - with self.assertRaisesRegex(ValueError, 'will not move'): - self.prepare(image_tag='main', commit=True, create_tag=True) - self.assertEqual(before, self.manifest.read_bytes()) - self.assertFalse((self.root / 'Cross.toml').exists()) - - def test_missing_lock_is_generated_only_in_real_preparation(self): - lock = self.root / 'Cargo.lock' - lock.unlink() - def fake_run(*args, **kwargs): - self.assertEqual(args, ('cargo', 'generate-lockfile')) - lock.write_text('version = 4\n') - with patch('worker.prepare.run', side_effect=fake_run) as commands: - self.prepare() - commands.assert_called_once() - self.assertTrue(lock.exists()) - - def test_preserves_custom_cross_config_and_explicit_image_override(self): - path = self.root / 'Cross.toml' - path.write_text('# Custom settings\n[build.env]\npassthrough=["MY_BUILD_VAR"]\n' - '[target.x86_64-unknown-linux-musl]\nimage="my/custom:image"\n') - plan = preparation_files(self.project, self.config, self.repo) - result = tomlkit.parse(plan[path]) - self.assertEqual(result['target']['x86_64-unknown-linux-musl']['image'], 'my/custom:image') - self.assertEqual(result['build']['env']['passthrough'], ['MY_BUILD_VAR', 'RUSTUP_TOOLCHAIN']) - self.assertIn('# Custom settings', plan[path]) - with self.assertRaises(ValueError): - preparation_files(self.project, self.config, self.repo, 'bad/tag') - - def test_no_git_writes_without_explicit_flags(self): - head = run('git', 'rev-parse', 'HEAD', cwd=self.root) - self.prepare() - self.assertEqual(run('git', 'rev-parse', 'HEAD', cwd=self.root), head) - self.assertFalse(run('git', 'tag', '--list', cwd=self.root)) - for flags in ({'create_tag': True}, {'push': True}, {'commit': True, 'push': True}): - with self.assertRaises(ValueError): - self.prepare(**flags) - - def test_cli_preparation_does_not_require_existing_tag_or_publish(self): - with patch('worker.cli.inspect_project', return_value=self.project), \ - patch('worker.cli.Gitea') as api, patch('worker.cli.build') as build_mock: - result = CliRunner().invoke(app, [str(self.root), '--prepare-repo', '--dry-run', - '--tag', 'v0.1.0', '--cross-image-tag', 'main']) - self.assertEqual(result.exit_code, 0, result.output) - api.assert_not_called() - build_mock.assert_not_called() - result = CliRunner().invoke(app, [str(self.root), '--create-tag']) - self.assertNotEqual(result.exit_code, 0) - self.assertIn('require --prepare-repo', result.output) - - def test_fresh_builds_never_reuse_previous_output(self): - old_dirs = [] - def fake_run(*args, **kwargs): - if args[0] == 'rustup': - return 'stable-x86_64-unknown-linux-gnu (default)' - directory = Path(args[args.index('--target-dir') + 1]) - old_dirs.append(directory) - binary = directory / 'x86_64-unknown-linux-musl/release/demo' - binary.parent.mkdir(parents=True) - binary.write_bytes(b'fresh') - self.assertEqual(kwargs['env']['RUSTUP_TOOLCHAIN'], 'stable-x86_64-unknown-linux-gnu') - return '' - settings = load_config(targets=['x86_64-unknown-linux-musl']) - with patch('worker.build.run', side_effect=fake_run), patch('worker.build.shutil.which', return_value='/rustup'): - first = build(self.project, settings, 'podman') - second = build(self.project, settings, 'podman') - self.assertNotEqual(old_dirs[0], old_dirs[1]) - self.assertTrue(next(iter(first.values())).exists()) - self.assertTrue(next(iter(second.values())).exists()) - - def test_push_uses_atomic_refs_and_token_only_in_askpass_environment(self): - def fake_run(*args, **kwargs): - if args[:3] == ('git', 'remote', 'get-url'): - return self.repo.url + '.git' - self.assertIn('--atomic', args) - self.assertIn('HEAD:refs/heads/main', args) - self.assertIn('refs/tags/v0.1.0', args) - self.assertNotIn('--force', args) - self.assertNotIn('test-secret', ' '.join(args)) - env = kwargs['env'] - self.assertEqual(env['PUBLISHER_GIT_TOKEN'], 'test-secret') - self.assertTrue(Path(env['GIT_ASKPASS']).exists()) - self.assertNotIn('test-secret', Path(env['GIT_ASKPASS']).read_text()) - return '' - with patch.dict(os.environ, {'GITEA_TOKEN': 'test-secret'}), patch('worker.project.run', side_effect=fake_run) as commands: - push_preparation(self.root, 'main', 'v0.1.0', self.repo, 'GITEA_TOKEN') - self.assertEqual(commands.call_count, 2) - with patch('worker.project.run', return_value='https://other.invalid/team/demo') as commands: - with self.assertRaisesRegex(ValueError, 'differs'): - push_preparation(self.root, 'main', 'v0.1.0', self.repo, 'GITEA_TOKEN') - commands.assert_called_once() diff --git a/tests/test_publisher.py b/tests/test_publisher.py deleted file mode 100644 index dafe6be..0000000 --- a/tests/test_publisher.py +++ /dev/null @@ -1,366 +0,0 @@ -import hashlib -import io -import json -import os -from pathlib import Path -import shutil -import subprocess -import tarfile -import tempfile -import unittest -from unittest.mock import patch - -import httpx -from typer.testing import CliRunner -from worker.cli import app -from worker.config import load_config -from worker.gitea import Gitea -from worker.manifest import inspect_project, metadata_issues, repair_metadata -from worker.models import RustProject, BuildTarget -from worker.package import package, checksum -from worker.policy import archive_name, binstall_metadata, expected_tag -from worker.project import parse_repository, acquire_project, validate_checkout_tag -from worker.runner import run, CommandError -from worker.verify import verify_archive, verify_published, verify_binstall - - -class Fixture(unittest.TestCase): - def setUp(self): - self.temp = tempfile.TemporaryDirectory() - self.addCleanup(self.temp.cleanup) - self.root = Path(self.temp.name) - self.manifest = self.root / 'Cargo.toml' - self.manifest.write_text('# Keep this comment\n[package]\nname = "demo"\nversion = "0.1.0"\n# Another comment\n') - self.project = RustProject(self.root, self.manifest, 'demo', '0.1.0', - 'https://host/gitea/org/demo', ['tool'], 'a' * 40) - - def artifacts(self): - binary = self.root / 'tool' - binary.write_bytes(b'example binary\x00') - binary.chmod(0o755) - return package(self.project, {'x86_64-unknown-linux-musl': binary, - 'x86_64-pc-windows-gnu': binary}) - - -class PolicyTests(Fixture): - def test_repository_urls(self): - for url in ('https://host/gitea/org/demo.git', 'https://host/gitea/org/demo/', - 'git@host:gitea/org/demo.git', 'ssh://git@host/gitea/org/demo.git'): - repo = parse_repository(url) - self.assertEqual(repo.url, 'https://host/gitea/org/demo') - self.assertEqual(repo.base_url, 'https://host/gitea') - self.assertEqual(parse_repository('https://host:3000/org/demo').base_url, 'https://host:3000') - for bad in ('--upload-pack=evil', 'file:///tmp/repo', 'https://token@host/org/repo', - 'https://host/org/repo?token=secret', 'https://host/../repo', 'https://host/org'): - with self.subTest(bad=bad), self.assertRaises(ValueError): - parse_repository(bad) - - def test_naming(self): - self.assertEqual(expected_tag('0.1.0'), 'v0.1.0') - self.assertEqual(expected_tag('0.1.0', 'release-'), 'release-0.1.0') - self.assertEqual(archive_name('demo', 'target', '0.1.0'), 'demo-target-v0.1.0.tgz') - self.assertEqual(BuildTarget('x86_64-pc-windows-gnu').binary_filename('tool'), 'tool.exe') - self.assertEqual(BuildTarget('aarch64-unknown-linux-musl').binary_filename('tool'), 'tool') - self.assertIn('/release-{ version }/', binstall_metadata('release-')['pkg-url']) - self.assertEqual(binstall_metadata()['bin-dir'], '{ name }-{ target }-v{ version }/{ bin }{ binary-ext }') - - def test_manifest_repair_preserves_comments_and_is_idempotent(self): - issues = metadata_issues(self.manifest, self.project.repository) - self.assertEqual(len(issues), 4) - repair_metadata(self.manifest, issues) - first = self.manifest.read_text() - self.assertIn('# Keep this comment', first) - self.assertIn('# Another comment', first) - self.assertEqual(metadata_issues(self.manifest, self.project.repository), []) - repair_metadata(self.manifest, []) - self.assertEqual(self.manifest.read_text(), first) - - def test_conflicting_overrides_removed(self): - self.manifest.write_text(self.manifest.read_text() + '\n[package.metadata.binstall.overrides.x86_64-pc-windows-gnu]\npkg-fmt="zip"\n') - issues = metadata_issues(self.manifest, self.project.repository) - self.assertIn('package.metadata.binstall.overrides', [i.key for i in issues]) - repair_metadata(self.manifest, issues) - self.assertEqual(metadata_issues(self.manifest, self.project.repository), []) - - def test_workspace_and_multiple_binaries_rejected(self): - data = {'packages': [{'id': 'demo', 'name': 'demo', 'targets': [{'kind': ['bin'], 'name': 'one'}, - {'kind': ['bin'], 'name': 'two'}]}], - 'workspace_members': ['demo']} - with patch('worker.manifest.run', return_value=json.dumps(data)), self.assertRaisesRegex(ValueError, 'one, two'): - inspect_project(self.root) - self.manifest.write_text('[workspace]\nmembers=[]\n') - with patch('worker.manifest.run', return_value=json.dumps(data)), self.assertRaisesRegex(ValueError, '--package'): - inspect_project(self.root) - - def test_symlink_manifest_cannot_repair_unrelated_file(self): - other = self.root / 'other.toml' - original = self.manifest.read_bytes() - self.manifest.rename(other) - self.manifest.symlink_to(other) - with self.assertRaisesRegex(ValueError, 'symlinked'): - repair_metadata(self.manifest, metadata_issues(self.manifest, self.project.repository)) - self.assertEqual(other.read_bytes(), original) - - def test_cross_metadata_hooks_rejected(self): - from worker.build import validate_cross_config - self.manifest.write_text(self.manifest.read_text() + '\n[workspace.metadata.cross.target.x86_64-unknown-linux-musl]\npre-build=["echo unsafe"]\n') - with self.assertRaisesRegex(ValueError, 'executable option'): - validate_cross_config(self.root) - - def test_config_overrides_and_validation(self): - config = load_config(targets=['x86_64-pc-windows-gnu'], locked=False, profile='dev') - self.assertFalse(config.locked) - self.assertEqual(config.profile, 'dev') - for targets in ([], ['../../oops'], ['--target']): - with self.assertRaises(ValueError): - load_config(targets=targets) - - def test_reproducible_archives_and_checksums(self): - artifacts, sums = self.artifacts() - again, _ = self.artifacts() - self.assertEqual([a.sha256 for a in artifacts], [a.sha256 for a in again]) - for artifact in artifacts: - verify_archive(artifact.path, artifact) - self.assertEqual(artifact.sha256, hashlib.sha256(artifact.path.read_bytes()).hexdigest()) - self.assertIn(f'{artifact.sha256} {artifact.path.name}\n', sums.read_text()) - self.assertEqual(len(list(sums.parent.iterdir())), 3) - - def test_malformed_archive_rejected_without_extraction(self): - artifacts, _ = self.artifacts() - path = self.root / 'malformed.tgz' - with tarfile.open(path, 'w:gz') as archive: - entry = tarfile.TarInfo('../../outside') - entry.size = 1 - archive.addfile(entry, io.BytesIO(b'x')) - with self.assertRaises(ValueError): - verify_archive(path, artifacts[0]) - self.assertFalse((self.root.parent / 'outside').exists()) - - def test_published_bytes_and_checksum_file(self): - artifacts, sums = self.artifacts() - paths = {p.name: p.read_bytes() for p in [a.path for a in artifacts] + [sums]} - def handler(request): - self.assertNotIn('authorization', request.headers) - return httpx.Response(200, content=paths[request.url.path.rsplit('/', 1)[1]]) - verify_published(self.project.repository, 'v0.1.0', artifacts, sums, httpx.MockTransport(handler)) - with self.assertRaisesRegex(ValueError, 'SHA-256'): - verify_published(self.project.repository, 'v0.1.0', artifacts, sums, - httpx.MockTransport(lambda r: httpx.Response(200, content=b'bad'))) - - def test_runner_failure_and_secret_redaction(self): - with patch.dict(os.environ, {'GITEA_TOKEN': 'super-secret'}): - with self.assertRaises(CommandError) as failure: - run('python3', '-c', 'import sys; print("super-secret", file=sys.stderr); sys.exit(3)') - self.assertNotIn('super-secret', str(failure.exception)) - self.assertIn('(3)', str(failure.exception)) - self.assertIn('[REDACTED]', str(failure.exception)) - self.assertEqual(run('python3', '-c', 'import os; print(os.getenv("GITEA_TOKEN", "missing"))'), 'missing') - - -class GiteaTests(Fixture): - def client(self, handler): - env = patch.dict(os.environ, {'GITEA_TOKEN': 'secret-token'}) - env.start() - self.addCleanup(env.stop) - client = Gitea(parse_repository(self.project.repository), 'GITEA_TOKEN', httpx.MockTransport(handler)) - self.addCleanup(client.client.close) - return client - - def test_create_and_reuse_release(self): - for exists in (False, True): - calls = [] - def handler(request): - calls.append(request) - self.assertTrue(request.url.path.startswith('/gitea/api/v1/repos/org/demo/')) - if '/tags/v0.1.0' in request.url.path and '/releases/' not in request.url.path: - return httpx.Response(200, json={'commit': {'sha': self.project.commit}}) - if request.method == 'POST': - body = json.loads(request.content) - self.assertTrue(body['prerelease']) - self.assertEqual(body['tag_name'], 'v0.1.0') - return httpx.Response(201, json={'id': 1}) - return httpx.Response(200 if exists else 404, json={'id': 1}) - self.assertEqual(self.client(handler).ensure_release('v0.1.0', self.project.commit, prerelease=True)['id'], 1) - self.assertEqual(sum(r.method == 'POST' for r in calls), int(not exists)) - - def test_collision_preflight_and_replace(self): - paths = [self.root / 'one.tgz', self.root / 'two.tgz'] - for path in paths: - path.write_bytes(b'archive') - for replace in (False, True): - calls = [] - def handler(request): - calls.append(request.method) - if request.method == 'GET': - return httpx.Response(200, json=[{'id': 9, 'name': 'two.tgz'}]) - return httpx.Response(204) if request.method == 'DELETE' else httpx.Response(201, json={'id': 10}) - client = self.client(handler) - if replace: - client.upload(1, paths, True) - self.assertEqual(calls, ['GET', 'POST', 'DELETE', 'POST']) - else: - with self.assertRaisesRegex(ValueError, 'already exist'): - client.upload(1, paths) - self.assertEqual(calls, ['GET']) - - def test_auth_failure_does_not_echo_body(self): - client = self.client(lambda r: httpx.Response(401, text='secret-token')) - with self.assertRaises(RuntimeError) as failure: - client.ensure_release('v0.1.0', self.project.commit) - self.assertNotIn('secret-token', str(failure.exception)) - self.assertIn('401', str(failure.exception)) - - def test_wrong_remote_commit_blocks_creation(self): - client = self.client(lambda r: httpx.Response(200, json={'commit': {'sha': 'wrong'}})) - with self.assertRaisesRegex(ValueError, 'differs'): - client.ensure_release('v0.1.0', self.project.commit) - - -class CliTests(Fixture): - def test_dry_run_never_repairs_or_builds(self): - original = self.manifest.read_bytes() - with patch('worker.cli.inspect_project', return_value=self.project), \ - patch('worker.cli.prerequisites', return_value=([], 'podman')), \ - patch('worker.cli.validate_checkout_tag', return_value=True), \ - patch('worker.cli.build') as build, patch('worker.cli.Gitea') as api: - result = CliRunner().invoke(app, [str(self.root), '--dry-run', '--fix-metadata']) - self.assertEqual(result.exit_code, 1, result.output) - self.assertIn('Dry run complete', result.output) - self.assertEqual(self.manifest.read_bytes(), original) - build.assert_not_called() - api.assert_not_called() - - def test_stop_metadata_and_tag_mismatch(self): - with patch('worker.cli.inspect_project', return_value=self.project), patch('worker.cli.prerequisites') as prereq: - result = CliRunner().invoke(app, [str(self.root), '--fix-metadata', '--stop-after', 'metadata']) - self.assertEqual(result.exit_code, 0, result.output) - prereq.assert_not_called() - with patch('worker.cli.inspect_project', return_value=self.project): - result = CliRunner().invoke(app, [str(self.root), '--tag', 'v9.9.9']) - self.assertNotEqual(result.exit_code, 0) - - def test_stop_package_never_contacts_gitea(self): - repair_metadata(self.manifest, metadata_issues(self.manifest, self.project.repository)) - binary = self.root / 'binary' - binary.write_bytes(b'fake') - with patch('worker.cli.inspect_project', return_value=self.project), \ - patch('worker.cli.prerequisites', return_value=([], 'podman')), \ - patch('worker.cli.validate_checkout_tag', return_value=False), \ - patch('worker.cli.verify_source'), \ - patch('worker.cli.build', return_value={'x86_64-unknown-linux-musl': binary}), \ - patch('worker.cli.Gitea') as api: - result = CliRunner().invoke(app, [str(self.root), '--stop-after', 'package']) - self.assertEqual(result.exit_code, 0, result.output) - api.assert_not_called() - self.assertEqual(len(list((self.root / 'dist').glob('*/SHA256SUMS'))), 1) - - def test_local_source_never_removed(self): - with acquire_project(str(self.root)) as root: - self.assertEqual(root, self.root) - self.assertTrue(self.manifest.exists()) - - -class WrapperTests(Fixture): - def setUp(self): - super().setUp() - self.wrapper = self.root / 'release.sh' - shutil.copy(Path(__file__).resolve().parents[1] / 'release.sh', self.wrapper) - - def invoke(self, env): - clean = os.environ.copy() - clean.pop('VIRTUAL_ENV', None) - clean.update(env) - return subprocess.run(['bash', str(self.wrapper), '--help'], stdin=subprocess.DEVNULL, - capture_output=True, text=True, env=clean, cwd='/tmp') - - def test_no_venv_noninteractive(self): - result = self.invoke({}) - self.assertNotEqual(result.returncode, 0) - self.assertIn('non-interactive', result.stderr) - - def test_existing_venv_needs_activation(self): - (self.root / '.venv').mkdir() - result = self.invoke({}) - self.assertNotEqual(result.returncode, 0) - self.assertIn(str(self.root / '.venv/bin/activate'), result.stdout) - - def test_wrong_venv_even_without_project_venv(self): - result = self.invoke({'VIRTUAL_ENV': '/tmp'}) - self.assertNotEqual(result.returncode, 0) - self.assertIn('non-interactive', result.stderr) - - def test_correct_venv_preserves_cwd(self): - bin_dir = self.root / '.venv' / 'bin' - bin_dir.mkdir(parents=True) - interpreter = bin_dir / 'python' - interpreter.write_text('#!/bin/sh\npwd\nprintf "%s\\n" "$@"\n') - interpreter.chmod(0o755) - result = self.invoke({'VIRTUAL_ENV': str(bin_dir.parent)}) - self.assertEqual(result.returncode, 0, result.stderr) - self.assertEqual(result.stdout.splitlines(), ['/tmp', '-P', '-m', 'worker', '--help']) - - -class PipelineTests(Fixture): - def test_full_pipeline_and_every_stage_boundary(self): - from contextlib import ExitStack - stages = ['inspect', 'metadata', 'verify', 'build', 'package', 'release', 'upload', 'binstall'] - repair_metadata(self.manifest, metadata_issues(self.manifest, self.project.repository)) - binary = self.root / 'tool' - binary.write_bytes(b'release binary') - for index, stage in enumerate(stages): - calls = [] - with self.subTest(stage=stage), ExitStack() as stack: - stack.enter_context(patch.dict(os.environ, {'GITEA_TOKEN': 'test-token'})) - stack.enter_context(patch('worker.cli.inspect_project', return_value=self.project)) - stack.enter_context(patch('worker.cli.runner.run', return_value='')) - stack.enter_context(patch('worker.cli.validate_checkout_tag', return_value=True)) - stack.enter_context(patch('worker.cli.prerequisites', return_value=([], 'podman'))) - stack.enter_context(patch('worker.cli.verify_source', side_effect=lambda *a: calls.append('verify'))) - def fake_build(*args): - calls.append('build') - return {'x86_64-unknown-linux-musl': binary} - stack.enter_context(patch('worker.cli.build', side_effect=fake_build)) - def fake_package(*args): - calls.append('package') - return package(*args) - stack.enter_context(patch('worker.cli.package', side_effect=fake_package)) - api = stack.enter_context(patch('worker.cli.Gitea')).return_value.__enter__.return_value - def fake_release(*args): - calls.append('release') - return {'id': 1, 'draft': False} - api.ensure_release.side_effect = fake_release - api.upload.side_effect = lambda *a: calls.append('upload') - stack.enter_context(patch('worker.cli.verify_published', side_effect=lambda *a: calls.append('download'))) - stack.enter_context(patch('worker.cli.verify_binstall', side_effect=lambda *a: calls.append('binstall'))) - result = CliRunner().invoke(app, [str(self.root), '--stop-after', stage]) - self.assertEqual(result.exit_code, 0, result.output) - expected = stages[2:index + 1] - if stage == 'binstall': - expected = expected[:-1] + ['download', 'binstall'] - self.assertEqual(calls, expected) - - def test_git_local_tag_policy_and_clone_cleanup(self): - run('git', 'init', str(self.root)) - run('git', 'add', 'Cargo.toml', cwd=self.root) - run('git', '-c', 'commit.gpgsign=false', '-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'commit', '-m', 'fixture', cwd=self.root) - run('git', '-c', 'tag.gpgsign=false', 'tag', 'v0.1.0', cwd=self.root) - self.assertTrue(validate_checkout_tag(self.root, 'v0.1.0')) - self.assertFalse(validate_checkout_tag(self.root, 'v0.2.0', required=False)) - self.manifest.write_text(self.manifest.read_text() + '\n# newer commit\n') - run('git', 'add', 'Cargo.toml', cwd=self.root) - run('git', '-c', 'commit.gpgsign=false', '-c', 'user.name=Test', '-c', 'user.email=test@example.invalid', 'commit', '-m', 'second', cwd=self.root) - with self.assertRaisesRegex(ValueError, 'does not point'): - validate_checkout_tag(self.root, 'v0.1.0') - managed = self.root / 'managed' - managed.mkdir() - with patch('worker.project.tempfile.mkdtemp', return_value=str(managed)), \ - patch('worker.project.run', side_effect=CommandError('clone failed')): - with self.assertRaises(CommandError): - with acquire_project('https://host/org/demo'): - pass - self.assertFalse(managed.exists()) - self.assertTrue(self.manifest.exists()) - - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_workspace.py b/tests/test_workspace.py deleted file mode 100644 index d9b180e..0000000 --- a/tests/test_workspace.py +++ /dev/null @@ -1,101 +0,0 @@ -import json -from pathlib import Path -import tempfile -import unittest -from unittest.mock import patch - -from worker.build import build, verify_source -from worker.config import load_config -from worker.manifest import inspect_project, metadata_issues, repair_metadata - - -class WorkspaceTests(unittest.TestCase): - def setUp(self): - self.temp = tempfile.TemporaryDirectory() - self.addCleanup(self.temp.cleanup) - self.root = Path(self.temp.name) - self.root_manifest = self.root / 'Cargo.toml' - self.root_manifest.write_text('[workspace]\nmembers=["cli", "library"]\n[workspace.package]\nversion="1.2.3"\n') - self.packages = [] - for name, kind in [('cli', 'bin'), ('library', 'lib')]: - folder = self.root / name - folder.mkdir() - manifest = folder / 'Cargo.toml' - manifest.write_text(f'# Preserve member comment\n[package]\nname="{name}"\nversion.workspace=true\n') - self.packages.append({'id': name, 'name': name, 'version': '1.2.3', - 'repository': 'https://example.org/team/tools', - 'manifest_path': str(manifest), - 'targets': [{'name': name, 'kind': [kind]}]}) - self.metadata = {'packages': self.packages, 'workspace_members': ['cli', 'library'], - 'workspace_root': str(self.root), 'target_directory': str(self.root / 'target')} - - def inspect(self, name=None, root=None): - with patch('worker.manifest.run', side_effect=[json.dumps(self.metadata), 'a' * 40]): - return inspect_project(root or self.root, name) - - def test_explicit_member_and_inherited_version(self): - project = self.inspect('cli') - self.assertEqual(project.manifest, self.root / 'cli/Cargo.toml') - self.assertEqual(project.root, self.root) - self.assertEqual(project.version, '1.2.3') - self.assertEqual(project.repository, 'https://example.org/team/tools') - self.assertEqual(project.target_directory, self.root / 'target') - - def test_ambiguous_root_and_member_path_require_selection(self): - for source in (self.root, self.root / 'cli'): - with self.assertRaisesRegex(ValueError, '--package NAME; discovered: cli, library'): - self.inspect(root=source) - - def test_unknown_library_and_multi_binary_rejected(self): - with self.assertRaisesRegex(ValueError, 'not found uniquely; discovered: cli, library'): - self.inspect('unknown') - with self.assertRaisesRegex(ValueError, 'exactly one binary; discovered: none'): - self.inspect('library') - self.packages[0]['targets'].append({'name': 'second', 'kind': ['bin']}) - with self.assertRaisesRegex(ValueError, 'cli, second'): - self.inspect('cli') - - def test_nonmembers_are_not_selectable(self): - self.metadata['workspace_members'] = ['cli'] - with self.assertRaisesRegex(ValueError, 'not found uniquely; discovered: cli'): - self.inspect('library') - - def test_only_selected_manifest_is_repaired(self): - root_before = self.root_manifest.read_bytes() - sibling = self.root / 'library/Cargo.toml' - sibling_before = sibling.read_bytes() - project = self.inspect('cli') - repair_metadata(project.manifest, metadata_issues(project.manifest, project.repository)) - self.assertEqual(self.root_manifest.read_bytes(), root_before) - self.assertEqual(sibling.read_bytes(), sibling_before) - self.assertIn('version.workspace=true', project.manifest.read_text()) - self.assertIn('# Preserve member comment', project.manifest.read_text()) - self.assertEqual(metadata_issues(project.manifest, project.repository), []) - - def test_build_check_and_test_select_package_at_workspace_root(self): - project = self.inspect('cli', self.root / 'cli') - settings = load_config(targets=['x86_64-unknown-linux-musl']) - def fake_run(*args, **kwargs): - if args[:2] == ('cross', 'build'): - target_dir = Path(args[args.index('--target-dir') + 1]) - binary = target_dir / settings.targets[0] / 'release/cli' - binary.parent.mkdir(parents=True) - binary.write_bytes(b'fixture') - return '' - with patch('worker.build.run', side_effect=fake_run) as run, patch('worker.build.shutil.which', return_value=None): - verify_source(project, settings, False) - outputs = build(project, settings, 'podman') - self.assertTrue(outputs[settings.targets[0]].is_file()) - self.assertIn('binstall-publisher', outputs[settings.targets[0]].parts) - self.assertEqual([c.args[:2] for c in run.call_args_list], - [('cargo', 'check'), ('cargo', 'test'), ('cross', 'build')]) - for call in run.call_args_list: - self.assertEqual(call.args[call.args.index('--package') + 1], 'cli') - self.assertEqual(call.kwargs['cwd'], self.root) - self.assertIn('--locked', call.args) - self.assertIn('--bin', run.call_args.args) - - def test_single_package_still_works_without_selection(self): - self.metadata['workspace_members'] = ['cli'] - project = self.inspect(root=self.root / 'cli') - self.assertEqual(project.package_name, 'cli') diff --git a/worker/__init__.py b/worker/__init__.py deleted file mode 100644 index 3dc1f76..0000000 --- a/worker/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__version__ = "0.1.0" diff --git a/worker/__main__.py b/worker/__main__.py deleted file mode 100644 index 21631a2..0000000 --- a/worker/__main__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .cli import app - - -if __name__ == "__main__": - app() diff --git a/worker/build.py b/worker/build.py deleted file mode 100644 index 7a71e7f..0000000 --- a/worker/build.py +++ /dev/null @@ -1,93 +0,0 @@ -import os -import shutil -import tempfile -from collections.abc import Mapping -import tomlkit -from pathlib import Path -from .config import Config -from .models import RustProject, BuildTarget -from .runner import run, CommandError, console - - - -def validate_cross_config(root: Path): - """Do not execute shell hooks supplied through repository cross metadata.""" - def inspect(value): - if isinstance(value, Mapping): - for key, child in value.items(): - if key in ('pre-build', 'dockerfile', 'runner'): - raise ValueError(f'Repository cross configuration contains unsupported executable option {key!r}.') - inspect(child) - manifest = tomlkit.parse((root / 'Cargo.toml').read_text()) - workspace = manifest.get('workspace', {}) - if isinstance(workspace, Mapping): - metadata = workspace.get('metadata', {}) - if isinstance(metadata, Mapping): - inspect(metadata.get('cross', {})) - path = root / 'Cross.toml' - if path.exists(): - inspect(tomlkit.parse(path.read_text())) - - -def prerequisites(project: RustProject, config: Config, binstall: bool = True) -> tuple[list[str], str | None]: - problems = [] - try: - validate_cross_config(project.root) - except ValueError as exc: - problems.append(str(exc)) - for tool in ['git', 'cargo', 'cross'] + (['cargo-binstall'] if binstall else []): - found = shutil.which(tool) - console.print(f'{tool}: {found or "missing"}') - if not found: - problems.append(f'{tool} is missing') - requested = os.environ.get('CROSS_CONTAINER_ENGINE') - engine = requested or next((t for t in ('docker', 'podman') if shutil.which(t)), None) - if engine not in ('docker', 'podman'): - problems.append('Set CROSS_CONTAINER_ENGINE to docker or podman with a usable backend') - engine = None - else: - try: - run(engine, 'info', timeout=20) - console.print(f'{engine}: usable') - except CommandError as exc: - problems.append(str(exc)) - if config.locked and not (project.root / 'Cargo.lock').is_file(): - problems.append('Cargo.lock is missing; generate it explicitly or use --unlocked') - return problems, engine - - -def verify_source(project: RustProject, config: Config, skip_tests: bool): - flags = ['--package', project.package_name] - if config.locked: - flags.append('--locked') - run('cargo', 'check', *flags, cwd=project.root, capture=False) - if not skip_tests: - run('cargo', 'test', *flags, cwd=project.root, capture=False) - - -def build(project: RustProject, config: Config, engine: str) -> dict[str, Path]: - flags = ['--release'] if config.profile == 'release' else ['--profile', config.profile] - if config.locked: - flags += ['--locked'] - # Explicit directory makes Cargo config / CARGO_TARGET_DIR overrides unambiguous. - target_base = (project.target_directory or project.root / 'target') / 'binstall-publisher' - target_base.mkdir(parents=True, exist_ok=True) - target_dir = Path(tempfile.mkdtemp(prefix='build-', dir=target_base)) - console.print(f'Fresh build directory: {target_dir}') - environment = {'CROSS_CONTAINER_ENGINE': engine} - if shutil.which('rustup'): - selected = run('rustup', 'show', 'active-toolchain', cwd=project.root).split() - if selected: - environment['RUSTUP_TOOLCHAIN'] = selected[0] - profile_dir = 'debug' if config.profile == 'dev' else config.profile - outputs = {} - for target in config.targets: - console.print(f'Building {target}') - run('cross', 'build', *flags, '--target', target, '--target-dir', str(target_dir), - '--package', project.package_name, '--bin', project.binaries[0], cwd=project.root, capture=False, - env=environment) - binary = target_dir / target / profile_dir / BuildTarget(target).binary_filename(project.binaries[0]) - if not binary.is_file() or binary.is_symlink(): - raise ValueError(f'Build did not produce a regular binary: {binary}') - outputs[target] = binary - return outputs diff --git a/worker/cli.py b/worker/cli.py deleted file mode 100644 index f65304f..0000000 --- a/worker/cli.py +++ /dev/null @@ -1,149 +0,0 @@ -"""CLI argument handling and ordered release orchestration.""" -import os -from pathlib import Path -import traceback -import typer -from . import runner -from .build import build, prerequisites, verify_source -from .config import DEFAULT_CONFIG, load_config -from .gitea import Gitea -from .manifest import inspect_project, metadata_issues, repair_metadata -from .models import Stage -from .prepare import prepare_repository -from .package import package -from .policy import archive_name, expected_tag -from .project import acquire_project, origin_url, parse_repository, validate_checkout_tag -from .verify import verify_binstall, verify_published - -app = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=False) -console = runner.console - - -@app.command() -def release( - source: str = typer.Argument(..., help='Local directory or HTTP(S)/SSH Git URL.'), - package_name: str | None = typer.Option(None, '--package', help='Exact workspace package name; required for workspaces.'), - tag: str | None = typer.Option(None, '--tag', '-t'), - tag_prefix: str | None = typer.Option(None, '--tag-prefix', help='Override version tag prefix; use an empty string for unprefixed tags.'), - target: list[str] | None = typer.Option(None, '--target', help='Rust target; repeatable.'), - dry_run: bool = typer.Option(False, '--dry-run'), - fix_metadata: bool = typer.Option(False, '--fix-metadata'), - prepare_repo: bool = typer.Option(False, '--prepare-repo', help='Prepare release inputs, then exit without publishing.'), - commit_preparation: bool = typer.Option(False, '--commit-preparation', help='Commit prepared files; requires a clean checkout.'), - create_tag: bool = typer.Option(False, '--create-tag', help='Create the version tag after committing preparation; never move tags.'), - push_preparation: bool = typer.Option(False, '--push-preparation', help='Atomically push prepared branch and tag to origin.'), - cross_image_tag: str | None = typer.Option(None, '--cross-image-tag', help='Set cross image tags during preparation, e.g. main.'), - skip_tests: bool = typer.Option(False, '--skip-tests', help='Skip cargo test; cargo check still runs.'), - skip_binstall_test: bool = typer.Option(False, '--skip-binstall-test'), - draft: bool = typer.Option(False, '--draft'), - prerelease: bool = typer.Option(False, '--prerelease'), - replace_assets: bool = typer.Option(False, '--replace-assets'), - keep_workdir: bool = typer.Option(False, '--keep-workdir'), - verbose: bool = typer.Option(False, '--verbose'), - stop_after: Stage | None = typer.Option(None, '--stop-after'), - config: Path = typer.Option(DEFAULT_CONFIG, '--config'), - profile: str | None = typer.Option(None, '--profile'), - unlocked: bool = typer.Option(False, '--unlocked'), - repository_url: str | None = typer.Option(None, '--repository-url', help='Explicit Gitea web repository URL.'), -): - runner.verbose = verbose - try: - settings = load_config(config, targets=target or None, profile=profile, locked=False if unlocked else None, tag_prefix=tag_prefix) - runner.register_secret(settings.token_env) - if (commit_preparation or create_tag or push_preparation or cross_image_tag is not None) and not prepare_repo: - raise ValueError('Preparation options require --prepare-repo.') - if prepare_repo and stop_after is not None: - raise ValueError('--prepare-repo is a separate mode; omit --stop-after.') - with acquire_project(source, None if prepare_repo else tag, keep_workdir or prepare_repo) as root: - project = inspect_project(root, package_name) - release_tag = expected_tag(project.version, settings.tag_prefix) - if tag is not None and tag != release_tag: - raise ValueError(f'Tag {tag!r} does not match package version; expected {release_tag!r}.') - project.tag = release_tag - publishing = not prepare_repo and not dry_run and stop_after not in (Stage.inspect, Stage.metadata, Stage.verify, Stage.build, Stage.package) - if publishing and runner.run('git', 'status', '--porcelain', '--untracked-files=normal', cwd=root): - raise ValueError('Publication requires a clean checkout before metadata repair.') - displayed_repository = 'missing' - if project.repository: - try: - displayed_repository = parse_repository(project.repository).url - except ValueError: - displayed_repository = 'invalid (use --repository-url to repair)' - for key, value in [('Package', project.package_name), ('Version', project.version), - ('Repository', displayed_repository), ('Binary', project.binaries[0]), - ('Manifest', str(project.manifest)), ('Commit', project.commit or 'unknown'), ('Tag', release_tag)]: - console.print(f'{key}: {value}') - if stop_after == Stage.inspect: - return - candidate = repository_url or project.repository or origin_url(root) - if not candidate: - raise ValueError('package.repository is missing; provide --repository-url and --fix-metadata.') - repository = parse_repository(candidate) - if prepare_repo: - prepare_repository(project, settings, repository, image_tag=cross_image_tag, - dry_run=dry_run, commit=commit_preparation, - create_tag=create_tag, push=push_preparation) - return - issues = metadata_issues(project.manifest, repository.url, settings.tag_prefix) - for issue in issues: - console.print(runner.redact(f'{issue.key}: {issue.actual!r} -> {issue.expected!r}')) - if not issues: - console.print('Binstall metadata: valid') - if issues and fix_metadata and not dry_run: - repair_metadata(project.manifest, issues) - console.print('Metadata repaired locally. Commit and push explicitly if desired.') - if issues and not fix_metadata and not dry_run: - raise ValueError('Metadata requires repair. Rerun with --fix-metadata.') - if stop_after == Stage.metadata: - return - problems, engine = prerequisites(project, settings, not skip_binstall_test and stop_after not in (Stage.verify, Stage.build, Stage.package, Stage.release, Stage.upload)) - tag_exists = validate_checkout_tag(root, release_tag, required=False) - if not tag_exists: - problems.append(f'Git tag {release_tag!r} is missing; create it explicitly before publishing') - console.print(f'Release destination: {repository.url}/releases/tag/{release_tag}') - for triple in settings.targets: - console.print(f'{triple}: {archive_name(project.package_name, triple, project.version)}') - for problem in problems: - console.print('Prerequisite: ' + problem) - if dry_run: - console.print('Dry run complete: no manifest edits, builds or release mutations.') - if issues or problems: - raise ValueError('Dry run found the metadata/prerequisite issues reported above.') - return - # Missing tags only prevent publishing; package-only work is useful before tagging. - build_problems = [p for p in problems if not p.startswith('Git tag ')] - if build_problems: - raise ValueError('Resolve the prerequisite issues above before building.') - if publishing: - validate_checkout_tag(root, release_tag) - if not os.environ.get(settings.token_env): - raise ValueError(f'Set {settings.token_env} before publishing.') - verify_source(project, settings, skip_tests) - if stop_after == Stage.verify: - return - outputs = build(project, settings, engine) - if stop_after == Stage.build: - return - artifacts, sums = package(project, outputs) - console.print(f'Artifacts: {sums.parent}') - if stop_after == Stage.package: - return - with Gitea(repository, settings.token_env) as api: - remote_release = api.ensure_release(release_tag, project.commit, draft, prerelease) - console.print(f'Release ready: {release_tag}') - if stop_after == Stage.release: - return - api.upload(remote_release['id'], [a.path for a in artifacts] + [sums], replace_assets) - if stop_after == Stage.upload: - return - if remote_release.get('draft'): - raise ValueError('Draft uploaded. Public verification requires publishing the draft; use --stop-after upload for draft workflows.') - verify_published(repository.url, release_tag, artifacts, sums) - if not skip_binstall_test: - verify_binstall(project, settings.targets) - console.print('Release completed and published artifacts verified.') - except Exception as exc: - if verbose: - console.print(runner.redact(traceback.format_exc())) - console.print('error: ' + runner.redact(str(exc))) - raise typer.Exit(1) from None diff --git a/worker/config.py b/worker/config.py deleted file mode 100644 index adc3216..0000000 --- a/worker/config.py +++ /dev/null @@ -1,45 +0,0 @@ -from dataclasses import dataclass -from pathlib import Path -import re -from collections.abc import Mapping -import tomlkit - -DEFAULT_CONFIG = Path(__file__).resolve().parent.parent / 'config.toml' - - -@dataclass -class Config: - targets: list[str] - profile: str - locked: bool - tag_prefix: str - token_env: str - - -def load_config(path: Path = DEFAULT_CONFIG, *, targets=None, profile=None, locked=None, tag_prefix=None) -> Config: - data = tomlkit.parse(path.read_text()) - for section in ('build', 'release', 'gitea', 'package'): - if not isinstance(data.get(section, {}), Mapping): - raise ValueError(f'Configuration [{section}] must be a table.') - build, release, gitea = (data.get(s, {}) for s in ('build', 'release', 'gitea')) - result = Config(targets if targets is not None else build.get('targets', []), - profile if profile is not None else build.get('profile', 'release'), - locked if locked is not None else build.get('locked', True), - tag_prefix if tag_prefix is not None else release.get('tag-prefix', 'v'), gitea.get('token-env', 'GITEA_TOKEN')) - if not isinstance(result.targets, list) or not result.targets or any( - not isinstance(t, str) or not re.fullmatch(r'[A-Za-z0-9_]+(?:-[A-Za-z0-9_]+){2,}', t) - for t in result.targets - ): - raise ValueError('build.targets must be a nonempty list of Rust target triples.') - result.targets = list(dict.fromkeys(result.targets)) - if not isinstance(result.profile, str) or not re.fullmatch(r'[A-Za-z][A-Za-z0-9_-]*', result.profile): - raise ValueError('build.profile must be a Cargo profile name.') - if not isinstance(result.locked, bool): - raise ValueError('build.locked must be a boolean.') - if not isinstance(result.tag_prefix, str) or not re.fullmatch(r'[A-Za-z0-9._-]*', result.tag_prefix): - raise ValueError('release.tag-prefix must contain only letters, digits, dots, underscores or hyphens.') - if not isinstance(result.token_env, str) or not re.fullmatch(r'[A-Za-z_][A-Za-z0-9_]*', result.token_env): - raise ValueError('gitea.token-env must be an environment variable name.') - if data.get('package', {}).get('format', 'tgz') != 'tgz': - raise ValueError('Only package.format = "tgz" is supported.') - return result diff --git a/worker/gitea.py b/worker/gitea.py deleted file mode 100644 index 3f1e46f..0000000 --- a/worker/gitea.py +++ /dev/null @@ -1,72 +0,0 @@ -"""Gitea API operations, isolated for MockTransport tests.""" -import os -from pathlib import Path -from urllib.parse import quote -import httpx -from .project import Repository -from .runner import console - - -class Gitea: - def __init__(self, repository: Repository, token_env: str, transport=None): - token = os.environ.get(token_env) - if not token: - raise ValueError(f'Set {token_env} before publishing.') - if not repository.base_url.startswith('https://'): - raise ValueError('Authenticated Gitea publishing requires HTTPS.') - self.client = httpx.Client( - base_url=repository.base_url + '/api/v1/', - headers={'Authorization': 'token ' + token}, timeout=120, - follow_redirects=False, transport=transport) - self.path = f'repos/{quote(repository.owner, safe="")}/{quote(repository.name, safe="")}' - - def __enter__(self): - return self - - def __exit__(self, *args): - self.client.close() - - def request(self, method, path, *, missing_ok=False, **kwargs): - try: - response = self.client.request(method, self.path + path, **kwargs) - except httpx.HTTPError: - raise RuntimeError(f'Gitea {method} {path}: connection failed.') from None - if missing_ok and response.status_code == 404: - return None - if not response.is_success: - # Do not echo server response bodies: they can reflect credentials. - raise RuntimeError(f'Gitea {method} {path}: HTTP {response.status_code}.') - return response.json() if response.content else None - - def ensure_release(self, tag, commit, draft=False, prerelease=False): - remote_tag = self.request('GET', '/tags/' + quote(tag, safe=''), missing_ok=True) - if remote_tag is None: - raise ValueError(f'Remote tag {tag!r} is missing. Push the tag explicitly before publishing.') - if remote_tag.get('commit', {}).get('sha') != commit: - raise ValueError('Remote tag commit differs from the checkout; refusing publication.') - release = self.request('GET', '/releases/tags/' + quote(tag, safe=''), missing_ok=True) - if release is None: - release = self.request('POST', '/releases', json={ - 'tag_name': tag, 'name': tag, 'target_commitish': commit, - 'draft': draft, 'prerelease': prerelease}) - return release - - def assets(self, release_id): - # Gitea's release attachment endpoint returns the full list, not paginated results. - return self.request('GET', f'/releases/{release_id}/assets') - - def upload(self, release_id, paths: list[Path], replace=False): - existing = self.assets(release_id) - names = {p.name for p in paths} - collisions = [a for a in existing if a['name'] in names] - if collisions and not replace: - raise ValueError('Release assets already exist: ' + ', '.join(a['name'] for a in collisions) + - '. Use --replace-assets to replace them explicitly.') - for path in paths: - for asset in collisions: - if asset['name'] == path.name: - self.request('DELETE', f'/releases/{release_id}/assets/{asset["id"]}') - with path.open('rb') as stream: - self.request('POST', f'/releases/{release_id}/assets', - params={'name': path.name}, files={'attachment': (path.name, stream, 'application/octet-stream')}) - console.print(f'Uploaded {path.name}') diff --git a/worker/manifest.py b/worker/manifest.py deleted file mode 100644 index b536989..0000000 --- a/worker/manifest.py +++ /dev/null @@ -1,96 +0,0 @@ -import json -import re -from collections.abc import Mapping -from pathlib import Path -import tomlkit -from .models import RustProject, MetadataIssue -from .policy import binstall_metadata -from .runner import run, CommandError - - -def read_manifest(path: Path): - document = tomlkit.parse(path.read_text(encoding='utf-8')) - if not isinstance(document.get('package'), Mapping): - raise ValueError('Cargo.toml contains no [package] table. Select a package manifest or use --package NAME at the workspace root.') - return document - - -def inspect_project(root: Path, package_name: str | None = None) -> RustProject: - source_manifest = root / 'Cargo.toml' - document = tomlkit.parse(source_manifest.read_text(encoding='utf-8')) - metadata = json.loads(run('cargo', 'metadata', '--format-version=1', '--no-deps', cwd=root)) - member_ids = set(metadata.get('workspace_members', [])) - packages = [p for p in metadata.get('packages', []) if p['id'] in member_ids] - discovered = ', '.join(sorted(p['name'] for p in packages)) or 'none' - if package_name is None: - if not isinstance(document.get('package'), Mapping) or len(packages) != 1: - raise ValueError('Workspace selection requires --package NAME; discovered: ' + discovered) - pkg = packages[0] - else: - matches = [p for p in packages if p['name'] == package_name] - if len(matches) != 1: - raise ValueError(f'Workspace package {package_name!r} was not found uniquely; discovered: {discovered}') - pkg = matches[0] - binaries = [t['name'] for t in pkg['targets'] if 'bin' in t['kind']] - if len(binaries) != 1: - raise ValueError(f'Package {pkg["name"]!r} must have exactly one binary; discovered: ' + - (', '.join(binaries) or 'none')) - for value in (pkg['name'], binaries[0], pkg['version']): - if not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.+-]*', value): - raise ValueError('Unsafe package, version or binary filename.') - workspace_root = Path(metadata['workspace_root']).resolve() - manifest = Path(pkg['manifest_path']) - if not manifest.resolve().is_relative_to(workspace_root): - raise ValueError('Selected package manifest is outside the workspace root.') - read_manifest(manifest) - try: - commit = run('git', 'rev-parse', 'HEAD', cwd=root) - except CommandError: - commit = None - return RustProject(workspace_root, manifest, pkg['name'], pkg['version'], pkg.get('repository'), - binaries, commit, target_directory=Path(metadata['target_directory'])) - - -def metadata_issues(path: Path, repository: str, prefix='v') -> list[MetadataIssue]: - package = read_manifest(path)['package'] - issues = [] - if package.get('repository') != repository: - issues.append(MetadataIssue('package.repository', package.get('repository'), repository)) - metadata = package.get('metadata', {}) - if not isinstance(metadata, Mapping): - raise ValueError('package.metadata must be a table.') - binstall = metadata.get('binstall', {}) - if not isinstance(binstall, Mapping): - raise ValueError('package.metadata.binstall must be a table.') - for key, value in binstall_metadata(prefix).items(): - if binstall.get(key) != value: - issues.append(MetadataIssue('package.metadata.binstall.' + key, binstall.get(key), value)) - # Overrides and other custom options can change URL resolution or prevent installation. - for key in binstall: - if key not in binstall_metadata(prefix): - issues.append(MetadataIssue('package.metadata.binstall.' + key, binstall[key], None)) - return issues - - -def repair_metadata(path: Path, issues: list[MetadataIssue], *, write: bool = True) -> str: - if path.is_symlink(): - raise ValueError('Refusing to repair a symlinked Cargo.toml.') - document = read_manifest(path) - for issue in issues: - keys = issue.key.split('.') - # Custom override names can contain dots; remove the top-level custom option. - if issue.expected is None: - document['package']['metadata']['binstall'].pop( - issue.key.removeprefix('package.metadata.binstall.'), None - ) - continue - table = document - for key in keys[:-1]: - if key not in table: - table[key] = tomlkit.table() - table = table[key] - table[keys[-1]] = issue.expected - rendered = tomlkit.dumps(document) - if write: - path.write_text(rendered, encoding='utf-8') - return rendered diff --git a/worker/models.py b/worker/models.py deleted file mode 100644 index 879e2e2..0000000 --- a/worker/models.py +++ /dev/null @@ -1,55 +0,0 @@ -from dataclasses import dataclass, field -from enum import Enum -from pathlib import Path - - -class Stage(str, Enum): - inspect = 'inspect' - metadata = 'metadata' - verify = 'verify' - build = 'build' - package = 'package' - release = 'release' - upload = 'upload' - binstall = 'binstall' - - -@dataclass -class RustProject: - root: Path - manifest: Path - package_name: str - version: str - repository: str | None = None - binaries: list[str] = field(default_factory=list) - commit: str | None = None - tag: str | None = None - target_directory: Path | None = None - - -@dataclass(frozen=True) -class BuildTarget: - triple: str - - @property - def is_windows(self): - return 'windows' in self.triple.split('-') - - def binary_filename(self, binary: str) -> str: - return binary + ('.exe' if self.is_windows else '') - - -@dataclass -class MetadataIssue: - key: str - actual: object - expected: object - - -@dataclass -class Artifact: - target: str - path: Path - root: str - binary: str - sha256: str diff --git a/worker/package.py b/worker/package.py deleted file mode 100644 index d6a7742..0000000 --- a/worker/package.py +++ /dev/null @@ -1,40 +0,0 @@ -import gzip -import hashlib -from pathlib import Path -import tarfile -import tempfile -from .models import Artifact, BuildTarget, RustProject -from .policy import archive_name, archive_root - - -def checksum(path: Path) -> str: - with path.open('rb') as stream: - return hashlib.file_digest(stream, 'sha256').hexdigest() - - -def package(project: RustProject, outputs: dict[str, Path]) -> tuple[list[Artifact], Path]: - dist = project.root / 'dist' - if dist.is_symlink(): - raise ValueError('Refusing a symlinked dist directory.') - dist.mkdir(exist_ok=True) - # A fresh managed directory avoids deleting or overwriting unrelated dist contents. - staging = Path(tempfile.mkdtemp(prefix='release-', dir=dist)) - artifacts = [] - for target, binary_path in outputs.items(): - root = archive_root(project.package_name, target, project.version) - binary = BuildTarget(target).binary_filename(project.binaries[0]) - path = staging / archive_name(project.package_name, target, project.version) - with path.open('wb') as raw, gzip.GzipFile(filename='', mode='wb', fileobj=raw, mtime=0) as gz: - with tarfile.open(fileobj=gz, mode='w', format=tarfile.USTAR_FORMAT) as archive: - directory = tarfile.TarInfo(root) - directory.type, directory.mode = tarfile.DIRTYPE, 0o755 - archive.addfile(directory) - info = tarfile.TarInfo(root + '/' + binary) - info.size = binary_path.stat().st_size - info.mode = 0o644 if BuildTarget(target).is_windows else (binary_path.stat().st_mode & 0o777) | 0o111 - with binary_path.open('rb') as content: - archive.addfile(info, content) - artifacts.append(Artifact(target, path, root, binary, checksum(path))) - sums = staging / 'SHA256SUMS' - sums.write_text(''.join(f'{a.sha256} {a.path.name}\n' for a in sorted(artifacts, key=lambda a: a.path.name))) - return artifacts, sums diff --git a/worker/policy.py b/worker/policy.py deleted file mode 100644 index 01288ae..0000000 --- a/worker/policy.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Single source of truth for archive and cargo-binstall conventions.""" - - -def expected_tag(version: str, prefix: str = 'v') -> str: - return prefix + version - - -def archive_root(name: str, target: str, version: str) -> str: - return f'{name}-{target}-v{version}' - - -def archive_name(name: str, target: str, version: str) -> str: - return archive_root(name, target, version) + '.tgz' - - -def binstall_metadata(prefix: str = 'v') -> dict[str, str]: - root = archive_root('{ name }', '{ target }', '{ version }') - return { - 'pkg-url': '{ repo }/releases/download/' + prefix + '{ version }/' + root + '.tgz', - 'pkg-fmt': 'tgz', - 'bin-dir': root + '/{ bin }{ binary-ext }', - } - - -def asset_url(repository: str, tag: str, filename: str) -> str: - from urllib.parse import quote - return f'{repository}/releases/download/{quote(tag, safe="")}/{quote(filename, safe="")}' diff --git a/worker/prepare.py b/worker/prepare.py deleted file mode 100644 index 855075e..0000000 --- a/worker/prepare.py +++ /dev/null @@ -1,124 +0,0 @@ -"""Reviewable, opt-in repository preparation; never rewrite Rust source or existing tags.""" -from collections.abc import Mapping -from difflib import unified_diff -from pathlib import Path -import re -import tomlkit - -from .config import Config -from .manifest import metadata_issues, repair_metadata -from .models import RustProject -from .project import Repository, push_preparation, validate_checkout_tag -from .runner import run, console, redact - - -def preparation_files(project: RustProject, config: Config, repository: Repository, - image_tag: str | None = None) -> dict[Path, str]: - if image_tag is not None and not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', image_tag): - raise ValueError('--cross-image-tag must be a container image tag such as main or 0.2.5.') - paths = [project.manifest, project.root / '.gitignore', project.root / 'Cross.toml', - project.root / 'Cargo.lock'] - if any(p.is_symlink() for p in paths): - raise ValueError('Repository preparation refuses symlinked managed files.') - issues = metadata_issues(project.manifest, repository.url, config.tag_prefix) - files = {project.manifest: repair_metadata(project.manifest, issues, write=False)} - ignore = project.root / '.gitignore' - contents = ignore.read_text() if ignore.exists() else '' - missing = [line for line in ('/dist/', '/target/', '!/Cargo.lock') if line not in contents.splitlines()] - if missing: - contents = contents.rstrip('\n') + '\n\n# Release publisher\n' + '\n'.join(missing) + '\n' - files[ignore] = contents - cross_path = project.root / 'Cross.toml' - cross = tomlkit.parse(cross_path.read_text()) if cross_path.exists() else tomlkit.document() - - def table(parent, key): - if key not in parent: - parent[key] = tomlkit.table() - if not isinstance(parent[key], Mapping): - raise ValueError(f'Cross.toml {key!r} must be a table.') - return parent[key] - - env = table(table(cross, 'build'), 'env') - passthrough = env.get('passthrough', []) - if not isinstance(passthrough, list) or any(not isinstance(v, str) for v in passthrough): - raise ValueError('Cross.toml build.env.passthrough must be a list of strings.') - if 'RUSTUP_TOOLCHAIN' not in passthrough: - env['passthrough'] = [*passthrough, 'RUSTUP_TOOLCHAIN'] - if image_tag: - targets = table(cross, 'target') - for triple in config.targets: - table(targets, triple)['image'] = f'ghcr.io/cross-rs/{triple}:{image_tag}' - files[cross_path] = tomlkit.dumps(cross) - return files - - -def prepare_repository(project: RustProject, config: Config, repository: Repository, *, - image_tag: str | None = None, dry_run=False, commit=False, - create_tag=False, push=False): - """Prepare known release inputs. Git writes happen only through explicit flags.""" - if push and not (commit and create_tag): - raise ValueError('--push-preparation requires --commit-preparation and --create-tag.') - if create_tag and not commit: - raise ValueError('--create-tag requires --commit-preparation.') - if not project.tag: - raise ValueError('Preparation requires a resolved release tag.') - files = preparation_files(project, config, repository, image_tag) - changed = {path: text for path, text in files.items() - if not path.exists() or path.read_text() != text} - lock = project.root / 'Cargo.lock' - generate_lock = not lock.exists() - branch = None - if commit: - if run('git', 'status', '--porcelain', '--untracked-files=normal', cwd=project.root): - raise ValueError('Start --commit-preparation from a clean checkout, including the index. ' - 'Review and commit earlier local edits separately.') - if create_tag: - exists = run('git', 'tag', '--list', project.tag, cwd=project.root) - untracked_managed = any( - path.exists() and not run('git', 'ls-files', '--', str(path), cwd=project.root) - for path in [*files, lock] - ) - if exists and (changed or generate_lock or untracked_managed): - raise ValueError(f'Tag {project.tag!r} already exists; preparation will not move it. ' - 'Use a new package version or prepare without --create-tag.') - if exists: - validate_checkout_tag(project.root, project.tag) - if push: - branch = run('git', 'symbolic-ref', '--quiet', '--short', 'HEAD', cwd=project.root) - if not branch: - raise ValueError('Pushing preparation requires a checked-out branch.') - for path, text in changed.items(): - old = path.read_text() if path.exists() else '' - label = str(path.relative_to(project.root)) - console.print(redact(''.join(unified_diff(old.splitlines(True), text.splitlines(True), - fromfile=label, tofile=label)))) - if generate_lock: - console.print('Cargo.lock: generate with cargo generate-lockfile (may download registry metadata).') - if commit: - console.print('Git: commit only the prepared files using the configured author and signing settings.') - if create_tag: - console.print(f'Git: create or validate annotated tag {project.tag}. Existing tags are never moved.') - if push: - console.print(f'Git: atomically push branch {branch} and tag {project.tag} to origin; no force push.') - if dry_run: - console.print('Preparation dry run complete; no files, commits, tags, or remote refs changed.') - return - for path, text in changed.items(): - path.write_text(text, encoding='utf-8') - if generate_lock: - run('cargo', 'generate-lockfile', cwd=project.root, capture=False) - if commit: - # Only these files can enter the commit. The initially clean index prevents accidental inclusion. - paths = [str(path) for path in files] + [str(lock)] - run('git', 'add', '--force', '--', *paths, cwd=project.root) - if run('git', 'diff', '--cached', '--name-only', cwd=project.root): - run('git', 'commit', '-m', f'Prepare {project.package_name} {project.version} binary release', - cwd=project.root, capture=False) - project.commit = run('git', 'rev-parse', 'HEAD', cwd=project.root) - if create_tag: - if not run('git', 'tag', '--list', project.tag, cwd=project.root): - run('git', 'tag', '-a', project.tag, '-m', f'{project.package_name} {project.version}', cwd=project.root) - validate_checkout_tag(project.root, project.tag) - if push: - push_preparation(project.root, branch, project.tag, repository, config.token_env) - console.print('Repository preparation complete. Review the changes, then run the publisher without --prepare-repo.') diff --git a/worker/project.py b/worker/project.py deleted file mode 100644 index bfa79c6..0000000 --- a/worker/project.py +++ /dev/null @@ -1,114 +0,0 @@ -from contextlib import contextmanager -from dataclasses import dataclass -from pathlib import Path -import re -import shutil -import tempfile -from urllib.parse import urlsplit -from .runner import run, CommandError, console, register_secret - - -@dataclass(frozen=True) -class Repository: - base_url: str - owner: str - name: str - - @property - def url(self): - return f'{self.base_url}/{self.owner}/{self.name}' - - -def parse_repository(value: str) -> Repository: - scp = re.fullmatch(r'[^/@\s]+@([^/:\s]+):(.+)', value) - if scp: - value = f'https://{scp[1]}/{scp[2]}' - parsed = urlsplit(value) - if parsed.scheme not in ('http', 'https', 'ssh') or not parsed.hostname: - raise ValueError('Repository must be an HTTP(S) or SSH Git URL; use --repository-url.') - if parsed.password or (parsed.scheme != 'ssh' and parsed.username) or parsed.query or parsed.fragment: - raise ValueError('Repository URLs must not contain credentials, query strings or fragments.') - parts = parsed.path.strip('/').split('/') - if len(parts) < 2 or any(not re.fullmatch(r'[A-Za-z0-9_.-]+', p) or p in ('.', '..') for p in parts): - raise ValueError('Repository URL must end in owner/repository.') - name = parts[-1].removesuffix('.git') - if not name or name in ('.', '..'): - raise ValueError('Repository name is empty.') - authority = parsed.netloc if parsed.scheme != 'ssh' else parsed.hostname - base = ('https' if parsed.scheme == 'ssh' else parsed.scheme) + '://' + authority - if len(parts) > 2: - base += '/' + '/'.join(parts[:-2]) - return Repository(base, parts[-2], name) - - -@contextmanager -def acquire_project(source: str, tag: str | None = None, keep_workdir: bool = False): - local = Path(source).expanduser() - temporary = None - try: - if local.exists(): - root = local.resolve() - if not root.is_dir(): - raise ValueError('Local source must be a directory.') - else: - parse_repository(source) # Reject options and credential-bearing URLs before Git logging. - temporary = Path(tempfile.mkdtemp(prefix='binstall-publisher-')) - root = temporary / 'source' - run('git', 'clone', '--', source, str(root)) - if tag: - run('git', 'checkout', '--detach', f'refs/tags/{tag}', cwd=root) - if tag and not temporary: - validate_checkout_tag(root, tag) - yield root - finally: - if temporary: - if keep_workdir: - console.print(f'Temporary workdir kept at {temporary}') - else: - shutil.rmtree(temporary) - - -def validate_checkout_tag(root: Path, tag: str, *, required: bool = True): - try: - commit = run('git', 'rev-parse', '--verify', f'refs/tags/{tag}^{{commit}}', cwd=root) - except CommandError: - if not required: - return False - raise ValueError(f'Git tag {tag!r} does not exist. Create the version tag explicitly first.') from None - if commit != run('git', 'rev-parse', 'HEAD', cwd=root): - raise ValueError(f'Git tag {tag!r} does not point to this checkout. Check it out explicitly.') - return True - - -def origin_url(root: Path): - try: - return run('git', 'remote', 'get-url', 'origin', cwd=root) - except CommandError: - return None - - -def push_preparation(root: Path, branch: str, tag: str, repository: Repository, token_env: str): - """Push explicit refs atomically; pass a matching Gitea token only to Git's askpass.""" - import os - origin = run('git', 'remote', 'get-url', '--push', 'origin', cwd=root) - # Never send a publishing token to a different host/repository selected by Git config. - if parse_repository(origin).url != repository.url: - raise ValueError('origin push URL differs from the release repository. Fix origin explicitly before pushing.') - command = ('git', 'push', '--atomic', 'origin', f'HEAD:refs/heads/{branch}', f'refs/tags/{tag}') - register_secret(token_env) - secret = os.environ.get(token_env) - if not secret or not origin.startswith('https://'): - run(*command, cwd=root, capture=False, env={'GIT_TERMINAL_PROMPT': '0'}) - return - with tempfile.TemporaryDirectory(prefix='publisher-git-auth-') as folder: - helper = Path(folder) / 'askpass' - helper.write_text('#!/usr/bin/env python3\nimport os, sys\n' - 'print("token" if "username" in sys.argv[1].lower() ' - 'else os.environ["PUBLISHER_GIT_TOKEN"])\n') - helper.chmod(0o700) - environment = {'GIT_ASKPASS': str(helper), 'GIT_TERMINAL_PROMPT': '0', - 'PUBLISHER_GIT_TOKEN': secret, 'GIT_TRACE': '0', 'GIT_TRACE_CURL': '0', - 'GIT_TRACE_CURL_NO_DATA': '1', 'GIT_CURL_VERBOSE': '0', - 'GIT_TRACE_PACKET': '0', 'GIT_TRACE2': '0', 'GIT_TRACE2_EVENT': '0', - 'GIT_TRACE2_PERF': '0'} - run('git', '-c', 'credential.helper=', *command[1:], cwd=root, env=environment, capture=False) diff --git a/worker/runner.py b/worker/runner.py deleted file mode 100644 index 6fb2e72..0000000 --- a/worker/runner.py +++ /dev/null @@ -1,59 +0,0 @@ -"""Subprocess execution without a shell; secrets are redacted even in errors.""" -import os -import shlex -import subprocess -from collections import deque -from pathlib import Path -from rich.console import Console - -console = Console(markup=False) -verbose = False -_secret_names = {'GITEA_TOKEN'} - - -def register_secret(name: str): - _secret_names.add(name) - - -def redact(value: str) -> str: - for name in _secret_names: - secret = os.environ.get(name) - if secret: - value = value.replace(secret, '[REDACTED]') - return value - - -class CommandError(RuntimeError): - pass - - -def run(*args: str, cwd: Path | None = None, capture: bool = True, - env: dict[str, str] | None = None, timeout: float | None = None) -> str: - command = redact(shlex.join(args)) - if verbose: - console.print('$ ' + command) - environment = os.environ.copy() - # Build scripts must not inherit publishing credentials. - for name in _secret_names: - environment.pop(name, None) - environment.update(env or {}) - try: - if capture: - proc = subprocess.run(args, cwd=cwd, env=environment, text=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - timeout=timeout) - output, error, status = proc.stdout, proc.stderr, proc.returncode - else: - tail = deque(maxlen=40) - with subprocess.Popen(args, cwd=cwd, env=environment, text=True, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc: - for line in proc.stdout: - tail.append(line) - console.print(redact(line.rstrip())) - status = proc.wait() - output, error = '', ''.join(tail) - except (OSError, subprocess.TimeoutExpired) as exc: - raise CommandError(f'could not run {command}: {redact(str(exc))}') from None - if status: - raise CommandError(f'command failed ({status}): {command}\n{redact(error.strip())}') - return output.rstrip('\n') diff --git a/worker/verify.py b/worker/verify.py deleted file mode 100644 index aaaf567..0000000 --- a/worker/verify.py +++ /dev/null @@ -1,64 +0,0 @@ -from pathlib import Path -import tarfile -import tempfile -import httpx -from .models import Artifact, BuildTarget, RustProject -from .package import checksum -from .policy import asset_url -from .runner import run, console - - -def verify_archive(path: Path, artifact: Artifact): - with tarfile.open(path, 'r:gz') as archive: - members = archive.getmembers() - if len(members) != 2: - raise ValueError(f'{path.name}: expected exactly a root directory and binary.') - root, binary = members - if (root.name != artifact.root or not root.isdir() or - binary.name != artifact.root + '/' + artifact.binary or not binary.isfile()): - raise ValueError(f'{path.name}: archive layout does not match binstall metadata.') - if 'windows' not in artifact.target and not binary.mode & 0o111: - raise ValueError(f'{path.name}: binary is not executable.') - - -def verify_published(repository: str, tag: str, artifacts: list[Artifact], sums: Path, transport=None): - # Anonymous downloads prove that the metadata works for ordinary consumers. - with tempfile.TemporaryDirectory(prefix='binstall-download-') as folder, httpx.Client( - follow_redirects=True, timeout=120, transport=transport) as client: - for local in [a.path for a in artifacts] + [sums]: - downloaded = Path(folder) / local.name - try: - with client.stream('GET', asset_url(repository, tag, local.name)) as response: - if response.status_code != 200: - raise ValueError(f'{local.name}: public download returned HTTP {response.status_code}.') - count = 0 - with downloaded.open('wb') as stream: - for chunk in response.iter_bytes(): - count += len(chunk) - if count > local.stat().st_size: - raise ValueError(f'{local.name}: download exceeds expected size.') - stream.write(chunk) - except httpx.HTTPError: - raise ValueError(f'{local.name}: public download failed.') from None - if checksum(downloaded) != checksum(local): - raise ValueError(f'{local.name}: published SHA-256 differs from local artifact.') - for artifact in artifacts: - if artifact.path == local: - verify_archive(downloaded, artifact) - console.print(f'Verified URL, checksum and archive: {artifact.target}') - console.print('Verified published SHA256SUMS') - - -def verify_binstall(project: RustProject, targets: list[str]): - # Installing a foreign-target binary is safe: nothing is executed. - for target in targets: - with tempfile.TemporaryDirectory(prefix='binstall-install-') as folder: - run('cargo', 'binstall', project.package_name, '--manifest-path', str(project.manifest), - '--version', '=' + project.version, '--targets', target, - '--strategies', 'crate-meta-data', '--no-confirm', '--no-track', - '--disable-telemetry', '--no-discover-github-token', - '--install-path', folder, cwd=project.root, capture=False) - installed = Path(folder) / BuildTarget(target).binary_filename(project.binaries[0]) - if not installed.is_file(): - raise ValueError(f'cargo-binstall did not install the expected binary for {target}.') - console.print(f'cargo-binstall installation verified: {target}')