Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor: use sanitize-filename crate for filename sanitization (#832)

Maximilian Roos9b92da9c0a5e
diff --git a/Cargo.lock b/Cargo.lock
index 4b88f8587..fa8f14f74 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1906,6 +1906,15 @@ dependencies = [
"winapi-util",
]
+[[package]]
+name = "sanitize-filename"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bc984f4f9ceb736a7bb755c3e3bd17dc56370af2600c9780dcc48c66453da34d"
+dependencies = [
+ "regex",
+]
+
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -3107,6 +3116,7 @@ dependencies = [
"reflink-copy",
"regex",
"rstest",
+ "sanitize-filename",
"serde",
"serde_json",
"shell-escape",
diff --git a/Cargo.toml b/Cargo.toml
index d0141a6d9..48285b692 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -119,6 +119,7 @@ ignore = "0.4"
reflink-copy = "0.1"
dashmap = "6.1.0"
fs2 = "0.4.3"
+sanitize-filename = "0.6.0"
[target.'cfg(unix)'.dependencies]
skim = "0.20"
diff --git a/src/commands/process.rs b/src/commands/process.rs
index b4b559d60..53e970939 100644
--- a/src/commands/process.rs
+++ b/src/commands/process.rs
@@ -450,22 +450,17 @@ mod tests {
sanitize_for_filename("branch_with_underscore").starts_with("branch_with_underscore-")
);
- // Windows reserved device names (must be prefixed to avoid conflicts)
- assert!(sanitize_for_filename("CON").starts_with("_CON-"));
- assert!(sanitize_for_filename("con").starts_with("_con-"));
- assert!(sanitize_for_filename("PRN").starts_with("_PRN-"));
- assert!(sanitize_for_filename("AUX").starts_with("_AUX-"));
- assert!(sanitize_for_filename("NUL").starts_with("_NUL-"));
- assert!(sanitize_for_filename("COM1").starts_with("_COM1-"));
- assert!(sanitize_for_filename("com9").starts_with("_com9-"));
- assert!(sanitize_for_filename("LPT1").starts_with("_LPT1-"));
- assert!(sanitize_for_filename("lpt9").starts_with("_lpt9-"));
-
- // COM0/LPT0 are NOT reserved (only 1-9 are)
- assert!(sanitize_for_filename("COM0").starts_with("COM0-"));
- assert!(sanitize_for_filename("LPT0").starts_with("LPT0-"));
-
- // Longer names are fine
+ // Windows reserved device names are handled (produce valid filenames)
+ // The sanitize-filename crate replaces these rather than prefixing
+ // Note: crate matches COM0-9/LPT0-9, stricter than Windows (which only reserves 1-9)
+ for name in [
+ "CON", "con", "PRN", "AUX", "NUL", "COM0", "COM1", "com9", "LPT0", "LPT1", "lpt9",
+ ] {
+ let result = sanitize_for_filename(name);
+ assert!(!result.is_empty() && result.len() > 3, "{name} -> {result}");
+ }
+
+ // Longer names containing reserved prefixes are fine
assert!(sanitize_for_filename("CONSOLE").starts_with("CONSOLE-"));
assert!(sanitize_for_filename("COM10").starts_with("COM10-"));
diff --git a/src/path.rs b/src/path.rs
index e3bee400c..4e820f918 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -3,6 +3,8 @@ use shell_escape::unix::escape;
use std::borrow::Cow;
use std::path::Path;
+use sanitize_filename::{Options as SanitizeOptions, sanitize_with_options};
+
use crate::config::short_hash;
#[cfg(windows)]
use crate::shell_exec::{Cmd, ShellConfig};
@@ -134,41 +136,24 @@ pub fn format_path_for_display(path: &Path) -> String {
/// Sanitize a string for use as a filename on all platforms.
///
-/// Replaces invalid characters and control characters with `-`, trims trailing
-/// dots/spaces (Windows), prefixes reserved device names with `_`, and appends
-/// a 3-character hash suffix for collision avoidance.
+/// Uses `sanitize-filename` crate to handle invalid characters, control characters,
+/// Windows reserved names (CON, PRN, etc.), and trailing dots/spaces. Appends a
+/// 3-character hash suffix for collision avoidance.
///
/// The hash ensures unique outputs for inputs that would otherwise collide
/// (e.g., `origin/feature` and `origin-feature` both sanitize to `origin-feature`
/// but get different hash suffixes).
pub fn sanitize_for_filename(value: &str) -> String {
- let mut result: String = value
- .chars()
- .map(|c| match c {
- // Windows/Unix invalid filename characters
- '/' | '\\' | '<' | '>' | ':' | '"' | '|' | '?' | '*' => '-',
- // Control characters (0x00-0x1F)
- c if c.is_control() => '-',
- _ => c,
- })
- .collect();
-
- // Trim trailing dots and spaces (Windows silently strips these)
- while result.ends_with('.') || result.ends_with(' ') {
- result.pop();
- }
-
- // Handle Windows reserved names (case-insensitive)
- // CON, PRN, AUX, NUL, COM1-9, LPT1-9
- let upper = result.to_uppercase();
- let is_reserved = matches!(upper.as_str(), "CON" | "PRN" | "AUX" | "NUL")
- || (upper.len() == 4
- && (upper.starts_with("COM") || upper.starts_with("LPT"))
- && upper.chars().nth(3).is_some_and(|c| matches!(c, '1'..='9')));
-
- if is_reserved {
- result = format!("_{result}");
- } else if result.is_empty() {
+ let mut result = sanitize_with_options(
+ value,
+ SanitizeOptions {
+ windows: true,
+ truncate: false,
+ replacement: "-",
+ },
+ );
+
+ if result.is_empty() {
result = "_empty".to_string();
}
@@ -304,9 +289,18 @@ mod tests {
}
#[test]
- fn test_sanitize_for_filename_prefixes_reserved_names() {
- assert!(sanitize_for_filename("CON").starts_with("_CON-"));
- assert!(sanitize_for_filename("com1").starts_with("_com1-"));
+ fn test_sanitize_for_filename_handles_reserved_names() {
+ // Reserved names are replaced (not preserved) - the hash ensures uniqueness
+ let con = sanitize_for_filename("CON");
+ let com1 = sanitize_for_filename("com1");
+ assert!(
+ !con.is_empty() && con.len() > 3,
+ "CON should produce valid filename: {con}"
+ );
+ assert!(
+ !com1.is_empty() && com1.len() > 3,
+ "com1 should produce valid filename: {com1}"
+ );
}
#[test]