@@ -15,7 +15,10 @@ use anyhow::{Context, Result};
15
15
use git2:: { Diff , Error , Patch , Repository } ;
16
16
17
17
// project specific modules/crates
18
- use crate :: common_fs:: { FileFilter , FileObj } ;
18
+ use crate :: {
19
+ cli:: LinesChangedOnly ,
20
+ common_fs:: { FileFilter , FileObj } ,
21
+ } ;
19
22
20
23
/// This (re-)initializes the repository located in the specified `path`.
21
24
///
@@ -100,7 +103,11 @@ fn parse_patch(patch: &Patch) -> (Vec<u32>, Vec<RangeInclusive<u32>>) {
100
103
///
101
104
/// The specified list of `extensions`, `ignored` and `not_ignored` files are used as
102
105
/// 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 > {
104
111
let mut files: Vec < FileObj > = Vec :: new ( ) ;
105
112
for file_idx in 0 ..diff. deltas ( ) . count ( ) {
106
113
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> {
112
119
{
113
120
let ( added_lines, diff_chunks) =
114
121
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
+ }
116
126
}
117
127
}
118
128
files
@@ -125,12 +135,20 @@ pub fn parse_diff(diff: &git2::Diff, file_filter: &FileFilter) -> Vec<FileObj> {
125
135
/// log warning and error are output when this occurs. Please report this instance for
126
136
/// troubleshooting/diagnosis as this likely means the diff is malformed or there is a
127
137
/// 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 > {
129
143
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 )
131
145
} else {
132
146
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
+ )
134
152
}
135
153
}
136
154
@@ -146,7 +164,10 @@ mod brute_force_parse_diff {
146
164
use regex:: Regex ;
147
165
use std:: { ops:: RangeInclusive , path:: PathBuf } ;
148
166
149
- use crate :: common_fs:: { FileFilter , FileObj } ;
167
+ use crate :: {
168
+ cli:: LinesChangedOnly ,
169
+ common_fs:: { FileFilter , FileObj } ,
170
+ } ;
150
171
151
172
fn get_filename_from_front_matter ( front_matter : & str ) -> Option < & str > {
152
173
let diff_file_name = Regex :: new ( r"(?m)^\+\+\+\sb?/(.*)$" ) . unwrap ( ) ;
@@ -209,7 +230,11 @@ mod brute_force_parse_diff {
209
230
( additions, diff_chunks)
210
231
}
211
232
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 > {
213
238
log:: error!( "Using brute force diff parsing!" ) ;
214
239
let mut results = Vec :: new ( ) ;
215
240
let diff_file_delimiter = Regex :: new ( r"(?m)^diff --git a/.*$" ) . unwrap ( ) ;
@@ -230,7 +255,11 @@ mod brute_force_parse_diff {
230
255
let file_path = PathBuf :: from ( file_name) ;
231
256
if file_filter. is_source_or_ignored ( & file_path) {
232
257
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
+ }
234
263
}
235
264
}
236
265
// } else {
@@ -247,6 +276,7 @@ mod brute_force_parse_diff {
247
276
248
277
use super :: parse_diff;
249
278
use crate :: {
279
+ cli:: LinesChangedOnly ,
250
280
common_fs:: { FileFilter , FileObj } ,
251
281
git:: parse_diff_from_buf,
252
282
} ;
@@ -274,6 +304,7 @@ rename to /tests/demo/some source.c
274
304
let files = parse_diff_from_buf (
275
305
diff_buf,
276
306
& FileFilter :: new ( & [ "target" . to_string ( ) ] , vec ! [ "c" . to_string( ) ] ) ,
307
+ & LinesChangedOnly :: Off ,
277
308
) ;
278
309
assert ! ( !files. is_empty( ) ) ;
279
310
assert ! ( files
@@ -289,6 +320,7 @@ rename to /tests/demo/some source.c
289
320
let files = parse_diff_from_buf (
290
321
diff_buf,
291
322
& FileFilter :: new ( & [ "target" . to_string ( ) ] , vec ! [ "c" . to_string( ) ] ) ,
323
+ & LinesChangedOnly :: Off ,
292
324
) ;
293
325
assert ! ( !files. is_empty( ) ) ;
294
326
}
@@ -301,8 +333,13 @@ rename to /tests/demo/some source.c
301
333
parse_diff_from_buf (
302
334
buf. as_bytes ( ) ,
303
335
& 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 ,
304
342
) ,
305
- parse_diff ( buf, & FileFilter :: new ( & ignore, extensions. to_owned ( ) ) ) ,
306
343
)
307
344
}
308
345
@@ -377,6 +414,7 @@ mod test {
377
414
use tempfile:: { tempdir, TempDir } ;
378
415
379
416
use crate :: {
417
+ cli:: LinesChangedOnly ,
380
418
common_fs:: FileFilter ,
381
419
rest_api:: { github:: GithubApiClient , RestApiClient } ,
382
420
} ;
@@ -406,7 +444,7 @@ mod test {
406
444
env:: set_var ( "CI" , "false" ) ; // avoid use of REST API when testing in CI
407
445
rest_api_client
408
446
. unwrap ( )
409
- . get_list_of_changed_files ( & file_filter)
447
+ . get_list_of_changed_files ( & file_filter, & LinesChangedOnly :: Off )
410
448
. await
411
449
. unwrap ( )
412
450
}
0 commit comments