Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable relative path ssh URL #378

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ func newURL(ref string, ssh, forceMe bool) (*url.URL, error) {
user := matched[1]
host := matched[2]
path := matched[3]
// If the path is a relative path not beginning with a slash like
// `path/to/repo`, we might convert to like
// `ssh://[email protected]/~/path/to/repo` using tilde, but
// since GitHub doesn't support it, we treat relative and absolute
// paths the same way.
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, strings.TrimPrefix(path, "/"))
// When two conditions below are satisfied:
// 1. the path is a relative path, which not beginning with a slash, like `path/to/repo`.
// 2. the host is not github.com, which doesn't support relative paths.
// then we convert the given SCP style URL to a normal style and relative path URL using tilde, like `ssh://[email protected]/~/path/to/repo`.
if strings.HasPrefix(path, "/") {
path = strings.TrimPrefix(path, "/")
} else if host != "github.com" {
path = "~/" + path
}
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
} else {
// If ref is like "github.com/motemen/ghq" convert to "https://github.com/motemen/ghq"
paths := strings.Split(ref, "/")
Expand Down
21 changes: 18 additions & 3 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,35 @@ func TestNewURL(t *testing.T) {
expect: "https://github.com/motemen/pusheen-explorer",
host: "github.com",
}, {
name: "scp", // Convert SCP-like URL to SSH URL
name: "scp to github", // Convert SCP-like URL to SSH URL
url: "[email protected]:motemen/pusheen-explorer.git",
expect: "ssh://[email protected]/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "scp with root",
name: "scp with root to github",
url: "[email protected]:/motemen/pusheen-explorer.git",
expect: "ssh://[email protected]/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "scp without user",
name: "scp without user to github",
url: "github.com:motemen/pusheen-explorer.git",
expect: "ssh://github.com/motemen/pusheen-explorer.git",
host: "github.com",
}, {
name: "scp to others",
url: "[email protected]:repo/www.git",
expect: "ssh://[email protected]/~/repo/www.git",
host: "example.com",
}, {
name: "scp with root to others",
url: "[email protected]:/repo/www.git",
expect: "ssh://[email protected]/repo/www.git",
host: "example.com",
}, {
name: "scp without user to others",
url: "example.com:repo/www.git",
expect: "ssh://example.com/~/repo/www.git",
host: "example.com",
}, {
name: "different name repository",
url: "motemen/ghq",
Expand Down
Loading