Skip to content

Commit 11018e1

Browse files
committed
refactor
1 parent ae6944e commit 11018e1

File tree

6 files changed

+180
-189
lines changed

6 files changed

+180
-189
lines changed

git-diff/src/lib.rs

+1-188
Original file line numberDiff line numberDiff line change
@@ -2,191 +2,4 @@
22
#![forbid(unsafe_code, rust_2018_idioms)]
33

44
///
5-
pub mod visit {
6-
use git_object::immutable;
7-
8-
#[derive(Default, Clone)]
9-
pub struct State {
10-
buf1: Vec<u8>,
11-
buf2: Vec<u8>,
12-
}
13-
14-
pub struct Changes<'a>(Option<&'a immutable::Tree<'a>>);
15-
16-
impl<'a, T> From<T> for Changes<'a>
17-
where
18-
T: Into<Option<&'a immutable::Tree<'a>>>,
19-
{
20-
fn from(v: T) -> Self {
21-
Changes(v.into())
22-
}
23-
}
24-
25-
mod changes {
26-
use crate::visit;
27-
use git_hash::{oid, ObjectId};
28-
use git_object::immutable;
29-
use quick_error::quick_error;
30-
31-
const EMPTY_TREE: immutable::Tree<'static> = immutable::Tree::empty();
32-
33-
quick_error! {
34-
#[derive(Debug)]
35-
pub enum Error {
36-
NotFound(oid: ObjectId) {
37-
display("The object {} referenced by the tree was not found in the database", oid)
38-
}
39-
Cancelled {
40-
display("The delegate cancelled the operation")
41-
}
42-
}
43-
}
44-
45-
impl<'a> visit::Changes<'a> {
46-
/// Returns the changes that need to be applied to `self` to get `other`.
47-
pub fn to_obtain_tree<LocateFn>(
48-
&self,
49-
_other: &git_object::immutable::Tree<'_>,
50-
_state: &mut visit::State,
51-
_locate: LocateFn,
52-
_delegate: &mut impl visit::Record,
53-
) -> Result<(), Error>
54-
where
55-
LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::Object<'b>>,
56-
{
57-
let _this = *self.0.as_ref().unwrap_or(&&EMPTY_TREE);
58-
todo!("changes tree to tree")
59-
}
60-
}
61-
}
62-
63-
pub mod record {
64-
use git_hash::ObjectId;
65-
use git_object::{bstr::BStr, tree};
66-
67-
pub enum Change {
68-
Addition { mode: tree::Mode, oid: ObjectId },
69-
Copy,
70-
Deletion,
71-
Modification,
72-
Renaming,
73-
Type,
74-
}
75-
76-
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
77-
pub enum PathComponentUpdateMode {
78-
Replace,
79-
Push,
80-
}
81-
82-
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
83-
pub struct PathComponent<'a> {
84-
pub name: &'a BStr,
85-
/// An ID referring uniquely to the path built thus far. Used to keep track of source paths
86-
/// in case of [renames][Change::Rename] and [copies][Change::Copy].
87-
pub id: usize,
88-
}
89-
90-
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
91-
pub enum Action {
92-
Continue,
93-
Cancel,
94-
}
95-
96-
pub trait Record {
97-
fn update_path_component(&mut self, component: PathComponent<'_>, mode: PathComponentUpdateMode);
98-
fn pop_path_component(&mut self);
99-
fn record(&mut self, change: Change) -> Action;
100-
}
101-
102-
#[cfg(test)]
103-
mod tests {
104-
use super::*;
105-
106-
#[test]
107-
fn size_of_change() {
108-
assert_eq!(
109-
std::mem::size_of::<Change>(),
110-
22,
111-
"this type shouldn't grow without us knowing"
112-
)
113-
}
114-
}
115-
}
116-
pub use record::Record;
117-
118-
pub mod recorder {
119-
use crate::visit::record;
120-
use git_hash::ObjectId;
121-
use git_object::{
122-
bstr::{BStr, BString, ByteSlice, ByteVec},
123-
tree,
124-
};
125-
use std::{ops::Deref, path::PathBuf};
126-
127-
#[derive(Clone, Debug, PartialEq, Eq)]
128-
pub enum Change {
129-
Addition {
130-
mode: tree::Mode,
131-
oid: ObjectId,
132-
path: PathBuf,
133-
},
134-
}
135-
136-
pub type Changes = Vec<Change>;
137-
138-
#[derive(Clone, Debug, Default)]
139-
pub struct Recorder {
140-
current_component: BString,
141-
pub records: Vec<Change>,
142-
}
143-
144-
impl Recorder {
145-
fn pop_element(&mut self) {
146-
if let Some(pos) = self.current_component.rfind_byte(b'/') {
147-
self.current_component.resize(pos, 0);
148-
}
149-
}
150-
151-
fn push_element(&mut self, name: &BStr) {
152-
self.current_component.push(b'/');
153-
self.current_component.push_str(name);
154-
}
155-
}
156-
157-
impl record::Record for Recorder {
158-
fn update_path_component(
159-
&mut self,
160-
component: record::PathComponent<'_>,
161-
mode: record::PathComponentUpdateMode,
162-
) {
163-
use record::PathComponentUpdateMode::*;
164-
match mode {
165-
Push => self.push_element(component.name),
166-
Replace => {
167-
self.pop_element();
168-
self.push_element(component.name);
169-
}
170-
}
171-
}
172-
173-
fn pop_path_component(&mut self) {
174-
self.pop_element();
175-
}
176-
177-
fn record(&mut self, change: record::Change) -> record::Action {
178-
use record::Change::*;
179-
self.records.push(match change {
180-
Addition { mode, oid } => Change::Addition {
181-
mode,
182-
oid,
183-
path: self.current_component.deref().clone().into_path_buf_lossy(),
184-
},
185-
_ => todo!("record other kinds of changes"),
186-
});
187-
record::Action::Continue
188-
}
189-
}
190-
}
191-
pub use recorder::Recorder;
192-
}
5+
pub mod visit;

