Skip to content

SCP-like URL to ssh conversion is lossyย #2467

@weihanglo

Description

@weihanglo

Current behavior ๐Ÿ˜ฏ

In SCP-like URL, the path git@github.com:rust-lang/cargo.git is home-relative. The path ought to be ~git/rust-lang/cargo.git. However, gix-url@0.35.2 converts git@github.com:rust-lang/cargo.git to ssh://git@github.com/rust-lang/cargo.git, which is lossy (not wrong). That SSH URL is absolute /rust-lang/cargo.git.

Expected behavior ๐Ÿค”

I don't really know.

git@github.com:rust-lang/cargo.git -> ssh://git@github.com/~/rust-lang/cargo.git seems to be a more correct behavior. However, as I found in rust-lang/cargo#16740 (comment), GitHub doesn't recognize ssh://git@github.com/~/rust-lang/cargo.git (perhaps for security reason to prevent file system traversal?)

Git behavior

Not related to Git. Git doesn't do conversion.

Steps to reproduce ๐Ÿ•น

Run this cargo script cargo +nightly -Zscript script.rs

---cargo
package.edition = "2024"
dependencies.gix-url = "=0.35.2" 
---
fn main() {
    for url in [
        "git@github.com:rust-lang/cargo.git",
        "git@github.com:/rust-lang/cargo.git",
        "git@github.com:~/rust-lang/cargo.git",
        "git@github.com:~git/rust-lang/cargo.git",
        "git@github.com:/~/rust-lang/cargo.git",
        "git@github.com:/~git/rust-lang/cargo.git",
    ] {
        print(url);
        eprintln!();
    }
}

fn print(url: &str) {
    let scp = gix_url::parse(url.into()).unwrap();
    let canonical = scp.clone().serialize_alternate_form(false).to_bstring();
    eprintln!("SCP-like: {url}");
    eprintln!("path:     {}", std::str::from_utf8(&scp.path).unwrap());
    eprintln!("SSH URL:  {}", std::str::from_utf8(&canonical).unwrap());
}

Output:

SCP-like: git@github.com:rust-lang/cargo.git
path:     rust-lang/cargo.git
SSH URL:  ssh://git@github.com/rust-lang/cargo.git

SCP-like: git@github.com:/rust-lang/cargo.git
path:     /rust-lang/cargo.git
SSH URL:  ssh://git@github.com/rust-lang/cargo.git

SCP-like: git@github.com:~/rust-lang/cargo.git
path:     ~/rust-lang/cargo.git
SSH URL:  ssh://git@github.com/~/rust-lang/cargo.git

SCP-like: git@github.com:~git/rust-lang/cargo.git
path:     ~git/rust-lang/cargo.git
SSH URL:  ssh://git@github.com/~git/rust-lang/cargo.git

SCP-like: git@github.com:/~/rust-lang/cargo.git
path:     ~/rust-lang/cargo.git
SSH URL:  ssh://git@github.com/~/rust-lang/cargo.git

SCP-like: git@github.com:/~git/rust-lang/cargo.git
path:     ~git/rust-lang/cargo.git
SSH URL:  ssh://git@github.com/~git/rust-lang/cargo.git

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions