Skip to content

Commit 5a9f611

Browse files
committed
Auto merge of #12359 - weihanglo:issue/12295, r=ehuss
fix(git): respect scp-like URL for nested submodules
2 parents ca1e880 + a7247ba commit 5a9f611

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,13 @@ impl<'a> GitCheckout<'a> {
392392
///
393393
/// [^1]: <https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-none>
394394
fn update_submodules(&self, cargo_config: &Config) -> CargoResult<()> {
395-
return update_submodules(&self.repo, cargo_config, self.remote_url());
395+
return update_submodules(&self.repo, cargo_config, self.remote_url().as_str());
396396

397397
/// Recusive helper for [`GitCheckout::update_submodules`].
398398
fn update_submodules(
399399
repo: &git2::Repository,
400400
cargo_config: &Config,
401-
parent_remote_url: &Url,
401+
parent_remote_url: &str,
402402
) -> CargoResult<()> {
403403
debug!("update submodules for: {:?}", repo.workdir().unwrap());
404404

@@ -420,7 +420,7 @@ impl<'a> GitCheckout<'a> {
420420
parent: &git2::Repository,
421421
child: &mut git2::Submodule<'_>,
422422
cargo_config: &Config,
423-
parent_remote_url: &Url,
423+
parent_remote_url: &str,
424424
) -> CargoResult<()> {
425425
child.init(false)?;
426426

@@ -444,30 +444,15 @@ impl<'a> GitCheckout<'a> {
444444
// See [`git submodule add`] documentation.
445445
//
446446
// [`git submodule add`]: https://git-scm.com/docs/git-submodule
447-
let child_remote_url = if child_url_str.starts_with("./")
448-
|| child_url_str.starts_with("../")
449-
{
450-
let mut new_parent_remote_url = parent_remote_url.clone();
451-
452-
let mut new_path = Cow::from(parent_remote_url.path());
453-
if !new_path.ends_with('/') {
454-
new_path.to_mut().push('/');
447+
let child_remote_url = if ["./", "../"].iter().any(|p| child_url_str.starts_with(p)) {
448+
let mut new_remote_url = parent_remote_url.to_string();
449+
if !new_remote_url.ends_with('/') {
450+
new_remote_url.push('/');
455451
}
456-
new_parent_remote_url.set_path(&new_path);
457-
458-
new_parent_remote_url.join(child_url_str).with_context(|| {
459-
format!(
460-
"failed to parse relative child submodule url `{child_url_str}` \
461-
using parent base url `{new_parent_remote_url}`"
462-
)
463-
})?
452+
new_remote_url.push_str(child_url_str);
453+
Cow::from(new_remote_url)
464454
} else {
465-
Url::parse(child_url_str).with_context(|| {
466-
let child_module_name = child.name().unwrap_or("");
467-
format!(
468-
"failed to parse url for submodule `{child_module_name}`: `{child_url_str}`"
469-
)
470-
})?
455+
Cow::from(child_url_str)
471456
};
472457

473458
// A submodule which is listed in .gitmodules but not actually
@@ -502,20 +487,17 @@ impl<'a> GitCheckout<'a> {
502487
let reference = GitReference::Rev(head.to_string());
503488
cargo_config
504489
.shell()
505-
.status("Updating", format!("git submodule `{}`", child_remote_url))?;
490+
.status("Updating", format!("git submodule `{child_remote_url}`"))?;
506491
fetch(
507492
&mut repo,
508-
child_remote_url.as_str(),
493+
&child_remote_url,
509494
&reference,
510495
cargo_config,
511496
RemoteKind::GitDependency,
512497
)
513498
.with_context(|| {
514-
format!(
515-
"failed to fetch submodule `{}` from {}",
516-
child.name().unwrap_or(""),
517-
child_remote_url
518-
)
499+
let name = child.name().unwrap_or("");
500+
format!("failed to fetch submodule `{name}` from {child_remote_url}",)
519501
})?;
520502

521503
let obj = repo.find_object(head, None)?;

tests/testsuite/git.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,7 +3633,7 @@ fn different_user_relative_submodules() {
36333633
.file("Cargo.toml", &basic_lib_manifest("dep1"))
36343634
.file("src/lib.rs", "")
36353635
});
3636-
let user2_git_project2 = git::new("user2/dep2", |project| {
3636+
let _user2_git_project2 = git::new("user2/dep2", |project| {
36373637
project
36383638
.file("Cargo.toml", &basic_lib_manifest("dep1"))
36393639
.file("src/lib.rs", "")
@@ -3673,14 +3673,14 @@ fn different_user_relative_submodules() {
36733673
"\
36743674
[UPDATING] git repository `{}`
36753675
[UPDATING] git submodule `{}`
3676-
[UPDATING] git submodule `{}`
3676+
[UPDATING] git submodule `{}/../dep2`
36773677
[COMPILING] dep1 v0.5.0 ({}#[..])
36783678
[COMPILING] foo v0.5.0 ([CWD])
36793679
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
36803680
",
36813681
path2url(&user1_git_project.root()),
36823682
path2url(&user2_git_project.root()),
3683-
path2url(&user2_git_project2.root()),
3683+
path2url(&user2_git_project.root()),
36843684
path2url(&user1_git_project.root()),
36853685
))
36863686
.run();

0 commit comments

Comments
 (0)