]>
git.proxmox.com Git - rustc.git/blob - vendor/gix/src/config/cache/util.rs
1 #![allow(clippy::result_large_err)]
5 config
::tree
::{gitoxide, Core}
,
6 revision
::spec
::parse
::ObjectKindHint
,
9 pub(crate) fn interpolate_context
<'a
>(
10 git_install_dir
: Option
<&'a std
::path
::Path
>,
11 home_dir
: Option
<&'a std
::path
::Path
>,
12 ) -> gix_config
::path
::interpolate
::Context
<'a
> {
13 gix_config
::path
::interpolate
::Context
{
16 home_for_user
: Some(gix_config
::path
::interpolate
::home_for_user
), // TODO: figure out how to configure this
20 pub(crate) fn base_options(lossy
: Option
<bool
>) -> gix_config
::file
::init
::Options
<'
static> {
21 gix_config
::file
::init
::Options
{
22 lossy
: lossy
.unwrap_or(!cfg
!(debug_assertions
)),
27 pub(crate) fn config_bool(
28 config
: &gix_config
::File
<'_
>,
29 key
: &'
static config
::tree
::keys
::Boolean
,
33 ) -> Result
<bool
, Error
> {
34 use config
::tree
::Key
;
38 "BUG: key name and hardcoded name must match"
41 .boolean_by_key(key_str
)
42 .map(|res
| key
.enrich_error(res
))
43 .unwrap_or(Ok(default))
45 .with_lenient_default(lenient
)
48 pub(crate) fn query_refupdates(
49 config
: &gix_config
::File
<'
static>,
51 ) -> Result
<Option
<gix_ref
::store
::WriteReflog
>, Error
> {
52 let key
= "core.logAllRefUpdates";
53 Core
::LOG_ALL_REF_UPDATES
54 .try_into_ref_updates(config
.boolean_by_key(key
), || config
.string_by_key(key
))
55 .with_leniency(lenient_config
)
59 pub(crate) fn reflog_or_default(
60 config_reflog
: Option
<gix_ref
::store
::WriteReflog
>,
62 ) -> gix_ref
::store
::WriteReflog
{
63 config_reflog
.unwrap_or(if has_worktree
{
64 gix_ref
::store
::WriteReflog
::Normal
66 gix_ref
::store
::WriteReflog
::Disable
70 /// Return `(pack_cache_bytes, object_cache_bytes)` as parsed from gix-config
71 pub(crate) fn parse_object_caches(
72 config
: &gix_config
::File
<'
static>,
74 mut filter_config_section
: fn(&gix_config
::file
::Metadata
) -> bool
,
75 ) -> Result
<(Option
<usize>, usize), Error
> {
76 let pack_cache_bytes
= config
77 .integer_filter_by_key("core.deltaBaseCacheLimit", &mut filter_config_section
)
78 .map(|res
| Core
::DELTA_BASE_CACHE_LIMIT
.try_into_usize(res
))
80 .with_leniency(lenient
)?
;
81 let object_cache_bytes
= config
82 .integer_filter_by_key("gitoxide.objects.cacheLimit", &mut filter_config_section
)
83 .map(|res
| gitoxide
::Objects
::CACHE_LIMIT
.try_into_usize(res
))
85 .with_leniency(lenient
)?
87 Ok((pack_cache_bytes
, object_cache_bytes
))
90 pub(crate) fn parse_core_abbrev(
91 config
: &gix_config
::File
<'
static>,
92 object_hash
: gix_hash
::Kind
,
93 ) -> Result
<Option
<usize>, Error
> {
95 .string_by_key("core.abbrev")
96 .map(|abbrev
| Core
::ABBREV
.try_into_abbreviation(abbrev
, object_hash
))
101 pub(crate) fn disambiguate_hint(
102 config
: &gix_config
::File
<'
static>,
103 lenient_config
: bool
,
104 ) -> Result
<Option
<ObjectKindHint
>, config
::key
::GenericErrorWithValue
> {
105 match config
.string_by_key("core.disambiguate") {
107 Some(value
) => Core
::DISAMBIGUATE
108 .try_into_object_kind_hint(value
)
109 .with_leniency(lenient_config
),
113 // TODO: Use a specialization here once trait specialization is stabilized. Would be perfect here for `T: Default`.
114 pub trait ApplyLeniency
{
115 fn with_leniency(self, is_lenient
: bool
) -> Self;
118 pub trait ApplyLeniencyDefault
{
119 fn with_lenient_default(self, is_lenient
: bool
) -> Self;
122 impl<T
, E
> ApplyLeniency
for Result
<Option
<T
>, E
> {
123 fn with_leniency(self, is_lenient
: bool
) -> Self {
126 Err(_
) if is_lenient
=> Ok(None
),
127 Err(err
) => Err(err
),
132 impl<T
, E
> ApplyLeniencyDefault
for Result
<T
, E
>
136 fn with_lenient_default(self, is_lenient
: bool
) -> Self {
139 Err(_
) if is_lenient
=> Ok(T
::default()),
140 Err(err
) => Err(err
),