|
1 | 1 | use std::path::Path; |
2 | 2 |
|
3 | 3 | use git2::Repository; |
4 | | -use snafu::{OptionExt as _, ResultExt as _, Snafu}; |
| 4 | +use snafu::{OptionExt as _, Snafu}; |
5 | 5 | use tracing::Span; |
6 | 6 | use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle}; |
7 | | -use url::Url; |
8 | 7 |
|
9 | | -/// Errors that can occur during URL rewriting. |
10 | 8 | #[derive(Debug, Snafu)] |
11 | 9 | pub enum UrlRewriteError { |
12 | | - #[snafu(display("Failed to parse URL {url:?}: {source}"))] |
13 | | - ParseUrl { source: url::ParseError, url: String }, |
14 | | - #[snafu(display("URL {url:?} has no host component"))] |
15 | | - NoHostInUrl { url: String }, |
| 10 | + #[snafu(display("URL does not have https schema: {}", url))] |
| 11 | + NoHttpsSchema { url: String }, |
16 | 12 | } |
17 | 13 |
|
18 | 14 | /// Rewrites a given URL to an SSH-style Git URL |
19 | 15 | /// For example, `https://github.com/user/repo.git` becomes `git@github.com:user/repo.git`. |
20 | | -pub fn rewrite_git_https_url_to_ssh( |
21 | | - original_url: &str, |
22 | | -) -> Result<String, UrlRewriteError> { |
23 | | - let parsed_url = Url::parse(original_url).context(ParseUrlSnafu { |
24 | | - url: original_url, |
25 | | - })?; |
26 | | - |
27 | | - let host = parsed_url.host_str().context(NoHostInUrlSnafu { |
28 | | - url: original_url, |
29 | | - })?; |
30 | | - let path = parsed_url.path().trim_start_matches('/'); |
31 | | - |
32 | | - Ok(format!("git@{}:{}", host, path)) |
| 16 | +pub fn rewrite_git_https_url_to_ssh(original_url: &str) -> Result<String, UrlRewriteError> { |
| 17 | + let schemaless = original_url |
| 18 | + .strip_prefix("https://") |
| 19 | + .context(NoHttpsSchemaSnafu { |
| 20 | + url: original_url.to_string(), |
| 21 | + })?; |
| 22 | + Ok(format!("ssh://git@{schemaless}")) |
33 | 23 | } |
34 | 24 |
|
35 | 25 | /// Runs a function whenever a `value` changes "enough". |
|
0 commit comments