@@ -9,10 +9,7 @@ use log::debug;
99use regex:: Regex ;
1010
1111pub 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