Skip to content

Commit

Permalink
feat: Implement --force (#127)
Browse files Browse the repository at this point in the history
* Fix lints

* Implement force for registry

* Typos

* Implement force for version mismatch
  • Loading branch information
Lindronics authored Sep 11, 2024
1 parent 5443190 commit e74c71a
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Override {

#[arg(long)]
/// Name of the registry to use.
/// Usually `cargo-override` can correctly determine which regestiry to use without needing this flag
/// Usually `cargo-override` can correctly determine which registry to use without needing this flag
pub registry: Option<String>,

/// Path to the `Cargo.toml` file that needs patching.
Expand All @@ -46,6 +46,10 @@ pub struct Override {
/// Equivalent to specifying both --locked and --offline
#[arg(long)]
pub frozen: bool,

/// Force the override, ignoring compatibility checks.
#[arg(long)]
pub force: bool,
}

#[derive(Args, Debug)]
Expand Down
5 changes: 5 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Context {
pub manifest_path: Option<Utf8PathBuf>,

pub mode: Mode,

pub force: bool,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -40,6 +42,7 @@ impl TryFrom<Cli> for Context {
manifest_path,
source: cli::Source { path, git },
git: cli::Git { branch, tag, rev },
force,
}),
}: Cli,
) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -79,6 +82,8 @@ impl TryFrom<Cli> for Context {
manifest_path,

mode,

force,
})
}
}
31 changes: 16 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use context::Context;

use std::path::{Path, PathBuf};

use anyhow::{bail, Context as _};
use anyhow::{bail, ensure, Context as _};
use fs_err as fs;

pub static DEFAULT_REGISTRY: &str = "crates-io";
Expand All @@ -24,6 +24,7 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
manifest_path,
registry_hint,
mode,
force,
} = args.try_into()?;

let path = match &mode {
Expand All @@ -47,7 +48,7 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {

let manifest_path = project_manifest(manifest_dir, cargo)?;

let project_deps = metadata::direct_dependencies(&manifest_dir, cargo)
let project_deps = metadata::direct_dependencies(manifest_dir, cargo)
.context("failed to get dependencies for current project")?;

let mut direct_deps = project_deps
Expand All @@ -56,19 +57,18 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
.peekable();

let dependency = if direct_deps.peek().is_some() {
if let Some(dep) = direct_deps.find(|dependency| match dependency.requirement {
Some(ref req) => req.matches(&patch_manifest.version),
None => false,
}) {
dep.clone()
} else {
bail!("patch could not be applied because version is incompatible")
}
direct_deps
.find(|dep| {
dep.requirement
.as_ref()
.is_some_and(|req| req.matches(&patch_manifest.version) || force)
})
.context("patch could not be applied because version is incompatible")?
} else {
let resolved_deps = metadata::resolved_dependencies(manifest_dir, cargo)
.context("failed to get dependencies for current project")?;

resolved_deps
&resolved_deps
.into_iter()
.find(|dep| dep.name == patch_manifest.name)
.with_context(|| {
Expand All @@ -82,7 +82,7 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
let dependency_registry = if dependency.registry == Some(DEFAULT_REGISTRY_URL.to_owned()) {
None
} else {
dependency.registry
dependency.registry.as_deref()
};

let registry = if let Some(registry_url) = &dependency_registry {
Expand All @@ -97,16 +97,17 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
registry_guess
}
(Some(registry_flag), Some(registry_guess)) => {
// TODO: force is unimplemented
bail!(
ensure!(
force,
"user provided registry `{}` with the `--registry` flag \
but dependency `{}` \
uses registry `{}`.
To use the registry, you passed, use `--force`",
registry_flag,
dependency.name,
registry_guess
)
);
registry_flag
}
(None, None) => bail!(
"unable to determine registry name for `{}`
Expand Down
4 changes: 3 additions & 1 deletion tests/all/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn override_subcommand_help_message() {
--rev <REV>
Specific commit to use when overriding from git
--registry <REGISTRY>
Name of the registry to use. Usually `cargo-override` can correctly determine which regestiry to use without needing this flag
Name of the registry to use. Usually `cargo-override` can correctly determine which registry to use without needing this flag
--manifest-path <MANIFEST_PATH>
Path to the `Cargo.toml` file that needs patching. By default, `cargo-override` searches for the `Cargo.toml` file in the current directory or any parent directory
--locked
Expand All @@ -69,6 +69,8 @@ fn override_subcommand_help_message() {
Prevents cargo from accessing the network
--frozen
Equivalent to specifying both --locked and --offline
--force
Force the override, ignoring compatibility checks
-h, --help
Print help
-V, --version
Expand Down
Loading

0 comments on commit e74c71a

Please sign in to comment.