Normalise staged paths and reject staging on an ended tour

`tour rm ./f.txt` recorded "./f.txt", which never matched carry-forward's
"f.txt", so the file silently survived into the next step; unstage had the
same mismatch. normalize_path is now shared by add/rm/unstage/commit and
also strips the leading "." that components() keeps.

add/rm/unstage gain the ended-tour check commit already had, via a shared
require_active, so a finalised tour can't accumulate uncommittable staging.
rm now warns instead of silently accepting paths no step tracks.

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-09-22 22:16:40 +01:00
co-authored by Claude Code
parent cbfb51f7c8
commit a81b1ace96
5 changed files with 86 additions and 18 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
use crate::error::TourError;
use crate::style::{green, reset};
use crate::utils::{require_tour, validate_paths};
use crate::utils::{normalize_path, require_active, validate_paths};
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
@@ -8,7 +8,7 @@ use std::path::PathBuf;
pub const STAGED_PATH: &str = "./.tour/staged";
pub fn add(files: Vec<PathBuf>) -> Result<(), TourError> {
require_tour()?;
require_active()?;
validate_paths(&files)?;
let existing = get_staged()?;
@@ -20,7 +20,7 @@ pub fn add(files: Vec<PathBuf>) -> Result<(), TourError> {
.open(STAGED_PATH)?;
for file in &files {
let normalized: PathBuf = file.components().collect();
let normalized = normalize_path(file);
if existing_set.contains(&normalized) {
println!("already staged: {}", normalized.display());
} else {
+3 -7
View File
@@ -2,20 +2,16 @@ use crate::add::{clear_staged, get_staged};
use crate::error::TourError;
use crate::rm::{clear_removed, get_removed};
use crate::style::{bold, reset};
use crate::utils::{copy_path, get_tour_step, require_tour, validate_paths};
use crate::utils::{copy_path, get_tour_step, normalize_path, require_active, validate_paths};
use crate::TOUR_DIR;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
pub fn commit(files: Vec<PathBuf>, message: String) -> Result<(), TourError> {
require_tour()?;
require_active()?;
let tour_dir = Path::new(TOUR_DIR);
if tour_dir.join("ended").exists() {
return Err(TourError::TourEnded);
}
let used_staging = files.is_empty();
let files = if used_staging {
let staged = get_staged()?;
@@ -24,7 +20,7 @@ pub fn commit(files: Vec<PathBuf>, message: String) -> Result<(), TourError> {
}
staged
} else {
files
files.iter().map(|f| normalize_path(f)).collect()
};
validate_paths(&files)?;
+14 -6
View File
@@ -1,6 +1,6 @@
use crate::error::TourError;
use crate::style::{red, reset};
use crate::utils::require_tour;
use crate::utils::{normalize_path, require_active};
use std::collections::HashSet;
use std::fs::{self, OpenOptions};
use std::io::Write;
@@ -9,7 +9,7 @@ use std::path::PathBuf;
pub const REMOVED_PATH: &str = "./.tour/removed";
pub fn rm(files: Vec<PathBuf>) -> Result<(), TourError> {
require_tour()?;
require_active()?;
let existing = get_removed()?;
let existing_set: HashSet<PathBuf> = existing.into_iter().collect();
@@ -19,13 +19,21 @@ pub fn rm(files: Vec<PathBuf>) -> Result<(), TourError> {
.create(true)
.open(REMOVED_PATH)?;
// Removal only has an effect on files a previous step actually snapshotted.
let tracked = crate::step::get_tracked_files()?;
for file in &files {
if existing_set.contains(file) {
let file = normalize_path(file);
if existing_set.contains(&file) {
println!("already marked for removal: {}", file.display());
} else {
writeln!(removed_file, "{}", file.display())?;
println!("{}marked for removal:{} {}", red(), reset(), file.display());
continue;
}
if !tracked.contains(&file) {
println!("not tracked by any step, ignoring: {}", file.display());
continue;
}
writeln!(removed_file, "{}", file.display())?;
println!("{}marked for removal:{} {}", red(), reset(), file.display());
}
Ok(())
+3 -2
View File
@@ -1,13 +1,14 @@
use crate::add::{get_staged, STAGED_PATH};
use crate::error::TourError;
use crate::utils::require_tour;
use crate::utils::{normalize_path, require_active};
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
pub fn unstage(files: Vec<PathBuf>) -> Result<(), TourError> {
require_tour()?;
require_active()?;
let staged = get_staged()?;
let files: Vec<PathBuf> = files.iter().map(|f| normalize_path(f)).collect();
let to_remove: HashSet<&PathBuf> = files.iter().collect();
let remaining: Vec<&PathBuf> = staged.iter().filter(|f| !to_remove.contains(f)).collect();
+63
View File
@@ -698,3 +698,66 @@ fn test_old_format_tour_still_navigates() {
tour_cmd().arg("prev").current_dir(dir.path()).output().unwrap();
assert_eq!(fs::read_to_string(dir.path().join("f.txt")).unwrap(), "v0");
}
// -- path normalisation, ended-tour guards, corrupt session --
#[test]
fn test_unstage_and_rm_normalize_paths() {
let dir = setup_dir();
init_tour(dir.path());
create_test_file(dir.path(), "f.txt", "a");
// Staged as "f.txt"; unstaging the "./f.txt" spelling must match it.
tour_cmd().args(["add", "f.txt"]).current_dir(dir.path()).output().unwrap();
let out = tour_cmd().args(["unstage", "./f.txt"]).current_dir(dir.path()).output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("unstaged: f.txt"), "got: {stdout}");
assert_eq!(fs::read_to_string(dir.path().join(".tour/staged")).unwrap(), "");
// Same for rm: "./f.txt" must match the tracked "f.txt" and be recorded normalized.
tour_cmd().args(["commit", "f.txt", "-m", "one"]).current_dir(dir.path()).output().unwrap();
let out = tour_cmd().args(["rm", "./f.txt"]).current_dir(dir.path()).output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("marked for removal: f.txt"), "got: {stdout}");
assert_eq!(fs::read_to_string(dir.path().join(".tour/removed")).unwrap(), "f.txt\n");
}
#[test]
fn test_rm_ignores_untracked_file() {
let dir = setup_dir();
init_tour(dir.path());
create_test_file(dir.path(), "f.txt", "a");
let out = tour_cmd().args(["rm", "f.txt"]).current_dir(dir.path()).output().unwrap();
assert!(String::from_utf8_lossy(&out.stdout).contains("not tracked"));
assert!(!dir.path().join(".tour/removed").exists()
|| fs::read_to_string(dir.path().join(".tour/removed")).unwrap().is_empty());
}
#[test]
fn test_staging_rejected_after_end() {
let dir = setup_dir();
init_tour(dir.path());
create_test_file(dir.path(), "f.txt", "a");
tour_cmd().args(["commit", "f.txt", "-m", "one"]).current_dir(dir.path()).output().unwrap();
tour_cmd().args(["end", "-m", "done"]).current_dir(dir.path()).output().unwrap();
for args in [vec!["add", "f.txt"], vec!["rm", "f.txt"], vec!["unstage", "f.txt"]] {
let out = tour_cmd().args(&args).current_dir(dir.path()).output().unwrap();
assert!(!out.status.success(), "{args:?} should fail on an ended tour");
assert!(String::from_utf8_lossy(&out.stderr).contains("already been ended"));
}
}
#[test]
fn test_corrupt_session_is_an_error() {
let dir = setup_dir();
init_tour(dir.path());
create_test_file(dir.path(), "f.txt", "a");
tour_cmd().args(["commit", "f.txt", "-m", "one"]).current_dir(dir.path()).output().unwrap();
fs::write(dir.path().join(".tour/session"), "STEP=banana").unwrap();
let out = tour_cmd().arg("status").current_dir(dir.path()).output().unwrap();
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("corrupted"));
}