Skip to content

Commit 12d2517

Browse files
committed
Implement --overrides flag for package and trust review
1 parent 6dd7c8d commit 12d2517

File tree

16 files changed

+620
-135
lines changed

16 files changed

+620
-135
lines changed

cargo-crev/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).
2020
- Deprecate `--no-dev-dependencies`. Make it the default. Introduce `--dev-dependencies` instead.
2121
- Fix binary releases by switching to Github Actions
2222
- Make `crate {goto,dir,open,expand}` assume `-u` outside of an existing Rust project.
23+
- Introduce trust and package review "overrides" which allow overriding (ignoring) specific
24+
trust / package review
2325

2426
## [0.22.2](https://github.com/dpc/crev/compare/cargo-crev-v0.21.4...v0.22.2) - 2022-01-11
2527

cargo-crev/src/edit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ pub fn edit_proof_content_iteractively<C: proof::ContentWithDraft>(
8989
content: &C,
9090
previous_date: Option<&proof::Date>,
9191
base_version: Option<&crev_data::Version>,
92-
extra_comment: Option<&str>,
92+
extra_leading_comment: Option<&str>,
93+
extra_follow_content_fn: impl FnOnce(&mut String) -> Result<()>,
9394
) -> Result<C> {
9495
let mut text = String::new();
9596
if let Some(date) = previous_date {
@@ -102,13 +103,14 @@ pub fn edit_proof_content_iteractively<C: proof::ContentWithDraft>(
102103
let draft = content.to_draft();
103104

104105
writeln!(&mut text, "# {}", draft.title())?;
105-
if let Some(extra_comment) = extra_comment {
106+
if let Some(extra_comment) = extra_leading_comment {
106107
writeln!(&mut text, "# {}", extra_comment)?;
107108
}
108109
if let Some(base_version) = base_version {
109110
writeln!(&mut text, "# Diff base version: {}", base_version)?;
110111
}
111112
text.write_str(draft.body())?;
113+
(extra_follow_content_fn)(&mut text)?;
112114
text.write_str("\n\n")?;
113115
for line in get_documentation_for(content).lines() {
114116
writeln!(&mut text, "# {}", line)?;

cargo-crev/src/main.rs

+55-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crev_lib::id::LockedId;
1212
use crev_lib::{self, local::Local};
1313
use std::{
1414
collections::{HashMap, HashSet},
15-
io::{self, BufRead, Write},
15+
fmt::Write as _,
16+
io::{self, BufRead, Write as _},
1617
panic,
1718
path::PathBuf,
1819
};
@@ -138,6 +139,7 @@ fn crate_review(args: opts::CrateReview) -> Result<()> {
138139
&args.common_proof_create,
139140
&args.diff,
140141
args.skip_activity_check || is_advisory || args.issue,
142+
args.overrides,
141143
args.cargo_opts.clone(),
142144
)?;
143145
let has_public_url = local
@@ -196,18 +198,22 @@ fn print_ids<'a>(
196198
Ok(())
197199
}
198200

201+
fn url_to_status_str<'a>(id_url: &UrlOfId<'a>) -> (&'static str, &'a str) {
202+
match id_url {
203+
UrlOfId::None => ("", ""),
204+
UrlOfId::FromSelfVerified(url) => ("==", url.url.as_str()),
205+
UrlOfId::FromSelf(url) => ("~=", url.url.as_str()),
206+
UrlOfId::FromOthers(url) => ("??", url.url.as_str()),
207+
}
208+
}
209+
199210
fn print_mvp_ids<'a>(
200211
ids: impl Iterator<Item = (&'a Id, u64)>,
201212
trust_set: &TrustSet,
202213
db: &ProofDB,
203214
) -> Result<()> {
204215
for (id, count) in ids {
205-
let (status, url) = match db.lookup_url(id) {
206-
UrlOfId::None => ("", ""),
207-
UrlOfId::FromSelfVerified(url) => ("==", url.url.as_str()),
208-
UrlOfId::FromSelf(url) => ("~=", url.url.as_str()),
209-
UrlOfId::FromOthers(url) => ("??", url.url.as_str()),
210-
};
216+
let (status, url) = url_to_status_str(&db.lookup_url(id));
211217
println!(
212218
"{:>3} {} {:6} {} {}",
213219
count,
@@ -296,6 +302,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
296302
&args.common_proof_create,
297303
args.level.unwrap_or(TrustLevel::Medium),
298304
args.level.is_none(),
305+
args.overrides,
299306
)?;
300307
}
301308
opts::Id::Untrust(args) => {
@@ -304,6 +311,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
304311
&args.common_proof_create,
305312
TrustLevel::None,
306313
true,
314+
args.overrides,
307315
)?;
308316
}
309317
opts::Id::Distrust(args) => {
@@ -312,6 +320,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
312320
&args.common_proof_create,
313321
TrustLevel::Distrust,
314322
true,
323+
args.overrides,
315324
)?;
316325
}
317326
opts::Id::Query(cmd) => match cmd {
@@ -427,6 +436,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
427436
&args.common_proof_create,
428437
args.level.unwrap_or(TrustLevel::Medium),
429438
args.level.is_none(),
439+
args.overrides,
430440
)?;
431441
// Make sure we have reviews for the new Ids we're trusting
432442
local.fetch_new_trusted(Default::default(), None)?;
@@ -496,6 +506,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
496506
&args.common_proof_create,
497507
&args.diff,
498508
args.skip_activity_check || is_advisory || args.issue,
509+
args.overrides,
499510
args.cargo_opts.clone(),
500511
)
501512
})?;
@@ -762,22 +773,56 @@ fn set_trust_level_for_ids(
762773
common_proof_create: &crate::opts::CommonProofCreate,
763774
trust_level: TrustLevel,
764775
edit_interactively: bool,
776+
show_override_suggestions: bool,
765777
) -> Result<()> {
766778
let local = ensure_crev_id_exists_or_make_one()?;
767779
let unlocked_id = local.read_current_unlocked_id(&term::read_passphrase)?;
768780

769-
let mut trust =
770-
local.build_trust_proof(unlocked_id.as_public_id(), ids.to_vec(), trust_level)?;
781+
let overrides = if ids.len() == 1 {
782+
let db = local.load_db()?;
783+
784+
db.get_trust_proof_between(&unlocked_id.id.id, &ids[0])
785+
.map(|trust_proof| trust_proof.override_.clone())
786+
.unwrap_or(vec![])
787+
} else {
788+
vec![]
789+
};
790+
791+
let mut trust = local.build_trust_proof(
792+
unlocked_id.as_public_id(),
793+
ids.to_vec(),
794+
trust_level,
795+
overrides,
796+
)?;
771797

772798
if edit_interactively {
773799
let extra_comment = if trust_level == TrustLevel::Distrust {
774800
Some("WARNING: Distrust has severe consequences. Read documentation below.")
775801
} else {
776802
None
777803
};
778-
trust = edit::edit_proof_content_iteractively(&trust, None, None, extra_comment)?;
804+
trust = edit::edit_proof_content_iteractively(&trust, None, None, extra_comment, |text| {
805+
if show_override_suggestions && trust.override_.is_empty() {
806+
writeln!(text, "# override:")?;
807+
}
808+
809+
if show_override_suggestions {
810+
let db = local.load_db()?;
811+
for (id, trust_level) in ids.into_iter().flat_map(|id| db.get_reverse_trust_for(id))
812+
{
813+
let (status, url) = url_to_status_str(&db.lookup_url(id));
814+
writeln!(text, "# - id-type: {}", "crev")?; // TODO: support other ids?
815+
writeln!(text, "# id: {} # level: {}", id, trust_level)?;
816+
writeln!(text, "# url: {} # {}", url, status)?;
817+
writeln!(text, "# comment: \"\"")?;
818+
}
819+
}
820+
821+
Ok(())
822+
})?;
779823
}
780824

825+
trust.touch_date();
781826
let proof = trust.sign_by(&unlocked_id)?;
782827

783828
if common_proof_create.print_unsigned {

cargo-crev/src/opts.rs

+12
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ pub struct CrateVerify {
397397

398398
#[derive(Debug, StructOpt, Clone)]
399399
pub struct IdTrust {
400+
#[structopt(long = "overrides")]
401+
/// Enable overrides suggestions
402+
pub overrides: bool,
403+
400404
/// Public IDs to create Trust Proof for
401405
pub public_ids: Vec<String>,
402406

@@ -410,6 +414,10 @@ pub struct IdTrust {
410414

411415
#[derive(Debug, StructOpt, Clone)]
412416
pub struct TrustUrls {
417+
#[structopt(long = "overrides")]
418+
/// Enable overrides suggestions
419+
pub overrides: bool,
420+
413421
/// Public IDs or proof repo URLs to create Trust Proof for
414422
pub public_ids_or_urls: Vec<String>,
415423

@@ -618,6 +626,10 @@ pub struct CrateReview {
618626
#[structopt(long = "skip-activity-check")]
619627
pub skip_activity_check: bool,
620628

629+
#[structopt(long = "overrides")]
630+
/// Enable overrides suggestions
631+
pub overrides: bool,
632+
621633
/// Review the delta since the given version
622634
#[structopt(long = "diff", name = "base-version")]
623635
#[allow(clippy::option_option)]

0 commit comments

Comments
 (0)