Skip to content

Commit 7d1416a

Browse files
committed
Simplify Recorder by wrapping gix_diff::tree::Recorder
1 parent f049b00 commit 7d1416a

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

gix-blame/src/file/function.rs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use gix_diff::blob::intern::TokenSource;
44
use gix_diff::tree::Visit;
55
use gix_hash::ObjectId;
66
use gix_object::{
7-
bstr::{BStr, BString, ByteSlice, ByteVec},
7+
bstr::{BStr, BString},
88
FindExt,
99
};
10-
use std::collections::VecDeque;
1110
use std::num::NonZeroU32;
1211
use std::ops::Range;
1312

@@ -344,64 +343,45 @@ fn tree_diff_at_file_path(
344343
// The name is preliminary and can potentially include more context. Also, this should probably be
345344
// moved to its own location.
346345
struct Recorder {
346+
inner: gix_diff::tree::Recorder,
347347
interesting_path: BString,
348-
path_deque: VecDeque<BString>,
349-
path: BString,
350348
change: Option<gix_diff::tree::recorder::Change>,
351349
}
352350

353351
impl Recorder {
354352
fn new(interesting_path: BString) -> Self {
353+
let inner = gix_diff::tree::Recorder::default().track_location(Some(gix_diff::tree::recorder::Location::Path));
354+
355355
Recorder {
356+
inner,
356357
interesting_path,
357-
path_deque: Default::default(),
358-
path: Default::default(),
359358
change: None,
360359
}
361360
}
362-
363-
fn pop_element(&mut self) {
364-
if let Some(pos) = self.path.rfind_byte(b'/') {
365-
self.path.resize(pos, 0);
366-
} else {
367-
self.path.clear();
368-
}
369-
}
370-
371-
fn push_element(&mut self, name: &BStr) {
372-
if name.is_empty() {
373-
return;
374-
}
375-
if !self.path.is_empty() {
376-
self.path.push(b'/');
377-
}
378-
self.path.push_str(name);
379-
}
380361
}
381362

382363
impl Visit for Recorder {
383364
fn pop_front_tracked_path_and_set_current(&mut self) {
384-
self.path = self.path_deque.pop_front().expect("every parent is set only once");
365+
self.inner.pop_front_tracked_path_and_set_current();
385366
}
386367

387368
fn push_back_tracked_path_component(&mut self, component: &BStr) {
388-
self.push_element(component);
389-
self.path_deque.push_back(self.path.clone());
369+
self.inner.push_back_tracked_path_component(component);
390370
}
391371

392372
fn push_path_component(&mut self, component: &BStr) {
393-
self.push_element(component);
373+
self.inner.push_path_component(component);
394374
}
395375

396376
fn pop_path_component(&mut self) {
397-
self.pop_element();
377+
self.inner.pop_path_component();
398378
}
399379

400380
fn visit(&mut self, change: gix_diff::tree::visit::Change) -> gix_diff::tree::visit::Action {
401381
use gix_diff::tree::visit::Action::*;
402382
use gix_diff::tree::visit::Change::*;
403383

404-
if self.path == self.interesting_path {
384+
if self.inner.path() == self.interesting_path {
405385
self.change = Some(match change {
406386
Deletion {
407387
entry_mode,
@@ -410,7 +390,7 @@ impl Visit for Recorder {
410390
} => gix_diff::tree::recorder::Change::Deletion {
411391
entry_mode,
412392
oid,
413-
path: self.path.clone(),
393+
path: self.inner.path_clone(),
414394
relation,
415395
},
416396
Addition {
@@ -420,7 +400,7 @@ impl Visit for Recorder {
420400
} => gix_diff::tree::recorder::Change::Addition {
421401
entry_mode,
422402
oid,
423-
path: self.path.clone(),
403+
path: self.inner.path_clone(),
424404
relation,
425405
},
426406
Modification {
@@ -433,7 +413,7 @@ impl Visit for Recorder {
433413
previous_oid,
434414
entry_mode,
435415
oid,
436-
path: self.path.clone(),
416+
path: self.inner.path_clone(),
437417
},
438418
});
439419

0 commit comments

Comments
 (0)