refactor the rest of repository.rs path handling

This commit is contained in:
Josh Junon 2024-03-14 11:30:24 +01:00
parent 5d6491f482
commit c5f8571e53

View File

@ -1,8 +1,4 @@
use std::{ use std::{io::Write, path::Path, str};
io::Write,
path::{self, Path},
str,
};
use git2::Submodule; use git2::Submodule;
use git2_hooks::HookResult; use git2_hooks::HookResult;
@ -31,25 +27,22 @@ impl From<git2::Repository> for Repository {
impl Repository { impl Repository {
#[cfg(test)] #[cfg(test)]
pub fn init_bare<P: AsRef<path::Path>>(path: P) -> Result<Self> { pub fn init_bare<P: AsRef<Path>>(path: P) -> Result<Self> {
let inner = git2::Repository::init_bare(path)?; let inner = git2::Repository::init_bare(path)?;
Ok(Repository(inner)) Ok(Repository(inner))
} }
pub fn init<P: AsRef<path::Path>>(path: P) -> Result<Self> { pub fn init<P: AsRef<Path>>(path: P) -> Result<Self> {
let inner = git2::Repository::init(path)?; let inner = git2::Repository::init(path)?;
Ok(Repository(inner)) Ok(Repository(inner))
} }
pub fn open<P: AsRef<path::Path>>(path: P) -> Result<Self> { pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let inner = git2::Repository::open(path)?; let inner = git2::Repository::open(path)?;
Ok(Repository(inner)) Ok(Repository(inner))
} }
pub fn init_opts<P: AsRef<path::Path>>( pub fn init_opts<P: AsRef<Path>>(path: P, opts: &git2::RepositoryInitOptions) -> Result<Self> {
path: P,
opts: &git2::RepositoryInitOptions,
) -> Result<Self> {
let inner = git2::Repository::init_opts(path, opts)?; let inner = git2::Repository::init_opts(path, opts)?;
Ok(Repository(inner)) Ok(Repository(inner))
} }
@ -67,9 +60,9 @@ impl Repository {
Ok(()) Ok(())
} }
pub fn add_submodule(&self, url: &Url, path: &path::Path) -> Result<Submodule<'_>> { pub fn add_submodule<P: AsRef<Path>>(&self, url: &Url, path: P) -> Result<Submodule<'_>> {
self.0 self.0
.submodule(&url.to_string(), path, false) .submodule(&url.to_string(), path.as_ref(), false)
.map_err(Into::into) .map_err(Into::into)
} }
@ -207,7 +200,7 @@ impl Repository {
self.0.revwalk().map_err(Into::into) self.0.revwalk().map_err(Into::into)
} }
pub fn is_path_ignored<P: AsRef<path::Path>>(&self, path: P) -> Result<bool> { pub fn is_path_ignored<P: AsRef<Path>>(&self, path: P) -> Result<bool> {
self.0.is_path_ignored(path).map_err(Into::into) self.0.is_path_ignored(path).map_err(Into::into)
} }
@ -231,8 +224,11 @@ impl Repository {
self.0.index().map(Into::into).map_err(Into::into) self.0.index().map(Into::into).map_err(Into::into)
} }
pub fn blob_path(&self, path: &path::Path) -> Result<Oid> { pub fn blob_path<P: AsRef<Path>>(&self, path: P) -> Result<Oid> {
self.0.blob_path(path).map(Into::into).map_err(Into::into) self.0
.blob_path(path.as_ref())
.map(Into::into)
.map_err(Into::into)
} }
pub fn cherry_pick(&self, base: &Commit, target: &Commit) -> Result<Index> { pub fn cherry_pick(&self, base: &Commit, target: &Commit) -> Result<Index> {
@ -309,11 +305,11 @@ impl Repository {
TreeBuilder::new(self, tree) TreeBuilder::new(self, tree)
} }
pub fn path(&self) -> &path::Path { pub fn path(&self) -> &Path {
self.0.path() self.0.path()
} }
pub fn workdir(&self) -> Option<&path::Path> { pub fn workdir(&self) -> Option<&Path> {
self.0.workdir() self.0.workdir()
} }
@ -390,9 +386,9 @@ impl Repository {
} }
} }
pub fn checkout_index_path<'a>(&'a self, path: &'a path::Path) -> Result<()> { pub fn checkout_index_path<'a, P: AsRef<Path>>(&'a self, path: P) -> Result<()> {
let mut builder = git2::build::CheckoutBuilder::new(); let mut builder = git2::build::CheckoutBuilder::new();
builder.path(path); builder.path(path.as_ref());
builder.force(); builder.force();
let mut index = self.0.index()?; let mut index = self.0.index()?;