History

refactor

Sebastian Thiel committed 5 years ago

Commit c0318cf1 parent aaba9dc7 files · +51 −51Browse files

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

Unified · Split

5 files
  1. git-diff/src/lib.rs +1 −1
  2. git-diff/src/tree/changes.rs +21 −21
  3. git-diff/src/tree/mod.rs +3 −3
  4. git-diff/src/tree/recorder.rs +8 −8
  5. git-diff/src/tree/visit.rs +4 −4
git-diff/src/lib.rs+1−1
@@ -3,4 +3,4 @@ · 2 unmodified lines
3#[deny(missing_docs)]3#[deny(missing_docs)]
4 4
5///5///
6pub mod visit;6pub mod tree;
git-diff/src/tree/changes.rsrenamed from git-diff/src/visit/changes.rs+21−21
@@ -1,9 +1,9 @@
1use crate::{1use crate::{
2 visit,2 tree,
3 visit::{changes::Error::Cancelled, record::Change, TreeInfo, TreeInfoPair},3 tree::{changes::Error::Cancelled, visit::Change, TreeInfo, TreeInfoPair},
4};4};
5use git_hash::{oid, ObjectId};5use git_hash::{oid, ObjectId};
6use git_object::{immutable, tree};6use git_object::immutable;
7use quick_error::quick_error;7use quick_error::quick_error;
8use std::{borrow::BorrowMut, collections::VecDeque};8use std::{borrow::BorrowMut, collections::VecDeque};
9 9
@@ -24,8 +24,8 @@ quick_error! { · 14 unmodified lines
24 }24 }
25}25}
26 26
27impl<'a> visit::Changes<'a> {27impl<'a> tree::Changes<'a> {
28 /// Returns the changes that need to be applied to `self` to get `other`.28 /// that need to be applied to `self` to get `other`.
29 /// TODO: Talk about choices in error handling and why Option<T> is enough, as opposed to the usual Result<Option<T>>29 /// TODO: Talk about choices in error handling and why Option<T> is enough, as opposed to the usual Result<Option<T>>
30 /// Talk about progress (done via delegate if needed)30 /// Talk about progress (done via delegate if needed)
31 ///31 ///
@@ -51,8 +51,8 @@ impl<'a> visit::Changes<'a> { · 19 unmodified lines
51 ) -> Result<(), Error>51 ) -> Result<(), Error>
52 where52 where
53 LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::tree::TreeIter<'b>>,53 LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::tree::TreeIter<'b>>,
54 R: visit::Record,54 R: tree::Visit,
55 StateMut: BorrowMut<visit::State<R::PathId>>,55 StateMut: BorrowMut<tree::State<R::PathId>>,
56 {56 {
57 let state = state.borrow_mut();57 let state = state.borrow_mut();
58 state.clear();58 state.clear();
@@ -111,14 +111,14 @@ impl<'a> visit::Changes<'a> { · 52 unmodified lines
111 }111 }
112}112}
113 113
114fn delete_entry_schedule_recursion<R: visit::Record>(114fn delete_entry_schedule_recursion<R: tree::Visit>(
115 entry: immutable::tree::Entry<'_>,115 entry: immutable::tree::Entry<'_>,
116 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,116 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
117 delegate: &mut R,117 delegate: &mut R,
118) -> Result<(), Error> {118) -> Result<(), Error> {
119 delegate.push_path_component(entry.filename);119 delegate.push_path_component(entry.filename);
120 if delegate120 if delegate
121 .record(Change::Deletion {121 .visit(Change::Deletion {
122 entry_mode: entry.mode,122 entry_mode: entry.mode,
123 oid: entry.oid.to_owned(),123 oid: entry.oid.to_owned(),
124 })124 })
@@ -140,14 +140,14 @@ fn delete_entry_schedule_recursion<R: visit::Record>( · 15 unmodified lines
140 Ok(())140 Ok(())
141}141}
142 142
143fn add_entry_schedule_recursion<R: visit::Record>(143fn add_entry_schedule_recursion<R: tree::Visit>(
144 entry: immutable::tree::Entry<'_>,144 entry: immutable::tree::Entry<'_>,
145 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,145 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
146 delegate: &mut R,146 delegate: &mut R,
147) -> Result<(), Error> {147) -> Result<(), Error> {
148 delegate.push_path_component(entry.filename);148 delegate.push_path_component(entry.filename);
149 if delegate149 if delegate
150 .record(Change::Addition {150 .visit(Change::Addition {
151 entry_mode: entry.mode,151 entry_mode: entry.mode,
152 oid: entry.oid.to_owned(),152 oid: entry.oid.to_owned(),
153 })153 })
@@ -168,7 +168,7 @@ fn add_entry_schedule_recursion<R: visit::Record>( · 14 unmodified lines
168 }168 }
169 Ok(())169 Ok(())
170}170}
171fn catchup_rhs_with_lhs<R: visit::Record>(171fn catchup_rhs_with_lhs<R: tree::Visit>(
172 rhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,172 rhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
173 lhs: immutable::tree::Entry<'_>,173 lhs: immutable::tree::Entry<'_>,
174 rhs: immutable::tree::Entry<'_>,174 rhs: immutable::tree::Entry<'_>,
@@ -208,7 +208,7 @@ fn catchup_rhs_with_lhs<R: visit::Record>( · 33 unmodified lines
208 Ok(())208 Ok(())
209}209}
210 210
211fn catchup_lhs_with_rhs<R: visit::Record>(211fn catchup_lhs_with_rhs<R: tree::Visit>(
212 lhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,212 lhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
213 lhs: immutable::tree::Entry<'_>,213 lhs: immutable::tree::Entry<'_>,
214 rhs: immutable::tree::Entry<'_>,214 rhs: immutable::tree::Entry<'_>,
@@ -248,19 +248,19 @@ fn catchup_lhs_with_rhs<R: visit::Record>( · 33 unmodified lines
248 Ok(())248 Ok(())
249}249}
250 250
251fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>(251fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
252 lhs: immutable::tree::Entry<'_>,252 lhs: immutable::tree::Entry<'_>,
253 rhs: immutable::tree::Entry<'_>,253 rhs: immutable::tree::Entry<'_>,
254 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,254 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,
255 delegate: &mut R,255 delegate: &mut R,
256) -> Result<(), Error> {256) -> Result<(), Error> {
257 use tree::EntryMode::*;257 use git_object::tree::EntryMode::*;
258 match (lhs.mode, rhs.mode) {258 match (lhs.mode, rhs.mode) {
259 (Tree, Tree) => {259 (Tree, Tree) => {
260 let path_id = delegate.push_tracked_path_component(lhs.filename);260 let path_id = delegate.push_tracked_path_component(lhs.filename);
261 if lhs.oid != rhs.oid261 if lhs.oid != rhs.oid
262 && delegate262 && delegate
263 .record(Change::Modification {263 .visit(Change::Modification {
264 previous_entry_mode: lhs.mode,264 previous_entry_mode: lhs.mode,
265 previous_oid: lhs.oid.to_owned(),265 previous_oid: lhs.oid.to_owned(),
266 entry_mode: rhs.mode,266 entry_mode: rhs.mode,
@@ -284,7 +284,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>( · 17 unmodified lines
284 (lhs_mode, Tree) if lhs_mode.is_no_tree() => {284 (lhs_mode, Tree) if lhs_mode.is_no_tree() => {
285 let path_id = delegate.push_tracked_path_component(lhs.filename);285 let path_id = delegate.push_tracked_path_component(lhs.filename);
286 if delegate286 if delegate
287 .record(Change::Deletion {287 .visit(Change::Deletion {
288 entry_mode: lhs.mode,288 entry_mode: lhs.mode,
289 oid: lhs.oid.to_owned(),289 oid: lhs.oid.to_owned(),
290 })290 })
@@ -293,7 +293,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>( · 2 unmodified lines
293 return Err(Cancelled);293 return Err(Cancelled);
294 };294 };
295 if delegate295 if delegate
296 .record(Change::Addition {296 .visit(Change::Addition {
297 entry_mode: rhs.mode,297 entry_mode: rhs.mode,
298 oid: rhs.oid.to_owned(),298 oid: rhs.oid.to_owned(),
299 })299 })
@@ -312,7 +312,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>( · 12 unmodified lines
312 (Tree, rhs_mode) if rhs_mode.is_no_tree() => {312 (Tree, rhs_mode) if rhs_mode.is_no_tree() => {
313 let path_id = delegate.push_tracked_path_component(lhs.filename);313 let path_id = delegate.push_tracked_path_component(lhs.filename);
314 if delegate314 if delegate
315 .record(Change::Deletion {315 .visit(Change::Deletion {
316 entry_mode: lhs.mode,316 entry_mode: lhs.mode,
317 oid: lhs.oid.to_owned(),317 oid: lhs.oid.to_owned(),
318 })318 })
@@ -321,7 +321,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>( · 2 unmodified lines
321 return Err(Error::Cancelled);321 return Err(Error::Cancelled);
322 }322 }
323 if delegate323 if delegate
324 .record(Change::Addition {324 .visit(Change::Addition {
325 entry_mode: rhs.mode,325 entry_mode: rhs.mode,
326 oid: rhs.oid.to_owned(),326 oid: rhs.oid.to_owned(),
327 })327 })
@@ -342,7 +342,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>( · 14 unmodified lines
342 debug_assert!(lhs_non_tree.is_no_tree() && rhs_non_tree.is_no_tree());342 debug_assert!(lhs_non_tree.is_no_tree() && rhs_non_tree.is_no_tree());
343 if lhs.oid != rhs.oid343 if lhs.oid != rhs.oid
344 && delegate344 && delegate
345 .record(Change::Modification {345 .visit(Change::Modification {
346 previous_entry_mode: lhs.mode,346 previous_entry_mode: lhs.mode,
347 previous_oid: lhs.oid.to_owned(),347 previous_oid: lhs.oid.to_owned(),
348 entry_mode: rhs.mode,348 entry_mode: rhs.mode,
git-diff/src/tree/mod.rsrenamed from git-diff/src/visit/mod.rs+3−3
@@ -41,9 +41,9 @@ where · 40 unmodified lines
41mod changes;41mod changes;
42 42
43///43///
44pub mod record;44pub mod visit;
45pub use record::Record;45pub use visit::Visit;
46 46
47/// Houses a [`record::Record`] to keep track of all seen changes. Useful for debugging primarily.47/// Houses a [`visit::Visit`] to keep track of all seen changes. Useful for debugging primarily.
48pub mod recorder;48pub mod recorder;
49pub use recorder::Recorder;49pub use recorder::Recorder;
git-diff/src/tree/recorder.rsrenamed from git-diff/src/visit/recorder.rs+8−8
@@ -1,4 +1,4 @@
1use crate::visit::record;1use crate::tree::visit;
2use git_hash::ObjectId;2use git_hash::ObjectId;
3use git_object::{3use git_object::{
4 bstr::{BStr, BString, ByteSlice, ByteVec},4 bstr::{BStr, BString, ByteSlice, ByteVec},
@@ -6,8 +6,8 @@ use git_object::{ · 1 unmodified line
6};6};
7use std::{collections::BTreeMap, ops::Deref, path::PathBuf};7use std::{collections::BTreeMap, ops::Deref, path::PathBuf};
8 8
9/// A Change as observed by a call to [`record::Record::record`], enhanced with the path affected by the change.9/// A Change as observed by a call to [`visit::Visit::visit`], enhanced with the path affected by the change.
10/// Its similar to [record::Change] but adds a path.10/// Its similar to [visit::Change] but adds a path.
11#[derive(Clone, Debug, PartialEq, Eq)]11#[derive(Clone, Debug, PartialEq, Eq)]
12#[allow(missing_docs)]12#[allow(missing_docs)]
13pub enum Change {13pub enum Change {
@@ -32,7 +32,7 @@ pub enum Change { · 18 unmodified lines
32 },32 },
33}33}
34 34
35/// A [record::Record] implementation to record every observed change and keep track of the changed paths.35/// A [visit::Visit] implementation to record every observed change and keep track of the changed paths.
36#[derive(Clone, Debug, Default)]36#[derive(Clone, Debug, Default)]
37pub struct Recorder {37pub struct Recorder {
38 path_count: usize,38 path_count: usize,
@@ -67,7 +67,7 @@ impl Recorder { · 28 unmodified lines
67 }67 }
68}68}
69 69
70impl record::Record for Recorder {70impl visit::Visit for Recorder {
71 type PathId = usize;71 type PathId = usize;
72 72
73 fn set_current_path(&mut self, path: Self::PathId) {73 fn set_current_path(&mut self, path: Self::PathId) {
@@ -90,8 +90,8 @@ impl record::Record for Recorder { · 16 unmodified lines
90 self.pop_element();90 self.pop_element();
91 }91 }
92 92
93 fn record(&mut self, change: record::Change) -> record::Action {93 fn visit(&mut self, change: visit::Change) -> visit::Action {
94 use record::Change::*;94 use visit::Change::*;
95 self.records.push(match change {95 self.records.push(match change {
96 Deletion { entry_mode, oid } => Change::Deletion {96 Deletion { entry_mode, oid } => Change::Deletion {
97 entry_mode,97 entry_mode,
@@ -116,6 +116,6 @@ impl record::Record for Recorder { · 18 unmodified lines
116 path: self.path_buf(),116 path: self.path_buf(),
117 },117 },
118 });118 });
119 record::Action::Continue119 visit::Action::Continue
120 }120 }
121}121}
git-diff/src/tree/visit.rsrenamed from git-diff/src/visit/record.rs+4−4
@@ -32,12 +32,12 @@ pub enum Change { · 31 unmodified lines
32 },32 },
33}33}
34 34
35/// What to do after a [Change] was [recorded][Record::record()].35/// What to do after a [Change] was [recorded][Visit::visit()].
36#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]36#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
37pub enum Action {37pub enum Action {
38 /// Continue the traversal of changes.38 /// Continue the traversal of changes.
39 Continue,39 Continue,
40 /// Stop the traversal of changes, making this te last call to [record(…)][Record::record()].40 /// Stop the traversal of changes, making this te last call to [record(…)][Visit::visit()].
41 Cancel,41 Cancel,
42}42}
43 43
@@ -50,7 +50,7 @@ impl Action { · 6 unmodified lines
50 50
51/// A trait to allow responding to a traversal designed to figure out the [changes][Change]51/// A trait to allow responding to a traversal designed to figure out the [changes][Change]
52/// to turn tree A into tree B.52/// to turn tree A into tree B.
53pub trait Record {53pub trait Visit {
54 /// A type capable of uniquely identifying paths in a tree.54 /// A type capable of uniquely identifying paths in a tree.
55 type PathId: Clone + Default;55 type PathId: Clone + Default;
56 56
@@ -65,7 +65,7 @@ pub trait Record { · 8 unmodified lines
65 /// Record a `change` and return an instruction whether to continue or not.65 /// Record a `change` and return an instruction whether to continue or not.
66 ///66 ///
67 /// The implementation may use the current path to lean where in the tree the change is located.67 /// The implementation may use the current path to lean where in the tree the change is located.
68 fn record(&mut self, change: Change) -> Action;68 fn visit(&mut self, change: Change) -> Action;
69}69}
70 70
71#[cfg(test)]71#[cfg(test)]