Skip to content

Commit e74c71a

Browse files
authored
feat: Implement --force (#127)
* Fix lints * Implement force for registry * Typos * Implement force for version mismatch
1 parent 5443190 commit e74c71a

File tree

5 files changed

+246
-20
lines changed

5 files changed

+246
-20
lines changed

src/cli.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct Override {
2929

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

3535
/// Path to the `Cargo.toml` file that needs patching.
@@ -46,6 +46,10 @@ pub struct Override {
4646
/// Equivalent to specifying both --locked and --offline
4747
#[arg(long)]
4848
pub frozen: bool,
49+
50+
/// Force the override, ignoring compatibility checks.
51+
#[arg(long)]
52+
pub force: bool,
4953
}
5054

5155
#[derive(Args, Debug)]

src/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub struct Context {
1313
pub manifest_path: Option<Utf8PathBuf>,
1414

1515
pub mode: Mode,
16+
17+
pub force: bool,
1618
}
1719

1820
#[derive(Copy, Clone)]
@@ -40,6 +42,7 @@ impl TryFrom<Cli> for Context {
4042
manifest_path,
4143
source: cli::Source { path, git },
4244
git: cli::Git { branch, tag, rev },
45+
force,
4346
}),
4447
}: Cli,
4548
) -> Result<Self, Self::Error> {
@@ -79,6 +82,8 @@ impl TryFrom<Cli> for Context {
7982
manifest_path,
8083

8184
mode,
85+
86+
force,
8287
})
8388
}
8489
}

src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use context::Context;
1111

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

14-
use anyhow::{bail, Context as _};
14+
use anyhow::{bail, ensure, Context as _};
1515
use fs_err as fs;
1616

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

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

4849
let manifest_path = project_manifest(manifest_dir, cargo)?;
4950

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

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

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

71-
resolved_deps
71+
&resolved_deps
7272
.into_iter()
7373
.find(|dep| dep.name == patch_manifest.name)
7474
.with_context(|| {
@@ -82,7 +82,7 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
8282
let dependency_registry = if dependency.registry == Some(DEFAULT_REGISTRY_URL.to_owned()) {
8383
None
8484
} else {
85-
dependency.registry
85+
dependency.registry.as_deref()
8686
};
8787

8888
let registry = if let Some(registry_url) = &dependency_registry {
@@ -97,16 +97,17 @@ pub fn run(working_dir: &Path, args: Cli) -> anyhow::Result<()> {
9797
registry_guess
9898
}
9999
(Some(registry_flag), Some(registry_guess)) => {
100-
// TODO: force is unimplemented
101-
bail!(
100+
ensure!(
101+
force,
102102
"user provided registry `{}` with the `--registry` flag \
103103
but dependency `{}` \
104104
uses registry `{}`.
105105
To use the registry, you passed, use `--force`",
106106
registry_flag,
107107
dependency.name,
108108
registry_guess
109-
)
109+
);
110+
registry_flag
110111
}
111112
(None, None) => bail!(
112113
"unable to determine registry name for `{}`

tests/all/cli.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn override_subcommand_help_message() {
6060
--rev <REV>
6161
Specific commit to use when overriding from git
6262
--registry <REGISTRY>
63-
Name of the registry to use. Usually `cargo-override` can correctly determine which regestiry to use without needing this flag
63+
Name of the registry to use. Usually `cargo-override` can correctly determine which registry to use without needing this flag
6464
--manifest-path <MANIFEST_PATH>
6565
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
6666
--locked
@@ -69,6 +69,8 @@ fn override_subcommand_help_message() {
6969
Prevents cargo from accessing the network
7070
--frozen
7171
Equivalent to specifying both --locked and --offline
72+
--force
73+
Force the override, ignoring compatibility checks
7274
-h, --help
7375
Print help
7476
-V, --version

0 commit comments

Comments
 (0)