@@ -326,104 +326,102 @@ fn tree_diff_at_file_path(
326326 let tree_iter = odb. find_tree_iter ( & tree_id, rhs_tree_buf) ?;
327327 stats. trees_decoded += 1 ;
328328
329- let mut recorder = Recorder :: new ( file_path. into ( ) ) ;
330- let result = gix_diff:: tree ( parent_tree_iter, tree_iter, state, & odb, & mut recorder) ;
331- stats. trees_diffed += 1 ;
332-
333- match result {
334- // `recorder` cancels the traversal by returning `Cancel` when a change to `file_path` is
335- // found. `gix_diff::tree` converts `Cancel` into `Err(Cancelled)` which is why we match on
336- // `Err(Cancelled)` in addition to `Ok`.
337- Ok ( _) | Err ( gix_diff:: tree:: Error :: Cancelled ) => Ok ( recorder. change ) ,
338- Err ( error) => Err ( Error :: DiffTree ( error) ) ,
329+ struct FindChangeToPath {
330+ inner : gix_diff:: tree:: Recorder ,
331+ interesting_path : BString ,
332+ change : Option < gix_diff:: tree:: recorder:: Change > ,
339333 }
340- }
341-
342- // TODO
343- // The name is preliminary and can potentially include more context. Also, this should probably be
344- // moved to its own location.
345- struct Recorder {
346- inner : gix_diff:: tree:: Recorder ,
347- interesting_path : BString ,
348- change : Option < gix_diff:: tree:: recorder:: Change > ,
349- }
350334
351- impl Recorder {
352- fn new ( interesting_path : BString ) -> Self {
353- let inner = gix_diff:: tree:: Recorder :: default ( ) . track_location ( Some ( gix_diff:: tree:: recorder:: Location :: Path ) ) ;
335+ impl FindChangeToPath {
336+ fn new ( interesting_path : BString ) -> Self {
337+ let inner =
338+ gix_diff:: tree:: Recorder :: default ( ) . track_location ( Some ( gix_diff:: tree:: recorder:: Location :: Path ) ) ;
354339
355- Recorder {
356- inner,
357- interesting_path,
358- change : None ,
340+ FindChangeToPath {
341+ inner,
342+ interesting_path,
343+ change : None ,
344+ }
359345 }
360346 }
361- }
362347
363- impl Visit for Recorder {
364- fn pop_front_tracked_path_and_set_current ( & mut self ) {
365- self . inner . pop_front_tracked_path_and_set_current ( ) ;
366- }
348+ impl Visit for FindChangeToPath {
349+ fn pop_front_tracked_path_and_set_current ( & mut self ) {
350+ self . inner . pop_front_tracked_path_and_set_current ( ) ;
351+ }
367352
368- fn push_back_tracked_path_component ( & mut self , component : & BStr ) {
369- self . inner . push_back_tracked_path_component ( component) ;
370- }
353+ fn push_back_tracked_path_component ( & mut self , component : & BStr ) {
354+ self . inner . push_back_tracked_path_component ( component) ;
355+ }
371356
372- fn push_path_component ( & mut self , component : & BStr ) {
373- self . inner . push_path_component ( component) ;
374- }
357+ fn push_path_component ( & mut self , component : & BStr ) {
358+ self . inner . push_path_component ( component) ;
359+ }
375360
376- fn pop_path_component ( & mut self ) {
377- self . inner . pop_path_component ( ) ;
378- }
361+ fn pop_path_component ( & mut self ) {
362+ self . inner . pop_path_component ( ) ;
363+ }
379364
380- fn visit ( & mut self , change : gix_diff:: tree:: visit:: Change ) -> gix_diff:: tree:: visit:: Action {
381- use gix_diff:: tree:: visit:: Action :: * ;
382- use gix_diff:: tree:: visit:: Change :: * ;
383-
384- if self . inner . path ( ) == self . interesting_path {
385- self . change = Some ( match change {
386- Deletion {
387- entry_mode,
388- oid,
389- relation,
390- } => gix_diff:: tree:: recorder:: Change :: Deletion {
391- entry_mode,
392- oid,
393- path : self . inner . path_clone ( ) ,
394- relation,
395- } ,
396- Addition {
397- entry_mode,
398- oid,
399- relation,
400- } => gix_diff:: tree:: recorder:: Change :: Addition {
401- entry_mode,
402- oid,
403- path : self . inner . path_clone ( ) ,
404- relation,
405- } ,
406- Modification {
407- previous_entry_mode,
408- previous_oid,
409- entry_mode,
410- oid,
411- } => gix_diff:: tree:: recorder:: Change :: Modification {
412- previous_entry_mode,
413- previous_oid,
414- entry_mode,
415- oid,
416- path : self . inner . path_clone ( ) ,
417- } ,
418- } ) ;
419-
420- // When we return `Cancel`, `gix_diff::tree` will convert this `Cancel` into an
421- // `Err(...)`. Keep this in mind when using `Recorder`.
422- Cancel
423- } else {
424- Continue
365+ fn visit ( & mut self , change : gix_diff:: tree:: visit:: Change ) -> gix_diff:: tree:: visit:: Action {
366+ use gix_diff:: tree:: visit:: Action :: * ;
367+ use gix_diff:: tree:: visit:: Change :: * ;
368+
369+ if self . inner . path ( ) == self . interesting_path {
370+ self . change = Some ( match change {
371+ Deletion {
372+ entry_mode,
373+ oid,
374+ relation,
375+ } => gix_diff:: tree:: recorder:: Change :: Deletion {
376+ entry_mode,
377+ oid,
378+ path : self . inner . path_clone ( ) ,
379+ relation,
380+ } ,
381+ Addition {
382+ entry_mode,
383+ oid,
384+ relation,
385+ } => gix_diff:: tree:: recorder:: Change :: Addition {
386+ entry_mode,
387+ oid,
388+ path : self . inner . path_clone ( ) ,
389+ relation,
390+ } ,
391+ Modification {
392+ previous_entry_mode,
393+ previous_oid,
394+ entry_mode,
395+ oid,
396+ } => gix_diff:: tree:: recorder:: Change :: Modification {
397+ previous_entry_mode,
398+ previous_oid,
399+ entry_mode,
400+ oid,
401+ path : self . inner . path_clone ( ) ,
402+ } ,
403+ } ) ;
404+
405+ // When we return `Cancel`, `gix_diff::tree` will convert this `Cancel` into an
406+ // `Err(...)`. Keep this in mind when using `FindChangeToPath`.
407+ Cancel
408+ } else {
409+ Continue
410+ }
425411 }
426412 }
413+
414+ let mut recorder = FindChangeToPath :: new ( file_path. into ( ) ) ;
415+ let result = gix_diff:: tree ( parent_tree_iter, tree_iter, state, & odb, & mut recorder) ;
416+ stats. trees_diffed += 1 ;
417+
418+ match result {
419+ // `recorder` cancels the traversal by returning `Cancel` when a change to `file_path` is
420+ // found. `gix_diff::tree` converts `Cancel` into `Err(Cancelled)` which is why we match on
421+ // `Err(Cancelled)` in addition to `Ok`.
422+ Ok ( _) | Err ( gix_diff:: tree:: Error :: Cancelled ) => Ok ( recorder. change ) ,
423+ Err ( error) => Err ( Error :: DiffTree ( error) ) ,
424+ }
427425}
428426
429427fn blob_changes (
0 commit comments