git-diff/src/visit/changes.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::visit;
2+
use git_hash::{oid, ObjectId};
3+
use git_object::immutable;
4+
use quick_error::quick_error;
5+
6+
const EMPTY_TREE: immutable::Tree<'static> = immutable::Tree::empty();
7+
8+
quick_error! {
9+
#[derive(Debug)]
10+
pub enum Error {
11+
NotFound(oid: ObjectId) {
12+
display("The object {} referenced by the tree was not found in the database", oid)
13+
}
14+
Cancelled {
15+
display("The delegate cancelled the operation")
16+
}
17+
}
18+
}
19+
20+
impl<'a> visit::Changes<'a> {
21+
/// Returns the changes that need to be applied to `self` to get `other`.
22+
pub fn to_obtain_tree<LocateFn>(
23+
&self,
24+
_other: &git_object::immutable::Tree<'_>,
25+
_state: &mut visit::State,
26+
_locate: LocateFn,
27+
_delegate: &mut impl visit::Record,
28+
) -> Result<(), Error>
29+
where
30+
LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::Object<'b>>,
31+
{
32+
let _this = *self.0.as_ref().unwrap_or(&&EMPTY_TREE);
33+
todo!("changes tree to tree")
34+
}
35+
}

git-diff/src/visit/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use git_object::immutable;
2+
3+
#[derive(Default, Clone)]
4+
pub struct State {
5+
buf1: Vec<u8>,
6+
buf2: Vec<u8>,
7+
}
8+
9+
pub struct Changes<'a>(Option<&'a immutable::Tree<'a>>);
10+
11+
impl<'a, T> From<T> for Changes<'a>
12+
where
13+
T: Into<Option<&'a immutable::Tree<'a>>>,
14+
{
15+
fn from(v: T) -> Self {
16+
Changes(v.into())
17+
}
18+
}
19+
20+
mod changes;
21+
22+
pub mod record;
23+
pub use record::Record;
24+
25+
pub mod recorder;
26+
pub use recorder::Recorder;

