|
| 1 | +use std::{ |
| 2 | + path::{Path, PathBuf}, |
| 3 | + process::Command, |
| 4 | +}; |
| 5 | + |
| 6 | +use core::fmt; |
| 7 | +use error_stack::{Context, Result, ResultExt}; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub enum Error { |
| 11 | + Io, |
| 12 | +} |
| 13 | + |
| 14 | +impl fmt::Display for Error { |
| 15 | + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 16 | + match self { |
| 17 | + Error::Io => fmt.write_str("Error::Io"), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl Context for Error {} |
| 23 | + |
| 24 | +pub(crate) fn untracked_files(base_path: &Path) -> Result<Vec<PathBuf>, Error> { |
| 25 | + let output = Command::new("git") |
| 26 | + .args(["ls-files", "--others", "--exclude-standard", "--full-name", "-z", "--", "."]) |
| 27 | + .current_dir(base_path) |
| 28 | + .output() |
| 29 | + .change_context(Error::Io)?; |
| 30 | + |
| 31 | + if !output.status.success() { |
| 32 | + return Ok(Vec::new()); |
| 33 | + } |
| 34 | + |
| 35 | + let results: Vec<PathBuf> = output |
| 36 | + .stdout |
| 37 | + .split(|&b| b == b'\0') |
| 38 | + .filter(|chunk| !chunk.is_empty()) |
| 39 | + .map(|rel| std::str::from_utf8(rel).change_context(Error::Io).map(|s| base_path.join(s))) |
| 40 | + .collect::<std::result::Result<_, _>>()?; |
| 41 | + |
| 42 | + Ok(results) |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(test)] |
| 46 | +mod tests { |
| 47 | + use super::*; |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn test_untracked_files() { |
| 51 | + let tmp_dir = tempfile::tempdir().unwrap(); |
| 52 | + let untracked = untracked_files(tmp_dir.path()).unwrap(); |
| 53 | + assert!(untracked.is_empty()); |
| 54 | + |
| 55 | + std::process::Command::new("git") |
| 56 | + .arg("init") |
| 57 | + .current_dir(tmp_dir.path()) |
| 58 | + .output() |
| 59 | + .expect("failed to run git init"); |
| 60 | + |
| 61 | + std::fs::write(tmp_dir.path().join("test.txt"), "test").unwrap(); |
| 62 | + let untracked = untracked_files(tmp_dir.path()).unwrap(); |
| 63 | + assert!(untracked.len() == 1); |
| 64 | + let expected = tmp_dir.path().join("test.txt"); |
| 65 | + assert!(untracked[0] == expected); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn test_untracked_files_with_spaces_and_parens() { |
| 70 | + let tmp_dir = tempfile::tempdir().unwrap(); |
| 71 | + |
| 72 | + std::process::Command::new("git") |
| 73 | + .arg("init") |
| 74 | + .current_dir(tmp_dir.path()) |
| 75 | + .output() |
| 76 | + .expect("failed to run git init"); |
| 77 | + |
| 78 | + // Nested dirs with spaces and parentheses |
| 79 | + let d1 = tmp_dir.path().join("dir with spaces"); |
| 80 | + let d2 = d1.join("(special)"); |
| 81 | + std::fs::create_dir_all(&d2).unwrap(); |
| 82 | + |
| 83 | + let f1 = d1.join("file (1).txt"); |
| 84 | + let f2 = d2.join("a b (2).rb"); |
| 85 | + std::fs::write(&f1, "one").unwrap(); |
| 86 | + std::fs::write(&f2, "two").unwrap(); |
| 87 | + |
| 88 | + let mut untracked = untracked_files(tmp_dir.path()).unwrap(); |
| 89 | + untracked.sort(); |
| 90 | + |
| 91 | + let mut expected = vec![f1, f2]; |
| 92 | + expected.sort(); |
| 93 | + |
| 94 | + assert_eq!(untracked, expected); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_untracked_files_multiple_files_order_insensitive() { |
| 99 | + let tmp_dir = tempfile::tempdir().unwrap(); |
| 100 | + |
| 101 | + std::process::Command::new("git") |
| 102 | + .arg("init") |
| 103 | + .current_dir(tmp_dir.path()) |
| 104 | + .output() |
| 105 | + .expect("failed to run git init"); |
| 106 | + |
| 107 | + let f1 = tmp_dir.path().join("a.txt"); |
| 108 | + let f2 = tmp_dir.path().join("b.txt"); |
| 109 | + let f3 = tmp_dir.path().join("c.txt"); |
| 110 | + std::fs::write(&f1, "A").unwrap(); |
| 111 | + std::fs::write(&f2, "B").unwrap(); |
| 112 | + std::fs::write(&f3, "C").unwrap(); |
| 113 | + |
| 114 | + let mut untracked = untracked_files(tmp_dir.path()).unwrap(); |
| 115 | + untracked.sort(); |
| 116 | + |
| 117 | + let mut expected = vec![f1, f2, f3]; |
| 118 | + expected.sort(); |
| 119 | + |
| 120 | + assert_eq!(untracked, expected); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_untracked_files_excludes_staged() { |
| 125 | + let tmp_dir = tempfile::tempdir().unwrap(); |
| 126 | + |
| 127 | + std::process::Command::new("git") |
| 128 | + .arg("init") |
| 129 | + .current_dir(tmp_dir.path()) |
| 130 | + .output() |
| 131 | + .expect("failed to run git init"); |
| 132 | + |
| 133 | + let staged = tmp_dir.path().join("staged.txt"); |
| 134 | + let unstaged = tmp_dir.path().join("unstaged.txt"); |
| 135 | + std::fs::write(&staged, "I will be staged").unwrap(); |
| 136 | + std::fs::write(&unstaged, "I remain untracked").unwrap(); |
| 137 | + |
| 138 | + // Stage one file |
| 139 | + let add_status = std::process::Command::new("git") |
| 140 | + .arg("add") |
| 141 | + .arg("staged.txt") |
| 142 | + .current_dir(tmp_dir.path()) |
| 143 | + .output() |
| 144 | + .expect("failed to run git add"); |
| 145 | + assert!( |
| 146 | + add_status.status.success(), |
| 147 | + "git add failed: {}", |
| 148 | + String::from_utf8_lossy(&add_status.stderr) |
| 149 | + ); |
| 150 | + |
| 151 | + let mut untracked = untracked_files(tmp_dir.path()).unwrap(); |
| 152 | + untracked.sort(); |
| 153 | + |
| 154 | + let expected = vec![unstaged]; |
| 155 | + assert_eq!(untracked, expected); |
| 156 | + } |
| 157 | +} |
0 commit comments