
Understand it
Branches name positions in history. Creating a branch does not copy a folder, and switching it is not merely changing a label: the target tree can replace editor files. A clean-checkout policy is easy to explain and test. It refuses the operation whenever the current worktree differs from its HEAD, including an untracked file or deletion.
Compare trees without relying on their enumeration order. A new branch can be created while preserving current edits, but switching uses the clean-tree precondition in this terminal. A desktop client may support more nuanced preservation or stash; those are distinct contracts. After committing, refresh status before switching, because a successful commit of one captured snapshot does not prove newer typing was also committed.
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 branch feature-cli, git status, then git switch feature-cli after the tree is clean.
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 clean(work: &[(&str, &str)], head: &[(&str, &str)]) -> bool {
work.iter()
.copied()
.collect::<std::collections::BTreeMap<_, _>>()
== head
.iter()
.copied()
.collect::<std::collections::BTreeMap<_, _>>()
}
fn main() {
assert_eq!(
clean(&[("b", "2"), ("a", "1")], &[("a", "1"), ("b", "2")]),
true,
"case 1"
);
assert_eq!(clean(&[], &[]), true, "case 2");
assert_eq!(
clean(&[("a", "new")], &[("a", "old")]),
false,
"case 3"
);
assert_eq!(clean(&[("extra", "x")], &[]), false, "case 4");
assert_eq!(clean(&[], &[("gone", "x")]), false, "case 5");
println!("worked example passed");
}
The worked implementation exercises protect dirty worktrees during checkout. Its tests demonstrate the stated finite contract.
Practice starter
pub fn clean(work: &[(&str,&str)], head: &[(&str,&str)]) -> bool {
todo!("implement the contract")
}
Try it
Return whether two unique-path trees have identical contents, independent of enumeration order. 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
Can an untracked file matter to a clean-checkout policy?
Reveal the answer
- Only tracked modifications can make a checkout unsafe
- Changing branch names preserves every file automatically
- Yes; the worktree differs from HEAD
An added or deleted file is a tree difference even when tracked file contents match.
Go deeper
Common misconception: Only tracked modifications can make a checkout unsafe
Require a clean worktree before this profile replaces files from another branch.
- Identify the boundary: Protect dirty worktrees during checkout. Write down its accepted inputs and observable output.
- Exercise the failure: An added or deleted file is a tree difference even when tracked file contents match.
- Check the independent caller: Return whether two unique-path trees have identical contents, independent of enumeration order.
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
- Recover One Historical File: Restore narrowly from a known revision