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
60 where60 where
61 LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::tree::TreeIter<'b>>,61 LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::tree::TreeIter<'b>>,
62 R: tree::Visit,62 R: tree::Visit,
63 StateMut: BorrowMut<tree::State<R::PathId>>,63 StateMut: BorrowMut<tree::State>,
64 {64 {
65 let state = state.borrow_mut();65 let state = state.borrow_mut();
66 state.clear();66 state.clear();
@@ -75,14 +75,14 @@ impl<'a> tree::Changes<'a> { · 8 unmodified lines
75 match (lhs_entries.next(), rhs_entries.next()) {75 match (lhs_entries.next(), rhs_entries.next()) {
76 (None, None) => {76 (None, None) => {
77 match state.trees.pop_front() {77 match state.trees.pop_front() {
78 Some((None, Some(mut rhs))) => {78 Some((None, Some(rhs))) => {
79 delegate.set_current_path(rhs.parent_path_id.take().expect("path-id is set"));79 delegate.pop_front_tracked_path_and_set_current();
80 rhs_entries = peekable(80 rhs_entries = peekable(
81 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,81 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
82 );82 );
83 }83 }
84 Some((Some(mut lhs), Some(rhs))) => {84 Some((Some(lhs), Some(rhs))) => {
85 delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set in lhs"));85 delegate.pop_front_tracked_path_and_set_current();
86 lhs_entries = peekable(86 lhs_entries = peekable(
87 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,87 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
88 );88 );
@@ -90,8 +90,8 @@ impl<'a> tree::Changes<'a> { · 1 unmodified line
90 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,90 locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound { oid: rhs.tree_id })?,
91 );91 );
92 }92 }
93 Some((Some(mut lhs), None)) => {93 Some((Some(lhs), None)) => {
94 delegate.set_current_path(lhs.parent_path_id.take().expect("path-id is set"));94 delegate.pop_front_tracked_path_and_set_current();
95 lhs_entries = peekable(95 lhs_entries = peekable(
96 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,96 locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound { oid: lhs.tree_id })?,
97 );97 );
@@ -125,7 +125,7 @@ impl<'a> tree::Changes<'a> { · 27 unmodified lines
125 125
126fn delete_entry_schedule_recursion<R: tree::Visit>(126fn delete_entry_schedule_recursion<R: tree::Visit>(
127 entry: immutable::tree::Entry<'_>,127 entry: immutable::tree::Entry<'_>,
128 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,128 queue: &mut VecDeque<TreeInfoPair>,
129 delegate: &mut R,129 delegate: &mut R,
130) -> Result<(), Error> {130) -> Result<(), Error> {
131 delegate.push_path_component(entry.filename);131 delegate.push_path_component(entry.filename);
@@ -140,7 +140,7 @@ fn delete_entry_schedule_recursion<R: tree::Visit>( · 8 unmodified lines
140 }140 }
141 if entry.mode.is_tree() {141 if entry.mode.is_tree() {
142 delegate.pop_path_component();142 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);
144 queue.push_back((144 queue.push_back((
145 Some(TreeInfo {145 Some(TreeInfo {
146 tree_id: entry.oid.to_owned(),146 tree_id: entry.oid.to_owned(),
@@ -154,7 +154,7 @@ fn delete_entry_schedule_recursion<R: tree::Visit>( · 7 unmodified lines
154 154
155fn add_entry_schedule_recursion<R: tree::Visit>(155fn add_entry_schedule_recursion<R: tree::Visit>(
156 entry: immutable::tree::Entry<'_>,156 entry: immutable::tree::Entry<'_>,
157 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,157 queue: &mut VecDeque<TreeInfoPair>,
158 delegate: &mut R,158 delegate: &mut R,
159) -> Result<(), Error> {159) -> Result<(), Error> {
160 delegate.push_path_component(entry.filename);160 delegate.push_path_component(entry.filename);
@@ -169,7 +169,7 @@ fn add_entry_schedule_recursion<R: tree::Visit>( · 8 unmodified lines
169 }169 }
170 if entry.mode.is_tree() {170 if entry.mode.is_tree() {
171 delegate.pop_path_component();171 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);
173 queue.push_back((173 queue.push_back((
174 None,174 None,
175 Some(TreeInfo {175 Some(TreeInfo {
@@ -184,7 +184,7 @@ fn catchup_rhs_with_lhs<R: tree::Visit>( · 8 unmodified lines
184 rhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,184 rhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
185 lhs: immutable::tree::Entry<'_>,185 lhs: immutable::tree::Entry<'_>,
186 rhs: immutable::tree::Entry<'_>,186 rhs: immutable::tree::Entry<'_>,
187 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,187 queue: &mut VecDeque<TreeInfoPair>,
188 delegate: &mut R,188 delegate: &mut R,
189) -> Result<(), Error> {189) -> Result<(), Error> {
190 use std::cmp::Ordering::*;190 use std::cmp::Ordering::*;
@@ -224,7 +224,7 @@ fn catchup_lhs_with_rhs<R: tree::Visit>( · 33 unmodified lines
224 lhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,224 lhs_entries: &mut IteratorType<immutable::TreeIter<'_>>,
225 lhs: immutable::tree::Entry<'_>,225 lhs: immutable::tree::Entry<'_>,
226 rhs: immutable::tree::Entry<'_>,226 rhs: immutable::tree::Entry<'_>,
227 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,227 queue: &mut VecDeque<TreeInfoPair>,
228 delegate: &mut R,228 delegate: &mut R,
229) -> Result<(), Error> {229) -> Result<(), Error> {
230 use std::cmp::Ordering::*;230 use std::cmp::Ordering::*;
@@ -263,13 +263,13 @@ fn catchup_lhs_with_rhs<R: tree::Visit>( · 32 unmodified lines
263fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(263fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>(
264 lhs: immutable::tree::Entry<'_>,264 lhs: immutable::tree::Entry<'_>,
265 rhs: immutable::tree::Entry<'_>,265 rhs: immutable::tree::Entry<'_>,
266 queue: &mut VecDeque<TreeInfoPair<R::PathId>>,266 queue: &mut VecDeque<TreeInfoPair>,
267 delegate: &mut R,267 delegate: &mut R,
268) -> Result<(), Error> {268) -> Result<(), Error> {
269 use git_object::tree::EntryMode::*;269 use git_object::tree::EntryMode::*;
270 match (lhs.mode, rhs.mode) {270 match (lhs.mode, rhs.mode) {
271 (Tree, Tree) => {271 (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);
273 if lhs.oid != rhs.oid273 if lhs.oid != rhs.oid
274 && delegate274 && delegate
275 .visit(Change::Modification {275 .visit(Change::Modification {
@@ -294,7 +294,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 18 unmodified lines
294 ));294 ));
295 }295 }
296 (lhs_mode, Tree) if lhs_mode.is_no_tree() => {296 (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);
298 if delegate298 if delegate
299 .visit(Change::Deletion {299 .visit(Change::Deletion {
300 entry_mode: lhs.mode,300 entry_mode: lhs.mode,
@@ -322,7 +322,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: tree::Visit>( · 21 unmodified lines
322 ));322 ));
323 }323 }
324 (Tree, rhs_mode) if rhs_mode.is_no_tree() => {324 (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);
326 if delegate326 if delegate
327 .visit(Change::Deletion {327 .visit(Change::Deletion {
328 entry_mode: lhs.mode,328 entry_mode: lhs.mode,
git-diff/src/tree/mod.rs+8−7
@@ -3,21 +3,22 @@ use git_object::immutable; · 2 unmodified lines
3use std::collections::VecDeque;3use std::collections::VecDeque;
4 4
5/// The state required to visit [Changes] to be instantiated with `State::default()`.5/// The state required to visit [Changes] to be instantiated with `State::default()`.
6#[derive(Default)]6#[derive(Default, Clone)]
7pub struct State<PathId> {7pub struct State {
8 buf1: Vec<u8>,8 buf1: Vec<u8>,
9 buf2: Vec<u8>,9 buf2: Vec<u8>,
10 trees: VecDeque<TreeInfoPair<PathId>>,10 trees: VecDeque<TreeInfoPair>,
11}11}
12 12
13type TreeInfoPair<PathId> = (Option<TreeInfo<PathId>>, Option<TreeInfo<PathId>>);13type TreeInfoPair = (Option<TreeInfo>, Option<TreeInfo>);
14 14
15pub(crate) struct TreeInfo<PathId> {15#[derive(Clone)]
16pub(crate) struct TreeInfo {
16 pub tree_id: ObjectId,17 pub tree_id: ObjectId,
17 pub parent_path_id: Option<PathId>,18 pub parent_path_id: Option<()>,
18}19}
19 20
20impl<P> State<P> {21impl State {
21 fn clear(&mut self) {22 fn clear(&mut self) {
22 self.trees.clear();23 self.trees.clear();
23 self.buf1.clear();24 self.buf1.clear();
git-diff/src/tree/recorder.rs+6−10
@@ -4,7 +4,7 @@ use git_object::{ · 3 unmodified lines
4 bstr::{BStr, BString, ByteSlice, ByteVec},4 bstr::{BStr, BString, ByteSlice, ByteVec},
5 tree,5 tree,
6};6};
7use std::collections::BTreeMap;7use std::collections::{BTreeMap, VecDeque};
8 8
9/// A Change as observed by a call to [`visit(…)`][visit::Visit::visit()], enhanced with the path affected by the change.9/// A Change as observed by a call to [`visit(…)`][visit::Visit::visit()], enhanced with the path affected by the change.
10/// Its similar to [visit::Change] but includes the path that changed.10/// Its similar to [visit::Change] but includes the path that changed.
@@ -37,6 +37,7 @@ pub enum Change { · 26 unmodified lines
37pub struct Recorder {37pub struct Recorder {
38 path_count: usize,38 path_count: usize,
39 path_map: BTreeMap<usize, BString>,39 path_map: BTreeMap<usize, BString>,
40 path_deque: VecDeque<BString>,
40 path: BString,41 path: BString,
41 /// The observed changes.42 /// The observed changes.
42 pub records: Vec<Change>,43 pub records: Vec<Change>,
@@ -64,18 +65,13 @@ impl Recorder { · 21 unmodified lines
64}65}
65 66
66impl visit::Visit for Recorder {67impl visit::Visit for Recorder {
67 type PathId = usize;68 fn pop_front_tracked_path_and_set_current(&mut self) {
68 69 self.path = self.path_deque.pop_front().expect("every parent is set only once");
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");
71 }70 }
72 71
73 fn push_tracked_path_component(&mut self, component: &BStr) -> Self::PathId {72 fn push_back_tracked_path_component(&mut self, component: &BStr) {
74 self.push_element(component);73 self.push_element(component);
75 self.path_map.insert(self.path_count, self.path_clone());74 self.path_deque.push_back(self.path.clone());
76 let res = self.path_count;
77 self.path_count += 1;
78 res
79 }75 }
80 76
81 fn push_path_component(&mut self, component: &BStr) {77 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
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 Visit {53pub trait Visit {
54 /// A type capable of uniquely identifying paths in a tree.54 /// Sets the full path path in front of the queue so future calls to push and pop components affect it instead.
55 type PathId;55 fn pop_front_tracked_path_and_set_current(&mut self);
56 56 /// Append a `component` to the end of a path, which may be empty.
57 /// Sets the path associated with the given `id` so future calls to push and pop components affect it instead.57 fn push_back_tracked_path_component(&mut self, component: &BStr);
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;
62 /// Append a `component` to the end of a path, which may be empty.58 /// Append a `component` to the end of a path, which may be empty.
63 fn push_path_component(&mut self, component: &BStr);59 fn push_path_component(&mut self, component: &BStr);
64 /// Removes the last component from the path, which may leave it empty.60 /// 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