Skip to content

Commit 585f8c9

Browse files
committed
refactor
- add `diff.ignoreSubmodules` configuration key with a strong type
1 parent 6693ab9 commit 585f8c9

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

gix/src/config/tree/sections/diff.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ impl Diff {
1515
.with_note(
1616
"The limit is actually squared, so 1000 stands for up to 1 million diffs if fuzzy rename tracking is enabled",
1717
);
18+
19+
/// The `diff.ignoreSubmodules` key.
20+
pub const IGNORE_SUBMODULES: keys::String = keys::String::new_string("ignoreSubmodules", &config::Tree::DIFF)
21+
.with_note("This setting affects only the submodule status, and thus the repository status in general.");
22+
1823
/// The `diff.renames` key.
1924
pub const RENAMES: Renames = Renames::new_renames("renames", &config::Tree::DIFF);
2025

@@ -45,6 +50,7 @@ impl Section for Diff {
4550
fn keys(&self) -> &[&dyn Key] {
4651
&[
4752
&Self::ALGORITHM,
53+
&Self::IGNORE_SUBMODULES,
4854
&Self::RENAME_LIMIT,
4955
&Self::RENAMES,
5056
&Self::DRIVER_COMMAND,
@@ -59,6 +65,9 @@ impl Section for Diff {
5965
/// The `diff.algorithm` key.
6066
pub type Algorithm = keys::Any<validate::Algorithm>;
6167

68+
/// The `diff.ignoreSubmodules` key.
69+
pub type Ignore = keys::Any<validate::Ignore>;
70+
6271
/// The `diff.renames` key.
6372
pub type Renames = keys::Any<validate::Renames>;
6473

@@ -71,12 +80,27 @@ mod algorithm {
7180
use crate::{
7281
bstr::BStr,
7382
config,
74-
config::{diff::algorithm::Error, tree::sections::diff::Algorithm},
83+
config::{
84+
diff::algorithm,
85+
key,
86+
tree::sections::diff::{Algorithm, Ignore},
87+
},
7588
};
7689

90+
impl Ignore {
91+
/// See if `value` is an actual ignore
92+
pub fn try_into_ignore(
93+
&self,
94+
value: Cow<'_, BStr>,
95+
) -> Result<gix_submodule::config::Ignore, key::GenericErrorWithValue> {
96+
gix_submodule::config::Ignore::try_from(value.as_ref())
97+
.or_else(|()| key::GenericErrorWithValue::from_value(self, value.into_owned()))
98+
}
99+
}
100+
77101
impl Algorithm {
78102
/// Derive the diff algorithm identified by `name`, case-insensitively.
79-
pub fn try_into_algorithm(&self, name: Cow<'_, BStr>) -> Result<gix_diff::blob::Algorithm, Error> {
103+
pub fn try_into_algorithm(&self, name: Cow<'_, BStr>) -> Result<gix_diff::blob::Algorithm, algorithm::Error> {
80104
let algo = if name.eq_ignore_ascii_case(b"myers") || name.eq_ignore_ascii_case(b"default") {
81105
gix_diff::blob::Algorithm::Myers
82106
} else if name.eq_ignore_ascii_case(b"minimal") {
@@ -88,7 +112,7 @@ mod algorithm {
88112
name: name.into_owned(),
89113
});
90114
} else {
91-
return Err(Error::Unknown {
115+
return Err(algorithm::Error::Unknown {
92116
name: name.into_owned(),
93117
});
94118
};
@@ -171,6 +195,14 @@ pub(super) mod validate {
171195
config::tree::{keys, Diff},
172196
};
173197

198+
pub struct Ignore;
199+
impl keys::Validate for Ignore {
200+
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
201+
Diff::ALGORITHM.try_into_ignore(value.into())?;
202+
Ok(())
203+
}
204+
}
205+
174206
pub struct Algorithm;
175207
impl keys::Validate for Algorithm {
176208
fn validate(&self, value: &BStr) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {

gix/src/status/index_worktree.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ mod submodule_status {
209209
use crate::{
210210
bstr,
211211
bstr::{BStr, BString},
212+
config,
212213
status::{index_worktree::BuiltinSubmoduleStatus, Submodule},
213214
};
214215

@@ -279,7 +280,7 @@ mod submodule_status {
279280
let (ignore, check_dirty) = match self.mode {
280281
Submodule::AsConfigured { check_dirty } => {
281282
// diff.ignoreSubmodules is the global setting, and if it exists, it overrides the submodule's own ignore setting.
282-
let global_ignore = repo.config_snapshot().string("diff.ignoreSubmodules");
283+
let global_ignore = repo.config_snapshot().string(&config::tree::Diff::IGNORE_SUBMODULES);
283284
if let Some(ignore_str) = global_ignore {
284285
let ignore = ignore_str
285286
.as_ref()

0 commit comments

Comments
 (0)