Skip to content

Commit

Permalink
Add SSH support
Browse files Browse the repository at this point in the history
Uses the `ParseURL()` func from `git-get` to parse SSH URLs for SSH
based remotes.
  • Loading branch information
arbourd committed Apr 1, 2024
1 parent 2be737a commit 71545b4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/arbourd/git-open

go 1.22

require github.com/ldez/go-git-cmd-wrapper/v2 v2.6.0
require (
github.com/arbourd/git-get v0.5.3
github.com/ldez/go-git-cmd-wrapper/v2 v2.6.0
)

require github.com/mitchellh/go-homedir v1.1.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/arbourd/git-get v0.5.3 h1:Up6h1Bh6Jbpt4AV0v5t9Qn11WOUsuwYPgJIVfwBDbfY=
github.com/arbourd/git-get v0.5.3/go.mod h1:/D/CsQxH8drPyEVOJOHIJXMNiThrS3xQ89SkTO3Cnvo=
github.com/ldez/go-git-cmd-wrapper/v2 v2.6.0 h1:o5QIusOiH9phm1gY2UGO6JQjYSPFYbgFCcntOigBvMg=
github.com/ldez/go-git-cmd-wrapper/v2 v2.6.0/go.mod h1:whnaSah+AmezZS8vwp8FyFzEBHZCLKywWILUj5D8Jq0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
22 changes: 15 additions & 7 deletions open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package open

import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
Expand All @@ -11,6 +10,7 @@ import (
"runtime"
"strings"

"github.com/arbourd/git-get/get"
"github.com/arbourd/git-open/gitw"
)

Expand Down Expand Up @@ -62,7 +62,11 @@ func GetURL(arg string) (string, error) {
if err != nil {
return "", err
}
host, repo := parseRemote(remote)

host, repo, err := parseRepository(remote)
if err != nil {
return "", err
}

// Find the provider by comparing hosts
var p Provider
Expand Down Expand Up @@ -128,11 +132,15 @@ func getRemoteRef(gitroot string) (remote string, ref string, err error) {
return remote, ref, err
}

// parseRemote parses the host and repository (username or organization and repository name) from a remote string
func parseRemote(remote string) (host, repo string) {
u, _ := url.Parse(remote)
repo = strings.TrimPrefix(strings.TrimSuffix(path.Clean(u.Path), ".git"), "/")
return u.Host, repo
// parseRepository parses the host and repository (username or organization and repository name) from a remote string
func parseRepository(remote string) (host string, repo string, err error) {
url, err := get.ParseURL(remote)
if err != nil {
return "", "", fmt.Errorf("unable to parse remote url: %s", err)
}

repo = strings.TrimPrefix(strings.TrimSuffix(path.Clean(url.Path), ".git"), "/")
return url.Host, repo, nil
}

var commitSHARegex = regexp.MustCompile(`^[0-9a-f]{7,64}$`)
Expand Down
34 changes: 26 additions & 8 deletions open/open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,54 @@ func TestGetURL(t *testing.T) {
}
}

func TestParseRemote(t *testing.T) {
func TestParseRepository(t *testing.T) {
cases := map[string]struct {
remote string
expectedHost string
expectedPath string
wantErr bool
}{
"simple": {
"https protocol": {
remote: "https://github.com/arbourd/git-open",
expectedHost: "github.com",
expectedPath: "arbourd/git-open",
},
".git suffix": {
"https with .git suffix": {
remote: "https://github.com/arbourd/git-open.git",
expectedHost: "github.com",
expectedPath: "arbourd/git-open",
},
"extra slashes": {
"https with extra slashes": {
remote: "https://github.com////arbourd/git-open.git",
expectedHost: "github.com",
expectedPath: "arbourd/git-open",
},
"ssh protocol": {
remote: "git@github.com:arbourd/git-open.git",
expectedHost: "github.com",
expectedPath: "arbourd/git-open",
},
"git protocol": {
remote: "git://github.com/arbourd/git-open.git",
expectedHost: "github.com",
expectedPath: "arbourd/git-open",
},
"invalid url": {
remote: "github/arbourd/git-open.git%x",
wantErr: true,
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
host, path := parseRemote(c.remote)
if host != c.expectedHost {
host, path, err := parseRepository(c.remote)
if err != nil && !c.wantErr {
t.Fatalf("unexpected error:\n\t(GOT): %s\n\t(WNT): nil", err)
} else if err == nil && c.wantErr {
t.Fatalf("expected error:\n\t(GOT): nil")
} else if host != c.expectedHost {
t.Fatalf("unexpected host:\n\t(GOT): %#v\n\t(WNT): %#v", host, c.expectedHost)
}
if path != c.expectedPath {
} else if path != c.expectedPath {
t.Fatalf("unexpected path:\n\t(GOT): %#v\n\t(WNT): %#v", path, c.expectedPath)
}
})
Expand Down

0 comments on commit 71545b4

Please sign in to comment.