Skip to content

Commit 0fbb2f3

Browse files
committed
fix: don't check files that were deleted/moved in working tree
We already filter out deleted files, but we were missing the case where a file is added/changed in the commit (and thus is included for checking) but then *deleted* in the working tree (and thus should be excluded).
1 parent 9fda576 commit 0fbb2f3

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

src/git.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ use log::debug;
99
use regex::Regex;
1010

1111
pub fn get_head() -> Result<String> {
12-
let output = Command::new("git")
13-
.arg("rev-parse")
14-
.arg("HEAD")
15-
.output()?;
12+
let output = Command::new("git").arg("rev-parse").arg("HEAD").output()?;
1613
ensure_output("git rev-parse", &output)?;
1714
let head = std::str::from_utf8(&output.stdout)?.trim();
1815
Ok(head.to_string())
@@ -118,9 +115,27 @@ pub fn get_changed_files(git_root: &AbsPath, relative_to: Option<&str>) -> Resul
118115

119116
log_files("Linting working tree diff files: ", &working_tree_files);
120117

121-
working_tree_files
118+
let deleted_working_tree_files: HashSet<String> = working_tree_files_str
119+
.lines()
120+
.filter(|line| !line.is_empty())
121+
// Filter IN deleted files.
122+
.filter(|line| line.starts_with('D'))
123+
// Strip the status prefix.
124+
.map(|line| re.replace(line, "").to_string())
125+
.collect();
126+
127+
log_files(
128+
"These files were deleted in the working tree and won't be checked: ",
129+
&working_tree_files,
130+
);
131+
132+
let all_files = working_tree_files
122133
.union(&commit_files)
123-
.into_iter()
134+
.map(|s| s.to_string())
135+
.collect::<HashSet<_>>();
136+
137+
all_files
138+
.difference(&deleted_working_tree_files)
124139
// Git reports files relative to the root of git root directory, so retrieve
125140
// that and prepend it to the file paths.
126141
.map(|f| format!("{}/{}", git_root.display(), f))
@@ -282,6 +297,30 @@ mod tests {
282297
Ok(())
283298
}
284299

300+
// Files that were deleted/moved in the working tree should not be checked,
301+
// since obviously they are gone.
302+
#[test]
303+
fn moved_files_working_tree() -> Result<()> {
304+
let git = GitCheckout::new()?;
305+
git.write_file("test_1.txt", "Initial commit")?;
306+
git.add(".")?;
307+
git.commit("commit 1")?;
308+
309+
git.write_file("test_2.txt", "foo")?;
310+
git.add(".")?;
311+
git.commit("commit 2")?;
312+
313+
let output = Command::new("git")
314+
.args(&["mv", "test_2.txt", "new.txt"])
315+
.current_dir(git.root.path())
316+
.output()?;
317+
assert!(output.status.success());
318+
319+
let files = git.changed_files(None)?;
320+
assert!(files.contains(&"new.txt".to_string()));
321+
Ok(())
322+
}
323+
285324
#[test]
286325
fn relative_revision() -> Result<()> {
287326
let git = GitCheckout::new()?;

0 commit comments

Comments
 (0)