Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bd8823

Browse files
committed
fix: opening repositories without 'strict' mode also ignores IO errors. (#790)
These will instead be logged, but won't make it impossible to open an otherwise fine repository.
1 parent e55f4ee commit 7bd8823

6 files changed

Lines changed: 42 additions & 10 deletions

File tree

gix/src/config/cache/incubate.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl StageOne {
3030
gix_config::Source::Local,
3131
git_dir_trust,
3232
lossy,
33+
lenient,
3334
)?;
3435

3536
// Note that we assume the repo is bare by default unless we are told otherwise. This is relevant if
@@ -64,6 +65,7 @@ impl StageOne {
6465
gix_config::Source::Worktree,
6566
git_dir_trust,
6667
lossy,
68+
lenient,
6769
)?;
6870
config.append(worktree_config);
6971
};
@@ -86,24 +88,47 @@ fn load_config(
8688
source: gix_config::Source,
8789
git_dir_trust: gix_sec::Trust,
8890
lossy: Option<bool>,
91+
lenient: bool,
8992
) -> Result<gix_config::File<'static>, Error> {
90-
buf.clear();
9193
let metadata = gix_config::file::Metadata::from(source)
9294
.at(&config_path)
9395
.with(git_dir_trust);
9496
let mut file = match std::fs::File::open(&config_path) {
9597
Ok(f) => f,
9698
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(gix_config::File::new(metadata)),
97-
Err(err) => return Err(err.into()),
99+
Err(err) => {
100+
let err = Error::Io {
101+
source: err,
102+
path: config_path,
103+
};
104+
if lenient {
105+
log::warn!("ignoring: {err:#?}");
106+
return Ok(gix_config::File::new(metadata));
107+
} else {
108+
return Err(err);
109+
}
110+
}
111+
};
112+
113+
buf.clear();
114+
if let Err(err) = std::io::copy(&mut file, buf) {
115+
let err = Error::Io {
116+
source: err,
117+
path: config_path,
118+
};
119+
if lenient {
120+
log::warn!("ignoring: {err:#?}");
121+
} else {
122+
return Err(err);
123+
}
98124
};
99-
std::io::copy(&mut file, buf)?;
100125

101126
let config = gix_config::File::from_bytes_owned(
102127
buf,
103128
metadata,
104129
gix_config::file::init::Options {
105130
includes: gix_config::file::includes::Options::no_follow(),
106-
..util::base_options(lossy)
131+
..util::base_options(lossy, lenient)
107132
},
108133
)?;
109134

gix/src/config/cache/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Cache {
6565
} else {
6666
gix_config::file::includes::Options::no_follow()
6767
},
68-
..util::base_options(lossy)
68+
..util::base_options(lossy, lenient_config)
6969
};
7070

7171
let config = {
@@ -118,7 +118,7 @@ impl Cache {
118118
)
119119
.map_err(|err| match err {
120120
gix_config::file::init::from_paths::Error::Init(err) => Error::from(err),
121-
gix_config::file::init::from_paths::Error::Io(err) => err.into(),
121+
gix_config::file::init::from_paths::Error::Io { source, path } => Error::Io { source, path },
122122
})?
123123
.unwrap_or_default();
124124

gix/src/config/cache/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ pub(crate) fn interpolate_context<'a>(
1717
}
1818
}
1919

20-
pub(crate) fn base_options(lossy: Option<bool>) -> gix_config::file::init::Options<'static> {
20+
pub(crate) fn base_options(lossy: Option<bool>, lenient: bool) -> gix_config::file::init::Options<'static> {
2121
gix_config::file::init::Options {
2222
lossy: lossy.unwrap_or(!cfg!(debug_assertions)),
23+
ignore_io_errors: lenient,
2324
..Default::default()
2425
}
2526
}

gix/src/config/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ pub enum Error {
6262
UnsupportedObjectFormat { name: BString },
6363
#[error(transparent)]
6464
CoreAbbrev(#[from] abbrev::Error),
65-
#[error("Could not read configuration file")]
66-
Io(#[from] std::io::Error),
65+
#[error("Could not read configuration file at \"{}\"", path.display())]
66+
Io {
67+
source: std::io::Error,
68+
path: std::path::PathBuf,
69+
},
6770
#[error(transparent)]
6871
Init(#[from] gix_config::file::init::Error),
6972
#[error(transparent)]

gix/src/open/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ impl Options {
134134
///
135135
/// This is recommended for all applications that prefer correctness over usability.
136136
/// `git` itself defaults to strict configuration mode, flagging incorrect configuration immediately.
137+
///
138+
/// Failure to read configuration files due to IO errors will also be a hard error if this mode is enabled, otherwise
139+
/// these errors will merely be logged.
137140
pub fn strict_config(mut self, toggle: bool) -> Self {
138141
self.lenient_config = !toggle;
139142
self

src/plumbing/options/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Args {
3838
#[clap(long, conflicts_with("verbose"))]
3939
pub progress: bool,
4040

41-
/// Don't default malformed configuration flags, but show an error instead.
41+
/// Don't default malformed configuration flags, but show an error instead. Ignore IO errors as well.
4242
///
4343
/// Note that some subcommands use strict mode by default.
4444
// TODO: needs a 'lenient' mutually exclusive counterpart. Opens the gate to auto-verbose some commands, and add --no-verbose

0 commit comments

Comments
 (0)