Skip to content

Commit 9ca57fa

Browse files
committed
[tree-diff] another green test
1 parent c6eb677 commit 9ca57fa

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

git-diff/src/visit/changes.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl<'a> visit::Changes<'a> {
5252
R: visit::Record,
5353
{
5454
state.clear();
55-
let mut lhs_entries = self.0.take().unwrap_or_default();
56-
let mut rhs_entries = other;
55+
let mut lhs_entries = peekable(self.0.take().unwrap_or_default());
56+
let mut rhs_entries = peekable(other);
5757
let mut avoid_popping_path: Option<()> = None;
5858

5959
loop {
@@ -65,16 +65,20 @@ impl<'a> visit::Changes<'a> {
6565
match state.trees.pop_front() {
6666
Some((None, Some(rhs))) => {
6767
delegate.set_current_path(rhs.parent_path_id.clone());
68-
rhs_entries = locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound(rhs.tree_id))?;
68+
rhs_entries =
69+
peekable(locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound(rhs.tree_id))?);
6970
}
7071
Some((Some(lhs), Some(rhs))) => {
7172
delegate.set_current_path(lhs.parent_path_id.clone());
72-
lhs_entries = locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound(lhs.tree_id))?;
73-
rhs_entries = locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound(rhs.tree_id))?;
73+
lhs_entries =
74+
peekable(locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound(lhs.tree_id))?);
75+
rhs_entries =
76+
peekable(locate(&rhs.tree_id, &mut state.buf2).ok_or(Error::NotFound(rhs.tree_id))?);
7477
}
7578
Some((Some(lhs), None)) => {
7679
delegate.set_current_path(lhs.parent_path_id.clone());
77-
lhs_entries = locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound(lhs.tree_id))?;
80+
lhs_entries =
81+
peekable(locate(&lhs.tree_id, &mut state.buf1).ok_or(Error::NotFound(lhs.tree_id))?);
7882
}
7983
Some((None, None)) => unreachable!("BUG: it makes no sense to fill the stack with empties"),
8084
None => return Ok(()),
@@ -89,9 +93,11 @@ impl<'a> visit::Changes<'a> {
8993
Less => {
9094
delete_entry_schedule_recursion(lhs, &mut state.trees, delegate)?;
9195
'inner_less: loop {
92-
match lhs_entries.next().transpose()? {
93-
Some(lhs) => match lhs.filename.cmp(rhs.filename) {
96+
match lhs_entries.peek() {
97+
Some(Ok(lhs)) => match lhs.filename.cmp(rhs.filename) {
9498
Equal => {
99+
let lhs =
100+
lhs_entries.next().transpose()?.expect("the peeked item tobe present");
95101
handle_lhs_and_rhs_with_equal_filenames(
96102
lhs,
97103
rhs,
@@ -101,15 +107,21 @@ impl<'a> visit::Changes<'a> {
101107
break 'inner_less;
102108
}
103109
Less => {
110+
let lhs =
111+
lhs_entries.next().transpose()?.expect("the peeked item tobe present");
104112
delegate.pop_path_component();
105113
delete_entry_schedule_recursion(lhs, &mut state.trees, delegate)?;
106114
}
107115
Greater => {
108-
todo!("LESS -> GREATER - we overshot")
116+
delegate.pop_path_component();
117+
add_entry_schedule_recursion(rhs, &mut state.trees, delegate)?;
118+
break 'inner_less;
109119
}
110120
},
121+
Some(Err(err)) => return Err(Error::EntriesDecode(err.to_owned())),
111122
None => {
112-
break 'inner_less;
123+
todo!("LESS: depleted lhs");
124+
// break 'inner_less;
113125
}
114126
}
115127
}
@@ -320,3 +332,7 @@ fn handle_lhs_and_rhs_with_equal_filenames<R: visit::Record>(
320332
};
321333
Ok(())
322334
}
335+
336+
fn peekable<I: Iterator>(iter: I) -> std::mem::ManuallyDrop<std::iter::Peekable<I>> {
337+
std::mem::ManuallyDrop::new(iter.peekable())
338+
}

0 commit comments

Comments
 (0)