Skip to content

fix(install): check if git url contains a rev #15489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {

let source = if let Some(url) = args.get_one::<String>("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 <SHA>` to specify a commit"
)
.into());
}

let gitref = if let Some(branch) = args.get_one::<String>("branch") {
GitReference::Branch(branch.clone())
} else if let Some(tag) = args.get_one::<String>("tag") {
Expand Down
22 changes: 22 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SHA>` 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
Expand Down