Skip to content

Commit 2bf6886

Browse files
committed
feat: emit help messages for github pull request url in dependency
1 parent 16bbbde commit 2bf6886

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

src/cargo/sources/git/oxide.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use tracing::debug;
1616

1717
/// For the time being, `repo_path` makes it easy to instantiate a gitoxide repo just for fetching.
1818
/// In future this may change to be the gitoxide repository itself.
19-
pub fn with_retry_and_progress(
19+
pub fn with_retry_and_progress<'a>(
2020
repo_path: &std::path::Path,
2121
gctx: &GlobalContext,
22+
repo_remote_url: &str,
2223
cb: &(
2324
dyn Fn(
2425
&std::path::Path,
@@ -54,7 +55,7 @@ pub fn with_retry_and_progress(
5455
*urls.borrow_mut() = Some(url.to_owned());
5556
},
5657
);
57-
amend_authentication_hints(res, urls.get_mut().take())
58+
amend_authentication_hints(res, repo_remote_url, urls.get_mut().take())
5859
});
5960
translate_progress_to_bar(&mut progress_bar, root, is_shallow)?;
6061
thread.join().expect("no panic in scoped thread")
@@ -180,6 +181,7 @@ fn translate_progress_to_bar(
180181

181182
fn amend_authentication_hints(
182183
res: Result<(), crate::sources::git::fetch::Error>,
184+
remote_url: &str,
183185
last_url_for_authentication: Option<gix::bstr::BString>,
184186
) -> CargoResult<()> {
185187
let Err(err) = res else { return Ok(()) };
@@ -189,6 +191,7 @@ fn amend_authentication_hints(
189191
) => Some(err),
190192
_ => None,
191193
};
194+
192195
if let Some(e) = e {
193196
let auth_message = match e {
194197
gix::protocol::handshake::Error::Credentials(_) => {
@@ -203,10 +206,14 @@ fn amend_authentication_hints(
203206
.into()
204207
}
205208
gix::protocol::handshake::Error::Transport(_) => {
206-
let msg = concat!(
207-
"network failure seems to have happened\n",
208-
"if a proxy or similar is necessary `net.git-fetch-with-cli` may help here\n",
209-
"https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli"
209+
let msg = format!(
210+
concat!(
211+
"network failure seems to have happened\n",
212+
"if a proxy or similar is necessary `net.git-fetch-with-cli` may help here\n",
213+
"https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli",
214+
"{}"
215+
),
216+
super::utils::note_github_pull_request(remote_url).unwrap_or_default()
210217
);
211218
return Err(anyhow::Error::from(err).context(msg));
212219
}

src/cargo/sources/git/utils.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,14 @@ where
798798
| ErrorClass::FetchHead
799799
| ErrorClass::Ssh
800800
| ErrorClass::Http => {
801-
let mut msg = "network failure seems to have happened\n".to_string();
802-
msg.push_str(
803-
"if a proxy or similar is necessary `net.git-fetch-with-cli` may help here\n",
804-
);
805-
msg.push_str(
806-
"https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli",
801+
let msg = format!(
802+
concat!(
803+
"network failure seems to have happened\n",
804+
"if a proxy or similar is necessary `net.git-fetch-with-cli` may help here\n",
805+
"https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli",
806+
"{}"
807+
),
808+
note_github_pull_request(url).unwrap_or_default()
807809
);
808810
err = err.context(msg);
809811
}
@@ -1142,6 +1144,7 @@ fn fetch_with_gitoxide(
11421144
let res = oxide::with_retry_and_progress(
11431145
git2_repo.path(),
11441146
gctx,
1147+
remote_url,
11451148
&|repo_path,
11461149
should_interrupt,
11471150
mut progress,
@@ -1589,6 +1592,33 @@ fn is_github(url: &Url) -> bool {
15891592
url.host_str() == Some("github.com")
15901593
}
15911594

1595+
// Give some messages on GitHub PR URL given as is
1596+
pub(crate) fn note_github_pull_request(url: &str) -> Option<String> {
1597+
if let Ok(url) = url.parse::<Url>()
1598+
&& is_github(&url)
1599+
{
1600+
let path_segments = url
1601+
.path_segments()
1602+
.map(|p| p.into_iter().collect::<Vec<_>>())
1603+
.unwrap_or_default();
1604+
if let [owner, repo, "pull", pr_number, ..] = path_segments[..] {
1605+
let repo_url = format!("https://github.com/{owner}/{repo}.git");
1606+
let rev = format!("refs/pull/{pr_number}/head");
1607+
return Some(format!(
1608+
concat!(
1609+
"\n\nnote: GitHub url {} is not a repository. \n",
1610+
"help: Replace the dependency with \n",
1611+
" `git = \"{}\" rev = \"{}\"` \n",
1612+
" to specify pull requests as dependencies' revision."
1613+
),
1614+
url, repo_url, rev
1615+
));
1616+
}
1617+
}
1618+
1619+
None
1620+
}
1621+
15921622
/// Whether a `rev` looks like a commit hash (ASCII hex digits).
15931623
fn looks_like_commit_hash(rev: &str) -> bool {
15941624
rev.len() >= 7 && rev.chars().all(|ch| ch.is_ascii_hexdigit())

tests/testsuite/bad_config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,11 @@ Caused by:
22902290
22912291
Caused by:
22922292
Unable to update https://github.com/foo/bar/pull/123
2293+
...
2294+
[NOTE] GitHub url https://github.com/foo/bar/pull/123 is not a repository.
2295+
[HELP] Replace the dependency with
2296+
`git = "https://github.com/foo/bar.git" rev = "refs/pull/123/head"`
2297+
to specify pull requests as dependencies' revision.
22932298
...
22942299
"#]])
22952300
.run();

tests/testsuite/patch.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ Caused by:
402402
if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
403403
https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
404404
405+
[NOTE] GitHub url https://github.com/foo/bar/pull/123 is not a repository.
406+
[HELP] Replace the dependency with
407+
`git = "https://github.com/foo/bar.git" rev = "refs/pull/123/head"`
408+
to specify pull requests as dependencies' revision.
409+
405410
Caused by:
406411
...
407412
"#

0 commit comments

Comments
 (0)