Skip to content

Commit

Permalink
feat!: Repository::prefix() turns Option<Result into `Result<Opti…
Browse files Browse the repository at this point in the history
…on`.

This makes it easier for the caller as they won't have to call transpose anymore.
  • Loading branch information
Byron committed Aug 14, 2023
1 parent 6892999 commit b1e55d6
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions gix/src/repository/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,30 @@ impl crate::Repository {
/// Note that there may be `None` if there is no work tree, even though the `PathBuf` will be empty
/// if the CWD is at the root of the work tree.
// TODO: tests, details - there is a lot about environment variables to change things around.
pub fn prefix(&self) -> Option<std::io::Result<PathBuf>> {
self.work_tree.as_ref().map(|root| {
std::env::current_dir().and_then(|cwd| {
gix_path::realpath_opts(root, &cwd, MAX_SYMLINKS)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
.and_then(|root| {
cwd.strip_prefix(&root)
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"CWD '{}' isn't within the work tree '{}'",
cwd.display(),
root.display()
),
)
})
.map(ToOwned::to_owned)
})
pub fn prefix(&self) -> std::io::Result<Option<PathBuf>> {
self.work_tree
.as_ref()
.map(|root| {
std::env::current_dir().and_then(|cwd| {
gix_path::realpath_opts(root, &cwd, MAX_SYMLINKS)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
.and_then(|root| {
cwd.strip_prefix(&root)
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"CWD '{}' isn't within the work tree '{}'",
cwd.display(),
root.display()
),
)
})
.map(ToOwned::to_owned)
})
})
})
})
.transpose()
}

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

0 comments on commit b1e55d6

Please sign in to comment.