]> git.proxmox.com Git - rustc.git/blob - vendor/gix/src/config/cache/util.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / gix / src / config / cache / util.rs
1 #![allow(clippy::result_large_err)]
2 use super::Error;
3 use crate::{
4 config,
5 config::tree::{gitoxide, Core},
6 revision::spec::parse::ObjectKindHint,
7 };
8
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 {
14 git_install_dir,
15 home_dir,
16 home_for_user: Some(gix_config::path::interpolate::home_for_user), // TODO: figure out how to configure this
17 }
18 }
19
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)),
23 ..Default::default()
24 }
25 }
26
27 pub(crate) fn config_bool(
28 config: &gix_config::File<'_>,
29 key: &'static config::tree::keys::Boolean,
30 key_str: &str,
31 default: bool,
32 lenient: bool,
33 ) -> Result<bool, Error> {
34 use config::tree::Key;
35 debug_assert_eq!(
36 key_str,
37 key.logical_name(),
38 "BUG: key name and hardcoded name must match"
39 );
40 config
41 .boolean_by_key(key_str)
42 .map(|res| key.enrich_error(res))
43 .unwrap_or(Ok(default))
44 .map_err(Error::from)
45 .with_lenient_default(lenient)
46 }
47
48 pub(crate) fn query_refupdates(
49 config: &gix_config::File<'static>,
50 lenient_config: bool,
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)
56 .map_err(Into::into)
57 }
58
59 pub(crate) fn reflog_or_default(
60 config_reflog: Option<gix_ref::store::WriteReflog>,
61 has_worktree: bool,
62 ) -> gix_ref::store::WriteReflog {
63 config_reflog.unwrap_or(if has_worktree {
64 gix_ref::store::WriteReflog::Normal
65 } else {
66 gix_ref::store::WriteReflog::Disable
67 })
68 }
69
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>,
73 lenient: bool,
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))
79 .transpose()
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))
84 .transpose()
85 .with_leniency(lenient)?
86 .unwrap_or_default();
87 Ok((pack_cache_bytes, object_cache_bytes))
88 }
89
90 pub(crate) fn parse_core_abbrev(
91 config: &gix_config::File<'static>,
92 object_hash: gix_hash::Kind,
93 ) -> Result<Option<usize>, Error> {
94 Ok(config
95 .string_by_key("core.abbrev")
96 .map(|abbrev| Core::ABBREV.try_into_abbreviation(abbrev, object_hash))
97 .transpose()?
98 .flatten())
99 }
100
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") {
106 None => Ok(None),
107 Some(value) => Core::DISAMBIGUATE
108 .try_into_object_kind_hint(value)
109 .with_leniency(lenient_config),
110 }
111 }
112
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;
116 }
117
118 pub trait ApplyLeniencyDefault {
119 fn with_lenient_default(self, is_lenient: bool) -> Self;
120 }
121
122 impl<T, E> ApplyLeniency for Result<Option<T>, E> {
123 fn with_leniency(self, is_lenient: bool) -> Self {
124 match self {
125 Ok(v) => Ok(v),
126 Err(_) if is_lenient => Ok(None),
127 Err(err) => Err(err),
128 }
129 }
130 }
131
132 impl<T, E> ApplyLeniencyDefault for Result<T, E>
133 where
134 T: Default,
135 {
136 fn with_lenient_default(self, is_lenient: bool) -> Self {
137 match self {
138 Ok(v) => Ok(v),
139 Err(_) if is_lenient => Ok(T::default()),
140 Err(err) => Err(err),
141 }
142 }
143 }