Skip to content

Commit

Permalink
Remember user --mode choices, and support diff.rs for inspect
Browse files Browse the repository at this point in the history
This change adds support to remember user's --mode choices, so that they
do not need to be used every time. In addition, it adds a basic support
for diff.rs to inspect (by loading a diff from a version to itself) such
that this mode memory can be shared between diff and inspect.

If xfbs/diff.rs#24 is merged, we should change the diff.rs inspect
behaviour to use the new browse endpoints instead.

Finally, this patch changes the default mode from sourcegraph to
diff.rs. As noted in #611, sourcegraph has been quite unreliable lately,
and diff.rs is likely to provide a better experience until that is
resolved.
  • Loading branch information
mystor committed Sep 18, 2024
1 parent 1d6c74e commit 6378b3c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 29 deletions.
35 changes: 20 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{path::PathBuf, str::FromStr};

use clap::{Parser, Subcommand, ValueEnum};
use serde::{Deserialize, Serialize};
use tracing::level_filters::LevelFilter;

use crate::format::{CriteriaName, ImportName, PackageName, VersionReq, VetVersion};
Expand Down Expand Up @@ -455,7 +456,7 @@ pub struct CheckArgs {}
#[derive(clap::Args)]
pub struct InitArgs {}

/// Fetches the crate to a temp location and pushd's to it
/// Inspect a crate at a specific version
#[derive(clap::Args)]
pub struct InspectArgs {
/// The package to inspect
Expand All @@ -465,11 +466,16 @@ pub struct InspectArgs {
#[clap(action)]
pub version: VetVersion,
/// How to inspect the source
#[clap(long, action, default_value = "sourcegraph")]
pub mode: InspectFetchMode,
///
/// Defaults to the most recently used --mode argument, or diff.rs if no
/// mode argument has been used.
///
/// This option is ignored if a git version is passed.
#[clap(long, action)]
pub mode: Option<FetchMode>,
}

/// Emits a diff of the two versions
/// View a diff between two versions of the given crate
#[derive(clap::Args)]
pub struct DiffArgs {
/// The package to diff
Expand All @@ -481,9 +487,14 @@ pub struct DiffArgs {
/// The target version to diff
#[clap(action)]
pub version2: VetVersion,
/// How to inspect the source
#[clap(long, action, default_value = "sourcegraph")]
pub mode: DiffFetchMode,
/// How to inspect the diff
///
/// Defaults to the most recently used --mode argument, or diff.rs if no
/// mode argument has been used.
///
/// This option is ignored if a git version is passed.
#[clap(long, action)]
pub mode: Option<FetchMode>,
}

/// Certifies a package as audited
Expand Down Expand Up @@ -772,14 +783,8 @@ pub enum Verbose {
Trace,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum InspectFetchMode {
Local,
Sourcegraph,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum DiffFetchMode {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum, Serialize, Deserialize)]
pub enum FetchMode {
Local,
Sourcegraph,
#[clap(name = "diff.rs")]
Expand Down
2 changes: 2 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Details of the file formats used by cargo vet
use crate::cli::FetchMode;
use crate::errors::{StoreVersionParseError, VersionParseError};
use crate::resolver::{DiffRecommendation, ViolationConflict};
use crate::serialization::{spanned::Spanned, Tidyable};
Expand Down Expand Up @@ -1182,6 +1183,7 @@ impl FetchCommand {
pub struct CommandHistory {
#[serde(flatten)]
pub last_fetch: Option<FetchCommand>,
pub last_fetch_mode: Option<FetchMode>,
}

////////////////////////////////////////////////////////////////////////////////////
Expand Down
39 changes: 29 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,23 @@ fn cmd_inspect(
version: version.clone(),
});

if sub_args.mode == InspectFetchMode::Sourcegraph && version.git_rev.is_none() {
let url = format!("https://sourcegraph.com/crates/{package}@v{version}");
// If an explicit mode was set, record it in the cache, then pull the
// most recent explicit mode to use for this call.
if let Some(mode) = sub_args.mode {
cache.set_last_fetch_mode(mode);
}
let mode = cache.get_last_fetch_mode().unwrap_or(FetchMode::DiffRs);

if mode != FetchMode::Local && version.git_rev.is_none() {
let url = match mode {
FetchMode::Sourcegraph => {
format!("https://sourcegraph.com/crates/{package}@v{version}")
}
FetchMode::DiffRs => {
format!("https://diff.rs/{package}/{version}/{version}/")
}
FetchMode::Local => unreachable!(),
};
tokio::runtime::Handle::current()
.block_on(prompt_criteria_eulas(
out,
Expand Down Expand Up @@ -2050,20 +2065,24 @@ fn cmd_diff(out: &Arc<dyn Out>, cfg: &Config, sub_args: &DiffArgs) -> Result<(),
version2: version2.clone(),
});

if sub_args.mode != DiffFetchMode::Local
&& version1.git_rev.is_none()
&& version2.git_rev.is_none()
{
let url = match sub_args.mode {
DiffFetchMode::Sourcegraph => {
// If an explicit mode was set, record it in the cache, then pull the
// most recent explicit mode to use for this call.
if let Some(mode) = sub_args.mode {
cache.set_last_fetch_mode(mode);
}
let mode = cache.get_last_fetch_mode().unwrap_or(FetchMode::DiffRs);

if mode != FetchMode::Local && version1.git_rev.is_none() && version2.git_rev.is_none() {
let url = match mode {
FetchMode::Sourcegraph => {
format!(
"https://sourcegraph.com/crates/{package}/-/compare/v{version1}...v{version2}?visible=7000"
)
}
DiffFetchMode::DiffRs => {
FetchMode::DiffRs => {
format!("https://diff.rs/{package}/{version1}/{version2}/")
}
DiffFetchMode::Local => unreachable!(),
FetchMode::Local => unreachable!(),
};
tokio::runtime::Handle::current()
.block_on(prompt_criteria_eulas(
Expand Down
11 changes: 11 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tar::Archive;
use tracing::{error, info, log::warn, trace};

use crate::{
cli::FetchMode,
criteria::CriteriaMapper,
errors::{
AggregateError, BadFormatError, BadWildcardEndDateError, CacheAcquireError,
Expand Down Expand Up @@ -2291,6 +2292,16 @@ impl Cache {
guard.command_history.last_fetch = Some(last_fetch);
}

pub fn get_last_fetch_mode(&self) -> Option<FetchMode> {
let guard = self.state.lock().unwrap();
guard.command_history.last_fetch_mode
}

pub fn set_last_fetch_mode(&self, mode: FetchMode) {
let mut guard = self.state.lock().unwrap();
guard.command_history.last_fetch_mode = Some(mode);
}

/// For a given package, fetch the list of versions published on crates.io,
/// along with the corresponding index entry.
///
Expand Down
16 changes: 12 additions & 4 deletions tests/snapshots/test_cli__markdown-help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ The version to inspect
#### `--mode <MODE>`
How to inspect the source
\[default: sourcegraph]
\[possible values: local, sourcegraph]
Defaults to the most recently used --mode argument, or diff.rs if no mode argument has
been used.
This option is ignored if a git version is passed.
\[possible values: local, sourcegraph, diff.rs]
#### `-h, --help`
Print help information
Expand Down Expand Up @@ -310,9 +314,13 @@ The target version to diff
### OPTIONS
#### `--mode <MODE>`
How to inspect the source
How to inspect the diff
Defaults to the most recently used --mode argument, or diff.rs if no mode argument has
been used.
This option is ignored if a git version is passed.
\[default: sourcegraph]
\[possible values: local, sourcegraph, diff.rs]
#### `-h, --help`
Expand Down

0 comments on commit 6378b3c

Please sign in to comment.