@@ -15,7 +15,10 @@ use anyhow::{Context, Result};
1515use git2:: { Diff , Error , Patch , Repository } ;
1616
1717// project specific modules/crates
18- use crate :: common_fs:: { FileFilter , FileObj } ;
18+ use crate :: {
19+ cli:: LinesChangedOnly ,
20+ common_fs:: { FileFilter , FileObj } ,
21+ } ;
1922
2023/// This (re-)initializes the repository located in the specified `path`.
2124///
@@ -100,7 +103,11 @@ fn parse_patch(patch: &Patch) -> (Vec<u32>, Vec<RangeInclusive<u32>>) {
100103///
101104/// The specified list of `extensions`, `ignored` and `not_ignored` files are used as
102105/// filters to expedite the process and only focus on the data cpp_linter can use.
103- pub fn parse_diff ( diff : & git2:: Diff , file_filter : & FileFilter ) -> Vec < FileObj > {
106+ pub fn parse_diff (
107+ diff : & git2:: Diff ,
108+ file_filter : & FileFilter ,
109+ lines_changed_only : & LinesChangedOnly ,
110+ ) -> Vec < FileObj > {
104111 let mut files: Vec < FileObj > = Vec :: new ( ) ;
105112 for file_idx in 0 ..diff. deltas ( ) . count ( ) {
106113 let diff_delta = diff. get_delta ( file_idx) . unwrap ( ) ;
@@ -112,7 +119,10 @@ pub fn parse_diff(diff: &git2::Diff, file_filter: &FileFilter) -> Vec<FileObj> {
112119 {
113120 let ( added_lines, diff_chunks) =
114121 parse_patch ( & Patch :: from_diff ( diff, file_idx) . unwrap ( ) . unwrap ( ) ) ;
115- files. push ( FileObj :: from ( file_path, added_lines, diff_chunks) ) ;
122+ if lines_changed_only. is_change_valid ( !added_lines. is_empty ( ) , !diff_chunks. is_empty ( ) )
123+ {
124+ files. push ( FileObj :: from ( file_path, added_lines, diff_chunks) ) ;
125+ }
116126 }
117127 }
118128 files
@@ -125,12 +135,20 @@ pub fn parse_diff(diff: &git2::Diff, file_filter: &FileFilter) -> Vec<FileObj> {
125135/// log warning and error are output when this occurs. Please report this instance for
126136/// troubleshooting/diagnosis as this likely means the diff is malformed or there is a
127137/// bug in libgit2 source.
128- pub fn parse_diff_from_buf ( buff : & [ u8 ] , file_filter : & FileFilter ) -> Vec < FileObj > {
138+ pub fn parse_diff_from_buf (
139+ buff : & [ u8 ] ,
140+ file_filter : & FileFilter ,
141+ lines_changed_only : & LinesChangedOnly ,
142+ ) -> Vec < FileObj > {
129143 if let Ok ( diff_obj) = & Diff :: from_buffer ( buff) {
130- parse_diff ( diff_obj, file_filter)
144+ parse_diff ( diff_obj, file_filter, lines_changed_only )
131145 } else {
132146 log:: warn!( "libgit2 failed to parse the diff" ) ;
133- brute_force_parse_diff:: parse_diff ( & String :: from_utf8_lossy ( buff) , file_filter)
147+ brute_force_parse_diff:: parse_diff (
148+ & String :: from_utf8_lossy ( buff) ,
149+ file_filter,
150+ lines_changed_only,
151+ )
134152 }
135153}
136154
@@ -146,7 +164,10 @@ mod brute_force_parse_diff {
146164 use regex:: Regex ;
147165 use std:: { ops:: RangeInclusive , path:: PathBuf } ;
148166
149- use crate :: common_fs:: { FileFilter , FileObj } ;
167+ use crate :: {
168+ cli:: LinesChangedOnly ,
169+ common_fs:: { FileFilter , FileObj } ,
170+ } ;
150171
151172 fn get_filename_from_front_matter ( front_matter : & str ) -> Option < & str > {
152173 let diff_file_name = Regex :: new ( r"(?m)^\+\+\+\sb?/(.*)$" ) . unwrap ( ) ;
@@ -209,7 +230,11 @@ mod brute_force_parse_diff {
209230 ( additions, diff_chunks)
210231 }
211232
212- pub fn parse_diff ( diff : & str , file_filter : & FileFilter ) -> Vec < FileObj > {
233+ pub fn parse_diff (
234+ diff : & str ,
235+ file_filter : & FileFilter ,
236+ lines_changed_only : & LinesChangedOnly ,
237+ ) -> Vec < FileObj > {
213238 log:: error!( "Using brute force diff parsing!" ) ;
214239 let mut results = Vec :: new ( ) ;
215240 let diff_file_delimiter = Regex :: new ( r"(?m)^diff --git a/.*$" ) . unwrap ( ) ;
@@ -230,7 +255,11 @@ mod brute_force_parse_diff {
230255 let file_path = PathBuf :: from ( file_name) ;
231256 if file_filter. is_source_or_ignored ( & file_path) {
232257 let ( added_lines, diff_chunks) = parse_patch ( & file_diff[ hunk_start..] ) ;
233- results. push ( FileObj :: from ( file_path, added_lines, diff_chunks) ) ;
258+ if lines_changed_only
259+ . is_change_valid ( !added_lines. is_empty ( ) , !diff_chunks. is_empty ( ) )
260+ {
261+ results. push ( FileObj :: from ( file_path, added_lines, diff_chunks) ) ;
262+ }
234263 }
235264 }
236265 // } else {
@@ -247,6 +276,7 @@ mod brute_force_parse_diff {
247276
248277 use super :: parse_diff;
249278 use crate :: {
279+ cli:: LinesChangedOnly ,
250280 common_fs:: { FileFilter , FileObj } ,
251281 git:: parse_diff_from_buf,
252282 } ;
@@ -274,6 +304,7 @@ rename to /tests/demo/some source.c
274304 let files = parse_diff_from_buf (
275305 diff_buf,
276306 & FileFilter :: new ( & [ "target" . to_string ( ) ] , vec ! [ "c" . to_string( ) ] ) ,
307+ & LinesChangedOnly :: Off ,
277308 ) ;
278309 assert ! ( !files. is_empty( ) ) ;
279310 assert ! ( files
@@ -289,6 +320,7 @@ rename to /tests/demo/some source.c
289320 let files = parse_diff_from_buf (
290321 diff_buf,
291322 & FileFilter :: new ( & [ "target" . to_string ( ) ] , vec ! [ "c" . to_string( ) ] ) ,
323+ & LinesChangedOnly :: Off ,
292324 ) ;
293325 assert ! ( !files. is_empty( ) ) ;
294326 }
@@ -301,8 +333,13 @@ rename to /tests/demo/some source.c
301333 parse_diff_from_buf (
302334 buf. as_bytes ( ) ,
303335 & FileFilter :: new ( & ignore, extensions. to_owned ( ) ) ,
336+ & LinesChangedOnly :: Off ,
337+ ) ,
338+ parse_diff (
339+ buf,
340+ & FileFilter :: new ( & ignore, extensions. to_owned ( ) ) ,
341+ & LinesChangedOnly :: Off ,
304342 ) ,
305- parse_diff ( buf, & FileFilter :: new ( & ignore, extensions. to_owned ( ) ) ) ,
306343 )
307344 }
308345
@@ -377,6 +414,7 @@ mod test {
377414 use tempfile:: { tempdir, TempDir } ;
378415
379416 use crate :: {
417+ cli:: LinesChangedOnly ,
380418 common_fs:: FileFilter ,
381419 rest_api:: { github:: GithubApiClient , RestApiClient } ,
382420 } ;
@@ -406,7 +444,7 @@ mod test {
406444 env:: set_var ( "CI" , "false" ) ; // avoid use of REST API when testing in CI
407445 rest_api_client
408446 . unwrap ( )
409- . get_list_of_changed_files ( & file_filter)
447+ . get_list_of_changed_files ( & file_filter, & LinesChangedOnly :: Off )
410448 . await
411449 . unwrap ( )
412450 }
0 commit comments