Skip to content

Commit 15a18e4

Browse files
committed
simplify looking up entries by path (#470)
1 parent 523418f commit 15a18e4

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

git-repository/src/object/tree/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ impl<'repo> Tree<'repo> {
6060
}
6161
Ok(None)
6262
}
63+
64+
/// Like [`lookup_path()`][Self::lookup_path()], but takes a `Path` directly via `relative_path`, a path relative to this tree.
65+
///
66+
/// # Note
67+
///
68+
/// If any path component contains illformed UTF-8 and thus can't be converted to bytes on platforms which can't do so natively,
69+
/// the returned component will be empty which makes the lookup fail.
70+
pub fn lookup_path_by_path(
71+
self,
72+
relative_path: impl AsRef<std::path::Path>,
73+
) -> Result<Option<git_object::tree::Entry>, find::existing::Error> {
74+
self.lookup_path(
75+
relative_path
76+
.as_ref()
77+
.components()
78+
.map(|c| git_path::os_str_into_bstr(c.as_os_str()).unwrap_or("".into()).as_ref()),
79+
)
80+
}
6381
}
6482

6583
///

git-repository/src/revision/spec/parse/delegate/navigate.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,13 @@ impl<'repo> delegate::Navigate for Delegate<'repo> {
124124
return Ok(tree_id);
125125
}
126126
let tree = repo.find_object(tree_id)?.into_tree();
127-
let entry = tree
128-
.lookup_path(git_path::from_bstr(path).components().map(|c| {
129-
git_path::os_str_into_bstr(c.as_os_str())
130-
.expect("no illformed UTF-8")
131-
.as_ref()
132-
}))?
133-
.ok_or_else(|| Error::PathNotFound {
134-
path: path.into(),
135-
object: obj.attach(repo).shorten_or_id(),
136-
tree: tree_id.attach(repo).shorten_or_id(),
137-
})?;
127+
let entry =
128+
tree.lookup_path_by_path(git_path::from_bstr(path))?
129+
.ok_or_else(|| Error::PathNotFound {
130+
path: path.into(),
131+
object: obj.attach(repo).shorten_or_id(),
132+
tree: tree_id.attach(repo).shorten_or_id(),
133+
})?;
138134
Ok(entry.oid)
139135
};
140136
for obj in objs.iter() {

0 commit comments

Comments
 (0)