Skip to content

Commit dcc8cef

Browse files
committed
fix(install): return error when rev is in git url
return error if the git url contains a # and hint user to use --rev
1 parent e40504d commit dcc8cef

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
173173

174174
let source = if let Some(url) = args.get_one::<String>("git") {
175175
let url = url.into_url()?;
176+
if url.fragment().is_some() {
177+
return Err(anyhow::format_err!(
178+
"invalid git url to install from\n\n\
179+
help: use `--rev <SHA>` to specify a commit"
180+
)
181+
.into());
182+
}
183+
176184
let gitref = if let Some(branch) = args.get_one::<String>("branch") {
177185
GitReference::Branch(branch.clone())
178186
} else if let Some(tag) = args.get_one::<String>("tag") {

tests/testsuite/install.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,16 +2180,11 @@ fn install_git_with_rev_in_url() {
21802180
url.push_str("#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4");
21812181

21822182
cargo_process("install --locked --git")
2183-
.arg(url.to_string())
2183+
.arg(url)
21842184
.with_stderr_data(str![[r#"
2185-
[UPDATING] git repository `[ROOTURL]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4`
2186-
[WARNING] spurious network error (3 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2)
2187-
[WARNING] spurious network error (2 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2)
2188-
[WARNING] spurious network error (1 try remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2)
2189-
[ERROR] failed to clone into: [ROOT]/home/.cargo/git/db/foo-[HASH]
2185+
[ERROR] invalid git url to install from
21902186
2191-
Caused by:
2192-
failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2)
2187+
[HELP] use `--rev <SHA>` to specify a commit
21932188
21942189
"#]])
21952190
.with_status(101)

0 commit comments

Comments
 (0)