Skip to content

Commit 130e11c

Browse files
committed
Auto merge of #7238 - ehuss:git-ssh-submodule, r=alexcrichton
Allow git dependency with shorthand ssh submodules to work. If a submodule is defined with a shorthand ssh url (like `git@github.com/user/repo.git`), then cargo was choking on it trying to convert it to a URL. The fix is to just pass around strings. An alternate solution would be to try to detect shorthand git urls and automatically add `ssh://` to the path. I'm concerned about matching git's heuristics for this, though. I'm willing to try if you think this would be better, though. I can't think of a good way to write a test for this, since we don't have any SSH test infrastructure. I verified running locally against github. Closes #7202
2 parents 15b436d + 0400879 commit 130e11c

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/cargo/sources/git/utils.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl GitRemote {
141141
fn fetch_into(&self, dst: &mut git2::Repository, cargo_config: &Config) -> CargoResult<()> {
142142
// Create a local anonymous remote in the repository to fetch the url
143143
let refspec = "refs/heads/*:refs/heads/*";
144-
fetch(dst, &self.url, refspec, cargo_config)
144+
fetch(dst, &self.url.as_str(), refspec, cargo_config)
145145
}
146146

147147
fn clone_into(&self, dst: &Path, cargo_config: &Config) -> CargoResult<git2::Repository> {
@@ -152,7 +152,7 @@ impl GitRemote {
152152
let mut repo = init(dst, true)?;
153153
fetch(
154154
&mut repo,
155-
&self.url,
155+
&self.url.as_str(),
156156
"refs/heads/*:refs/heads/*",
157157
cargo_config,
158158
)?;
@@ -276,7 +276,7 @@ impl<'a> GitCheckout<'a> {
276276
// need authentication information we may want progress bars and such.
277277
let url = database.path.into_url()?;
278278
let mut repo = None;
279-
with_fetch_options(&git_config, &url, config, &mut |fopts| {
279+
with_fetch_options(&git_config, url.as_str(), config, &mut |fopts| {
280280
let mut checkout = git2::build::CheckoutBuilder::new();
281281
checkout.dry_run(); // we'll do this below during a `reset`
282282

@@ -312,7 +312,7 @@ impl<'a> GitCheckout<'a> {
312312
info!("fetch {}", self.repo.path().display());
313313
let url = self.database.path.into_url()?;
314314
let refspec = "refs/heads/*:refs/heads/*";
315-
fetch(&mut self.repo, &url, refspec, cargo_config)?;
315+
fetch(&mut self.repo, url.as_str(), refspec, cargo_config)?;
316316
Ok(())
317317
}
318318

@@ -393,10 +393,8 @@ impl<'a> GitCheckout<'a> {
393393
init(&path, false)?
394394
}
395395
};
396-
397396
// Fetch data from origin and reset to the head commit
398397
let refspec = "refs/heads/*:refs/heads/*";
399-
let url = url.into_url()?;
400398
fetch(&mut repo, &url, refspec, cargo_config).chain_err(|| {
401399
internal(format!(
402400
"failed to fetch submodule `{}` from {}",
@@ -640,13 +638,13 @@ fn reset(repo: &git2::Repository, obj: &git2::Object<'_>, config: &Config) -> Ca
640638

641639
pub fn with_fetch_options(
642640
git_config: &git2::Config,
643-
url: &Url,
641+
url: &str,
644642
config: &Config,
645643
cb: &mut dyn FnMut(git2::FetchOptions<'_>) -> CargoResult<()>,
646644
) -> CargoResult<()> {
647645
let mut progress = Progress::new("Fetch", config);
648646
network::with_retry(config, || {
649-
with_authentication(url.as_str(), git_config, |f| {
647+
with_authentication(url, git_config, |f| {
650648
let mut rcb = git2::RemoteCallbacks::new();
651649
rcb.credentials(f);
652650

@@ -669,7 +667,7 @@ pub fn with_fetch_options(
669667

670668
pub fn fetch(
671669
repo: &mut git2::Repository,
672-
url: &Url,
670+
url: &str,
673671
refspec: &str,
674672
config: &Config,
675673
) -> CargoResult<()> {
@@ -685,14 +683,17 @@ pub fn fetch(
685683

686684
// If we're fetching from GitHub, attempt GitHub's special fast path for
687685
// testing if we've already got an up-to-date copy of the repository
688-
if url.host_str() == Some("github.com") {
689-
if let Ok(oid) = repo.refname_to_id("refs/remotes/origin/master") {
690-
let mut handle = config.http()?.borrow_mut();
691-
debug!("attempting GitHub fast path for {}", url);
692-
if github_up_to_date(&mut handle, url, &oid) {
693-
return Ok(());
694-
} else {
695-
debug!("fast path failed, falling back to a git fetch");
686+
687+
if let Ok(url) = Url::parse(url) {
688+
if url.host_str() == Some("github.com") {
689+
if let Ok(oid) = repo.refname_to_id("refs/remotes/origin/master") {
690+
let mut handle = config.http()?.borrow_mut();
691+
debug!("attempting GitHub fast path for {}", url);
692+
if github_up_to_date(&mut handle, &url, &oid) {
693+
return Ok(());
694+
} else {
695+
debug!("fast path failed, falling back to a git fetch");
696+
}
696697
}
697698
}
698699
}
@@ -732,7 +733,7 @@ pub fn fetch(
732733
loop {
733734
debug!("initiating fetch of {} from {}", refspec, url);
734735
let res = repo
735-
.remote_anonymous(url.as_str())?
736+
.remote_anonymous(url)?
736737
.fetch(&[refspec], Some(&mut opts), None);
737738
let err = match res {
738739
Ok(()) => break,
@@ -759,7 +760,7 @@ pub fn fetch(
759760

760761
fn fetch_with_cli(
761762
repo: &mut git2::Repository,
762-
url: &Url,
763+
url: &str,
763764
refspec: &str,
764765
config: &Config,
765766
) -> CargoResult<()> {
@@ -768,7 +769,7 @@ fn fetch_with_cli(
768769
.arg("--tags") // fetch all tags
769770
.arg("--force") // handle force pushes
770771
.arg("--update-head-ok") // see discussion in #2078
771-
.arg(url.to_string())
772+
.arg(url)
772773
.arg(refspec)
773774
// If cargo is run by git (for example, the `exec` command in `git
774775
// rebase`), the GIT_DIR is set by git and will point to the wrong

src/cargo/sources/registry/remote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
230230
let url = self.source_id.url();
231231
let refspec = "refs/heads/master:refs/remotes/origin/master";
232232
let repo = self.repo.borrow_mut().unwrap();
233-
git::fetch(repo, url, refspec, self.config)
233+
git::fetch(repo, url.as_str(), refspec, self.config)
234234
.chain_err(|| format!("failed to fetch `{}`", url))?;
235235
self.config.updated_sources().insert(self.source_id);
236236

0 commit comments

Comments
 (0)