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