Repository navigation
Conversation
e040043 to
a3035fc
Compare
| patch.push(b'\n'); | ||
| // Like Git, end a name that has spaces with a tab, so readers know | ||
| // where it stops. | ||
| let end: &[u8] = if a[0] != b'"' && path.contains(&b' ') { |
There was a problem hiding this comment.
Quoted paths with spaces break cargo-mutants
A path with both a space and a byte that makes header_path quote it (backslash, quote, control) skips the tab because a[0] == b'"', so the ---/+++ lines end without the tab Git writes; flickzeug 0.4.5 splits the quoted name at the space and rejects the diff with invalid char in unquoted filename, failing the mutation check. The test covers space-only and backslash-only paths, not the combination.
oak · automated · a3035fc
| // `core.quotePath=false`. | ||
| fn header_path(prefix: &str, path: &[u8]) -> Vec<u8> { | ||
| let full = [prefix.as_bytes(), path].concat(); | ||
| let special = |byte: &u8| *byte < 0x20 || *byte == 0x7f || matches!(byte, b'"' | b'\\'); |
There was a problem hiding this comment.
Non-ASCII paths with backslashes break cargo-mutants
A path that is non-ASCII and also contains a backslash (or quote/control) is quoted, and gix::quote emits octal escapes for the non-ASCII bytes; flickzeug 0.4.5's escaped_filename rejects them with invalid escaped character, so cargo mutants --in-diff fails. The comment's workaround only covers a name that is only non-ASCII, and the test covers non-ASCII-only and backslash-only names, not the combination.
oak · automated · a3035fc
| // Build the commit's diff against its first parent, if its flake opts in by | ||
| // declaring a top-level `mutants-diff` input in its committed flake.lock. | ||
| // The diff is written to a temporary file outside the checkout. | ||
| pub(crate) fn prepare_diff( | ||
| repo: &Repository, | ||
| id: &CommitId, | ||
| ) -> Result<Option<tempfile::NamedTempFile>, Error> { | ||
| let error = diff_error(id); | ||
| let commit = repo.find_commit(id.0).map_err(&error)?; | ||
| let tree = commit.tree().map_err(&error)?; | ||
| if !declares_mutants_diff(&tree, id)? { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| // Merges use their first parent, and a root commit the empty tree. gix | ||
| // reports the first parent even past a shallow boundary. Fetch it there | ||
| // rather than diffing against nothing; in a full clone a missing parent | ||
| // means a broken repository, so let the lookup below report it. | ||
| let parent = commit.parent_ids().next().map(|parent| parent.detach()); | ||
| let parent_tree = match parent { | ||
| Some(parent) => { | ||
| if !repo.has_object(parent) && repo.is_shallow().map_err(&error)? { | ||
| fetch(repo, &[parent], None, &mut std::io::sink())?; | ||
| } | ||
| let parent = repo.find_commit(parent).map_err(&error)?; | ||
| Some(parent.tree().map_err(&error)?) | ||
| } | ||
| None => None, | ||
| }; | ||
|
|
||
| let mut options = gix::diff::Options::default().with_rewrites(None); | ||
| options.track_path(); | ||
| let changes = repo | ||
| .diff_tree_to_tree(parent_tree.as_ref(), Some(&tree), options) | ||
| .map_err(&error)?; | ||
| let mut patch = Vec::new(); | ||
| for change in changes { | ||
| let (path, before, after) = match change { | ||
| ChangeDetached::Addition { | ||
| location, | ||
| entry_mode, | ||
| id, | ||
| .. | ||
| } => (location, None, Some((entry_mode, id))), | ||
| ChangeDetached::Deletion { | ||
| location, | ||
| entry_mode, | ||
| id, | ||
| .. | ||
| } => (location, Some((entry_mode, id)), None), | ||
| ChangeDetached::Modification { | ||
| location, | ||
| previous_entry_mode, | ||
| previous_id, | ||
| entry_mode, | ||
| id, | ||
| .. | ||
| } => ( | ||
| location, | ||
| Some((previous_entry_mode, previous_id)), | ||
| Some((entry_mode, id)), | ||
| ), | ||
| // Rename detection is off, so there are no rewrites. | ||
| ChangeDetached::Rewrite { .. } => continue, | ||
| }; | ||
| // cargo-mutants only reads Rust source files. A symlink or submodule | ||
| // side counts as absent, so a symlink replaced by a regular file is | ||
| // an addition and the reverse a deletion. | ||
| let is_file = |side: &(gix::object::tree::EntryMode, ObjectId)| side.0.is_blob(); | ||
| let (before, after) = (before.filter(is_file), after.filter(is_file)); | ||
| if !path.ends_with(b".rs") || (before.is_none() && after.is_none()) { | ||
| continue; | ||
| } | ||
| let read = |side: Option<(gix::object::tree::EntryMode, ObjectId)>| match side { | ||
| Some((_, id)) => Ok(repo.find_blob(id).map_err(&error)?.take_data()), | ||
| None => Ok(Vec::new()), | ||
| }; | ||
| let (old, new) = (read(before)?, read(after)?); | ||
| write_file_patch( | ||
| &mut patch, | ||
| &path, | ||
| before.is_some(), | ||
| after.is_some(), | ||
| &old, | ||
| &new, | ||
| ) | ||
| .map_err(Error::WriteMutationDiff)?; | ||
| } | ||
|
|
||
| let mut file = tempfile::NamedTempFile::new().map_err(Error::WriteMutationDiff)?; | ||
| file.write_all(&patch).map_err(Error::WriteMutationDiff)?; | ||
| Ok(Some(file)) | ||
| } | ||
|
|
||
| // Wrap any gix error as a failure to build this commit's diff. | ||
| fn diff_error<E: Into<Source>>(id: &CommitId) -> impl Fn(E) -> Error { | ||
| move |source| Error::MutationDiff { | ||
| commit: id.clone(), | ||
| source: source.into(), | ||
| } | ||
| } | ||
|
|
||
| // Whether the commit's flake.lock has a top-level `mutants-diff` input. | ||
| fn declares_mutants_diff(tree: &gix::Tree<'_>, id: &CommitId) -> Result<bool, Error> { | ||
| let Some(entry) = tree.find_entry("flake.lock") else { | ||
| return Ok(false); | ||
| }; | ||
| let lock = entry.object().map_err(diff_error(id))?; | ||
| let lock: serde_json::Value = | ||
| serde_json::from_slice(&lock.data).map_err(|source| Error::ParseLock { | ||
| commit: id.clone(), | ||
| source, | ||
| })?; | ||
| let root = lock["root"].as_str().unwrap_or("root"); | ||
| Ok(lock["nodes"][root]["inputs"].get("mutants-diff").is_some()) | ||
| } | ||
|
|
||
| // Write one file's changes in Git's patch format, which cargo-mutants reads. | ||
| fn write_file_patch( | ||
| patch: &mut Vec<u8>, | ||
| path: &[u8], | ||
| existed: bool, | ||
| exists: bool, | ||
| old: &[u8], | ||
| new: &[u8], | ||
| ) -> std::io::Result<()> { | ||
| let (a, b) = (header_path("a/", path), header_path("b/", path)); | ||
| patch.extend(b"diff --git "); | ||
| patch.extend(&a); | ||
| patch.push(b' '); | ||
| patch.extend(&b); | ||
| patch.push(b'\n'); | ||
| // Like Git, end a name that has spaces with a tab, so readers know | ||
| // where it stops. | ||
| let end: &[u8] = if a[0] != b'"' && path.contains(&b' ') { | ||
| b"\t\n" | ||
| } else { | ||
| b"\n" | ||
| }; | ||
| for (marker, name, present) in [(b"--- ", &a, existed), (b"+++ ", &b, exists)] { | ||
| patch.extend(marker); | ||
| if present { | ||
| patch.extend(name); | ||
| patch.extend(end); | ||
| } else { | ||
| patch.extend(b"/dev/null\n"); | ||
| } | ||
| } | ||
| let input = InternedInput::new(old, new); | ||
| let diff = diff_with_slider_heuristics(Algorithm::Histogram, &input); | ||
| let hunks = UnifiedDiff::new( | ||
| &diff, | ||
| &input, | ||
| ConsumeBinaryHunk::new(Vec::new(), "\n"), | ||
| ContextSize::symmetrical(3), | ||
| ) | ||
| .consume()?; | ||
| patch.extend_from_slice(&hunks); | ||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
i think this can be a unified diff, no need to reimplement patch formatting iiuc:
https://docs.rs/gix/latest/gix/diff/blob/struct.UnifiedDiff.html
gix is nice for being pure rust, but if cargo mutants requires a git patch, it seems like this much reimplementation of a not entirely standardized thing might justify libgit2 dep?
https://docs.rs/git2/latest/git2/struct.Patch.html#method.to_buf
this amount of reimplementation, and specifically for a format kind of seems to justify libgit2, assuming nix it should be straightforward to build
| ContextSize::symmetrical(3), | ||
| ) | ||
| .consume()?; | ||
| patch.extend_from_slice(&hunks); |
There was a problem hiding this comment.
Hunkless diff entry makes cargo-mutants reject the patch
An added empty .rs file has no content lines, so write_file_patch emits diff --git/---/+++ lines and no @@ hunk. cargo mutants --in-diff then fails with Failed to parse diff, so nix flake check fails for that commit even when the rest of the diff is valid.
oak · automated · a3035fc
a3035fc to
ac039aa
Compare
|
@Mshehu5
One thread settled at this head waits for its reviewer to resolve; the ACK does not wait on it:
|
ac039aa to
e649590
Compare
| source, | ||
| })?; | ||
| let root = lock["root"].as_str().unwrap_or("root"); | ||
| Ok(lock["nodes"][root]["inputs"].get("mutants-diff").is_some()) |
There was a problem hiding this comment.
Default flake mutants-diff input breaks override
The opt-in check accepts any top-level mutants-diff input, but check_flake overrides it with a diff file; when the input is a flake (the default, flake = true), Nix fails with flake.nix does not exist. The test flake works only because it sets flake = false, which the comment does not state.
flake = false;
— fungi-protocol/validate-commits@e649590 · tests/flakes/mutants/flake.nix#L7
// A flake that declares a
mutants-diffinput gets this commit's diff
oakagent · automated · e649590
There was a problem hiding this comment.
a mutants-diff input without flake = false now stops with a clear error
| diff: &std::path::Path, | ||
| ) -> Result<String, Error> { | ||
| let output = Command::new("nix") | ||
| .args(["store", "add", "--name", "mutants-diff"]) |
There was a problem hiding this comment.
Unrooted diff path can be garbage-collected
nix store add leaves the diff path unrooted, so a concurrent nix store gc can delete it before nix flake check reads it, failing the commit's check.
oakagent · automated · e649590
There was a problem hiding this comment.
Fixed by dropping nix store add: the diff file is passed as path: and Nix copies and holds it for the whole check
A flake opts in by declaring a mutants-diff input with flake = false. For those commits, diff the commit's Rust files against its first parent and pass the diff file with --override-input, so cargo mutants --in-diff only mutates what the commit changed. A parent cut off by a shallow clone is fetched first. libgit2 diffs the trees, filters Rust files with a pathspec, splits a symlink replaced by a file into a deletion and an addition, and writes the hunks. Only the file name lines are written here, because cargo-mutants can't read how libgit2 writes names with spaces or non-ASCII characters. The integration test runs real Nix and real cargo mutants --list on a small crate, so cargo-mutants must be on PATH to run the tests.
e649590 to
394aa76
Compare
Closes #10.
Ports the validator side of fungi-protocol/concurrent-psbt#21. Running
cargo-mutants on the whole crate for every commit is slow, so each
commit should only mutate the code it changed. A flake can't see Git
history, so the tool builds the diff and passes it in.
mutants-diffinput, pointing at aplaceholder in the repo. Run without the tool, the flake sees the
placeholder and can run the full mutation suite.
its first parent with gix, adds the diff to the Nix store and passes
it with
--override-input mutants-diff. The flake's check then runscargo mutants --in-diff.The integration test runs real Nix and real
cargo mutants --liston asmall crate, so
cargo-mutantsmust be on PATH to run the tests.