composer_patches/lib.rs
1//! Native reimplementation of the `cweagans/composer-patches` plugin.
2//!
3//! this crate never runs the PHP plugin (per the no-plugins invariant); this crate
4//! reproduces its effect: resolve the set of `(target package → [patches])`
5//! from the **root** project, materialize each patch, and apply it to the
6//! freshly-extracted package tree during install. Patches are applied
7//! in-process with the [`flickzeug`] fuzzy-matching diff library — never by
8//! shelling out to `git apply` / `patch` — so behavior is identical across
9//! Linux, macOS, and Windows.
10//!
11//! The crate is intentionally FS/PHP-agnostic, like `composer-installers` and
12//! the sibling installer crates: it parses, plans, and applies; the host
13//! (`composer-install`'s install orchestrator) decides *when* to call
14//! it and wires it into the install lifecycle.
15//!
16//! # Module map
17//!
18//! - [`model`] — the internal [`Patch`] model + v1/v2 dialect normalization.
19//! - [`diff`] — multi-file diff splitting + `-p` path stripping (over flickzeug).
20//! - [`build`] — build a project's [`PatchPlan`] from its `composer.json`.
21//! - [`apply`] — apply a patch to a tree with the `-p` fallback loop.
22//! - [`make`] — author a patch from an edited tree (the inverse of `apply`).
23//! - [`resolve`] — build the root patch set from `composer.json` + patches-file.
24//! - [`plan`] — the materialized [`PatchPlan`] + re-application fingerprints.
25//! - [`pass`] — the per-package apply pass (+ `PATCHES.txt`).
26//! - [`lock`] — the `patches.lock.json` applied-state store.
27//! - [`target`] — the `patches/` dir header-inference router.
28//! - [`report`] — per-application outcome ([`ApplyReport`]).
29
30pub mod apply;
31pub mod build;
32pub mod diff;
33pub mod lock;
34pub mod make;
35pub mod model;
36pub mod pass;
37pub mod plan;
38pub mod report;
39pub mod resolve;
40pub mod target;
41
42pub use apply::{ApplyOptions, apply_patch_text};
43pub use build::{
44 NoFetch, PatchFetcher, PlanOptions, build_plan, lock_install_paths, patches_dir_name,
45 resolve_all,
46};
47pub use model::{DepthSpec, FailureMode, Patch, PatchScope, PatchSource, parse_target_patches};
48pub use pass::{PackageApplyResult, append_patches_txt, apply_package_patches};
49pub use plan::{DeferredPatch, MaterializedPatch, PatchPlan, RootPatch, fingerprint};
50pub use report::{ApplyReport, FileAction, FileOutcome};
51pub use resolve::{resolve_patches_dir, resolve_root};
52pub use target::{InferredTarget, infer_target};
53
54use sha2::{Digest, Sha256};
55
56/// Lowercase hex sha256 of arbitrary bytes.
57///
58/// Used both to verify a downloaded patch against a declared `sha256` (v2 TOFU)
59/// and to build the per-package re-application fingerprint (Phase B): a patch's
60/// *content* hash is the thing that flips when a local `patches/foo.patch` is
61/// edited, forcing a clean re-extract + re-apply.
62pub fn content_sha256(bytes: &[u8]) -> String {
63 let mut hasher = Sha256::new();
64 hasher.update(bytes);
65 let digest = hasher.finalize();
66 let mut out = String::with_capacity(digest.len() * 2);
67 for byte in digest {
68 use std::fmt::Write;
69 let _ = write!(out, "{byte:02x}");
70 }
71 out
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn sha256_known_vector() {
80 // sha256("") well-known digest.
81 assert_eq!(
82 content_sha256(b""),
83 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
84 );
85 }
86}