Unified · Split
| @@ -270,36 +270,7 @@ pub mod mailmap { · 269 unmodified lines | |||
| 270 | } | 270 | } |
| 271 | 271 | ||
| 272 | /// | 272 | /// |
| 273 | pub mod worktree { | 273 | pub mod worktree; |
| 274 | use crate::Repository; | ||
| 275 | #[cfg(all(feature = "unstable", feature = "git-worktree"))] | ||
| 276 | pub use git_worktree::*; | ||
| 277 | |||
| 278 | /// | ||
| 279 | #[cfg(feature = "git-index")] | ||
| 280 | pub mod open_index { | ||
| 281 | use crate::bstr::BString; | ||
| 282 | |||
| 283 | /// The error returned by [`Worktree::open_index()`][crate::Worktree::open_index()]. | ||
| 284 | #[derive(Debug, thiserror::Error)] | ||
| 285 | #[allow(missing_docs)] | ||
| 286 | pub enum Error { | ||
| 287 | #[error("Could not interpret value '{}' as 'index.threads'", .value)] | ||
| 288 | ConfigIndexThreads { | ||
| 289 | value: BString, | ||
| 290 | #[source] | ||
| 291 | err: git_config::value::parse::Error, | ||
| 292 | }, | ||
| 293 | #[error(transparent)] | ||
| 294 | IndexFile(#[from] git_index::file::init::Error), | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | /// A structure to make the API more stuctured. | ||
| 299 | pub struct Platform<'repo> { | ||
| 300 | pub(crate) parent: &'repo Repository, | ||
| 301 | } | ||
| 302 | } | ||
| 303 | 274 | ||
| 304 | /// | 275 | /// |
| 305 | pub mod rev_parse { | 276 | pub mod rev_parse { |
| @@ -19,38 +19,3 @@ impl<'repo> worktree::Platform<'repo> { · 18 unmodified lines | |||
| 19 | }) | 19 | }) |
| 20 | } | 20 | } |
| 21 | } | 21 | } |
| 22 | |||
| 23 | impl<'repo> Worktree<'repo> { | ||
| 24 | /// Open a new copy of the index file and decode it entirely. | ||
| 25 | /// | ||
| 26 | /// It will use the `index.threads` configuration key to learn how many threads to use. | ||
| 27 | #[cfg(feature = "git-index")] | ||
| 28 | pub fn open_index(&self) -> Result<git_index::File, crate::worktree::open_index::Error> { | ||
| 29 | use std::convert::{TryFrom, TryInto}; | ||
| 30 | let repo = self.parent; | ||
| 31 | let thread_limit = repo | ||
| 32 | .config | ||
| 33 | .resolved | ||
| 34 | .boolean("index", None, "threads") | ||
| 35 | .map(|res| { | ||
| 36 | res.map(|value| if value { 0usize } else { 1 }).or_else(|err| { | ||
| 37 | git_config::values::Integer::try_from(err.input.as_ref()) | ||
| 38 | .map_err(|err| crate::worktree::open_index::Error::ConfigIndexThreads { | ||
| 39 | value: err.input.clone(), | ||
| 40 | err, | ||
| 41 | }) | ||
| 42 | .map(|value| value.to_decimal().and_then(|v| v.try_into().ok()).unwrap_or(1)) | ||
| 43 | }) | ||
| 44 | }) | ||
| 45 | .transpose()?; | ||
| 46 | git_index::File::at( | ||
| 47 | repo.git_dir().join("index"), | ||
| 48 | git_index::decode::Options { | ||
| 49 | object_hash: repo.object_hash(), | ||
| 50 | thread_limit, | ||
| 51 | min_extension_block_in_bytes_for_threading: 0, | ||
| 52 | }, | ||
| 53 | ) | ||
| 54 | .map_err(Into::into) | ||
| 55 | } | ||
| 56 | } | ||
| @@ -0,0 +1,64 @@ | |||
| 1 | use crate::Repository; | ||
| 2 | #[cfg(all(feature = "unstable", feature = "git-worktree"))] | ||
| 3 | pub use git_worktree::*; | ||
| 4 | |||
| 5 | /// | ||
| 6 | #[cfg(feature = "git-index")] | ||
| 7 | pub mod open_index { | ||
| 8 | use crate::bstr::BString; | ||
| 9 | |||
| 10 | /// The error returned by [`Worktree::open_index()`][crate::Worktree::open_index()]. | ||
| 11 | #[derive(Debug, thiserror::Error)] | ||
| 12 | #[allow(missing_docs)] | ||
| 13 | pub enum Error { | ||
| 14 | #[error("Could not interpret value '{}' as 'index.threads'", .value)] | ||
| 15 | ConfigIndexThreads { | ||
| 16 | value: BString, | ||
| 17 | #[source] | ||
| 18 | err: git_config::value::parse::Error, | ||
| 19 | }, | ||
| 20 | #[error(transparent)] | ||
| 21 | IndexFile(#[from] git_index::file::init::Error), | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | /// A structure to make the API more stuctured. | ||
| 26 | pub struct Platform<'repo> { | ||
| 27 | pub(crate) parent: &'repo Repository, | ||
| 28 | } | ||
| 29 | |||
| 30 | impl<'repo> crate::Worktree<'repo> { | ||
| 31 | /// Open a new copy of the index file and decode it entirely. | ||
| 32 | /// | ||
| 33 | /// It will use the `index.threads` configuration key to learn how many threads to use. | ||
| 34 | // TODO: test | ||
| 35 | #[cfg(feature = "git-index")] | ||
| 36 | pub fn open_index(&self) -> Result<git_index::File, crate::worktree::open_index::Error> { | ||
| 37 | use std::convert::{TryFrom, TryInto}; | ||
| 38 | let repo = self.parent; | ||
| 39 | let thread_limit = repo | ||
| 40 | .config | ||
| 41 | .resolved | ||
| 42 | .boolean("index", None, "threads") | ||
| 43 | .map(|res| { | ||
| 44 | res.map(|value| if value { 0usize } else { 1 }).or_else(|err| { | ||
| 45 | git_config::values::Integer::try_from(err.input.as_ref()) | ||
| 46 | .map_err(|err| crate::worktree::open_index::Error::ConfigIndexThreads { | ||
| 47 | value: err.input.clone(), | ||
| 48 | err, | ||
| 49 | }) | ||
| 50 | .map(|value| value.to_decimal().and_then(|v| v.try_into().ok()).unwrap_or(1)) | ||
| 51 | }) | ||
| 52 | }) | ||
| 53 | .transpose()?; | ||
| 54 | git_index::File::at( | ||
| 55 | repo.git_dir().join("index"), | ||
| 56 | git_index::decode::Options { | ||
| 57 | object_hash: repo.object_hash(), | ||
| 58 | thread_limit, | ||
| 59 | min_extension_block_in_bytes_for_threading: 0, | ||
| 60 | }, | ||
| 61 | ) | ||
| 62 | .map_err(Into::into) | ||
| 63 | } | ||
| 64 | } | ||