diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 0f97071e69a..4ee693c57f1 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -173,6 +173,14 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let source = if let Some(url) = args.get_one::("git") { let url = url.into_url()?; + if url.fragment().is_some() { + return Err(anyhow::format_err!( + "invalid git url to install from\n\n\ + help: use `--rev ` to specify a commit" + ) + .into()); + } + let gitref = if let Some(branch) = args.get_one::("branch") { GitReference::Branch(branch.clone()) } else if let Some(tag) = args.get_one::("tag") { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index dfff7a680c8..03980a8078b 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2169,6 +2169,28 @@ fn git_install_reads_workspace_manifest() { .run(); } +#[cargo_test] +fn install_git_with_rev_in_url() { + let p = git::repo(&paths::root().join("foo")) + .file("Cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("src/main.rs", "fn main() {}") + .build(); + + let mut url = p.url().to_string(); + url.push_str("#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4"); + + cargo_process("install --locked --git") + .arg(url) + .with_stderr_data(str![[r#" +[ERROR] invalid git url to install from + +[HELP] use `--rev ` to specify a commit + +"#]]) + .with_status(101) + .run(); +} + #[cargo_test] fn install_git_with_symlink_home() { // Ensure that `cargo install` with a git repo is OK when CARGO_HOME is a