git-diff/src/visit/record.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use git_hash::ObjectId;
2+
use git_object::{bstr::BStr, tree};
3+
4+
pub enum Change {
5+
Addition { mode: tree::Mode, oid: ObjectId },
6+
Copy,
7+
Deletion,
8+
Modification,
9+
Renaming,
10+
Type,
11+
}
12+
13+
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
14+
pub enum PathComponentUpdateMode {
15+
Replace,
16+
Push,
17+
}
18+
19+
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
20+
pub struct PathComponent<'a> {
21+
pub name: &'a BStr,
22+
/// An ID referring uniquely to the path built thus far. Used to keep track of source paths
23+
/// in case of [renames][Change::Rename] and [copies][Change::Copy].
24+
pub id: usize,
25+
}
26+
27+
#[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
28+
pub enum Action {
29+
Continue,
30+
Cancel,
31+
}
32+
33+
pub trait Record {
34+
fn update_path_component(&mut self, component: PathComponent<'_>, mode: PathComponentUpdateMode);
35+
fn pop_path_component(&mut self);
36+
fn record(&mut self, change: Change) -> Action;
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
43+
#[test]
44+
fn size_of_change() {
45+
assert_eq!(
46+
std::mem::size_of::<Change>(),
47+
22,
48+
"this type shouldn't grow without us knowing"
49+
)
50+
}
51+
}

git-diff/src/visit/recorder.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use crate::visit::record;
2+
use git_hash::ObjectId;
3+
use git_object::{
4+
bstr::{BStr, BString, ByteSlice, ByteVec},
5+
tree,
6+
};
7+
use std::{ops::Deref, path::PathBuf};
8+
9+
#[derive(Clone, Debug, PartialEq, Eq)]
10+
pub enum Change {
11+
Addition {
12+
mode: tree::Mode,
13+
oid: ObjectId,
14+
path: PathBuf,
15+
},
16+
}
17+
18+
pub type Changes = Vec<Change>;
19+
20+
#[derive(Clone, Debug, Default)]
21+
pub struct Recorder {
22+
current_component: BString,
23+
pub records: Vec<Change>,
24+
}
25+
26+
impl Recorder {
27+
fn pop_element(&mut self) {
28+
if let Some(pos) = self.current_component.rfind_byte(b'/') {
29+
self.current_component.resize(pos, 0);
30+
}
31+
}
32+
33+
fn push_element(&mut self, name: &BStr) {
34+
self.current_component.push(b'/');
35+
self.current_component.push_str(name);
36+
}
37+
}
38+
39+
impl record::Record for Recorder {
40+
fn update_path_component(&mut self, component: record::PathComponent<'_>, mode: record::PathComponentUpdateMode) {
41+
use record::PathComponentUpdateMode::*;
42+
match mode {
43+
Push => self.push_element(component.name),
44+
Replace => {
45+
self.pop_element();
46+
self.push_element(component.name);
47+
}
48+
}
49+
}
50+
51+
fn pop_path_component(&mut self) {
52+
self.pop_element();
53+
}
54+
55+
fn record(&mut self, change: record::Change) -> record::Action {
56+
use record::Change::*;
57+
self.records.push(match change {
58+
Addition { mode, oid } => Change::Addition {
59+
mode,
60+
oid,
61+
path: self.current_component.deref().clone().into_path_buf_lossy(),
62+
},
63+
_ => todo!("record other kinds of changes"),
64+
});
65+
record::Action::Continue
66+
}
67+
}

git-diff/tests/visit/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ mod changes {
6767
path: "f".into()
6868
}]
6969
);
70-
todo!("detect an added file in the root tree")
7170
}
7271
}
7372
}

0 commit comments

Comments
 (0)