@@ -19,7 +19,7 @@ use git2::{
1919use gix:: {
2020 bstr:: ByteSlice ,
2121 diff:: blob:: {
22- unified_diff:: { ConsumeHunk , ContextSize , NewlineSeparator } ,
22+ unified_diff:: { ConsumeHunk , ContextSize , DiffLineKind } ,
2323 UnifiedDiff ,
2424 } ,
2525 ObjectId ,
@@ -60,6 +60,16 @@ impl From<git2::DiffLineType> for DiffLineType {
6060 }
6161}
6262
63+ impl From < DiffLineKind > for DiffLineType {
64+ fn from ( value : DiffLineKind ) -> Self {
65+ match value {
66+ DiffLineKind :: Context => Self :: None ,
67+ DiffLineKind :: Add => Self :: Add ,
68+ DiffLineKind :: Remove => Self :: Delete ,
69+ }
70+ }
71+ }
72+
6373///
6474#[ derive( Default , Clone , Hash , Debug ) ]
6575pub struct DiffLine {
@@ -218,76 +228,9 @@ impl ConsumeHunk for FileDiff {
218228
219229 fn consume_hunk (
220230 & mut self ,
221- before_hunk_start : u32 ,
222- before_hunk_len : u32 ,
223- after_hunk_start : u32 ,
224- after_hunk_len : u32 ,
225- header : & str ,
226- hunk : & [ u8 ] ,
231+ header : gix:: diff:: blob:: unified_diff:: HunkHeader ,
232+ hunk_lines : & [ ( DiffLineKind , & [ u8 ] ) ] ,
227233 ) -> std:: io:: Result < ( ) > {
228- let non_header_lines = hunk. lines ( ) . scan (
229- ( before_hunk_start, after_hunk_start) ,
230- |( old_lineno, new_lineno) , line| {
231- let ( line_type, content, old_lineno, new_lineno) =
232- match line {
233- [ b'+' , rest @ ..] => {
234- let result = (
235- DiffLineType :: Add ,
236- rest,
237- None ,
238- Some ( * new_lineno) ,
239- ) ;
240- * new_lineno += 1 ;
241- result
242- }
243- [ b'-' , rest @ ..] => {
244- let result = (
245- DiffLineType :: Delete ,
246- rest,
247- Some ( * old_lineno) ,
248- None ,
249- ) ;
250- * old_lineno += 1 ;
251- result
252- }
253- [ b' ' , rest @ ..] => {
254- let result = (
255- DiffLineType :: None ,
256- rest,
257- Some ( * old_lineno) ,
258- Some ( * new_lineno) ,
259- ) ;
260- * old_lineno += 1 ;
261- * new_lineno += 1 ;
262- result
263- }
264- _ => {
265- // Empty lines or unknown prefixes are treated as context.
266- let result = (
267- DiffLineType :: None ,
268- line,
269- Some ( * old_lineno) ,
270- Some ( * new_lineno) ,
271- ) ;
272- * old_lineno += 1 ;
273- * new_lineno += 1 ;
274- result
275- }
276- } ;
277-
278- Some ( DiffLine {
279- position : DiffLinePosition {
280- old_lineno,
281- new_lineno,
282- } ,
283- content : String :: from_utf8_lossy ( content)
284- . trim_matches ( is_newline)
285- . into ( ) ,
286- line_type,
287- } )
288- } ,
289- ) ;
290-
291234 let mut lines = vec ! [ DiffLine {
292235 content: header. to_string( ) . into( ) ,
293236 line_type: DiffLineType :: Header ,
@@ -296,22 +239,42 @@ impl ConsumeHunk for FileDiff {
296239 new_lineno: None ,
297240 } ,
298241 } ] ;
299- lines. extend ( non_header_lines) ;
242+ lines. extend ( hunk_lines. iter ( ) . enumerate ( ) . map (
243+ |( i, ( kind, line) ) | {
244+ DiffLine {
245+ content : line
246+ . to_str_lossy ( )
247+ . trim_matches ( is_newline)
248+ . into ( ) ,
249+ line_type : ( * kind) . into ( ) ,
250+ position : DiffLinePosition {
251+ old_lineno : Some (
252+ header. before_hunk_start
253+ + u32:: try_from ( i) . unwrap ( ) ,
254+ ) ,
255+ new_lineno : Some (
256+ header. after_hunk_start
257+ + u32:: try_from ( i) . unwrap ( ) ,
258+ ) ,
259+ } ,
260+ }
261+ } ,
262+ ) ) ;
300263
301264 let hunk_header = HunkHeader {
302- old_start : before_hunk_start,
303- old_lines : before_hunk_len,
304- new_start : after_hunk_start,
305- new_lines : after_hunk_len,
265+ old_start : header . before_hunk_start ,
266+ old_lines : header . before_hunk_len ,
267+ new_start : header . after_hunk_start ,
268+ new_lines : header . after_hunk_len ,
306269 } ;
307270
271+ self . lines += lines. len ( ) ;
272+
308273 self . hunks . push ( Hunk {
309274 header_hash : hash ( & hunk_header) ,
310275 lines,
311276 } ) ;
312277
313- self . lines += hunk. lines ( ) . count ( ) ;
314-
315278 Ok ( ( ) )
316279 }
317280
@@ -468,7 +431,6 @@ pub fn get_diff(
468431 let unified_diff = UnifiedDiff :: new (
469432 & input,
470433 FileDiff :: default ( ) ,
471- NewlineSeparator :: AfterHeaderAndLine ( "\n " ) ,
472434 ContextSize :: symmetrical ( context_size) ,
473435 ) ;
474436
0 commit comments