Skip to content

Commit b756685

Browse files
Dragyxfaukah
authored andcommitted
Add force-correctness flag.
This flag is added in preparation for a later inclusion of machine-readable output. It allows the user to specify that the output should prioritize correctness at all costs. This rules out the use of immutable database connections or lazy queries. The former have no correctness guarantees and lazy queries could (theoretically) raise an error for a later row e.g. because the path returned from the store is not a valid `StorePath`.
1 parent 4f3bcd2 commit b756685

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/diff.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ use crate::{
5353
},
5454
};
5555

56+
fn create_backend<'a>(
57+
force_correctness: bool,
58+
) -> store::CombinedStoreBackend<'a> {
59+
if force_correctness {
60+
store::CombinedStoreBackend::default_eager()
61+
} else {
62+
store::CombinedStoreBackend::default_lazy()
63+
}
64+
}
65+
5666
#[derive(Debug, Eq, PartialEq)]
5767
pub struct Diff<T = Vec<Version>> {
5868
pub name: String,
@@ -190,6 +200,7 @@ pub fn write_package_diff(
190200
writer: &mut impl fmt::Write,
191201
path_old: &Path,
192202
path_new: &Path,
203+
force_correctness: bool,
193204
) -> Result<usize> {
194205
let mut connection = create_backend(force_correctness);
195206
connection.connect()?;
@@ -252,7 +263,8 @@ pub fn write_paths_diffln(
252263
path_old: &Path,
253264
path_new: &Path,
254265
) -> Result<usize> {
255-
write_package_diff(writer, path_old, path_new)
266+
// Setting `force_correctness` to false mimics the old behaviour.
267+
write_package_diff(writer, path_old, path_new, false)
256268
}
257269

258270
/// Computes the Levenshtein distance between two slices using dynamic
@@ -939,6 +951,7 @@ fn fmt_version_piece_pair(
939951
pub fn spawn_size_diff(
940952
path_old: PathBuf,
941953
path_new: PathBuf,
954+
force_correctness: bool,
942955
) -> thread::JoinHandle<Result<(Size, Size)>> {
943956
log::debug!("calculating closure sizes in background");
944957

src/main.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ struct Cli {
4646
global = true,
4747
)]
4848
color: clap::ColorChoice,
49+
50+
/// Fall back to a backend that is focused solely on absolutely guaranteeing
51+
/// correct results at the cost of memory usage and query speed.
52+
///
53+
/// This is relevant if the output of dix is to be used for more
54+
/// critical applications and not just as human-readable overview.
55+
///
56+
/// In the vast, vast majority of cases, the default backend should be
57+
/// sufficient.
58+
#[arg(long, default_value_t = false, global = true)]
59+
force_correctness: bool,
4960
}
5061

5162
fn real_main() -> Result<()> {
@@ -54,6 +65,7 @@ fn real_main() -> Result<()> {
5465
new_path,
5566
verbose,
5667
color,
68+
force_correctness,
5769
} = Cli::parse();
5870

5971
yansi::whenever(match color {
@@ -76,6 +88,12 @@ fn real_main() -> Result<()> {
7688
writeln!(out, "{header} {message}", message = arguments.args())
7789
})
7890
.init();
91+
if force_correctness {
92+
log::warn!(
93+
"Falling back to slower but more robust backends (force_correctness is \
94+
set)."
95+
);
96+
}
7997

8098
let mut out = WriteFmt(io::stdout());
8199

@@ -96,9 +114,10 @@ fn real_main() -> Result<()> {
96114

97115
// Handle to the thread collecting closure size information.
98116
let closure_size_handle =
99-
dix::spawn_size_diff(old_path.clone(), new_path.clone());
117+
dix::spawn_size_diff(old_path.clone(), new_path.clone(), force_correctness);
100118

101-
let wrote = dix::write_package_diff(&mut out, &old_path, &new_path)?;
119+
let wrote =
120+
dix::write_package_diff(&mut out, &old_path, &new_path, force_correctness)?;
102121

103122
let (size_old, size_new) = closure_size_handle.join().map_err(|_| {
104123
anyhow!(

0 commit comments

Comments
 (0)