Skip to content

Commit b1e55d6

Browse files
committed
feat!: Repository::prefix() turns Option<Result into Result<Option.
This makes it easier for the caller as they won't have to call transpose anymore.
1 parent 6892999 commit b1e55d6

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

gix/src/repository/location.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,30 @@ impl crate::Repository {
4545
/// Note that there may be `None` if there is no work tree, even though the `PathBuf` will be empty
4646
/// if the CWD is at the root of the work tree.
4747
// TODO: tests, details - there is a lot about environment variables to change things around.
48-
pub fn prefix(&self) -> Option<std::io::Result<PathBuf>> {
49-
self.work_tree.as_ref().map(|root| {
50-
std::env::current_dir().and_then(|cwd| {
51-
gix_path::realpath_opts(root, &cwd, MAX_SYMLINKS)
52-
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
53-
.and_then(|root| {
54-
cwd.strip_prefix(&root)
55-
.map_err(|_| {
56-
std::io::Error::new(
57-
std::io::ErrorKind::Other,
58-
format!(
59-
"CWD '{}' isn't within the work tree '{}'",
60-
cwd.display(),
61-
root.display()
62-
),
63-
)
64-
})
65-
.map(ToOwned::to_owned)
66-
})
48+
pub fn prefix(&self) -> std::io::Result<Option<PathBuf>> {
49+
self.work_tree
50+
.as_ref()
51+
.map(|root| {
52+
std::env::current_dir().and_then(|cwd| {
53+
gix_path::realpath_opts(root, &cwd, MAX_SYMLINKS)
54+
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
55+
.and_then(|root| {
56+
cwd.strip_prefix(&root)
57+
.map_err(|_| {
58+
std::io::Error::new(
59+
std::io::ErrorKind::Other,
60+
format!(
61+
"CWD '{}' isn't within the work tree '{}'",
62+
cwd.display(),
63+
root.display()
64+
),
65+
)
66+
})
67+
.map(ToOwned::to_owned)
68+
})
69+
})
6770
})
68-
})
71+
.transpose()
6972
}
7073

7174
/// Return the kind of repository, either bare or one with a work tree.

0 commit comments

Comments
 (0)