
Understand it
A worktree is the collection you are editing. A commit records a particular tree; autosave protects edits but does not create a commit. Review both before selecting paths. A file can disappear, appear, or keep its name while its contents change. Looking only for new names misses modifications and deletions.
Build the candidate path set from both trees and compare values. Return each changed path once in lexical order. File ordering is presentation, not Git object identity. This lab models text trees; real Git also stores modes and opaque bytes. In the native terminal, inspect actual history through the existing libgit2-backed commands. The selected-path commit profile commits exactly the reviewed paths, so unrelated edits stay uncommitted.
The route project is Task Ledger: first keep a reproducible source history, then expose its data through a CLI and HTTP boundary, persist it in SQL, diagnose failures and prepare a release. This lesson owns one contract in that project. Run the worked example before changing the starter and explain which input makes the tempting shortcut fail.
Terminal practice: Run git status and git diff src/main.rs before committing. The terminal uses real Git; the lab isolates the tree-comparison contract.
The compiler lab is independent of the reading question. Edit solution.rs; the app supplies a separate caller with normal, empty and adversarial inputs. A successful unchanged demonstration does not prove your implementation. After the lab, apply the same contract to the route companion project. Its README separates the on-device adapter from the desktop Cargo/TCP profile.
Read the code
pub fn changed(
before: &[(&str, &str)],
after: &[(&str, &str)],
) -> Vec<String> {
let a: std::collections::BTreeMap<_, _> =
before.iter().copied().collect();
let b: std::collections::BTreeMap<_, _> =
after.iter().copied().collect();
a.keys()
.chain(b.keys())
.copied()
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.filter(|k| a.get(k) != b.get(k))
.map(str::to_owned)
.collect()
}
fn main() {
assert_eq!(
changed(&[("a", "1")], &[("a", "2"), ("b", "3")]),
vec!["a".to_owned(), "b".to_owned()],
"case 1"
);
assert_eq!(changed(&[], &[]), Vec::<String>::new(), "case 2");
assert_eq!(
changed(&[("gone", "x")], &[]),
vec!["gone".to_owned()],
"case 3"
);
assert_eq!(
changed(&[("a", "1")], &[("a", "1")]),
Vec::<String>::new(),
"case 4"
);
println!("worked example passed");
}
The worked implementation exercises distinguish changed, added and deleted paths. Its tests demonstrate the stated finite contract.
Practice starter
pub fn changed(before: &[(&str, &str)], after: &[(&str, &str)]) -> Vec<String> {
todo!("implement the contract")
}
Try it
Return the sorted unique paths whose contents differ between two trees; include additions and deletions. Each input has unique paths. Keep the public signature. Open the compiler lab and edit solution.rs; do not edit the private verification caller.
What success looks like: The separate caller accepts the changed solution and rejects unchanged TODO, a wrong implementation and a printed success message.
Check your understanding
An existing file keeps its name but changes content. Should it appear in the review?
Reveal the answer
- Autosave makes the edit a commit
- Yes, content is part of the snapshot
- Only newly named files belong in a change review
Comparing only path names misses modified content; comparing only the new tree misses deletions.
Go deeper
Common misconception: Only newly named files belong in a change review
Compare source snapshots by path and content before selecting a commit.
- Identify the boundary: Distinguish changed, added and deleted paths. Write down its accepted inputs and observable output.
- Exercise the failure: Comparing only path names misses modified content; comparing only the new tree misses deletions.
- Check the independent caller: Return the sorted unique paths whose contents differ between two trees; include additions and deletions. Each input has unique paths.
Transfer challenge: Apply this contract to Task Ledger, introduce the failure described in the lesson, and show the failing check before fixing it.
Connections
- Commit Only Reviewed Paths: Construct the selected commit tree