Detect wider variety of usernames for SSH-based remotes (#21508)
Closes #21507 Release Notes: - Fixed detection of git remotes when using SSH and username is not "git".
This commit is contained in:
parent
28650b2fac
commit
aff17322f3
3 changed files with 16 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -5131,6 +5131,7 @@ dependencies = [
|
|||
"log",
|
||||
"parking_lot",
|
||||
"pretty_assertions",
|
||||
"regex",
|
||||
"rope",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
|
|
@ -21,6 +21,7 @@ gpui.workspace = true
|
|||
http_client.workspace = true
|
||||
log.workspace = true
|
||||
parking_lot.workspace = true
|
||||
regex.workspace = true
|
||||
rope.workspace = true
|
||||
serde.workspace = true
|
||||
smol.workspace = true
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
use derive_more::Deref;
|
||||
use regex::Regex;
|
||||
use url::Url;
|
||||
|
||||
/// The URL to a Git remote.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Deref)]
|
||||
pub struct RemoteUrl(Url);
|
||||
|
||||
static USERNAME_REGEX: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"^[0-9a-zA-Z\-_]+@").expect("Failed to create USERNAME_REGEX"));
|
||||
|
||||
impl std::str::FromStr for RemoteUrl {
|
||||
type Err = url::ParseError;
|
||||
|
||||
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
||||
if input.starts_with("git@") {
|
||||
if USERNAME_REGEX.is_match(input) {
|
||||
// Rewrite remote URLs like `git@github.com:user/repo.git` to `ssh://git@github.com/user/repo.git`
|
||||
let ssh_url = input.replacen(':', "/", 1).replace("git@", "ssh://git@");
|
||||
let ssh_url = format!("ssh://{}", input.replacen(':', "/", 1));
|
||||
Ok(RemoteUrl(Url::parse(&ssh_url)?))
|
||||
} else {
|
||||
Ok(RemoteUrl(Url::parse(input)?))
|
||||
|
@ -40,6 +46,12 @@ mod tests {
|
|||
"github.com",
|
||||
"/octocat/zed.git",
|
||||
),
|
||||
(
|
||||
"org-000000@github.com:octocat/zed.git",
|
||||
"ssh",
|
||||
"github.com",
|
||||
"/octocat/zed.git",
|
||||
),
|
||||
(
|
||||
"ssh://git@github.com/octocat/zed.git",
|
||||
"ssh",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue