|
@@ -30,8 +30,8 @@ use std::cmp::Ordering;
|
|
|
const S_IFMT: u32 = 0o170000;
|
|
|
const S_IFDIR: u32 = 0o040000;
|
|
|
|
|
|
-fn add_hash(get: &PyAny, set: &PyAny, string: &[u8], py: Python) -> PyResult<()> {
|
|
|
- let str_obj = PyBytes::new(py, string);
|
|
|
+fn add_hash(get: &Bound<PyAny>, set: &Bound<PyAny>, string: &[u8], py: Python) -> PyResult<()> {
|
|
|
+ let str_obj = PyBytes::new_bound(py, string);
|
|
|
let hash_obj = str_obj.hash()?;
|
|
|
let value = get.call1((hash_obj,))?;
|
|
|
let n = string.len();
|
|
@@ -40,9 +40,9 @@ fn add_hash(get: &PyAny, set: &PyAny, string: &[u8], py: Python) -> PyResult<()>
|
|
|
}
|
|
|
|
|
|
#[pyfunction]
|
|
|
-fn _count_blocks(py: Python, obj: &PyAny) -> PyResult<PyObject> {
|
|
|
- let default_dict_cls = PyModule::import(py, "collections")?.getattr("defaultdict")?;
|
|
|
- let int_cls = PyModule::import(py, "builtins")?.getattr("int")?;
|
|
|
+fn _count_blocks(py: Python, obj: &Bound<PyAny>) -> PyResult<PyObject> {
|
|
|
+ let default_dict_cls = PyModule::import_bound(py, "collections")?.getattr("defaultdict")?;
|
|
|
+ let int_cls = PyModule::import_bound(py, "builtins")?.getattr("int")?;
|
|
|
|
|
|
let counts = default_dict_cls.call1((int_cls,))?;
|
|
|
let get = counts.getattr("__getitem__")?;
|
|
@@ -56,7 +56,7 @@ fn _count_blocks(py: Python, obj: &PyAny) -> PyResult<PyObject> {
|
|
|
}
|
|
|
|
|
|
let num_chunks = chunks.extract::<Vec<PyObject>>()?.len();
|
|
|
- let pym = py.import("dulwich.diff_tree")?;
|
|
|
+ let pym = py.import_bound("dulwich.diff_tree")?;
|
|
|
let block_size = pym.getattr("_BLOCK_SIZE")?.extract::<usize>()?;
|
|
|
let mut block: Vec<u8> = Vec::with_capacity(block_size);
|
|
|
|
|
@@ -70,20 +70,20 @@ fn _count_blocks(py: Python, obj: &PyAny) -> PyResult<PyObject> {
|
|
|
for c in chunk_str {
|
|
|
block.push(*c);
|
|
|
if *c == b'\n' || block.len() == block_size {
|
|
|
- add_hash(get, set, &block, py)?;
|
|
|
+ add_hash(&get, &set, &block, py)?;
|
|
|
block.clear();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if !block.is_empty() {
|
|
|
- add_hash(get, set, &block, py)?;
|
|
|
+ add_hash(&get, &set, &block, py)?;
|
|
|
}
|
|
|
|
|
|
Ok(counts.to_object(py))
|
|
|
}
|
|
|
|
|
|
#[pyfunction]
|
|
|
-fn _is_tree(_py: Python, entry: &PyAny) -> PyResult<bool> {
|
|
|
+fn _is_tree(_py: Python, entry: &Bound<PyAny>) -> PyResult<bool> {
|
|
|
let mode = entry.getattr("mode")?;
|
|
|
|
|
|
if mode.is_none() {
|
|
@@ -94,12 +94,12 @@ fn _is_tree(_py: Python, entry: &PyAny) -> PyResult<bool> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-fn tree_entries(path: &[u8], tree: &PyAny, py: Python) -> PyResult<Vec<PyObject>> {
|
|
|
+fn tree_entries(path: &[u8], tree: &Bound<PyAny>, py: Python) -> PyResult<Vec<PyObject>> {
|
|
|
if tree.is_none() {
|
|
|
return Ok(Vec::new());
|
|
|
}
|
|
|
|
|
|
- let dom = py.import("dulwich.objects")?;
|
|
|
+ let dom = py.import_bound("dulwich.objects")?;
|
|
|
let tree_entry_cls = dom.getattr("TreeEntry")?;
|
|
|
|
|
|
let items = tree
|
|
@@ -117,25 +117,27 @@ fn tree_entries(path: &[u8], tree: &PyAny, py: Python) -> PyResult<Vec<PyObject>
|
|
|
}
|
|
|
new_path.extend_from_slice(name.as_slice());
|
|
|
|
|
|
- let tree_entry = tree_entry_cls.call1((PyBytes::new(py, &new_path), mode, sha))?;
|
|
|
+ let tree_entry = tree_entry_cls.call1((PyBytes::new_bound(py, &new_path), mode, sha))?;
|
|
|
result.push(tree_entry.to_object(py));
|
|
|
}
|
|
|
|
|
|
Ok(result)
|
|
|
}
|
|
|
|
|
|
-fn entry_path_cmp(entry1: &PyAny, entry2: &PyAny) -> PyResult<Ordering> {
|
|
|
- let path1 = entry1.getattr("path")?.extract::<&[u8]>()?;
|
|
|
- let path2 = entry2.getattr("path")?.extract::<&[u8]>()?;
|
|
|
+fn entry_path_cmp(entry1: &Bound<PyAny>, entry2: &Bound<PyAny>) -> PyResult<Ordering> {
|
|
|
+ let path1_o = entry1.getattr("path")?;
|
|
|
+ let path1 = path1_o.extract::<&[u8]>()?;
|
|
|
+ let path2_o = entry2.getattr("path")?;
|
|
|
+ let path2 = path2_o.extract::<&[u8]>()?;
|
|
|
Ok(path1.cmp(path2))
|
|
|
}
|
|
|
|
|
|
#[pyfunction]
|
|
|
-fn _merge_entries(py: Python, path: &[u8], tree1: &PyAny, tree2: &PyAny) -> PyResult<PyObject> {
|
|
|
+fn _merge_entries(py: Python, path: &[u8], tree1: &Bound<PyAny>, tree2: &Bound<PyAny>) -> PyResult<PyObject> {
|
|
|
let entries1 = tree_entries(path, tree1, py)?;
|
|
|
let entries2 = tree_entries(path, tree2, py)?;
|
|
|
|
|
|
- let pym = py.import("dulwich.diff_tree")?;
|
|
|
+ let pym = py.import_bound("dulwich.diff_tree")?;
|
|
|
let null_entry = pym.getattr("_NULL_ENTRY")?.to_object(py);
|
|
|
|
|
|
let mut result = Vec::new();
|
|
@@ -143,13 +145,13 @@ fn _merge_entries(py: Python, path: &[u8], tree1: &PyAny, tree2: &PyAny) -> PyRe
|
|
|
let mut i1 = 0;
|
|
|
let mut i2 = 0;
|
|
|
while i1 < entries1.len() && i2 < entries2.len() {
|
|
|
- let cmp = entry_path_cmp(entries1[i1].as_ref(py), entries2[i2].as_ref(py))?;
|
|
|
+ let cmp = entry_path_cmp(entries1[i1].bind(py), entries2[i2].bind(py))?;
|
|
|
let (e1, e2) = match cmp {
|
|
|
- Ordering::Equal => (entries1[i1].clone(), entries2[i2].clone()),
|
|
|
- Ordering::Less => (entries1[i1].clone(), null_entry.clone()),
|
|
|
- Ordering::Greater => (null_entry.clone(), entries2[i2].clone()),
|
|
|
+ Ordering::Equal => (entries1[i1].clone_ref(py), entries2[i2].clone_ref(py)),
|
|
|
+ Ordering::Less => (entries1[i1].clone_ref(py), null_entry.clone_ref(py)),
|
|
|
+ Ordering::Greater => (null_entry.clone_ref(py), entries2[i2].clone_ref(py)),
|
|
|
};
|
|
|
- let pair = PyTuple::new(py, &[e1, e2]);
|
|
|
+ let pair = PyTuple::new_bound(py, &[e1, e2]);
|
|
|
result.push(pair);
|
|
|
match cmp {
|
|
|
Ordering::Equal => {
|
|
@@ -166,22 +168,22 @@ fn _merge_entries(py: Python, path: &[u8], tree1: &PyAny, tree2: &PyAny) -> PyRe
|
|
|
}
|
|
|
|
|
|
while i1 < entries1.len() {
|
|
|
- let pair = PyTuple::new(py, &[entries1[i1].clone(), null_entry.clone()]);
|
|
|
+ let pair = PyTuple::new_bound(py, &[entries1[i1].clone_ref(py), null_entry.clone_ref(py)]);
|
|
|
result.push(pair);
|
|
|
i1 += 1;
|
|
|
}
|
|
|
|
|
|
while i2 < entries2.len() {
|
|
|
- let pair = PyTuple::new(py, &[null_entry.clone(), entries2[i2].clone()]);
|
|
|
+ let pair = PyTuple::new_bound(py, &[null_entry.clone_ref(py), entries2[i2].clone_ref(py)]);
|
|
|
result.push(pair);
|
|
|
i2 += 1;
|
|
|
}
|
|
|
|
|
|
- Ok(PyList::new(py, &result).to_object(py))
|
|
|
+ Ok(PyList::new_bound(py, &result).to_object(py))
|
|
|
}
|
|
|
|
|
|
#[pymodule]
|
|
|
-fn _diff_tree(_py: Python, m: &PyModule) -> PyResult<()> {
|
|
|
+fn _diff_tree(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
|
|
|
m.add_function(wrap_pyfunction!(_count_blocks, m)?)?;
|
|
|
m.add_function(wrap_pyfunction!(_is_tree, m)?)?;
|
|
|
m.add_function(wrap_pyfunction!(_merge_entries, m)?)?;
|