forked from rust-lang/cargo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: normalize relative git submodule urls with
ssh://
Git only assumes a URL is a relative path if it starts with `./` or `../`. See <https://git-scm.com/docs/git-submodule>. To fetch the current repo, we need to construct the full submodule URL. At this moment it comes with some limitations: * GitHub only normalizes HTTPS protocol with relative path `..`. (`ssh://[email protected]/rust-lang/cargo.git/relative/..` is invalid) * `url` crate cannot parse SCP-like URLs. (`[email protected]:rust-lang/cargo.git` is not a valid WHATWG URL) For the first one, we use `url` crate to normalize. For the second one, we append the relative path directly to parent URL. That says, the only failing case downs to one: SCP-like parent remote URLs with relative submodule URLs on GitHub or git services not normalizing them.
- Loading branch information
Showing
2 changed files
with
40 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -441,16 +441,46 @@ impl<'a> GitCheckout<'a> { | |
} | ||
|
||
// Git only assumes a URL is a relative path if it starts with `./` or `../`. | ||
// See [`git submodule add`] documentation. | ||
// See <https://git-scm.com/docs/git-submodule>. To fetch the current repo, | ||
// we need to construct the full submodule URL. | ||
// | ||
// [`git submodule add`]: https://git-scm.com/docs/git-submodule | ||
// At this moment it comes with some limitations: | ||
// | ||
// * GitHub only normalizes HTTPS protocol with relative path `..`. | ||
// (`ssh://[email protected]/rust-lang/cargo.git/relative/..` is invalid) | ||
// * `url` crate cannot parse SCP-like URLs. | ||
// (`[email protected]:rust-lang/cargo.git` is not a valid WHATWG URL) | ||
// | ||
// For the first one, we use `url` crate to normalize. For the second one, | ||
// we append the relative path directly to parent URL. That says, the only | ||
// failing case downs to one: SCP-like parent remote URLs with relative | ||
// submodule URLs on GitHub or git services not normalizing them. | ||
// | ||
// See rust-lang/cargo#12404 and rust-lang/cargo#12295. | ||
let child_remote_url = if ["./", "../"].iter().any(|p| child_url_str.starts_with(p)) { | ||
let mut new_remote_url = parent_remote_url.to_string(); | ||
if !new_remote_url.ends_with('/') { | ||
new_remote_url.push('/'); | ||
match Url::parse(parent_remote_url) { | ||
Ok(mut parent_url) => { | ||
let path = parent_url.path(); | ||
if !path.ends_with('/') { | ||
parent_url.set_path(&format!("{path}/")); | ||
} | ||
let new_remote_url = parent_url.join(child_url_str).with_context(|| { | ||
format!( | ||
"failed to parse relative child submodule url `{child_url_str}` \ | ||
using parent base url `{parent_url}`" | ||
) | ||
})?; | ||
Cow::from(new_remote_url.to_string()) | ||
} | ||
Err(_) => { | ||
let mut new_remote_url = parent_remote_url.to_string(); | ||
if !new_remote_url.ends_with('/') { | ||
new_remote_url.push('/'); | ||
} | ||
new_remote_url.push_str(child_url_str); | ||
Cow::from(new_remote_url) | ||
} | ||
} | ||
new_remote_url.push_str(child_url_str); | ||
Cow::from(new_remote_url) | ||
} else { | ||
Cow::from(child_url_str) | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters