History

[git-diff] refactor

Sebastian Thiel committed 5 years ago

Commit 087e8531 parent 323d92f10 files · +45 −53Browse files

Showing the changes to git-diff/src (4 of 10 files). Show all

Unified · Split

4 files
  1. git-diff/src/tree/changes.rs +17 −17
  2. git-diff/src/tree/mod.rs +8 −7
  3. git-diff/src/tree/recorder.rs +6 −10
  4. git-diff/src/tree/visit.rs +4 −8
git-diff/src/tree/changes.rs+17−17
@@ -60,7 +60,7 @@ impl<'a> tree::Changes<'a> { · 59 unmodified lines
6060 where
6161 LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::tree::TreeIter<'b>>,
6262 R: tree::Visit,
63− StateMut: BorrowMut<tree::State<R::PathId>>,
63+ StateMut: BorrowMut<tree::State>,
6464 {
6565 let state = state.borrow_mut();
6666 state.clear();
@@ -75,14 +75,14 @@ impl<'a> tree::Changes<'a> { · 8 unmodified lines
7575 match (lhs_entries.next(), rhs_entries.next()) {
7676 (None, None) => {
7777 match state.trees.pop_front() {
78− Some((None, Some(mut rhs))) => {
79− delegate.set_current_path(rhs.parent_path_id.take().expect("path-id is set"));
78+ Some((None, Some(rhs))) => {
79+ delegate.pop_front_tracked_path_and_set_current();
8080 rhs_entries = peekable(
8181 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
8282 );
8383 }
84− Some((Some(mut lhs), Some(rhs))) => {
85− delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set in lhs"));
84+ Some((Some(lhs), Some(rhs))) => {
85+ delegate.pop_front_tracked_path_and_set_current();
8686 lhs_entries = peekable(
8787 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
8888 );
@@ -90,8 +90,8 @@ impl<'a> tree::Changes<'a> { · 1 unmodified line
9090 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
9191 );
9292 }
93− Some((Some(mut lhs), None)) => {
94− delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set"));
93+ Some((Some(lhs), None)) => {
94+ delegate.pop_front_tracked_path_and_set_current();
9595 lhs_entries = peekable(
9696 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
9797 );
@@ -125,7 +125,7 @@ impl<'a> tree::Changes<'a> { · 27 unmodified lines
125125
126126 fn delete_entry_schedule_recursion<R: tree::Visit>(
127127 entry: immutable::tree::Entry<'_>,
128− queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
128+ queue: &mut VecDeque<TreeInfoPair>,
129129 delegate: &mut R,
130130 ) -> Result<(), Error> {
131131 delegate.push_path_component(entry.filename);
@@ -140,7 +140,7 @@ fn delete_entry_schedule_recursion<R: tree::Visit>( · 8 unmodified lines
140140 }
141141 if entry.mode.is_tree() {
142142 delegate.pop_path_component();
143− let path_id = delegate.push_tracked_path_component(entry.filename);
143+ let path_id = delegate.push_back_tracked_path_component(entry.filename);
144144 queue.push_back((
145145 Some(TreeInfo {
146146 tree_id: entry.oid.to_owned(),
@@ -154,7 +154,7 @@ fn delete_entry_schedule_recursion<R: tree::Visit>( · 7 unmodified lines
154154
155155 fn add_entry_schedule_recursion<R: tree::Visit>(
156156 entry: immutable::tree::Entry<'_>,
157− queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
157+ queue: &mut VecDeque<TreeInfoPair>,
158158 delegate: &mut R,
159159 ) -> Result<(), Error> {
160160 delegate.push_path_component(entry.filename);
@@ -169,7 +169,7 @@ fn add_entry_schedule_recursion<R: tree::Visit>( · 8 unmodified lines
169169 }
170170 if entry.mode.is_tree() {
171171 delegate.pop_path_component();
172− let path_id = delegate.push_tracked_path_component(entry.filename);
172+ let path_id = delegate.push_back_tracked_path_component(entry.filename);
173173 queue.push_back((
174174 None,
175175 Some(TreeInfo {
@@ -184,7 +184,7 @@ fn catchup_rhs_with_lhs<R: tree::Visit>( · 8 unmodified lines
184184 rhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
185185 lhs: immutable::tree::Entry<'_>,
186186 rhs: immutable::tree::Entry<'_>,
187− queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
187+ queue: &mut VecDeque<TreeInfoPair>,
188188 delegate: &mut R,
189189 ) -> Result<(), Error> {
190190 use std::cmp::Ordering::*;
@@ -224,7 +224,7 @@ fn catchup_lhs_with_rhs<R: tree::Visit>( · 33 unmodified lines
224224 lhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
225225 lhs: immutable::tree::Entry<'_>,
226226 rhs: immutable::tree::Entry<'_>,
227− queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
227+ queue: &mut VecDeque<TreeInfoPair>,
228228 delegate: &mut R,
229229 ) -> Result<(), Error> {
230230 use std::cmp::Ordering::*;
@@ -263,13 +263,13 @@ fn catchup_lhs_with_rhs<R: tree::Visit>( · 32 unmodified lines
263263 fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
264264 lhs: immutable::tree::Entry<'_>,
265265 rhs: immutable::tree::Entry<'_>,
266− queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
266+ queue: &mut VecDeque<TreeInfoPair>,
267267 delegate: &mut R,
268268 ) -> Result<(), Error> {
269269 use git_object::tree::EntryMode::*;
270270 match (lhs.mode, rhs.mode) {
271271 (Tree, Tree) => {
272− let path_id = delegate.push_tracked_path_component(lhs.filename);
272+ let path_id = delegate.push_back_tracked_path_component(lhs.filename);
273273 if lhs.oid != rhs.oid
274274 && delegate
275275 .visit(Change::Modification {
@@ -294,7 +294,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 18 unmodified lines
294294 ));
295295 }
296296 (lhs_mode, Tree) if lhs_mode.is_no_tree() => {
297− let path_id = delegate.push_tracked_path_component(lhs.filename);
297+ let path_id = delegate.push_back_tracked_path_component(lhs.filename);
298298 if delegate
299299 .visit(Change::Deletion {
300300 entry_mode: lhs.mode,
@@ -322,7 +322,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 21 unmodified lines
322322 ));
323323 }
324324 (Tree, rhs_mode) if rhs_mode.is_no_tree() => {
325− let path_id = delegate.push_tracked_path_component(lhs.filename);
325+ let path_id = delegate.push_back_tracked_path_component(lhs.filename);
326326 if delegate
327327 .visit(Change::Deletion {
328328 entry_mode: lhs.mode,
git-diff/src/tree/mod.rs+8−7
@@ -3,21 +3,22 @@ use git_object::immutable; · 2 unmodified lines
33 use std::collections::VecDeque;
44
55 /// The state required to visit [Changes] to be instantiated with `State::default()`.
6−#[derive(Default)]
7−pub struct State<PathId> {
6+#[derive(Default, Clone)]
7+pub struct State {
88 buf1: Vec<u8>,
99 buf2: Vec<u8>,
10− trees: VecDeque<TreeInfoPair<PathId>>,
10+ trees: VecDeque<TreeInfoPair>,
1111 }
1212
13−type TreeInfoPair<PathId> = (Option<TreeInfo<PathId>>, Option<TreeInfo<PathId>>);
13+type TreeInfoPair = (Option<TreeInfo>, Option<TreeInfo>);
1414
15−pub(crate) struct TreeInfo<PathId> {
15+#[derive(Clone)]
16+pub(crate) struct TreeInfo {
1617 pub tree_id: ObjectId,
17− pub parent_path_id: Option<PathId>,
18+ pub parent_path_id: Option<()>,
1819 }
1920
20−impl<P> State<P> {
21+impl State {
2122 fn clear(&mut self) {
2223 self.trees.clear();
2324 self.buf1.clear();
git-diff/src/tree/recorder.rs+6−10
@@ -4,7 +4,7 @@ use git_object::{ · 3 unmodified lines
44 bstr::{BStr, BString, ByteSlice, ByteVec},
55 tree,
66 };
7−use std::collections::BTreeMap;
7+use std::collections::{BTreeMap, VecDeque};
88
99 /// A Change as observed by a call to [`visit(…)`][visit::Visit::visit()], enhanced with the path affected by the change.
1010 /// Its similar to [visit::Change] but includes the path that changed.
@@ -37,6 +37,7 @@ pub enum Change { · 26 unmodified lines
3737 pub struct Recorder {
3838 path_count: usize,
3939 path_map: BTreeMap<usize, BString>,
40+ path_deque: VecDeque<BString>,
4041 path: BString,
4142 /// The observed changes.
4243 pub records: Vec<Change>,
@@ -64,18 +65,13 @@ impl Recorder { · 21 unmodified lines
6465 }
6566
6667 impl visit::Visit for Recorder {
67− type PathId = usize;
68−
69− fn set_current_path(&mut self, path: Self::PathId) {
70− self.path = self.path_map.remove(&path).expect("every parent is set only once");
68+ fn pop_front_tracked_path_and_set_current(&mut self) {
69+ self.path = self.path_deque.pop_front().expect("every parent is set only once");
7170 }
7271
73− fn push_tracked_path_component(&mut self, component: &BStr) -> Self::PathId {
72+ fn push_back_tracked_path_component(&mut self, component: &BStr) {
7473 self.push_element(component);
75− self.path_map.insert(self.path_count, self.path_clone());
76− let res = self.path_count;
77− self.path_count += 1;
78− res
74+ self.path_deque.push_back(self.path.clone());
7975 }
8076
8177 fn push_path_component(&mut self, component: &BStr) {
git-diff/src/tree/visit.rs+4−8
@@ -51,14 +51,10 @@ impl Action { · 50 unmodified lines
5151 /// A trait to allow responding to a traversal designed to figure out the [changes][Change]
5252 /// to turn tree A into tree B.
5353 pub trait Visit {
54− /// A type capable of uniquely identifying paths in a tree.
55− type PathId;
56−
57− /// Sets the path associated with the given `id` so future calls to push and pop components affect it instead.
58− /// It will only ever be called once with a given `id`.
59− fn set_current_path(&mut self, id: Self::PathId);
60− /// Append a `component` to the end of a path, which may be empty, and associate it with the returned path id.
61− fn push_tracked_path_component(&mut self, component: &BStr) -> Self::PathId;
54+ /// Sets the full path path in front of the queue so future calls to push and pop components affect it instead.
55+ fn pop_front_tracked_path_and_set_current(&mut self);
56+ /// Append a `component` to the end of a path, which may be empty.
57+ fn push_back_tracked_path_component(&mut self, component: &BStr);
6258 /// Append a `component` to the end of a path, which may be empty.
6359 fn push_path_component(&mut self, component: &BStr);
6460 /// Removes the last component from the path, which may leave it empty.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t